Fix ArgumentOutOfRangeException when header value is empty#488
Fix ArgumentOutOfRangeException when header value is empty#488josesimoes merged 7 commits intonanoframework:mainfrom
ArgumentOutOfRangeException when header value is empty#488Conversation
Added bounds check before substring operation to prevent ArgumentOutOfRangeException.
📝 WalkthroughWalkthroughWebHeaderCollection.Add(string header) now handles a trailing colon by assigning an empty string when the colon is the last character instead of calling Substring. A new test class WebHeaderCollectionTests was added with unit tests covering Authorization header parsing and various invalid/edge-case header inputs. ChangesHTTP Header Bounds Check
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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 |
|
已收到您的邮件,我会及时查看的!
|
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
Pull request overview
Fixes a crash in the nanoFramework System.Net header parsing path by safely handling header strings that end with a colon and have no value (e.g., "Authorization:") when using WebHeaderCollection.Add(string header).
Changes:
- Adds a bounds check before extracting the header value substring in
WebHeaderCollection.Add(string header).
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@nanoFramework.System.Net.Http/Http/System.Net.WebHeaders.cs`:
- Around line 430-439: Replace the multi-line if/else that assigns value based
on colpos and header with a concise ternary assignment: set value = (colpos + 1
>= header.Length) ? string.Empty : header.Substring(colpos + 1); locating the
logic that uses the variables header, colpos and the local variable value inside
the WebHeaders parsing code (the block currently handling "Handle empty header
value") and swap the block for the single-line ternary form.
🪄 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: b3f50d60-6a7b-46ee-bcc6-462dc3c49dd0
📒 Files selected for processing (1)
nanoFramework.System.Net.Http/Http/System.Net.WebHeaders.cs
This test class verifies the behavior of adding various Authorization headers to a WebHeaderCollection, ensuring that no exceptions are thrown for valid inputs and that the values are stored correctly.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@Tests/HttpUnitTests/WebHeaderCollectionTests.cs`:
- Around line 14-26: Add a regression test that covers the exact crash case
where the header name is present but there is no value after the colon; in the
WebHeaderCollectionTests class add a new test method (e.g.,
Add_Authorization_EmptyValue_ShouldNotThrow) which creates a WebHeaderCollection
and calls headers.Add("Authorization:") and asserts that no exception is thrown
(mirror the pattern used in
Add_Authorization_BearerWithSpaceAndNoValue_ShouldNotThrow and
Add_Authorization_NoSpaceSingleChar_ShouldNotThrow) so the code path in
WebHeaderCollection.Add that handles colpos + 1 >= header.Length is exercised.
🪄 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: 75f60ac4-d697-452f-a0c4-0fda7a5221a5
📒 Files selected for processing (1)
Tests/HttpUnitTests/WebHeaderCollectionTests.cs
josesimoes
left a comment
There was a problem hiding this comment.
LGTM!
I've added more unit tests that are meant to throw.
I've also added this new unit test group to the project as it wasn't building nor being executed.
|
/azp runpipeline |
|
Command 'runpipeline' is not supported by Azure Pipelines. Supported commands
See additional documentation. |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
ArgumentOutOfRangeException when header value is empty
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@Tests/HttpUnitTests/WebHeaderCollectionTests.cs`:
- Around line 73-99: The tests in WebHeaderCollectionTests (methods
Add_NullHeader_ThrowsArgumentNullException,
Add_EmptyHeader_ThrowsArgumentNullException,
Add_HeaderWithNoColon_ThrowsArgumentException,
Add_HeaderNameWithSpace_ThrowsArgumentException) use Assert.ThrowsException
which does not exist in the current test framework; replace each
Assert.ThrowsException(...) call with Assert.Throws(...) using the same
exception type and lambda/action argument so the tests compile and correctly
assert exceptions (follow the pattern used in HttpClientTest.cs).
🪄 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: 6f380f96-dbdf-4a03-9ca7-3d56b39922e0
📒 Files selected for processing (2)
Tests/HttpUnitTests/HttpUnitTests.nfprojTests/HttpUnitTests/WebHeaderCollectionTests.cs
|
@benyuz thank you again for your contribution! 🙏😄 .NET nanoFramework is all about community involvement, and no contribution is too small. Please edit it and add an entry with your GitHub username in the appropriate location (names are sorted alphabetically): (Feel free to adjust your name if it's not correct) |
Description
Substringcall inWebHeaderCollection.Add(string header)method.ArgumentOutOfRangeExceptionwhen header ends with ':' and has no value (e.g.,"Authorization:").Motivation and Context
Add(string header)with a header string that has no value after the colon (e.g.,"Authorization:") causes a crash becauseSubstring(colpos + 1)is called without checking bounds.How Has This Been Tested?
curl -H "Authorization:" http://<device-ip>/hello.Screenshots
Types of changes
Checklist: