Created
April 12, 2020 23:46
-
-
Save pavelsr/6d8a8737669f230f983b8817299e9c74 to your computer and use it in GitHub Desktop.
Yancy submit form using jquery.post - bypass of CSRF token validation failed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Demo of 'CSRF token validation failed' error bypass using jquery.post and serialize form data as JSON | |
use Mojolicious::Lite; | |
use Mojo::SQLite; | |
helper sqlite => sub { | |
state $path = app->home->child( 'data.db' ); | |
state $sqlite = Mojo::SQLite->new( 'sqlite:' . $path ); | |
return $sqlite; | |
}; | |
app->sqlite->auto_migrate(1)->migrations->from_data; | |
plugin Yancy => { | |
backend => { Sqlite => app->sqlite }, | |
read_schema => 1 | |
}; | |
get '/' => sub { | |
my ( $c ) = @_; | |
$c->render( template => 'index'); | |
}; | |
app->routes->post( '/cu' )->to( 'yancy#set', schema => 'users' ); | |
app->routes->get( '/lu' )->to( 'yancy#list', schema => 'users' ); | |
app->start; | |
__DATA__ | |
@@ migrations | |
-- 1 up | |
CREATE TABLE users ( | |
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | |
email VARCHAR UNIQUE, | |
is_admin BOOLEAN DEFAULT FALSE, | |
password VARCHAR | |
); | |
@@ index.html.ep | |
% layout 'default'; | |
% title 'Demo of issue #95'; | |
%= app->yancy->form->form_for( 'users', properties => [ qw/email password/ ], action => '/cu' ) | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" crossorigin="anonymous"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.serializeJSON/2.9.0/jquery.serializejson.min.js" crossorigin="anonymous"></script> | |
<script type="text/javascript"> | |
setTimeout(function () { | |
$(document).ready(function() { | |
$( "form" ).submit(function(e){ | |
e.preventDefault(); | |
$.post( "/cu", $('form').serializeJSON(), function( response ) { | |
console.log( response ); | |
}, "json"); | |
}); | |
}); | |
}, 0); | |
</script> | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment