@pairdraw/kiwi 0.16.2 → 0.17.0

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.
@@ -38,6 +38,10 @@ declare function loadWasmFigDataDecompressor(): Promise<FigDataDecompressor>;
38
38
  * this first, so a document session, its worker, and the patches they write never wait.
39
39
  */
40
40
  declare function loadFigDataDecompressor(): Promise<FigDataDecompressor>;
41
+ /**
42
+ * The chunks of a raw `fig-kiwi` container, or null for other data or fewer than two chunks.
43
+ * Fewer than four trailing bytes declare no chunk; a chunk longer than the bytes left throws.
44
+ */
41
45
  declare function parseFigKiwiChunks(binary: Uint8Array): Uint8Array[] | null;
42
46
  /** A data chunk decompressed at once; a zstd chunk needs `loadFigDataDecompressor` first. */
43
47
  declare function decompressFigKiwiData(compressed: Uint8Array): Uint8Array;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pairdraw/kiwi",
3
- "version": "0.16.2",
3
+ "version": "0.17.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -124,6 +124,10 @@ function decompressZstd(compressed: Uint8Array): Uint8Array {
124
124
  return wasmZstd.decompress(compressed)
125
125
  }
126
126
 
127
+ /**
128
+ * The chunks of a raw `fig-kiwi` container, or null for other data or fewer than two chunks.
129
+ * Fewer than four trailing bytes declare no chunk; a chunk longer than the bytes left throws.
130
+ */
127
131
  export function parseFigKiwiChunks(binary: Uint8Array): Uint8Array[] | null {
128
132
  if (!figFileKind(binary)) return null
129
133
 
@@ -131,9 +135,13 @@ export function parseFigKiwiChunks(binary: Uint8Array): Uint8Array[] | null {
131
135
  let offset = 12
132
136
 
133
137
  const chunks: Uint8Array[] = []
134
- while (offset < binary.length) {
138
+ while (offset + 4 <= binary.length) {
135
139
  const chunkLen = view.getUint32(offset, true)
136
140
  offset += 4
141
+ if (chunkLen > binary.length - offset)
142
+ throw new Error(
143
+ `Corrupted .fig file: chunk at offset ${offset - 4} declares length ${chunkLen} but only ${binary.length - offset} bytes remain`
144
+ )
137
145
  chunks.push(binary.slice(offset, offset + chunkLen))
138
146
  offset += chunkLen
139
147
  }
@@ -159,7 +159,8 @@ export function parseVarint(data: Uint8Array, pos: number): [number, number] {
159
159
  while (pos < data.length) {
160
160
  const byte = data[pos]
161
161
  pos++
162
- value |= (byte & KIWI.VARINT_VALUE_MASK) << shift
162
+ // Multiplied, not shifted: a shift wraps at 32 bits and turns the fifth byte's bits negative.
163
+ value += (byte & KIWI.VARINT_VALUE_MASK) * 2 ** shift
163
164
 
164
165
  if (!(byte & KIWI.VARINT_CONTINUE_BIT)) {
165
166
  break
@@ -2,6 +2,15 @@ let int32 = new Int32Array(1)
2
2
  let float32 = new Float32Array(int32.buffer)
3
3
  const textDecoder = new TextDecoder()
4
4
 
5
+ /**
6
+ * A read that would pass the end of the bytes. A varint is checked once, after its last byte
7
+ * rather than at each: past its end a typed array yields `undefined`, which ends the varint, so
8
+ * one that ran over finishes past `length` and is refused before its value is used.
9
+ */
10
+ function truncated(): never {
11
+ throw new Error('Unexpected end of Kiwi message')
12
+ }
13
+
5
14
  export class ByteBuffer {
6
15
  private _data: Uint8Array
7
16
  private _index: number
@@ -42,18 +51,23 @@ export class ByteBuffer {
42
51
  }
43
52
 
44
53
  readByte(): number {
45
- return this._data[this._index++]
54
+ const index = this._index
55
+ if (index >= this.length) truncated()
56
+ this._index = index + 1
57
+ return this._data[index]
46
58
  }
47
59
 
48
60
  readByteArray(): Uint8Array {
49
61
  const length = this.readVarUint()
50
62
  const start = this._index
63
+ if (length > this.length - start) truncated()
51
64
  this._index = start + length
52
65
  return this._data.slice(start, start + length)
53
66
  }
54
67
 
55
68
  skipByteArray(): void {
56
69
  const length = this.readVarUint()
70
+ if (length > this.length - this._index) truncated()
57
71
  this._index += length
58
72
  }
59
73
 
@@ -62,10 +76,12 @@ export class ByteBuffer {
62
76
  const data = this._data
63
77
  const first = data[index]
64
78
  if (first === 0) {
79
+ if (index >= this.length) truncated()
65
80
  this._index = index + 1
66
81
  return 0
67
82
  }
68
83
 
84
+ if (index + 4 > this.length) truncated()
69
85
  let bits = first | (data[index + 1] << 8) | (data[index + 2] << 16) | (data[index + 3] << 24)
70
86
  this._index = index + 4
71
87
  bits = (bits << 23) | (bits >>> 9)
@@ -78,32 +94,25 @@ export class ByteBuffer {
78
94
  let i = this._index
79
95
  let b = data[i++]
80
96
  let value = b & 127
81
- if (b < 128) {
82
- this._index = i
83
- return value
84
- }
85
- b = data[i++]
86
- value |= (b & 127) << 7
87
- if (b < 128) {
88
- this._index = i
89
- return value
90
- }
91
- b = data[i++]
92
- value |= (b & 127) << 14
93
- if (b < 128) {
94
- this._index = i
95
- return value
96
- }
97
- b = data[i++]
98
- value |= (b & 127) << 21
99
- if (b < 128) {
100
- this._index = i
101
- return value
97
+ if (b > 127) {
98
+ b = data[i++]
99
+ value |= (b & 127) << 7
100
+ if (b > 127) {
101
+ b = data[i++]
102
+ value |= (b & 127) << 14
103
+ if (b > 127) {
104
+ b = data[i++]
105
+ value |= (b & 127) << 21
106
+ if (b > 127) {
107
+ b = data[i++]
108
+ value = (value | ((b & 127) << 28)) >>> 0
109
+ }
110
+ }
111
+ }
102
112
  }
103
- b = data[i++]
104
- value |= (b & 127) << 28
113
+ if (i > this.length) truncated()
105
114
  this._index = i
106
- return value >>> 0
115
+ return value
107
116
  }
108
117
 
109
118
  readVarInt(): number {
@@ -146,9 +155,10 @@ export class ByteBuffer {
146
155
 
147
156
  private findStringTerminator(start: number): number {
148
157
  const data = this._data
158
+ const end = this.length
149
159
  let index = start
150
- while (index < data.length && data[index] !== 0) index++
151
- if (index >= data.length) throw new Error('Unterminated string in Kiwi message')
160
+ while (index < end && data[index] !== 0) index++
161
+ if (index >= end) throw new Error('Unterminated string in Kiwi message')
152
162
  return index
153
163
  }
154
164
 
@@ -245,13 +255,18 @@ export class ByteBuffer {
245
255
  let codePoint
246
256
 
247
257
  for (let i = 0; i < value.length; i++) {
248
- // Decode UTF-16
258
+ // Decode UTF-16; a surrogate without its pair is U+FFFD, as `TextEncoder` writes it
249
259
  let a = value.charCodeAt(i)
250
- if (i + 1 === value.length || a < 0xd800 || a >= 0xdc00) {
260
+ if (a < 0xd800 || a >= 0xe000) {
251
261
  codePoint = a
252
262
  } else {
253
- let b = value.charCodeAt(++i)
254
- codePoint = (a << 10) + b + (0x10000 - (0xd800 << 10) - 0xdc00)
263
+ let b = value.charCodeAt(i + 1)
264
+ if (a < 0xdc00 && b >= 0xdc00 && b < 0xe000) {
265
+ i++
266
+ codePoint = (a << 10) + b + (0x10000 - (0xd800 << 10) - 0xdc00)
267
+ } else {
268
+ codePoint = 0xfffd
269
+ }
255
270
  }
256
271
 
257
272
  // Strings are null-terminated
@@ -152,6 +152,22 @@ function invalidMessage(): never {
152
152
  throw new Error('Attempted to parse invalid message')
153
153
  }
154
154
 
155
+ /**
156
+ * An array's declared length, refused when more items are declared than bytes remain: every
157
+ * item takes at least a byte (only a struct with no fields takes none), so such an array cannot
158
+ * be in the message, and trusting its length would allocate and loop over it before any read
159
+ * found the end.
160
+ */
161
+ function arrayLength(bb: ByteBuffer): number {
162
+ const length = bb.readVarUint()
163
+ const remaining = bb.length - bb.offset
164
+ if (length > remaining)
165
+ throw new Error(
166
+ `Kiwi array declares ${length} items, more than the bytes left in the message (${remaining})`
167
+ )
168
+ return length
169
+ }
170
+
155
171
  function compileDefinitions(schema: Schema, headers?: HeaderFields): CompiledDefinition[] {
156
172
  const byName = new Map<string, Definition>()
157
173
  for (const definition of schema.definitions) byName.set(definition.name, definition)
@@ -266,7 +282,7 @@ export function compileSchemaTables(schema: Schema, headers?: HeaderFields): Com
266
282
  const read = (field: CompiledField, bb: ByteBuffer): RuntimeValue => {
267
283
  if (!field.isArray) return readValue(field, bb)
268
284
  if (field.type === TYPE.BYTE) return bb.readByteArray()
269
- const length = bb.readVarUint()
285
+ const length = arrayLength(bb)
270
286
  const values: RuntimeValue[] = Array.from({ length })
271
287
  for (let i = 0; i < length; i++) values[i] = readValue(field, bb)
272
288
  return values
@@ -313,7 +329,7 @@ export function compileSchemaTables(schema: Schema, headers?: HeaderFields): Com
313
329
  bb.skipByteArray()
314
330
  return
315
331
  }
316
- for (let length = bb.readVarUint(); length > 0; length--) skipValue(field, bb)
332
+ for (let length = arrayLength(bb); length > 0; length--) skipValue(field, bb)
317
333
  }
318
334
 
319
335
  const readInto = (field: CompiledField, bb: ByteBuffer, result: RuntimeMessage): void => {