fix(caip): reject CAIP-2/CAIP-10 IDs with extra colon-delimited segments - #150
fix(caip): reject CAIP-2/CAIP-10 IDs with extra colon-delimited segments#150batuhankocyigit wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
WalkthroughThe CAIP-2 and CAIP-10 parsers now reject identifiers with extra colon-delimited segments. Tests cover the new validation and existing invalid-input cases. ChangesCAIP identifier segment validation
Estimated code review effort: 2 (Simple) | ~10 minutes Mergeability Score: ⚪ Minimal · up to This PR makes CAIP parsing reject malformed IDs with extra segments while preserving valid parsing behavior; the affected tests and downstream package checks pass, so no actionable merge-blocking risk remains beyond normal checks. Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Bug
caip2Partsandcaip10Partsparse their input withcaip.split(":")anddestructure the expected number of parts, but never check whether there
were more parts than expected. A string like
"eip155:1:evil"is invalidper the CAIP-2 spec (the reference component
[-_a-zA-Z0-9]{1,32}cannotcontain a colon), but
caip2Partscurrently accepts it, silently droppingthe "evil" segment and returning
{ namespace: "eip155", reference: "1" }instead of throwing. Same issue in
caip10Partswith a 4th segment.This mirrors the exact bug class fixed in
createCaip10AccountIdin #67 —that fix validates with
caip2ChainIdRegex/caip10AccountAddressRegexbefore constructing an ID, but the "Parts" parsing functions going the
other direction (string → components) were never covered, and have no
tests for malformed multi-segment input.
Both functions are exported from
@agentcommercekit/caip's public API, sodownstream consumers calling them directly on untrusted input (e.g. a CAIP
string embedded in a DID or credential) would get a silently-truncated
parse instead of a validation error.
Current call sites in this repo (
packages/did/src/methods/did-pkh.ts)happen to already validate upstream with
isCaip10AccountId/createCaip10AccountId, so this isn't exploitable through them today —this is a defense-in-depth / spec-conformance fix for the exported function
itself.
Fix
Both functions now also check that
split(":")produced exactly theexpected number of segments, throwing the existing error message otherwise.
Testing
Added tests reproducing the bug (confirmed they fail on unpatched
main:"expected [Function] to throw an error"), passing with the fix. Also added
missing basic coverage for
caip10Parts, which had none before.packages/caip: 86/86 passing (was 27/27 before this PR's added tests)packages/did(the package that actually callscaip10Parts): 74/74passing, no regressions
oxlintandtsc --noEmit: cleanAI usage disclosure
Per AI_POLICY.md: this fix was found and implemented with Claude (Anthropic).
I asked it to review the caip package for validation gaps given the recent
#67 fix in the same area; it identified the split-based truncation issue,
wrote the failing tests, confirmed them against unpatched
main, thenapplied and verified the fix and ran the affected package + downstream
consumer test suites. I've reviewed the diff and understand the bug and
the fix — happy to answer any questions about it.
Summary by CodeRabbit
Bug Fixes
Tests