Fix stale $query, whitelist captures+data, stream all body chunks#7
Draft
troglodyne-bot wants to merge 1 commit into
Draft
Conversation
…ody chunks
Three bugs fixed in TPSGI.pm:
1. `my $query = ... if $env->{QUERY_STRING}` is undefined behavior in Perl
when the condition is false: $query may retain its value from the previous
call (persisting across keepalive requests) rather than resetting to undef.
Replaced with ternary so $query is always a fresh hashref.
2. extract_query strict-validation whitelist omitted capture names and data
keys injected by the route itself. Any route combining `parameters` with
`captures` or `data` returned a spurious 400 Bad Request for every valid
request. Capture and data keys are now explicitly whitelisted.
3. stream_raw_psgi wrote only $response->[2][0], discarding all body elements
after the first. Changed to `print $fh $_ for @{$response->[2]}` so the
full PSGI body array is emitted.
Also adds t/extract_query.t (18 tests) and t/stream_raw_psgi.t (6 tests) with
supporting t/lib/TPSGITestStubs.pm and t/lib/TestTPSGI.pm.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Three bugs fixed in
lib/TPSGI.pm, with a new test suite covering all three.Why
Bug 1 — stale `$query` across keepalive requests
my $query = URL::Encode::url_params_mixed(...) if \$env->{QUERY_STRING}is undefined behavior in Perl when the condition is false. When QUERY_STRING is empty,\$querymay retain its value from the previous subroutine call rather than resetting to undef. On a keepalive connection, subsequent requests with no query string would inherit the previous request's query params — causing unexpected 400s from the whitelist check, or worse, leaking params between routes.Fixed: ternary form so
\$queryis always a fresh{}.Bug 2 — whitelist missing captures and data keys
When a route defines
parameters(strict validation mode), only keys in%\$parameterswere whitelisted. Capture fields populated by the route regex (captureskey) and static fields fromroute->{data}were not added to the allowed list. Any route that combinedcaptures+parametersreturned a spurious 400 Bad Request for every valid request.Fixed: capture names and data keys are now explicitly pushed into
@known_paramsbefore the whitelist sweep.Bug 3 — stream_raw_psgi drops body after first chunk
print \$fh \$response->[2][0]wrote only the first element of the PSGI body arrayref. Multi-chunk responses (e.g. templates stitched together) silently lost everything after the first string.Fixed:
print \$fh \$_ for @{\$response->[2]}.How
t/lib/TPSGITestStubs.pm: graceful stub loader for the 8 CPAN deps absent in the test environment (prefers real modules when loadable).t/lib/TestTPSGI.pm: minimal subclass that bypassesnew()system checks and silences logging for unit tests.t/extract_query.t: 18 tests covering no-params passthrough, captures whitelisted, data keys whitelisted, mixed captures+data+params, invalid capture rejection, unknown param rejection, empty params hash.t/stream_raw_psgi.t: 6 tests using fork+pipe to capture raw HTTP output and verify single/multi/empty body handling.Testing
All 24 tests pass.