Skip to content

fix(content_id): validate the frozen CID profile at every ingress - #73

Merged
hartsock merged 1 commit into
mainfrom
fix/contentid-profile-validation
Aug 4, 2026
Merged

fix(content_id): validate the frozen CID profile at every ingress#73
hartsock merged 1 commit into
mainfrom
fix/contentid-profile-validation

Conversation

@hartsock

@hartsock hartsock commented Aug 4, 2026

Copy link
Copy Markdown
Owner

What this PR does

Enforces the frozen ContentId profile — CIDv1 + dag-cbor (0x71) + BLAKE3
(0x1e) + a 32-byte digest
— at every ingress, closing a hole surfaced by the
README/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_digest
always produce the profile. But from_bytes, FromStr, binary Deserialize, and
impl From<Cid> wrapped whatever the inner Cid accepted — 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 ContentId and then hit the
.expect(…) in digest_bytes() / digest_hex(). Python inherited it via
ContentId.parse / from_bytes and decoded IPLD (tag-42) links.

The fix

  • validate_profile(&Cid) checks version == V1, codec == 0x71,
    hash().code() == 0x1e, hash().digest().len() == 32.
  • All ingress routes through it: from_bytes, FromStr, binary Deserialize,
    and a new TryFrom<Cid> that replaces the infallible From<Cid>. The
    outward From<ContentId> for Cid is unchanged (dropping the newtype is safe).
  • New additive ContentError::InvalidCidProfile { reason } (the enum is
    #[non_exhaustive], so this is not a breaking change).
  • Python: the decoded-link path uses try_from and raises ValueError; parse /
    from_bytes inherit the core validation.
  • digest_bytes() is now provably total — its expect is unreachable for any
    ContentId, not just minted ones.

Breaking change (deliberate, pre-0.1.0)

impl From<Cid> for ContentIdimpl TryFrom<Cid>. It has no in-crate callers
(the full Rust gate is green), and it is made before the 0.1.0 freeze precisely
so 0.1.0 ships the enforced invariant rather than a promise the type doesn't keep.

Test plan

  • Rust (content_id::profile_validation_tests): CIDv0/SHA-256, a non-dag-cbor
    codec, 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; an
    on-profile id still passes every path. 6 tests, all green.
  • Python (tests/test_profile_validation.py, run by CI's python job):
    from_bytes / parse / from_canonical_dagcbor-link reject a foreign CID;
    positive round-trip preserved.
  • Full gate green locally: 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.

"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>
@hartsock
hartsock merged commit 7678df4 into main Aug 4, 2026
8 checks passed
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.

1 participant