Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid draining the ArrayPool with undisposed JsonDocuments #2178

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ internal class JsonClaimSet

internal JsonClaimSet(JsonDocument jsonDocument)
{
RootElement = jsonDocument.RootElement;
// This method is assuming ownership of the JsonDocument, which is backed by one or more ArrayPool arrays.
// We need to dispose of it to avoid leaking arrays from the pool. To achieve that, we clone the root element,
// which will result in a new JsonElement being created that's not tied to the original and that's not backed by
// ArrayPool memory, after which point we can dispose of the original to return the array(s) to the pool.
RootElement = jsonDocument.RootElement.Clone();
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
jsonDocument.Dispose();
}

internal JsonClaimSet(byte[] jsonBytes)
internal JsonClaimSet(byte[] jsonBytes) : this(JsonDocument.Parse(jsonBytes))
{
RootElement = JsonDocument.Parse(jsonBytes).RootElement;
}

internal JsonClaimSet(string json)
internal JsonClaimSet(string json) : this(JsonDocument.Parse(json))
{
RootElement = JsonDocument.Parse(json).RootElement;
}

internal JsonElement RootElement { get; }
Expand Down