@ocap/message 1.30.23 → 1.30.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -297,13 +297,27 @@ function buildDecodeIndex(type) {
297
297
  function mapsToPlainObjects(value) {
298
298
  if (value instanceof Map) {
299
299
  const obj = {};
300
- for (const [k, v] of value.entries()) obj[String(k)] = mapsToPlainObjects(v);
300
+ for (const [k, v] of value.entries()) {
301
+ const key = String(k);
302
+ if (key === "__proto__" || key === "constructor" || key === "prototype") throw new Error(`canonical-cbor: forbidden map key "${key}"`);
303
+ obj[key] = mapsToPlainObjects(v);
304
+ }
301
305
  return obj;
302
306
  }
303
307
  if (Array.isArray(value)) return value.map(mapsToPlainObjects);
304
308
  return value;
305
309
  }
310
+ function assertCanonicalIntMapKeys(payload) {
311
+ let prevId = -1;
312
+ for (const key of payload.keys()) {
313
+ const id = typeof key === "bigint" ? Number(key) : key;
314
+ if (typeof id !== "number" || !Number.isInteger(id) || id < 0) throw new Error(`canonical-cbor: invalid map key ${String(key)}`);
315
+ if (id <= prevId) throw new Error(`canonical-cbor: map keys not in canonical ascending order (${id} after ${prevId})`);
316
+ prevId = id;
317
+ }
318
+ }
306
319
  function decodeAnyValue(payload) {
320
+ assertCanonicalIntMapKeys(payload);
307
321
  const typeUrl = payload.get(0);
308
322
  if (!typeUrl || typeof typeUrl !== "string") throw new Error("canonical-cbor: Any payload missing typeUrl at key 0");
309
323
  if (typeUrl === "json" || typeUrl === "vc" || typeUrl === "fg:x:address") return {
@@ -322,6 +336,18 @@ function decodeAnyValue(payload) {
322
336
  value: payload.get(1)
323
337
  };
324
338
  const innerObj = { typeUrl };
339
+ if (BIGINT_WRAPPER_TYPES.has(innerType)) {
340
+ if (payload.has(1)) {
341
+ const decoded = decodeFieldValue(payload.get(1), {
342
+ id: 1,
343
+ type: innerType
344
+ });
345
+ if (decoded && typeof decoded === "object" && "value" in decoded) Object.assign(innerObj, decoded);
346
+ else innerObj.value = decoded;
347
+ }
348
+ if (innerType === "BigSint" && payload.has(2)) innerObj.minus = Boolean(payload.get(2));
349
+ return innerObj;
350
+ }
325
351
  for (const [id, raw] of payload.entries()) {
326
352
  if (id === 0) continue;
327
353
  const descriptor = Object.entries(innerFields).find(([, f]) => f.id === id);
@@ -341,17 +367,18 @@ function decodeFieldValue(raw, field) {
341
367
  };
342
368
  return raw.map((item) => decodeFieldValue(item, itemField));
343
369
  }
344
- if (BIGINT_WRAPPER_TYPES.has(type)) {
345
- if (raw instanceof Tagged) {
346
- if (raw.tag !== TAG_POSITIVE_BIGNUM && raw.tag !== TAG_NEGATIVE_BIGNUM) throw new Error(`canonical-cbor: BigInt wrapper expects tag 2/3, got ${raw.tag}`);
347
- const bytes = raw.value;
348
- const negative = raw.tag === TAG_NEGATIVE_BIGNUM;
349
- const result = { value: bytes };
350
- if (type === "BigSint") result.minus = negative;
351
- return result;
352
- }
353
- return raw;
370
+ if (raw instanceof Tagged) {
371
+ if (raw.tag !== TAG_POSITIVE_BIGNUM && raw.tag !== TAG_NEGATIVE_BIGNUM) throw new Error(`canonical-cbor: BigInt wrapper expects tag 2/3, got ${raw.tag}`);
372
+ if (!(raw.value instanceof Uint8Array)) throw new Error("canonical-cbor: BigInt bignum tag value must be byte string");
373
+ const bytes = raw.value;
374
+ if (bytes.length === 0 || bytes[0] === 0) throw new Error("canonical-cbor: non-canonical bignum (empty or leading zero byte)");
375
+ if (!BIGINT_WRAPPER_TYPES.has(type)) throw new Error(`canonical-cbor: unexpected bignum tag ${raw.tag} for field type "${type}"`);
376
+ const negative = raw.tag === TAG_NEGATIVE_BIGNUM;
377
+ const result = { value: bytes };
378
+ if (type === "BigSint") result.minus = negative;
379
+ return result;
354
380
  }
381
+ if (BIGINT_WRAPPER_TYPES.has(type)) return raw;
355
382
  if (TIMESTAMP_TYPES.has(type)) {
356
383
  if (typeof raw !== "string") throw new Error("canonical-cbor: Timestamp expected string");
357
384
  return raw;
@@ -370,6 +397,20 @@ function decodeFieldValue(raw, field) {
370
397
  return raw;
371
398
  }
372
399
  function decodeMessageMap(type, payload) {
400
+ assertCanonicalIntMapKeys(payload);
401
+ if (BIGINT_WRAPPER_TYPES.has(type)) {
402
+ const out$1 = {};
403
+ if (payload.has(1)) {
404
+ const decoded = decodeFieldValue(payload.get(1), {
405
+ id: 1,
406
+ type
407
+ });
408
+ if (decoded && typeof decoded === "object" && "value" in decoded) Object.assign(out$1, decoded);
409
+ else out$1.value = decoded;
410
+ }
411
+ if (type === "BigSint" && payload.has(2)) out$1.minus = Boolean(payload.get(2));
412
+ return out$1;
413
+ }
373
414
  const ctx = buildDecodeIndex(type);
374
415
  const out = {};
375
416
  for (const [id, raw] of payload.entries()) {
@@ -385,12 +426,13 @@ function decodeMessageMap(type, payload) {
385
426
  function parseCanonical(type, bytes) {
386
427
  if (!bytes || !(bytes instanceof Uint8Array)) throw new Error("canonical-cbor: input must be Uint8Array");
387
428
  if (bytes.length < 3 || bytes[0] !== SELF_DESCRIBE_PREFIX[0] || bytes[1] !== SELF_DESCRIBE_PREFIX[1] || bytes[2] !== SELF_DESCRIBE_PREFIX[2]) throw new Error("canonical-cbor: missing self-describe tag 55799 prefix");
388
- const decoded = decode(bytes, {
389
- tags: {
390
- ...Tagged.preserve(TAG_POSITIVE_BIGNUM, TAG_NEGATIVE_BIGNUM),
391
- [TAG_SELF_DESCRIBE]: (decodeFn) => decodeFn()
392
- },
393
- useMaps: true
429
+ const tags = { ...Tagged.preserve(TAG_POSITIVE_BIGNUM, TAG_NEGATIVE_BIGNUM) };
430
+ const decoded = decode(bytes.subarray(SELF_DESCRIBE_PREFIX.length), {
431
+ tags,
432
+ useMaps: true,
433
+ strict: true,
434
+ rejectDuplicateMapKeys: true,
435
+ allowIndefinite: false
394
436
  });
395
437
  if (!(decoded instanceof Map)) throw new Error("canonical-cbor: payload is not a CBOR map");
396
438
  return decodeMessageMap(type, decoded);
@@ -298,13 +298,27 @@ function buildDecodeIndex(type) {
298
298
  function mapsToPlainObjects(value) {
299
299
  if (value instanceof Map) {
300
300
  const obj = {};
301
- for (const [k, v] of value.entries()) obj[String(k)] = mapsToPlainObjects(v);
301
+ for (const [k, v] of value.entries()) {
302
+ const key = String(k);
303
+ if (key === "__proto__" || key === "constructor" || key === "prototype") throw new Error(`canonical-cbor: forbidden map key "${key}"`);
304
+ obj[key] = mapsToPlainObjects(v);
305
+ }
302
306
  return obj;
303
307
  }
304
308
  if (Array.isArray(value)) return value.map(mapsToPlainObjects);
305
309
  return value;
306
310
  }
311
+ function assertCanonicalIntMapKeys(payload) {
312
+ let prevId = -1;
313
+ for (const key of payload.keys()) {
314
+ const id = typeof key === "bigint" ? Number(key) : key;
315
+ if (typeof id !== "number" || !Number.isInteger(id) || id < 0) throw new Error(`canonical-cbor: invalid map key ${String(key)}`);
316
+ if (id <= prevId) throw new Error(`canonical-cbor: map keys not in canonical ascending order (${id} after ${prevId})`);
317
+ prevId = id;
318
+ }
319
+ }
307
320
  function decodeAnyValue(payload) {
321
+ assertCanonicalIntMapKeys(payload);
308
322
  const typeUrl = payload.get(0);
309
323
  if (!typeUrl || typeof typeUrl !== "string") throw new Error("canonical-cbor: Any payload missing typeUrl at key 0");
310
324
  if (typeUrl === "json" || typeUrl === "vc" || typeUrl === "fg:x:address") return {
@@ -323,6 +337,18 @@ function decodeAnyValue(payload) {
323
337
  value: payload.get(1)
324
338
  };
325
339
  const innerObj = { typeUrl };
340
+ if (BIGINT_WRAPPER_TYPES.has(innerType)) {
341
+ if (payload.has(1)) {
342
+ const decoded = decodeFieldValue(payload.get(1), {
343
+ id: 1,
344
+ type: innerType
345
+ });
346
+ if (decoded && typeof decoded === "object" && "value" in decoded) Object.assign(innerObj, decoded);
347
+ else innerObj.value = decoded;
348
+ }
349
+ if (innerType === "BigSint" && payload.has(2)) innerObj.minus = Boolean(payload.get(2));
350
+ return innerObj;
351
+ }
326
352
  for (const [id, raw] of payload.entries()) {
327
353
  if (id === 0) continue;
328
354
  const descriptor = Object.entries(innerFields).find(([, f]) => f.id === id);
@@ -342,17 +368,18 @@ function decodeFieldValue(raw, field) {
342
368
  };
343
369
  return raw.map((item) => decodeFieldValue(item, itemField));
344
370
  }
345
- if (BIGINT_WRAPPER_TYPES.has(type)) {
346
- if (raw instanceof require_tagged.Tagged) {
347
- if (raw.tag !== TAG_POSITIVE_BIGNUM && raw.tag !== TAG_NEGATIVE_BIGNUM) throw new Error(`canonical-cbor: BigInt wrapper expects tag 2/3, got ${raw.tag}`);
348
- const bytes = raw.value;
349
- const negative = raw.tag === TAG_NEGATIVE_BIGNUM;
350
- const result = { value: bytes };
351
- if (type === "BigSint") result.minus = negative;
352
- return result;
353
- }
354
- return raw;
371
+ if (raw instanceof require_tagged.Tagged) {
372
+ if (raw.tag !== TAG_POSITIVE_BIGNUM && raw.tag !== TAG_NEGATIVE_BIGNUM) throw new Error(`canonical-cbor: BigInt wrapper expects tag 2/3, got ${raw.tag}`);
373
+ if (!(raw.value instanceof Uint8Array)) throw new Error("canonical-cbor: BigInt bignum tag value must be byte string");
374
+ const bytes = raw.value;
375
+ if (bytes.length === 0 || bytes[0] === 0) throw new Error("canonical-cbor: non-canonical bignum (empty or leading zero byte)");
376
+ if (!BIGINT_WRAPPER_TYPES.has(type)) throw new Error(`canonical-cbor: unexpected bignum tag ${raw.tag} for field type "${type}"`);
377
+ const negative = raw.tag === TAG_NEGATIVE_BIGNUM;
378
+ const result = { value: bytes };
379
+ if (type === "BigSint") result.minus = negative;
380
+ return result;
355
381
  }
382
+ if (BIGINT_WRAPPER_TYPES.has(type)) return raw;
356
383
  if (TIMESTAMP_TYPES.has(type)) {
357
384
  if (typeof raw !== "string") throw new Error("canonical-cbor: Timestamp expected string");
358
385
  return raw;
@@ -371,6 +398,20 @@ function decodeFieldValue(raw, field) {
371
398
  return raw;
372
399
  }
373
400
  function decodeMessageMap(type, payload) {
401
+ assertCanonicalIntMapKeys(payload);
402
+ if (BIGINT_WRAPPER_TYPES.has(type)) {
403
+ const out$1 = {};
404
+ if (payload.has(1)) {
405
+ const decoded = decodeFieldValue(payload.get(1), {
406
+ id: 1,
407
+ type
408
+ });
409
+ if (decoded && typeof decoded === "object" && "value" in decoded) Object.assign(out$1, decoded);
410
+ else out$1.value = decoded;
411
+ }
412
+ if (type === "BigSint" && payload.has(2)) out$1.minus = Boolean(payload.get(2));
413
+ return out$1;
414
+ }
374
415
  const ctx = buildDecodeIndex(type);
375
416
  const out = {};
376
417
  for (const [id, raw] of payload.entries()) {
@@ -386,12 +427,13 @@ function decodeMessageMap(type, payload) {
386
427
  function parseCanonical(type, bytes) {
387
428
  if (!bytes || !(bytes instanceof Uint8Array)) throw new Error("canonical-cbor: input must be Uint8Array");
388
429
  if (bytes.length < 3 || bytes[0] !== SELF_DESCRIBE_PREFIX[0] || bytes[1] !== SELF_DESCRIBE_PREFIX[1] || bytes[2] !== SELF_DESCRIBE_PREFIX[2]) throw new Error("canonical-cbor: missing self-describe tag 55799 prefix");
389
- const decoded = require_decode.decode(bytes, {
390
- tags: {
391
- ...require_tagged.Tagged.preserve(TAG_POSITIVE_BIGNUM, TAG_NEGATIVE_BIGNUM),
392
- [TAG_SELF_DESCRIBE]: (decodeFn) => decodeFn()
393
- },
394
- useMaps: true
430
+ const tags = { ...require_tagged.Tagged.preserve(TAG_POSITIVE_BIGNUM, TAG_NEGATIVE_BIGNUM) };
431
+ const decoded = require_decode.decode(bytes.subarray(SELF_DESCRIBE_PREFIX.length), {
432
+ tags,
433
+ useMaps: true,
434
+ strict: true,
435
+ rejectDuplicateMapKeys: true,
436
+ allowIndefinite: false
395
437
  });
396
438
  if (!(decoded instanceof Map)) throw new Error("canonical-cbor: payload is not a CBOR map");
397
439
  return decodeMessageMap(type, decoded);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/message",
3
3
  "description": "Utility functions to encode and decode message that can send to forge",
4
- "version": "1.30.23",
4
+ "version": "1.30.25",
5
5
  "author": {
6
6
  "name": "wangshijun",
7
7
  "email": "shijun@arcblock.io",
@@ -18,8 +18,8 @@
18
18
  "access": "public"
19
19
  },
20
20
  "dependencies": {
21
- "@ocap/proto": "1.30.23",
22
- "@ocap/util": "1.30.23",
21
+ "@ocap/proto": "1.30.25",
22
+ "@ocap/util": "1.30.25",
23
23
  "cborg": "^5.1.0",
24
24
  "debug": "^4.4.3",
25
25
  "google-protobuf": "3.21.0",