Repository navigation
Fix route parameter redaction for actions sharing a route template - #7933
evgenyfedorov2 merged 5 commits into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the change intended to prevent classification reuse across controller actions sharing a route template.
No open issues 🎉
What changed in this PR
Fixes incoming HTTP log redaction when controller actions share a route template, preventing one action’s parameter classifications from being reused for another.
Changes:
- Caches parameter classifications by endpoint rather than route template, and skips caching endpoints without controller metadata.
- Adds unit and acceptance tests for shared templates across HTTP methods and conventional routes.
- Replaces
NETCOREAPP3_1_OR_GREATERguards withNETin the utility and its unit tests.
| File | Description |
|---|---|
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/IncomingHttpRouteUtilityTests.cs | Tests classification isolation between endpoints sharing a template; updates compilation guards. |
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/ApiRoutingController.cs | Adds GET and POST actions with different classifications on the same template. |
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/AcceptanceTests.Routing.cs | Tests redaction for actions sharing a conventional route. |
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/AcceptanceTests.Mvc.cs | Tests redaction for GET and POST actions sharing a template. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/IncomingHttpRouteUtility.cs | Keys the cache by endpoint, bypasses non-controller endpoints, and updates compilation guards. |
Review effort: Balanced
85b3a98 to
19a479d
Compare
IncomingHttpRouteUtility cached route parameter classifications by route
template, but several controller actions can have the same template: one
template on different HTTP methods, or all actions of a conventional route.
The classifications of the first action requested were then used for all of
them. For example, with these two actions:
[HttpGet("api/users/{userId}")]
public IActionResult Get([NoDataClassification] string userId) => Ok();
[HttpPost("api/users/{userId}")]
public IActionResult Update([PrivateData] string userId) => Ok();
after one GET request, the user ID of every POST request was not redacted:
POST /api/users/123 was logged as api/users/123.
Cache the classifications per endpoint, and only for controller endpoints,
since other endpoints have no parameter classifications.
Also remove #if NETCOREAPP3_1_OR_GREATER and its dead #else code from the
changed files, since the library and its tests target only .NET.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
19a479d to
53a7f83
Compare
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1673031&view=codecoverage-tab |
The cache of route parameter classifications held its endpoints for the lifetime of the app. MVC builds new endpoints for all actions whenever its list of actions changes, for example on hot reload or when an app's IActionDescriptorChangeProvider signals a change, so the old endpoints piled up in the cache. Use a ConditionalWeakTable keyed by endpoint, so each entry goes away together with its endpoint. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
GetSensitiveParameter_ActionsWithSameRoute_ReturnsSensitiveParametersOfEachAction already checks that endpoints sharing a route template get their own classifications, and the logging interceptor that the acceptance tests go through is unchanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1677357&view=codecoverage-tab |
IncomingHttpRouteUtilitycached route parameter classifications by route template, but several controller actions can have the same template: one template on different HTTP methods, or all actions of a conventional route. The classifications of the first action requested were then used for all of them. For example, with these two actions:after one GET request, the user ID of every POST request is not redacted:
POST /api/users/123is logged asapi/users/123.Fix: cache the classifications per endpoint, and only for controller endpoints, since other endpoints have no parameter classifications.
Microsoft Reviewers: Open in CodeFlow