Skip to content

fix(mercury,castor): replace unsafe JWK as any casts with runtime type validation - #645

Open
A-Chronicle wants to merge 1 commit into
hyperledger-identus:mainfrom
A-Chronicle:fix/secure-jwk-casts
Open

fix(mercury,castor): replace unsafe JWK as any casts with runtime type validation#645
A-Chronicle wants to merge 1 commit into
hyperledger-identus:mainfrom
A-Chronicle:fix/secure-jwk-casts

Conversation

@A-Chronicle

@A-Chronicle A-Chronicle commented May 21, 2026

Copy link
Copy Markdown
Contributor

Description

Replace unsafe as any casts when accessing x and kid fields on publicKeyJwk with 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 anyexpect(method.publicKeyJwk?.x, CastorError.InvalidKeyError) which throws InvalidKeyError if x is missing at runtime. (method.publicKeyJwk as any).kidmethod.publicKeyJwk?.kid (the as any was unnecessary since kid is already typed on JWK.Base).

prism/index.ts: publicKeyJwk.x as any → explicit typeof guard throwing CastorError.InvalidKeyError when x is missing or not a string.

Existing Mercury DIDResolver test (happy path) continues to pass. New test validates that a verification method with JWK lacking x throws CastorError.InvalidKeyError.

Fixes #625

Alternatives Considered (optional)

  • Creating a shared parseJWKCoordinate utility — the existing expect() helper and explicit type guards are simpler and sufficient for these two locations
  • Using a non-null assertion (!) — this would only silence TypeScript without runtime protection

Checklist

  • My PR follows the contribution guidelines of this project
  • My PR is free of third-party dependencies that don't comply with the Allowlist
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked the PR title to follow the conventional commit specification

…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>
@A-Chronicle
A-Chronicle requested a review from a team as a code owner May 21, 2026 09:01
@sonarqubecloud

Copy link
Copy Markdown

@A-Chronicle

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Security: Unsafe type casts in DID verification with JWK parsing

1 participant