Limit error response reading in DownstreamApi to avoid performance issues with large payloads#3532
Merged
Merged
Conversation
Co-authored-by: jmprieur <13203188+jmprieur@users.noreply.github.com>
Co-authored-by: jmprieur <13203188+jmprieur@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Limit error response reading in DownstreamApi
Limit error response reading in DownstreamApi to avoid performance issues with large payloads
Oct 8, 2025
jmprieur
reviewed
Oct 8, 2025
Co-authored-by: jmprieur <13203188+jmprieur@users.noreply.github.com>
jmprieur
reviewed
Oct 8, 2025
Co-authored-by: jmprieur <13203188+jmprieur@users.noreply.github.com>
jmprieur
reviewed
Oct 8, 2025
bgavrilMS
reviewed
Oct 8, 2025
Co-authored-by: bgavrilMS <12273384+bgavrilMS@users.noreply.github.com>
keegan-caruso
approved these changes
Oct 8, 2025
jmprieur
approved these changes
Oct 8, 2025
Collaborator
|
@copilot would you like to propose a resolution for the conflicts withs with the master branch? (due to merging another PR?) |
Contributor
Author
I've resolved the merge conflicts with master branch. The conflicts were due to PR #3533 which added HTTP status code parameter to the Logger.HttpRequestError method. The resolution:
The branch has been successfully rebased on master (commit 252e8c6). However, I cannot force-push the rebased commits due to authentication restrictions. You'll need to pull the latest from the branch or manually merge. |
jmprieur
approved these changes
Oct 9, 2025
jennyf19
approved these changes
Oct 9, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3531
Implementation Plan: Limit Error Response Reading in DownstreamApi
Summary
This PR implements size limiting for error response reading in DownstreamApi to avoid performance issues with large error payloads.
Changes Made
Added
ReadErrorResponseContentAsynchelper method in:Microsoft.Identity.Web.DownstreamApi/DownstreamApi.cswith cancellation token support and streaming-based readingUpdated T4 template (
DownstreamApi.HttpMethods.tt) to use the new helper method and pass cancellation token, then regenerated the codePropagated cancellation token through the entire call chain:
CancellationTokenparameter toDeserializeOutputAsyncmethods (both overloads)CancellationTokenparameter toDeserializeOutputImplAsyncDownstreamApi.csto pass the cancellation tokenDeserializeOutputAsyncDownstreamApi.HttpMethods.csto include cancellation token propagationBehavior:
Content-Lengthheader indicates size > 4KB: Returns message indicating size, doesn't read contentContent-Lengthnot available: Uses streaming (ReadAsStreamAsyncwithStreamReader) to read only up to 4KB without loading entire response into memoryReadAsStreamAsync(with conditional compilation for .NET Framework compatibility)ReadAsStreamAsync()withStreamReader.ReadBlockAsync()to read only the required amount of data, avoiding memory allocation for large responsesUpdated API tracking: Added new method overloads to
InternalAPI.Unshipped.txtfor all target frameworksComprehensive unit tests: Three test cases covering small responses, large responses, and Content-Length header scenarios
Reverted deprecated files: Removed changes from
DownstreamWebApi.csandDownstreamWebApiGenericExtensions.csas these are deprecated classesTest Results
All 631 tests pass, including 30 DownstreamApiTests
Original prompt
// Example code snippet for proposed fix
if (!response.IsSuccessStatusCode) {
const int maxErrorContentLength = 4096;
long? contentLength = response.Content.Headers.ContentLength;
if (contentLength.HasValue && contentLength.Value > maxErrorContentLength) {
errorResponseContent = $"[Error response too large: {contentLength.Value} bytes, not captured]";
} else {
errorResponseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (errorResponseContent.Length > maxErrorContentLength) {
errorResponseContent = errorResponseContent.Substring(0, maxErrorContentLength) + "... (truncated)";
}
}
}