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

LFT-1285 - Ignore use=enc JWKs in JWKS parsing #364

Merged
merged 12 commits into from
Jul 31, 2024

Conversation

j3parker
Copy link
Member

We never have use=enc keys and so our parsing throws when it encounters them.

We don't distinguish between unparsable keys vs. keys that we don't want to use for signing (e.g. use=enc, but also other constraints we impose like having a suitable kty, kid etc.)

In an ideal world I think we would push more of that logic out a bit and parse more keys, ignoring ones that aren't relevant to us anyway and emitting errors in a more sensible location.

This PR makes the minimal change possible to allow us to parse a JWKS response that includes a use=enc key that we will otherwise not need. This is needed for interoperability with LTI vendors that have a JWKS URL serving multiple purposes.

JsonWebKey key = JsonWebKey.FromJson( keyJson );

if( !JsonWebKey.TryParseJsonWebKey( keyJson, out var key, out var error, out var exception, out var useEncKey ) ) {
if( useEncKey ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This filtering could apply more generally but for now I just want to do the minimal thing to get the integrations working. I think the API needs a redesign overall.

@@ -49,91 +51,169 @@ public abstract class JsonWebKey {
/// <returns>A JWK DTO</returns>
public abstract object ToJwkDto();


/// <summary>
/// Deserialize a JWK
/// </summary>
/// <param name="json">The json JWK</param>
/// <returns>A <see cref="JsonWebKey"/></returns>
public static JsonWebKey FromJson( string json ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Maintaining this API + behaviour for people that call this directly (> 0 in our code)

[NullWhen( true )]
out Exception? exception,
out bool useEncKey
) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Mostly changed to TryX from throwing, but also added this separate useEncKey signal.

@@ -0,0 +1,10 @@
namespace System.Diagnostics.CodeAnalysis;
Copy link
Member Author

Choose a reason for hiding this comment

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

(Note filepath: .net4x.cs is treated specially via our build.)

@j3parker j3parker marked this pull request as ready for review July 31, 2024 13:41
@j3parker j3parker requested a review from omsmith as a code owner July 31, 2024 13:41
Comment on lines 93 to 99
if( data.ContainsKey( "use" ) && data[ "use" ] != null && data[ "use" ].ToString() != "sig" ) {
string msg = String.Format( "invalid 'use' value in JSON web key: {0}", data[ "use" ] );
throw new JsonWebKeyParseException( msg );
result = null;
error = "invalid 'use' value in JSON web key: " + data[ "use" ];
exception = null;
useEncKey = data[ "use" ].ToString() == "enc";
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would maybe make this ignored in general (and not just "enc")? Perhaps a ParseResult { Success, Error, Ignore } vs bool?

It would ultimately be nice to be able to say "the key {kid} is not intended for singnatures" rather than "the key {kid} was not found" though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, so I mentioned in the PR description basically I think our parser should just parse and deciding if a key was valid or not ought to be up to the user.

All of our users currently only want signing keys so I think it's OK practically to just filter them (vs. exploding) but I only exposed that on JWKS because the API for getting one key implies it will never return null (it throws currently) and it gets used e.g. parsing keys from the database, and I didn't want to add sig=enc logic for right now.

Copy link
Member Author

Choose a reason for hiding this comment

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

In other words rather than returning ignore from here I'd prefer if we just successfully parsed all valid JWKs and then moved the policy decisions elsewhere

Copy link
Contributor

@omsmith omsmith left a comment

Choose a reason for hiding this comment

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

Seems fine enough

@j3parker j3parker merged commit 0bad498 into master Jul 31, 2024
2 checks passed
@j3parker j3parker deleted the jwks-allow-use-enc-keys branch July 31, 2024 14:58
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