Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.
This repository was archived by the owner on May 31, 2022. It is now read-only.

Ensure double-checked locking when loading Jwk definitions #1405

Closed
@cgaskill

Description

@cgaskill

https://github.com/spring-projects/spring-security-oauth/blob/cc4c0a52086309a6eed3fd8a2304b31055e7c9d7/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/token/store/jwk/JwkDefinitionSource.java#L89

JwkDefinitionSource.getDefinitionLoadIfNecessary should check if the definition has been loaded in the synchronized before clearing and loading definitions. The current implementation has the side effect of a second thread waiting for the lock will attempt to also reload the definitions.

The following code should prevent this duplicate work.

JwkDefinitionHolder getDefinitionLoadIfNecessary(String keyId) {
	JwkDefinitionHolder result = this.getDefinition(keyId);
	if (result != null) {
		return result;
	}
	synchronized (this.jwkDefinitions) {
		result = this.getDefinition(keyId);
		if(result != null) {
			return result;
		}
		this.jwkDefinitions.clear();
		for (URL jwkSetUrl : jwkSetUrls) {
			this.jwkDefinitions.putAll(loadJwkDefinitions(jwkSetUrl));
		}
		return this.getDefinition(keyId);
	}
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions