|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use Test2::V0; |
| 4 | +use PAGI::Middleware::Debug; |
| 5 | +use Future::AsyncAwait; |
| 6 | + |
| 7 | +# Test that scope values are HTML-escaped in the debug panel |
| 8 | + |
| 9 | +my $debug = PAGI::Middleware::Debug->new( |
| 10 | + enabled => 1, |
| 11 | + show_scope => 1, |
| 12 | + show_headers => 0, |
| 13 | + show_timing => 0, |
| 14 | +); |
| 15 | + |
| 16 | +# Build a scope with XSS payloads in every scope value |
| 17 | +my $xss = '<script>alert("xss")</script>'; |
| 18 | +my $scope = { |
| 19 | + type => 'http', |
| 20 | + method => $xss, |
| 21 | + path => $xss, |
| 22 | + query_string => $xss, |
| 23 | + scheme => $xss, |
| 24 | + headers => [], |
| 25 | +}; |
| 26 | + |
| 27 | +# Capture the response body from the debug panel |
| 28 | +my $captured_body; |
| 29 | + |
| 30 | +my $inner_app = async sub { |
| 31 | + my ($scope, $receive, $send) = @_; |
| 32 | + await $send->({ |
| 33 | + type => 'http.response.start', |
| 34 | + status => 200, |
| 35 | + headers => [['content-type', 'text/html']], |
| 36 | + }); |
| 37 | + await $send->({ |
| 38 | + type => 'http.response.body', |
| 39 | + body => '<html><body>Hello</body></html>', |
| 40 | + more => 0, |
| 41 | + }); |
| 42 | +}; |
| 43 | + |
| 44 | +my $wrapped = $debug->wrap($inner_app); |
| 45 | + |
| 46 | +my $send = async sub { |
| 47 | + my ($event) = @_; |
| 48 | + if ($event->{type} eq 'http.response.body') { |
| 49 | + $captured_body = $event->{body}; |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +my $receive = async sub { return { type => 'http.disconnect' } }; |
| 54 | + |
| 55 | +# Run the middleware |
| 56 | +$wrapped->($scope, $receive, $send)->get; |
| 57 | + |
| 58 | +# The raw XSS string must NOT appear in the output |
| 59 | +unlike($captured_body, qr/<script>alert/, 'no raw script tags in debug panel output'); |
| 60 | + |
| 61 | +# The escaped version MUST appear |
| 62 | +like($captured_body, qr/<script>alert/, 'XSS payload is HTML-escaped in debug panel'); |
| 63 | + |
| 64 | +# Verify each field individually |
| 65 | +like($captured_body, qr{<th>Method</th><td><script>}, 'method field is escaped'); |
| 66 | +like($captured_body, qr{<th>Path</th><td><script>}, 'path field is escaped'); |
| 67 | +like($captured_body, qr{<th>Query</th><td><script>}, 'query field is escaped'); |
| 68 | +like($captured_body, qr{<th>Scheme</th><td><script>}, 'scheme field is escaped'); |
| 69 | + |
| 70 | +done_testing; |
0 commit comments