Skip to content

Fix HttpRequestExtensions.UnvalidatedGetHeader always returning empty string #3126

Merged
rajkumar-rangaraj merged 3 commits intomainfrom
rajrang/UnvalidatedGetHeader
Feb 20, 2026
Merged

Fix HttpRequestExtensions.UnvalidatedGetHeader always returning empty string #3126
rajkumar-rangaraj merged 3 commits intomainfrom
rajrang/UnvalidatedGetHeader

Conversation

@rajkumar-rangaraj
Copy link
Copy Markdown
Member

Fixes #3105

Changes

HttpRequestExtensions.UnvalidatedGetHeader was always returning string.Empty instead of the actual header value. This caused all callers (e.g., WebTestActivityProcessor, ClientIpHeaderActivityProcessor) to fall through to the request.Headers[...] fallback path, bypassing ASP.NET's Unvalidated accessor.

Changes

  • HttpRequestExtensions.cs — Fixed UnvalidatedGetHeader to return the header value with max-length enforcement (1024 chars), matching the 2.x behavior.
  • RequestTrackingConstants.cs — Added RequestHeaderMaxLength constant.
  • WebTestActivityProcessorTests.cs — Added test verifying header truncation at max length.

2.x reference: https://github.com/microsoft/ApplicationInsights-dotnet/blob/2.x/WEB/Src/Web/Web/Implementation/HttpRequestExtensions.cs#L22

…ty string instead of the actual header value.
Copilot AI review requested due to automatic review settings February 20, 2026 19:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression in Microsoft.ApplicationInsights.Web where HttpRequestExtensions.UnvalidatedGetHeader always returned string.Empty, causing callers to bypass ASP.NET’s Unvalidated accessor and lose header max-length enforcement.

Changes:

  • Correct UnvalidatedGetHeader to return the actual header value and truncate it to a defined maximum length (1024).
  • Introduce RequestTrackingConstants.RequestHeaderMaxLength to centralize the header-length limit.
  • Add a test validating header truncation behavior and update the changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
WEB/Src/Web/Web/Implementation/RequestTrackingConstants.cs Adds a shared constant for request header max length.
WEB/Src/Web/Web/Implementation/HttpRequestExtensions.cs Fixes UnvalidatedGetHeader return value and enforces max-length truncation.
WEB/Src/Web/Web.Tests/WebTestActivityProcessorTests.cs Adds a unit test asserting truncation of oversized synthetic test headers.
CHANGELOG.md Adds an Unreleased changelog entry for the fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CHANGELOG.md
Comment thread WEB/Src/Web/Web/Implementation/RequestTrackingConstants.cs
Comment thread WEB/Src/Web/Web/Implementation/HttpRequestExtensions.cs
@rajkumar-rangaraj rajkumar-rangaraj enabled auto-merge (squash) February 20, 2026 21:39
@rajkumar-rangaraj rajkumar-rangaraj merged commit fb6b66d into main Feb 20, 2026
18 checks passed
@rajkumar-rangaraj rajkumar-rangaraj deleted the rajrang/UnvalidatedGetHeader branch February 20, 2026 21:47
This was referenced Apr 2, 2026
This was referenced Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spotted a possible bug in HttpRequestExtensions.UnvalidatedGetHeader

3 participants