Skip to content

Commit 622ebcd

Browse files
authored
fix(schema): preserve precision when serializing integers (#190)
* fix(schema): preserve precision when serializing integers * fix(schema): preserve integer coercion compatibility * fix(schema): preserve legacy integer conversion behavior
1 parent c5583d9 commit 622ebcd

2 files changed

Lines changed: 158 additions & 2 deletions

File tree

‎packages/schema/src/converters.spec.ts‎

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,152 @@
1+
import * as asn1js from "asn1js";
12
import { describe, expect, it } from "vitest";
2-
import { AsnAnyConverter, AsnConstructedOctetStringConverter, AsnGeneralizedTimeConverter, AsnUTCTimeConverter, OctetString } from "./index";
3+
import { AsnAnyConverter, AsnConstructedOctetStringConverter, AsnConvert, AsnGeneralizedTimeConverter, AsnIntegerConverter, AsnProp, AsnPropTypes, AsnUTCTimeConverter, OctetString } from "./index";
34

45
describe("converters", () => {
6+
describe("Integer", () => {
7+
class Test {
8+
@AsnProp({ type: AsnPropTypes.Integer })
9+
public value!: string | number;
10+
}
11+
12+
// Fixed DER fixtures cover sign padding, the number/string parse boundary,
13+
// the safe integer limit, and values larger than 64 bits.
14+
const integers = [
15+
["0", "020100"],
16+
["127", "02017f"],
17+
["128", "02020080"],
18+
["-128", "020180"],
19+
["-129", "0202ff7f"],
20+
["-32767", "02028001"],
21+
["8388607", "02037fffff"],
22+
["8388608", "020400800000"],
23+
["-8388608", "0203800000"],
24+
["-8388609", "0204ff7fffff"],
25+
["9007199254740991", "02071fffffffffffff"],
26+
["9007199254740992", "020720000000000000"],
27+
["9007199254740993", "020720000000000001"],
28+
["-9007199254740991", "0207e0000000000001"],
29+
["-9007199254740992", "0207e0000000000000"],
30+
["-9007199254740993", "0207dfffffffffffff"],
31+
["18446744073709551616", "0209010000000000000000"],
32+
["-18446744073709551616", "0209ff0000000000000000"],
33+
["123456789012345678901234567890", "020d018ee90ff6c373e0ee4e3f0ad2"],
34+
["-123456789012345678901234567890", "020dfe7116f0093c8c1f11b1c0f52e"],
35+
];
36+
37+
it.each(integers)("serializes decimal string %s exactly", (value, hex) => {
38+
const asn = AsnIntegerConverter.toASN(value);
39+
expect(Buffer.from(asn.toBER()).toString("hex")).toBe(hex);
40+
});
41+
42+
it.each(integers)("serializes a directly assigned decimal string %s", (value, hex) => {
43+
const obj = new Test();
44+
obj.value = value;
45+
const integer = Buffer.from(hex, "hex");
46+
const expected = Buffer.concat([Buffer.from([0x30, integer.length]), integer]);
47+
48+
expect(Buffer.from(AsnConvert.serialize(obj))).toEqual(expected);
49+
});
50+
51+
it.each(integers)("preserves %s when parsing and serializing", (value, hex) => {
52+
const integer = Buffer.from(hex, "hex");
53+
const der = Buffer.concat([Buffer.from([0x30, integer.length]), integer]);
54+
const obj = AsnConvert.parse(der, Test);
55+
56+
expect(obj.value).toBe(integer[1] >= 4 ? value : Number(value));
57+
expect(Buffer.from(AsnConvert.serialize(obj))).toEqual(der);
58+
});
59+
60+
it.each(integers.filter(([value]) => Number.isSafeInteger(Number(value))))("preserves numeric input %s", (value, hex) => {
61+
const asn = AsnIntegerConverter.toASN(Number(value));
62+
expect(Buffer.from(asn.toBER()).toString("hex")).toBe(hex);
63+
});
64+
65+
it.each([
66+
[2 ** 53, "020720000000000000"],
67+
[2 ** 60, "02081000000000000000"],
68+
[2 ** 64, "0209010000000000000000"],
69+
] as const)("preserves integer number input %s above the safe integer limit", (value, hex) => {
70+
const asn = AsnIntegerConverter.toASN(value);
71+
expect(Buffer.from(asn.toBER()).toString("hex")).toBe(hex);
72+
});
73+
74+
it.each([
75+
["1e3", "020203e8"],
76+
["1e20", "0209056bc75e2d63100000"],
77+
["1.0", "020101"],
78+
["9007199254740992.0", "020720000000000000"],
79+
["9007199254740993.0", "020720000000000000"],
80+
["18446744073709551616.0", "0209010000000000000000"],
81+
["9007199254740993e0", "020720000000000000"],
82+
[" 42 ", "02012a"],
83+
["0x10", "020110"],
84+
["", "020100"],
85+
[" \t\n", "020100"],
86+
])("preserves legacy numeric string input %s", (value, hex) => {
87+
const asn = AsnIntegerConverter.toASN(value);
88+
expect(Buffer.from(asn.toBER()).toString("hex")).toBe(hex);
89+
});
90+
91+
it.each(["invalid", "1.5", "1e-3", "Infinity", "1e309", "1_000"])("rejects invalid numeric string input %s", (value) => {
92+
expect(() => AsnIntegerConverter.toASN(value)).toThrow(RangeError);
93+
});
94+
95+
it.each(["2147483647", "9007199254740992", "-9007199254740992", "18446744073709551616", "-18446744073709551616", " -0 "])(
96+
"preserves legacy Integer metadata for exactly representable decimal string %s",
97+
(value) => {
98+
const asn = AsnIntegerConverter.toASN(value);
99+
const legacy = new asn1js.Integer({ value: +value });
100+
101+
expect(asn.valueBlock.valueDec).toBe(legacy.valueBlock.valueDec);
102+
expect(asn.valueBlock.isHexOnly).toBe(legacy.valueBlock.isHexOnly);
103+
expect(asn.toJSON()).toEqual(legacy.toJSON());
104+
},
105+
);
106+
107+
it.each([
108+
[" +9007199254740993 ", "020720000000000001"],
109+
[" \t-9007199254740993\n", "0207dfffffffffffff"],
110+
])("serializes signed decimal string %s exactly", (value, hex) => {
111+
const asn = AsnIntegerConverter.toASN(value);
112+
expect(Buffer.from(asn.toBER()).toString("hex")).toBe(hex);
113+
});
114+
115+
it.each([
116+
[(1n << 1024n).toString(), `02818101${"00".repeat(128)}`],
117+
[(-(1n << 1024n)).toString(), `028181ff${"00".repeat(128)}`],
118+
])("serializes decimal string %s beyond the finite Number range exactly", (value, hex) => {
119+
const asn = AsnIntegerConverter.toASN(value);
120+
expect(Number.isFinite(+value)).toBe(false);
121+
expect(Buffer.from(asn.toBER()).toString("hex")).toBe(hex);
122+
expect(asn.toBigInt()).toBe(BigInt(value));
123+
});
124+
125+
it("rejects undefined runtime input", () => {
126+
expect(() => AsnIntegerConverter.toASN(undefined as unknown as number)).toThrow(RangeError);
127+
});
128+
129+
it("rejects holes in repeated INTEGER properties", () => {
130+
class RepeatedTest {
131+
@AsnProp({ type: AsnPropTypes.Integer, repeated: "sequence" })
132+
public values: number[] = [1, 2];
133+
}
134+
const obj = new RepeatedTest();
135+
delete obj.values[0];
136+
137+
expect(() => AsnConvert.serialize(obj)).toThrow(RangeError);
138+
});
139+
140+
it.each([
141+
[null, "020100"],
142+
[false, "020100"],
143+
[true, "020101"],
144+
] as const)("preserves numeric coercion for runtime input %s", (value, hex) => {
145+
const asn = AsnIntegerConverter.toASN(value as unknown as number);
146+
expect(Buffer.from(asn.toBER()).toString("hex")).toBe(hex);
147+
});
148+
});
149+
5150
describe("Any", () => {
6151
it("null", () => {
7152
const asn = AsnAnyConverter.toASN(null);

‎packages/schema/src/converters.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@ export const AsnIntegerConverter: IAsnConverter<IntegerConverterType, asn1js.Int
3535
value.valueBlock.valueHexView.byteLength >= 4
3636
? value.valueBlock.toString() // use string format
3737
: value.valueBlock.valueDec, // use number format
38-
toASN: (value: IntegerConverterType) => new asn1js.Integer({ value: +value }),
38+
toASN: (value: IntegerConverterType) => {
39+
if (typeof value === "string" && /^\s*[+-]?\d+\s*$/.test(value)) {
40+
const exact = BigInt(value);
41+
const numeric = +value;
42+
// Preserve the legacy Integer metadata whenever Number retains the exact value.
43+
if (Number.isInteger(numeric) && BigInt(numeric) === exact) {
44+
return new asn1js.Integer({ value: numeric });
45+
}
46+
return asn1js.Integer.fromBigInt(exact);
47+
}
48+
return new asn1js.Integer({ value: +value });
49+
},
3950
};
4051

4152
/**

0 commit comments

Comments
 (0)