An AT Protocol Personal Data Server written in JavaScript pdsjs.dev

Space records containing $link, $bytes or integers >= 2^32 get CIDs that don't match other implementations #4

Open trezy.codes
Open opened by trezy.codes

Writing a space record whose value contains a CID link, bytes, or a large integer produces a different CID than other implementations produce for the same record. Since the space's commit hash is computed from those CIDs, another host can't reproduce it. For example, any record that holds a blob ref ({"ref": {"$link": …}}) can't be migrated into pds.js with its hash intact.

Steps #

Call com.atproto.space.putRecord with each record below, then compare the returned cid:

record pds.js reference PDS
{"$type":"x.y.z","l":{"$link":"bafyreihn37cccblw3mmwmmm6jewugcohecjf5kpzuciirdvisasq7k3siu"}} bafyreigun4a37nt6p44ukdfhqf3entqajk3xz6y45oukbajhhxkelzbsuu bafyreifmellnasy2yzh6oqv5neyemd42ul2bue5ktuaqy3tplfudd2hlki
{"$type":"x.y.z","r":{"$bytes":"AAECAwQFBgc"}} bafyreieyx42erp3sdfu4uzafoe4lyuaff4ouzxqupvz4a7g7f7mq7h2zby bafyreif5yjfrjqzh5p3lzqunrl5ipzy3l7ft5topuxfn2lxw6fwhxffpoa
{"$type":"x.y.z","n":9007199254740991} bafyreidapehd433jxrrrhgwqczcxtee2uowmdn5mn2yqtsqigsj7jzva2a bafyreidvgrj72eun7jf35d4bgskkehomonug2nbpvsuppecfiqimhkuszm

Plain JSON values (strings, small integers, nulls, arrays, nested objects) give matching CIDs.

Cause #

  • packages/spaces/src/writer.js passes the record JSON straight to cborEncodeDagCbor. {"$link": …} is encoded as a map instead of a CID, and {"$bytes": …} as a map instead of a byte string. It needs the same JSON-to-data-model conversion used for public records.
  • encodeHead in packages/core/src/repo.js (around line 57) has no branch for values >= 2^32, so larger integers aren't encoded correctly.

Dug a bit further into the large-integer case. The value isn't just encoded differently: it gets dropped from the block entirely, so the stored CBOR is malformed.

Repro:

  1. com.atproto.space.putRecord with {"$type": "com.example.conformance.note", "v": 9007199254740991}.
  2. pds.js returns bafyreianpq5ttxqgvpv2bv4t3fxb5jgnc4lcigkwyqpjg6bcghxfyfh32y.
  3. In com.atproto.space.getRepo, the record's block is:
a2                      map, 2 entries
  61 76                 "v"
                        <- no value here
  65 24 74 79 70 65     "$type"
  78 1c 63 6f 6d 2e …   "com.example.conformance.note"

The header says two entries, but there are only three items. The v value is missing, so "$type" gets read as v's value and the NSID as a dangling key. Hashing exactly those bytes gives pds.js's CID, so that's what's being stored and hashed.

The correct encoding of v is 1b 00 1f ff ff ff ff ff ff (uint64), giving bafyreiciwjycqqgzdnzlb27b423cbfkwpazbuvwr6cuo2iky2izjpcy47i. Same thing for negatives past 32 bits: -4294967297 should be 3b 00 00 00 01 00 00 00 00.

That fits the encodeHead note in the issue: with no branch for values >= 2^32, it looks like nothing gets written at all rather than a wrong head. Since the record goes into the commit hash, a record like this can't round-trip through any other implementation, and a strict CBOR decoder would refuse the block.

The $link / $bytes part still reproduces at this build too: the CIDs equal DRISL over the lex-JSON as-is (maps).

Sign up or Sign in to add to the discussion.