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

chore(backend): Fix JWKS cache #3321

Merged
merged 12 commits into from
May 7, 2024
Merged

chore(backend): Fix JWKS cache #3321

merged 12 commits into from
May 7, 2024

Conversation

BRKalow
Copy link
Member

@BRKalow BRKalow commented May 3, 2024

Description

Fixes a bug in the JWKS cache where it was getting cleared but not treated as expired.

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Copy link

changeset-bot bot commented May 3, 2024

🦋 Changeset detected

Latest commit: 698cc4c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@clerk/backend Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/remix Patch
@clerk/clerk-sdk-node Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@BRKalow BRKalow marked this pull request as ready for review May 6, 2024 20:37
Comment on lines -33 to -41
if (jwksCacheTtlInMs >= 0) {
setTimeout(() => {
if (jwk) {
delete cache[jwk.kid];
} else {
cache = {};
}
}, jwksCacheTtlInMs);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

We already have a hard-coded 5 minute TTL for the values in the jwks cache. This setTimeout appears to be resulting in a scenario where the cache is cleared by the timeout here immediately after the 5 minute TTL is hit, so the cache remains empty for the remainder of the 5 minute TTL.

@@ -116,11 +106,10 @@ export async function loadClerkJWKFromRemote({
apiUrl = API_URL,
apiVersion = API_VERSION,
kid,
jwksCacheTtlInMs = JWKS_CACHE_TTL_MS,
Copy link
Member Author

@BRKalow BRKalow May 6, 2024

Choose a reason for hiding this comment

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

This doesn't seem to be functionally doing anything if beyond 5 minutes (except for introducing this bug), as we have the hardcoded 5 minute max cache time.

@@ -184,5 +185,38 @@ export default (QUnit: QUnit) => {
}
}
});

test('cache TTLs do not conflict', async assert => {
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 a really specific test case for the observed bug. I confirmed that this test fails on main and passes on this branch. Not sure if it's worthwhile to keep around for a long time with the removal of the timer-based cache eviction. 🤔

@BRKalow BRKalow requested a review from a team May 6, 2024 20:48
@BRKalow BRKalow enabled auto-merge (squash) May 7, 2024 15:27
@BRKalow BRKalow merged commit 4f4375e into main May 7, 2024
10 checks passed
@BRKalow BRKalow deleted the brk.fix/jwks-cache branch May 7, 2024 15:41
@clerk-cookie clerk-cookie mentioned this pull request May 7, 2024
Copy link
Contributor

@dimkl dimkl left a comment

Choose a reason for hiding this comment

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

☁️ The reason behind adding the MAX_CACHE_LAST_UPDATED_AT_SECONDS to be 5 minutes is to refresh the cache if the requested kid is missing from the cache and the cache is older than 5 minutes. This is used to cover cases where the kid is missing in the cache but exists in the jwks endpoint.
There may be some issue in the existing code and it may not be clear, but i think that we shouldn't deprecate the jwksCacheTtlInMs. Those two are meant to be used for different reasons and i think that we should always provide a way to configure the cache TTL instead of hard-coding the value.
Therefore i believe we should revert this PR and apply the appropriate fix to resolve the issue with the setTimeout.
Also, we are using a similar approach in our ClerkJS to handle session token cache, should we avoid using the setTimeout there and clear the cache if it's expired to avoid a similar race condition?
cc: @BRKalow , @nikosdouvlis


if (isExpired) {
cache = {};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should not mutate the cache in this function. We should use the result of this function and clear the cache in the same place where we set it.
With this change we have converted a pure function that is supposed to return a boolean to a function with side-effects. This may cause issues in the future. I think we need to keep separate the pure logic and the functions used to determine the state from the functions mutating it.
cc: @BRKalow , @nikosdouvlis

Copy link
Member

Choose a reason for hiding this comment

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

I agree that a predicate function shouldn't mutate external state

@@ -208,7 +196,19 @@ async function fetchJWKSFromBAPI(apiUrl: string, key: string, apiVersion: string
}

function cacheHasExpired() {
return Date.now() - lastUpdatedAt >= MAX_CACHE_LAST_UPDATED_AT_SECONDS * 1000;
Copy link
Member

Choose a reason for hiding this comment

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

@dimkl do you remember why this was capped at 5minutes? Also, why is this defined in seconds but the other constant defined in ms? We should probably align the two in any case

Copy link
Contributor

@dimkl dimkl May 8, 2024

Choose a reason for hiding this comment

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

@nikosdouvlis I agree that we should align all of them to be in MS. It was capped at 5 minutes because we considered that it would be safe to expect that in 5 minutes all the systems related to the JWKs would have been updated to include a new instance or an update to the JWKs initiated by our Clerk API (eg Edge cache, read-only replicas, ...)

Copy link
Member

@nikosdouvlis nikosdouvlis May 8, 2024

Choose a reason for hiding this comment

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

That makes sense but we probably need to document jwksCacheTtlInMs better if we don't deprecate it as the prop is somewhat confusing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants