Skip to content

Commit 22dd493

Browse files
committed
fix(openconditions): preserve invalid Unicode contract
1 parent b4eaa64 commit 22dd493

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

packages/openconditions-contrib-client/src/conformance.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ describe("cross-repo conformance", () => {
5454
it("canonicalClaimBytes matches the pinned RFC 8785 hex", () => {
5555
expect(toHex(canonicalClaimBytes(FIXED_CLAIM))).toBe(PINNED_CLAIM_HEX);
5656
});
57+
58+
it("rejects invalid Unicode through the public TypeError contract", () => {
59+
expect(() => canonicalClaimBytes({ note: "\ud800" })).toThrow(TypeError);
60+
});
5761
});
5862

5963
describe("round-trip signing", () => {

packages/openconditions-contrib-client/src/jcs.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const encoder = new TextEncoder();
1515
* `canonicalize` package (the JCS reference implementation, EXACT version
1616
* @openconditions/contrib-core uses); nothing here is hand-rolled.
1717
*
18-
* @throws TypeError when the value contains a non-finite number, is nested too
19-
* deeply to canonicalize, or is not JSON-serializable at all
18+
* @throws TypeError when the value contains a non-finite number or lone
19+
* surrogate, is nested too deeply to canonicalize, or is not JSON-serializable at all
2020
* (undefined, function, symbol); Error on a circular reference.
2121
*/
2222
export function canonicalClaimBytes(claim: unknown): Uint8Array<ArrayBuffer> {
@@ -30,6 +30,9 @@ export function canonicalClaimBytes(claim: unknown): Uint8Array<ArrayBuffer> {
3030
if (err instanceof Error && /NaN|Infinity/.test(err.message)) {
3131
throw new TypeError("canonicalClaimBytes: non-finite number in value");
3232
}
33+
if (err instanceof Error && /Lone surrogate/.test(err.message)) {
34+
throw new TypeError("canonicalClaimBytes: lone surrogate in value");
35+
}
3336
throw err;
3437
}
3538
if (text === undefined) {

0 commit comments

Comments
 (0)