fix(mercury,castor): replace unsafe JWK as any casts with runtime type validation - #645
Open
A-Chronicle wants to merge 1 commit into
Open
fix(mercury,castor): replace unsafe JWK as any casts with runtime type validation#645A-Chronicle wants to merge 1 commit into
A-Chronicle wants to merge 1 commit into
Conversation
…e validation Remove unsafe `as any` casts when accessing `x` and `kid` fields on publicKeyJwk. Use the SDKs existing `expect()` utility and explicit type guards to validate that JWK coordinates are present before passing them to cryptographic operations. - DIDCommDIDResolver: use `expect()` instead of `as any` for `x`, drop unnecessary `as any` for `kid` (already typed on JWK.Base) - prism/index.ts: replace `as any` on `x` with typeof guard throwing InvalidKeyError when x is missing or not a string - Add test: missing JWK x coordinate in DIDDoc throws InvalidKeyError Fixes hyperledger-identus#625 Signed-off-by: A-Chronicle <chaubeyanshika319@gmail.com>
|
Contributor
Author
|
@elribonazo — this PR replaces unsafe as any casts in JWK handling with proper runtime type validation (fixes #625). CI is green and SonarCloud passed. Would appreciate a review when you get a chance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Description
Replace unsafe
as anycasts when accessingxandkidfields onpublicKeyJwkwith proper runtime validation. The previous casts allowed malformed JWK data (missing coordinates, wrong types) to silently pass through the cryptographic verification layer.DIDCommDIDResolver.ts:
publicKeyJwk?.x as any→expect(method.publicKeyJwk?.x, CastorError.InvalidKeyError)which throwsInvalidKeyErrorifxis missing at runtime.(method.publicKeyJwk as any).kid→method.publicKeyJwk?.kid(theas anywas unnecessary sincekidis already typed onJWK.Base).prism/index.ts:
publicKeyJwk.x as any→ explicittypeofguard throwingCastorError.InvalidKeyErrorwhenxis missing or not a string.Existing Mercury DIDResolver test (happy path) continues to pass. New test validates that a verification method with JWK lacking
xthrowsCastorError.InvalidKeyError.Fixes #625
Alternatives Considered (optional)
parseJWKCoordinateutility — the existingexpect()helper and explicit type guards are simpler and sufficient for these two locations!) — this would only silence TypeScript without runtime protectionChecklist