Skip to content

fix: Verify signed tokens audiences are all trusted - #3309

Open
efevans wants to merge 1 commit into
googleapis:mainfrom
efevans:fix/b593062491
Open

efevans wants to merge 1 commit into
googleapis:mainfrom
efevans:fix/b593062491

Conversation

@efevans

@efevans efevans commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

b/593062491

@efevans
efevans requested a review from a team as a code owner December 3, 2026 23:43

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for array-based audience claims in JSON Web Tokens (JWTs) and ensures that tokens contain at least one audience claim when trusted audiences are configured. It also includes comprehensive unit tests for these changes. The review feedback highlights two main improvements: first, avoiding potential null elements in AudienceAsList by using a null-coalescing operator to default to an empty string, and second, removing a redundant .ToList() call in the verification logic to prevent unnecessary memory allocations.

{
get
{
var asList = Audience as List<string>;

@robertvoinescu-work robertvoinescu-work Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we cast to IEnumerable<string> instead here and then return asEnumerable without allocating a new list?

@robertvoinescu-work robertvoinescu-work Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just in case we could also do the following to avoid passing forward something that has deferred execution (like a linq query). But if this is only coming from json maybe it's not a concern:

if (Audience is IReadOnlyCollection<string> asCollection)
{
    return asCollection;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since Audience on this Payload class is an object, the default deserialization in SignedToken.cs:

var payloadValue = NewtonsoftJsonSerializer.Instance.Deserialize<TJwsPayload>(TokenEncodingHelpers.Base64UrlToString(encodedPayload));

Converts an array on the aud field into a JArray. The problem here is a Newtonsoft.Json JArray object cannot be cast to a strongly typed List/IEnumerable. I think if we wanted to keep the changes in this AudienceAsList to a minimal on both lines and allocations, it would be adding a custom deserializer to convert a json array of scalar values into a strongly typed list.

@amanda-tarafa
amanda-tarafa self-requested a review December 4, 2026 07:25
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.

2 participants