Add JWK parsing limits and align TS implementation with Rust#15
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7ac206a0d2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Greptile SummaryThis PR aligns the TypeScript Confidence Score: 4/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
subgraph "toJWK / toJWKString"
A[KeyData + Options] --> B{key.type?}
B -->|public| C[encodeBase64Url bytes]
B -->|private| D{includePrivate?}
D -->|false| C
D -->|true| E[encodeBase64Url public + private]
C --> F{kid provided?}
E --> F
F -->|yes| G[Add kid to JWK]
F -->|no| H[PQJwk]
G --> H
H -->|toJWKString| I[JSON.stringify]
end
subgraph "fromJWKString"
J[JSON string] --> K{size ≤ 64 KiB?}
K -->|no| L[Error: max size]
K -->|yes| M[extractTopLevelKeys]
M --> N{fields ≤ 32?}
N -->|no| O[Error: too many fields]
N -->|yes| P{duplicate known fields?}
P -->|yes| Q[Error: duplicate field]
P -->|no| R[JSON.parse]
R --> S[fromJWK]
end
subgraph "fromJWK"
S --> T{Validate kty, alg, x, d, kid}
T -->|invalid| U[InvalidInputError]
T -->|valid| V[decodeBase64Url x]
V --> W[validateTrailingBits RFC 4648]
W --> X{has d field?}
X -->|yes| Y[decodeBase64Url d → private KeyData]
X -->|no| Z[public KeyData]
end
Last reviewed commit: 7ac206a |

fromJWKString / toJWKString — new functions for JSON string round-trips with the same resource limits (64 KiB size, 32 fields, duplicate known field rejection)
Base64 trailing bits validation (RFC 4648 §3.5) — reject non-canonical base64 matching Rust strictness
kid field support — toJWK accepts kid option, fromJWK validates kid type
Test coverage — from 53 → 86 tests; all 18 algorithms round-tripped through JWK object and JSON string paths, plus error cases for bad kty/alg/x/d, size mismatch, oversized input, field count, and duplicates