Akron | 354f453 | 2021-01-21 17:29:44 +0100 | [diff] [blame^] | 1 | use Mojolicious::Lite; |
| 2 | use Test::Mojo; |
| 3 | use Test::More; |
| 4 | |
| 5 | my $t = Test::Mojo->new; |
| 6 | |
| 7 | plugin 'Kalamar::Plugin::CSP' => { |
| 8 | 'style-src' => ['self','unsafe-inline'], |
| 9 | 'script-src' => '*', |
| 10 | 'img-src' => ['self', 'data:'] |
| 11 | }; |
| 12 | |
| 13 | get '/' => sub { |
| 14 | shift->render(text => 'hello world'); |
| 15 | }; |
| 16 | |
| 17 | my $csp = 'Content-Security-Policy'; |
| 18 | |
| 19 | $t->get_ok('/') |
| 20 | ->status_is(200) |
| 21 | ->content_is('hello world') |
| 22 | ->header_is($csp, "img-src 'self' data:;script-src *;style-src 'self' 'unsafe-inline';") |
| 23 | ; |
| 24 | |
| 25 | $t->app->csp->add('img-src' => 'stats.ids-mannheim.de'); |
| 26 | |
| 27 | $t->get_ok('/') |
| 28 | ->status_is(200) |
| 29 | ->content_is('hello world') |
| 30 | ->header_is($csp, "img-src 'self' data: stats.ids-mannheim.de;script-src *;style-src 'self' 'unsafe-inline';") |
| 31 | ; |
| 32 | |
| 33 | $t->get_ok('/') |
| 34 | ->status_is(200) |
| 35 | ->content_is('hello world') |
| 36 | ->header_is($csp, "img-src 'self' data: stats.ids-mannheim.de;script-src *;style-src 'self' 'unsafe-inline';") |
| 37 | ; |
| 38 | |
| 39 | $t->app->csp->add('img-src' => 'stats.ids-mannheim.de'); |
| 40 | |
| 41 | $t->get_ok('/') |
| 42 | ->status_is(200) |
| 43 | ->content_is('hello world') |
| 44 | ->header_is($csp, "img-src 'self' data: stats.ids-mannheim.de;script-src *;style-src 'self' 'unsafe-inline';") |
| 45 | ; |
| 46 | |
| 47 | $t->app->csp->add('script-src' => '*'); |
| 48 | |
| 49 | $t->get_ok('/') |
| 50 | ->status_is(200) |
| 51 | ->content_is('hello world') |
| 52 | ->header_is($csp, "img-src 'self' data: stats.ids-mannheim.de;script-src *;style-src 'self' 'unsafe-inline';") |
| 53 | ; |
| 54 | |
| 55 | |
| 56 | # New |
| 57 | $t = Test::Mojo->new; |
| 58 | $t->app->config( |
| 59 | CSP => { |
| 60 | 'style-src' => ['self','unsafe-inline'], |
| 61 | 'img-src' => ['self', 'data:'] |
| 62 | } |
| 63 | ); |
| 64 | |
| 65 | $t->app->plugin('Kalamar::Plugin::CSP' => { |
| 66 | 'script-src' => '*', |
| 67 | 'img-src' => 'self' |
| 68 | }); |
| 69 | |
| 70 | $t->app->routes->get('/n')->to( |
| 71 | cb => sub { |
| 72 | shift->render(text => 'hello world'); |
| 73 | } |
| 74 | ); |
| 75 | |
| 76 | $t->get_ok('/n') |
| 77 | ->status_is(200) |
| 78 | ->content_is('hello world') |
| 79 | ->header_is($csp, "img-src 'self' data:;script-src *;style-src 'self' 'unsafe-inline';") |
| 80 | ; |
| 81 | |
| 82 | |
| 83 | done_testing; |
| 84 | __END__ |