fix(content_id): validate the frozen CID profile at every ingress - #73
Merged
Conversation
"Every ContentId is CIDv1 + dag-cbor (0x71) + BLAKE3 (0x1e) + a 32-byte digest"
was true for MINTED ids (from_canonical_bytes / _checked / from_blake3_content_
digest) but NOT for ids entering through parsing/conversion: from_bytes, FromStr,
binary Deserialize, and `impl From<Cid>` wrapped whatever the inner `Cid`
accepted, with no profile check. A successfully-parsed foreign CID (a CIDv0, a
non-dag-cbor codec, a SHA-256 hash, or a non-32-byte digest) could then reach
`digest_bytes()` / `digest_hex()` and PANIC on their `.expect(...)`. The Python
face inherited this via ContentId.parse / from_bytes and decoded IPLD links.
Fix (the invariant is now enforced, not merely asserted):
- Add `validate_profile(&Cid)` checking version==V1, codec==0x71, hash==0x1e,
digest.len()==32, and route ALL ingress through it: `from_bytes`, `FromStr`,
binary `Deserialize`, and a new `TryFrom<Cid>` that REPLACES the infallible
`From<Cid>` (the outward `From<ContentId> for Cid` stays — dropping the newtype
is always safe). A foreign CID is rejected with the new additive
`ContentError::InvalidCidProfile { reason }` (ContentError is #[non_exhaustive],
so the variant is additive — not a breaking change).
- Python: the decoded-IPLD-link path now uses `try_from` and raises `ValueError`
on a foreign link; parse/from_bytes inherit the core validation.
- `digest_bytes` is now provably total (every ContentId is on-profile), so its
`expect` is unreachable for ANY id, not just minted ones.
Tests: Rust rejects CIDv0/SHA-256, non-dag-cbor codec, SHA-256 hash, non-32-byte
digest, and a foreign CID link in dag-cbor, across TryFrom/from_bytes/FromStr/
Deserialize; an on-profile id still passes every path. Python (tests/test_profile
_validation.py, run by CI's python job): from_bytes / parse / link-decode reject a
foreign CID, positive round-trip preserved.
NOTE: `impl From<Cid> for ContentId` -> `impl TryFrom<Cid>` is a breaking change,
made deliberately BEFORE the 0.1.0 freeze (it has no in-crate callers). It closes
a hole that would otherwise freeze a "fixed-profile" promise the type did not
enforce.
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
What this PR does
Enforces the frozen
ContentIdprofile — CIDv1 + dag-cbor (0x71) + BLAKE3(
0x1e) + a 32-byte digest — at every ingress, closing a hole surfaced by theREADME/STABILITY cleanup (#72): the "fixed-profile newtype" promise held for
minted ids but not for parsed/converted ones, and a foreign CID could reach a
presentation accessor and panic.
The hole
from_canonical_bytes/from_canonical_bytes_checked/from_blake3_content_digestalways produce the profile. But
from_bytes,FromStr, binaryDeserialize, andimpl From<Cid>wrapped whatever the innerCidaccepted — no version/codec/hash/digest-length check. A well-formed foreign CID (CIDv0, non-dag-cbor codec, SHA-256
hash, or non-32-byte digest) could therefore become a
ContentIdand then hit the.expect(…)indigest_bytes()/digest_hex(). Python inherited it viaContentId.parse/from_bytesand decoded IPLD (tag-42) links.The fix
validate_profile(&Cid)checksversion == V1,codec == 0x71,hash().code() == 0x1e,hash().digest().len() == 32.from_bytes,FromStr, binaryDeserialize,and a new
TryFrom<Cid>that replaces the infallibleFrom<Cid>. Theoutward
From<ContentId> for Cidis unchanged (dropping the newtype is safe).ContentError::InvalidCidProfile { reason }(the enum is#[non_exhaustive], so this is not a breaking change).try_fromand raisesValueError;parse/from_bytesinherit the core validation.digest_bytes()is now provably total — itsexpectis unreachable for anyContentId, not just minted ones.Breaking change (deliberate, pre-
0.1.0)impl From<Cid> for ContentId→impl TryFrom<Cid>. It has no in-crate callers(the full Rust gate is green), and it is made before the
0.1.0freeze preciselyso
0.1.0ships the enforced invariant rather than a promise the type doesn't keep.Test plan
content_id::profile_validation_tests): CIDv0/SHA-256, a non-dag-cborcodec, a SHA-256 hash, a non-32-byte digest, and a foreign CID link in dag-cbor
are each rejected across
TryFrom/from_bytes/FromStr/Deserialize; anon-profile id still passes every path. 6 tests, all green.
tests/test_profile_validation.py, run by CI'spythonjob):from_bytes/parse/from_canonical_dagcbor-link reject a foreign CID;positive round-trip preserved.
cargo fmt --check,clippy --all-features -D warnings,cargo test --all-features,cargo doc --all-features.Out of scope
No change to the CID byte profile, the canonical encoding, the presentation forms,
or any minting path. The docs PR #72 (which named the invariant) should rebase on
this and can then merge; the stale source doc-comment that over-attributed the
tag-42 golden (flagged during #72 review) can be reconciled separately.