{"record":{"id":"6e1c266dc9f4a78a","repo":"jwtk/jjwt","slug":"jwks-are-immutable-and-may-not-be-modified","errorCode":null,"errorMessage":"JWKs are immutable and may not be modified.","messagePattern":"JWKs are immutable and may not be modified\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/security/AbstractJwk.java","lineNumber":245,"sourceCode":"    }\n\n    @Override\n    public Set<String> keySet() {\n        return Collections.immutable(this.context.keySet());\n    }\n\n    @Override\n    public Collection<Object> values() {\n        return Collections.immutable(this.context.values());\n    }\n\n    @Override\n    public Set<Entry<String, Object>> entrySet() {\n        return Collections.immutable(this.context.entrySet());\n    }\n\n    private static Object immutable() {\n        throw new UnsupportedOperationException(IMMUTABLE_MSG);\n    }\n\n    @Override\n    public Object put(String s, Object o) {\n        return immutable();\n    }\n\n    @Override\n    public Object remove(Object o) {\n        return immutable();\n    }\n\n    @Override\n    public void putAll(Map<? extends String, ?> m) {\n        immutable();\n    }\n\n    @Override","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/security/AbstractJwk.java#L227-L263","documentation":"JWK instances expose a read-only Map view over their name/value pairs. Any mutation attempt (put, remove, putAll, clear) throws UnsupportedOperationException because RFC 7517 JWKs are treated as immutable once built.","triggerScenarios":"Calling jwk.put(\"kid\", v), jwk.remove(...), jwk.putAll(...), or jwk.clear() on any Jwk instance, or mutating a collection obtained via jwk.entrySet().","commonSituations":"Attempting to add a 'kid' or custom claim to an already-parsed JWK; framework code that mutates maps generically; code ported from a mutable-map style.","solutions":["Set all desired fields on the JwkContext before building: Jwks.builder().put(...).build().","Create a new JWK with the additional values instead of mutating the existing one.","If you need a mutable copy: new HashMap<>(jwk) and mutate that.","Never write through the entrySet() view; it is wrapped by Collections.immutable."],"exampleFix":"// before\njwk.put(\"kid\", \"my-key-id\");\n// after\nJwk<?> updated = Jwks.builder().putAll(jwk).put(\"kid\", \"my-key-id\").build();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"boolean isMutable(Map<String,Object> m) { try { m.size(); return !(m instanceof Jwk); } catch (UnsupportedOperationException e) { return false; } }","tryCatchPattern":"try { map.put(k, v); }\ncatch (UnsupportedOperationException e) { throw new IllegalStateException(\"JWKs are immutable; rebuild instead\", e); }","preventionTips":["Treat Jwk as read-only value objects.","Configure all fields via the builder before build().","Copy into a HashMap when mutation is needed.","Never write through entrySet()."],"tags":["immutability","jwk","unsupported-operation","jjwt"],"backgroundTag":"unsupported-operation","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}