Fix ArgumentOutOfRangeException when validating Basic Authorization h…#492
Fix ArgumentOutOfRangeException when validating Basic Authorization h…#492benyuz wants to merge 1 commit intonanoframework:mainfrom
Conversation
📝 WalkthroughWalkthroughThe PR adds defensive validation to ChangesAuthorization Header Validation and Testing
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@nanoFramework.System.Net.Http/Http/System.Net.HttpListenerRequest.cs`:
- Around line 215-219: The Basic auth parsing in HttpListenerRequest currently
calls Convert.FromBase64String(authInfo) and builds strAuthInfo without guarding
FormatException; wrap the Base64 decode and subsequent UTF8 char
conversion/credential split (the authInfo, authInfoDecoded, authInfoDecChar and
strAuthInfo logic) in a try-catch that catches FormatException (and optionally
ArgumentException) and simply ignores the malformed Authorization header (i.e.,
do not throw, continue processing other headers), mirroring the graceful
behavior used in the Content-Length parsing; ensure you reference and protect
the Convert.FromBase64String call and the UTF8 decoding and string construction
so malformed Base64 does not propagate an exception to callers.
In `@Tests/HttpUnitTests/HttpListenerRequest.cs`:
- Around line 13-29: Add tests that exercise the actual parser path by invoking
HttpListenerRequest.ParseHTTPRequest with full raw HTTP request text (including
request line, headers, and CRLF terminator) so the fixed parser path is
validated; create two test cases (e.g.,
Add_Authorization_NoSpaceMultipleChars_ParseHTTPRequest_ShouldNotThrow and
Add_Authorization_ValidBasicToken_ParseHTTPRequest_ShouldSucceed) that build raw
requests containing "Authorization: a111111" and "Authorization: Basic a111111"
respectively, call HttpListenerRequest.ParseHTTPRequest (or the public factory
that uses it) to produce an HttpListenerRequest, and assert the resulting header
value for "Authorization" matches expected ("a111111" for malformed path
behavior you want to lock in, and "Basic a111111" for the valid case) to ensure
the parser change is covered.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 23dd44bb-dfd8-42ab-8467-677e82068cbe
📒 Files selected for processing (3)
Tests/HttpUnitTests/HttpListenerRequest.csTests/HttpUnitTests/HttpUnitTests.nfprojnanoFramework.System.Net.Http/Http/System.Net.HttpListenerRequest.cs
|
Yes, please create a follow-up issue for the Base64 parsing exception. That would be very helpful. Thank you! |
Fix crash when Authorization validation for Basic format
Description
ArgumentOutOfRangeExceptioncrash inHttpListenerRequest.ParseHTTPRequestwhen theAuthorizationheader value does not contain a space (e.g.,Authorization: a111111or malformed Basic authentication).if (sepSpace > 0)before parsing the Authorization header value.Motivation and Context
Authorization: a111111), theParseHTTPRequestmethod crashes withArgumentOutOfRangeExceptionbecausesepSpaceis -1 andSubstring(0, sepSpace)is called.How Has This Been Tested?
curl -H "Authorization: a111111" http://<device-ip>/helloAdd_Authorization_NoSpaceMultipleChars_ShouldNotThrowinHttpListenerRequestTests.csScreenshots
Types of changes
Checklist: