-
Notifications
You must be signed in to change notification settings - Fork 155
[ES-2673] [ES-2655] Public key validations #1606
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
Conversation
Signed-off-by: Md-Humair-KK <[email protected]>
WalkthroughValidate and enforce presence/values of Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
esignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java (1)
186-196: Consider adding negative test cases for new validation branches.This positive test case is good for verifying the happy path with a complete RSA JWK. However, the new validation logic in
getJWKStringintroduces several validation branches that lack test coverage:
- Invalid algorithm for RSA (e.g.,
alg: "ES256"withkty: "RSA")- Invalid algorithm for EC (e.g.,
alg: "RS256"withkty: "EC")- Invalid
usefield (e.g.,use: "enc")- Invalid RSA exponent (e.g.,
e: "AQE")- Valid EC key with algorithm specified
🧪 Example additional test cases
@Test public void getJWKString_withInvalidAlgForRSA_thenFail() { Map<String, Object> jwkMap = new HashMap<>(); jwkMap.put("kty", "RSA"); jwkMap.put("n", "oahUIzUup5kqncCkHk5Zb1pRrLx7e6YtM-9jX1f5e6mHnZFkC2LJUZ0sEh0n5Y5KnQfW9s7d7gK2b8P0EEl0h3ZyHkWzA3YbsgzB4pDxP4RxMZ1I8xD2z3UvfA1zjvKDHz6wEweq4hVJ8nS8GzZJ2E_vb3s"); jwkMap.put("e", "AQAB"); jwkMap.put("alg", "ES256"); // Invalid for RSA EsignetException ex = Assertions.assertThrows(EsignetException.class, () -> IdentityProviderUtil.getJWKString(jwkMap)); Assertions.assertEquals(ErrorConstants.INVALID_PUBLIC_KEY, ex.getMessage()); } @Test public void getJWKString_withInvalidUse_thenFail() { Map<String, Object> jwkMap = new HashMap<>(); jwkMap.put("kty", "RSA"); jwkMap.put("n", "oahUIzUup5kqncCkHk5Zb1pRrLx7e6YtM-9jX1f5e6mHnZFkC2LJUZ0sEh0n5Y5KnQfW9s7d7gK2b8P0EEl0h3ZyHkWzA3YbsgzB4pDxP4RxMZ1I8xD2z3UvfA1zjvKDHz6wEweq4hVJ8nS8GzZJ2E_vb3s"); jwkMap.put("e", "AQAB"); jwkMap.put("use", "enc"); // Invalid - only "sig" allowed EsignetException ex = Assertions.assertThrows(EsignetException.class, () -> IdentityProviderUtil.getJWKString(jwkMap)); Assertions.assertEquals(ErrorConstants.INVALID_PUBLIC_KEY, ex.getMessage()); } @Test public void getJWKString_withInvalidRSAExponent_thenFail() { Map<String, Object> jwkMap = new HashMap<>(); jwkMap.put("kty", "RSA"); jwkMap.put("n", "oahUIzUup5kqncCkHk5Zb1pRrLx7e6YtM-9jX1f5e6mHnZFkC2LJUZ0sEh0n5Y5KnQfW9s7d7gK2b8P0EEl0h3ZyHkWzA3YbsgzB4pDxP4RxMZ1I8xD2z3UvfA1zjvKDHz6wEweq4hVJ8nS8GzZJ2E_vb3s"); jwkMap.put("e", "Aw"); // Invalid exponent (base64url of 3) EsignetException ex = Assertions.assertThrows(EsignetException.class, () -> IdentityProviderUtil.getJWKString(jwkMap)); Assertions.assertEquals(ErrorConstants.INVALID_PUBLIC_KEY, ex.getMessage()); }
📜 Review details
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.javaesignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: sacrana0
Repo: mosip/esignet PR: 1594
File: esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java:253-280
Timestamp: 2025-12-23T05:15:42.700Z
Learning: In the MOSIP esignet codebase, the property `mosip.esignet.public-key-hash.fields` (which configures RSA and EC hash fields for public key hashing) is permanently defined in application default properties and is guaranteed to be non-null. Therefore, null checks for this configuration value in IdentityProviderUtil are not required.
📚 Learning: 2025-12-23T05:15:42.700Z
Learnt from: sacrana0
Repo: mosip/esignet PR: 1594
File: esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java:253-280
Timestamp: 2025-12-23T05:15:42.700Z
Learning: In the MOSIP esignet codebase, the property `mosip.esignet.public-key-hash.fields` (which configures RSA and EC hash fields for public key hashing) is permanently defined in application default properties and is guaranteed to be non-null. Therefore, null checks for this configuration value in IdentityProviderUtil are not required.
Applied to files:
esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.javaesignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java
🧬 Code graph analysis (1)
esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java (1)
esignet-core/src/main/java/io/mosip/esignet/core/constants/ErrorConstants.java (1)
ErrorConstants(8-90)
🔇 Additional comments (2)
esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java (2)
221-261: Validation logic is well-structured and addresses key security concerns.The enhanced validation correctly:
- Enforces algorithm-key type compatibility when
algis specified- Restricts
useto signature keys only- Enforces standard RSA public exponent (65537 encoded as "AQAB")
The comment on line 231 appropriately documents why custom validations are needed.
One minor consideration: the
jwk.get("e")comparison at line 245 relies on the value being aString. If a malformed JWK contains a non-String type fore, the.equals("AQAB")call will safely returnfalse(triggering the exception), so this is not a bug. However, if you want to be defensive, you could add an explicit type check.
68-77: Document the rationale for limiting RSA algorithms to RS256 and PS256. The current implementation intentionally restrictsRSA_ALGORITHMSto only 256-bit variants (RS256, PS256), which differs fromEC_ALGORITHMSthat supports all standard variants (ES256, ES384, ES512). While test evidence confirms this limitation is by design—TokenServiceTest.java verifies that PS384 is properly rejected as INVALID_PUBLIC_KEY—the design rationale is not documented in code comments. Add a comment explaining whether this restriction is for security policy compliance, performance considerations, or other reasons.Likely an incorrect or invalid review comment.
Signed-off-by: Md-Humair-KK <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
esignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java (2)
224-234: LGTM with a minor suggestion.The test correctly validates that RSA exponent must be
"AQAB"(65537). Note that"Aw"decodes to 3, which is mathematically valid but considered insecure. Consider adding a brief comment clarifying the security rationale.📝 Optional: Add clarifying comment
@Test public void getJWKString_withInvalidRSAExponent_thenFail() { Map<String, Object> jwkMap = new HashMap<>(); jwkMap.put("kty", "RSA"); jwkMap.put("n", "oahUIzUup5kqncCkHk5Zb1pRrLx7e6YtM-9jX1f5e6mHnZFkC2LJUZ0sEh0n5Y5KnQfW9s7d7gK2b8P0EEl0h3ZyHkWzA3YbsgzB4pDxP4RxMZ1I8xD2z3UvfA1zjvKDHz6wEweq4hVJ8nS8GzZJ2E_vb3s"); - jwkMap.put("e", "Aw"); + jwkMap.put("e", "Aw"); // Base64url for 3; rejected because only 65537 ("AQAB") is allowed EsignetException ex = Assertions.assertThrows(EsignetException.class, () -> IdentityProviderUtil.getJWKString(jwkMap)); Assertions.assertEquals(ErrorConstants.INVALID_PUBLIC_KEY, ex.getMessage()); }
236-246: Add EC-specific validation failure tests to match RSA test coverage.The implementation validates EC keys symmetrically with RSA keys (lines 236-237: algorithm validation via EC_ALGORITHMS containing ES256, ES384, ES512; lines 240-241: use validation requiring "sig"). However, test coverage is asymmetric: RSA has five tests including failure cases, while EC has only the valid-key test.
Consider adding corresponding EC failure tests:
getJWKString_withInvalidAlgForEC_thenFail– EC key with RSA algorithm (e.g., "RS256")getJWKString_withInvalidUseForEC_thenFail– EC key with non-signature use (e.g., "enc")This ensures both validation paths are equally exercised in tests.
📜 Review details
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
esignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: sacrana0
Repo: mosip/esignet PR: 1594
File: esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java:253-280
Timestamp: 2025-12-23T05:15:42.700Z
Learning: In the MOSIP esignet codebase, the property `mosip.esignet.public-key-hash.fields` (which configures RSA and EC hash fields for public key hashing) is permanently defined in application default properties and is guaranteed to be non-null. Therefore, null checks for this configuration value in IdentityProviderUtil are not required.
📚 Learning: 2025-12-23T05:15:42.700Z
Learnt from: sacrana0
Repo: mosip/esignet PR: 1594
File: esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java:253-280
Timestamp: 2025-12-23T05:15:42.700Z
Learning: In the MOSIP esignet codebase, the property `mosip.esignet.public-key-hash.fields` (which configures RSA and EC hash fields for public key hashing) is permanently defined in application default properties and is guaranteed to be non-null. Therefore, null checks for this configuration value in IdentityProviderUtil are not required.
Applied to files:
esignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java
🧬 Code graph analysis (1)
esignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java (1)
esignet-core/src/main/java/io/mosip/esignet/core/constants/ErrorConstants.java (1)
ErrorConstants(8-90)
🔇 Additional comments (3)
esignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java (3)
186-196: LGTM!The test correctly validates that a full RSA JWK with
kty,n,e,alg, andusefields is accepted. The assertion confirms the key type is properly included in the output.
198-209: LGTM!Good test for algorithm-key type mismatch. Uses modern
assertThrowspattern appropriately.
211-222: LGTM!Correctly validates that only
"sig"key use is accepted, rejecting"enc"as expected.
esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java
Outdated
Show resolved
Hide resolved
esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Md-Humair-KK <[email protected]>
Signed-off-by: Md-Humair-KK <[email protected]>
Signed-off-by: Md-Humair-KK <[email protected]>
sacrana0
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In
`@esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java`:
- Around line 221-247: In getJWKString, add explicit allow-list checks for alg
and use before constructing RsaJsonWebKey or EllipticCurveJsonWebKey: verify use
equals "sig" and for keyType == "RSA" ensure alg is one of
{"RS256","RS384","RS512"}, and for keyType == "EC" ensure alg is one of
{"ES256","ES384","ES512"}; if any check fails log an error and throw
EsignetException(ErrorConstants.INVALID_PUBLIC_KEY) so mismatched key/alg or
wrong use are rejected prior to calling RsaJsonWebKey or
EllipticCurveJsonWebKey.
In
`@esignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java`:
- Around line 231-254: The two tests call IdentityProviderUtil.getJWKString but
each omits the complementary JWK field so they fail for missing fields rather
than invalid values; update getJWKString_withInvalidAlgForRSA_thenFail to
include a valid "use" (e.g., "sig") alongside the invalid "alg" ("ES256"), and
update getJWKString_withInvalidUse_thenFail to include a valid "alg" (e.g.,
"RS256") alongside the invalid "use" ("enc"), so the failure is triggered by
invalid value checks in IdentityProviderUtil.getJWKString.
esignet-core/src/main/java/io/mosip/esignet/core/util/IdentityProviderUtil.java
Show resolved
Hide resolved
esignet-core/src/test/java/io/mosip/esignet/core/IdentityProviderUtilTest.java
Show resolved
Hide resolved
Signed-off-by: Md-Humair-KK <[email protected]>
Signed-off-by: Md-Humair-KK <[email protected]>
Added custom validations on top of public key validations
Summary by CodeRabbit
Bug Fixes
Tests
Documentation
Style
✏️ Tip: You can customize this high-level summary in your review settings.