Fix ArgumentOutOfRangeException when validating Basic Authorization h…#492
Fix ArgumentOutOfRangeException when validating Basic Authorization h…#492benyuz wants to merge 5 commits into
Conversation
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Repository: nanoframework/coderabbit/.coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PR adds defensive validation to HttpListenerRequest.ParseHTTPRequest to check for a space delimiter in Authorization headers before parsing, preventing exceptions on malformed values. It includes two unit tests verifying both malformed (no space) and valid (Basic auth) header handling, plus a project file update to compile the new test file. 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! |
There was a problem hiding this comment.
Pull request overview
Fixes a server-side crash in the nanoFramework System.Net.Http implementation when parsing malformed Authorization headers in HttpListenerRequest.ParseHTTPRequest, preventing an ArgumentOutOfRangeException when the header value lacks a space separator.
Changes:
- Added a guard (
sepSpace > 0) before splitting theAuthorizationheader into scheme/credentials. - Added a new unit test file and included it in the HttpUnitTests project.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
nanoFramework.System.Net.Http/Http/System.Net.HttpListenerRequest.cs |
Adds a safety check to avoid substring with a negative length when parsing Authorization. |
Tests/HttpUnitTests/HttpUnitTests.nfproj |
Includes a new test source file in the test project build. |
Tests/HttpUnitTests/HttpListenerRequest.cs |
Adds new tests intended to cover malformed and valid Authorization header formats. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…est.cs Co-authored-by: José Simões <jose.simoes@eclo.solutions>
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: