c929195a66da

read exact-length final chunk past the frame marker (v5.3.32)

time
2026-11-23 00:21 UTC · 5 days ago
machine
Junes-MacBook-Pro.local
agent
gpt-5.6-sol
session
remediate interim audit findings
parent
fd2766f30216
6 files manifest.firefox.json+1 −1manifest.json+1 −1package.json+1 −1src/shared/sync-kk-transport.test.ts+121 −6src/shared/sync-padding.ts+12 −5src/shared/sync-transport.ts+43 −25
manifest.firefox.json+1 −1
@@ -1,7 +1,7 @@11 {22   "manifest_version": 3,33   "name": "Peach Password Manager",4-  "version": "5.3.31",4+  "version": "5.3.32",55   "description": "Zero-knowledge password manager",66   "browser_specific_settings": {77     "gecko": {

view file

manifest.json+1 −1
@@ -2,7 +2,7 @@22   "manifest_version": 3,33   "minimum_chrome_version": "116",44   "name": "Peach Password Manager",5-  "version": "5.3.31",5+  "version": "5.3.32",66   "description": "Zero-knowledge password manager",77   "action": {88     "default_popup": "src/popup/index.html",

view file

package.json+1 −1
@@ -1,6 +1,6 @@11 {22   "name": "peach",3-  "version": "5.3.31",3+  "version": "5.3.32",44   "description": "Zero-knowledge password manager",55   "license": "GPL-3.0-only",66   "type": "module",

view file

src/shared/sync-kk-transport.test.ts+121 −6
@@ -623,18 +623,25 @@623623 624624   it("reassembles a frame delivered one payload byte at a time", async () => {625625     // A hostile or broken chunker: the length header arrives in pieces, so the626-    // declared frame size is not known until four payload bytes are in.626+    // declared frame size is not known until four payload bytes are in. The627+    // last two padded bytes arrive as one unmarked message, so no marked628+    // message is exactly as long as the bytes still missing and the frame can629+    // only end where the sender meant it to.627630     const payload = payloadBytes(40);628631     const padded = padSyncFrame(payload);629632     const chunks: Uint8Array[] = [];630-    for (let index = 0; index < padded.byteLength - 1; index += 1) {633+    for (let index = 0; index < padded.byteLength - 2; index += 1) {631634       chunks.push(632635         new Uint8Array([FRAME_CONTINUATION_MARKER, padded[index]!]),633636       );634637     }635-    chunks.push(new Uint8Array([padded[padded.byteLength - 1]!]));636-    expect(chunks).toHaveLength(padded.byteLength);637-    await expect(receiveChunks(chunks).receive()).resolves.toEqual(payload);638+    chunks.push(padded.subarray(padded.byteLength - 2));639+    expect(chunks).toHaveLength(padded.byteLength - 1);640+    const session = receiveChunks(chunks);641+    await expect(session.receive()).resolves.toEqual(payload);642+    // Every chunk was consumed: a receiver that returned early would leave one643+    // queued, and this call would decode it instead of running out.644+    await expect(session.receive()).rejects.toThrow(/no more chunks/);638645   });639646 640647   it("rejects a frame whose final chunk stops short of the declared size", async () => {@@ -705,7 +712,9 @@705712   });706713 707714   it("still trips the frame bound when a frame declared at the cap keeps streaming", async () => {708-    // The declared size fits, so only the bytes received can bound this.715+    // The declared size fits, so only the bytes received can bound this. Every716+    // chunk is marked and longer than what is still missing, so none of them717+    // can be read as a final chunk: only the in-loop bound can reject this.709718     const cap = SYNC_LIMITS.MAX_FRAME_BYTES;710719     const chunk = new Uint8Array(MAX_NOISE_PLAINTEXT_BYTES);711720     chunk[0] = FRAME_CONTINUATION_MARKER;@@ -751,5 +760,111 @@751760       setSpy.mockRestore();752761     }753762     expect(peak).toBeLessThan(cap / 100);763+  });764+});765+766+/**767+ * PB-26-950 marker ambiguity — a final chunk may begin with the same 0x01 byte768+ * the sender reserves for its continuation marker, so the marker alone cannot769+ * say whether a message ends the frame. Once the frame's own length header is770+ * read, one length is ambiguous: a message exactly as long as the bytes still771+ * missing from the declared frame may be that final chunk, and the receiver772+ * reads its first byte as data. Every other message keeps the marker rule.773+ * These tests pin that choice and the outcomes it must not disturb.774+ */775+describe("PB-26-950 marker ambiguity", () => {776+  /** A payload whose bucket is exactly `frameBytes`, so the frame carries no777+   *  discarded padding and every frame byte belongs to the payload. */778+  function frameFillingPayload(frameBytes: number): Uint8Array {779+    const payload = payloadBytes(frameBytes - PADDED_FRAME_HEADER_BYTES);780+    expect(padSyncFrame(payload).byteLength).toBe(frameBytes);781+    return payload;782+  }783+784+  /** Payload index of the first byte the sender puts in the last chunk. */785+  function finalChunkStart(chunkCount: number): number {786+    return (chunkCount - 1) * (MAX_NOISE_PLAINTEXT_BYTES - 1)787+      - PADDED_FRAME_HEADER_BYTES;788+  }789+790+  it("reads a marked message as long as the bytes still missing as the final chunk", async () => {791+    // The ambiguous wire shape: a message whose length equals the bytes still792+    // missing and whose first byte is the marker. It can be read as a final793+    // chunk whose first byte is data, or as a continuation plus a one-byte794+    // final chunk. The receiver takes the first reading, so the frame byte795+    // here is the marker itself.796+    const frameBytes = 2 * 32 * 1024;797+    const splitAt = MAX_NOISE_PLAINTEXT_BYTES - 1;798+    const payload = frameFillingPayload(frameBytes);799+    payload[splitAt - PADDED_FRAME_HEADER_BYTES] = FRAME_CONTINUATION_MARKER;800+    const padded = padSyncFrame(payload);801+    const first = new Uint8Array(1 + splitAt);802+    first[0] = FRAME_CONTINUATION_MARKER;803+    first.set(padded.subarray(0, splitAt), 1);804+    const final = new Uint8Array(1 + frameBytes - splitAt - 1);805+    final[0] = FRAME_CONTINUATION_MARKER;806+    final.set(padded.subarray(splitAt + 1), 1);807+    expect(final.byteLength).toBe(frameBytes - splitAt);808+    await expect(receiveChunks([first, final]).receive()).resolves.toEqual(payload);809+  });810+811+  it("completes a two-chunk frame whose final chunk begins with the marker byte", async () => {812+    // The sender's own shape: the final chunk is the last 18 frame bytes and is813+    // exactly as long as the bytes still missing. A marker-only receiver814+    // strips its first byte and waits for a chunk that never comes.815+    const payload = frameFillingPayload(2 * 32 * 1024);816+    payload[finalChunkStart(2)] = FRAME_CONTINUATION_MARKER;817+    const chunks = chunkPaddedSyncFrame(padSyncFrame(payload));818+    expect(chunks).toHaveLength(2);819+    expect(chunks[1]![0]).toBe(FRAME_CONTINUATION_MARKER);820+    await expect(receiveChunks(chunks).receive()).resolves.toEqual(payload);821+  });822+823+  it("reassembles a continuation chunk whose payload begins with the marker byte", async () => {824+    // The second chunk's frame bytes start with 0x01 right after its marker,825+    // so the message begins 0x01 0x01 and must still be stripped once.826+    const payload = frameFillingPayload(4 * 32 * 1024);827+    payload[finalChunkStart(2)] = FRAME_CONTINUATION_MARKER;828+    const chunks = chunkPaddedSyncFrame(padSyncFrame(payload));829+    expect(chunks).toHaveLength(3);830+    expect(chunks[1]![0]).toBe(FRAME_CONTINUATION_MARKER);831+    expect(chunks[1]![1]).toBe(FRAME_CONTINUATION_MARKER);832+    await expect(receiveChunks(chunks).receive()).resolves.toEqual(payload);833+  });834+835+  it("keeps reading when a marked message is longer than the bytes still missing", async () => {836+    // Only the exact-length message is ambiguous. An over-long marked message837+    // stays a continuation, so a receiver with nothing left to read waits for838+    // the rest of the frame instead of returning one short.839+    const payload = frameFillingPayload(4 * 32 * 1024);840+    payload[finalChunkStart(3)] = FRAME_CONTINUATION_MARKER;841+    const chunks = chunkPaddedSyncFrame(padSyncFrame(payload));842+    const last = chunks[chunks.length - 1]!;843+    expect(last[0]).toBe(FRAME_CONTINUATION_MARKER);844+    const overSent = new Uint8Array(last.byteLength + 512);845+    overSent.set(last);846+    chunks[chunks.length - 1] = overSent;847+    await expect(receiveChunks(chunks).receive()).rejects.toThrow(/no more chunks/);848+  });849+850+  it("does not return a truncated frame whose final chunk begins with the marker byte", async () => {851+    // One byte short of what is still missing, so the marker keeps it a852+    // continuation: the receiver waits for the rest rather than handing a853+    // short frame to the unpadder. Reading one byte too many lengths as final854+    // would hand back a short frame and fail on the unpadder instead.855+    const payload = frameFillingPayload(4 * 32 * 1024);856+    payload[finalChunkStart(3)] = FRAME_CONTINUATION_MARKER;857+    const chunks = chunkPaddedSyncFrame(padSyncFrame(payload));858+    const last = chunks[chunks.length - 1]!;859+    expect(last[0]).toBe(FRAME_CONTINUATION_MARKER);860+    chunks[chunks.length - 1] = last.subarray(0, last.byteLength - 1);861+    await expect(receiveChunks(chunks).receive()).rejects.toThrow(/no more chunks/);862+  });863+864+  it("still returns a single-chunk frame from its first message", async () => {865+    const payload = Uint8Array.from([0x01, 0x01, 0xff, 0x00]);866+    const chunks = chunkPaddedSyncFrame(padSyncFrame(payload));867+    expect(chunks).toHaveLength(1);868+    await expect(receiveChunks(chunks).receive()).resolves.toEqual(payload);754869   });755870 });

view file

src/shared/sync-padding.ts+12 −5
@@ -71,8 +71,8 @@7171  * Padded size a frame's own length header declares. The prefix must already7272  * hold the header; fewer bytes is a caller bug, not a frame the wire can7373  * produce. Receivers use the result to size one reassembly buffer, which74- * bounds what is stored — the frame itself ends at the first chunk without the75- * continuation marker.74+ * bounds what is stored — the frame ends once its declared bytes have75+ * arrived.7676  */7777 export function declaredPaddedFrameBytes(paddedPrefix: Uint8Array): number {7878   if (paddedPrefix.length < PADDED_FRAME_HEADER_BYTES) {@@ -103,9 +103,16 @@103103 /**104104  * The first byte of a chunked padded frame. Every chunk carries it except the105105  * last, so a receiver keeps reading until a chunk arrives without it and only106- * then hands the frame to the unpadder. The frame's own length header, not the107- * number of chunks, bounds how much of those bytes it keeps. A frame that fits108- * one Noise message is sent as one unmarked chunk, as before.106+ * then hands the frame to the unpadder. A final chunk may itself begin with107+ * this byte, so a receiver that has read the frame's declared size uses that108+ * size for one case only: a message whose length exactly equals the bytes still109+ * missing is that final chunk, and its first byte is data. A marked message110+ * longer than the bytes still missing stays a continuation, so a chunker that111+ * ends a frame with a final chunk of one byte or less is not supported — a lone112+ * marker byte there is indistinguishable from a continuation carrying no data.113+ * The frame's own length header, not the number of chunks, bounds how much of114+ * those bytes it keeps. A frame that fits one Noise message is sent as one115+ * unmarked chunk, as before.109116  */110117 export const FRAME_CONTINUATION_MARKER: number = 0x01;111118 

view file

src/shared/sync-transport.ts+43 −25
@@ -508,6 +508,15 @@508508     return this.seen;509509   }510510 511+  /** Frame bytes still needed to complete the declared frame. Undefined until512+   *  the length header has been read; before that only the marker can show513+   *  that a frame is split. */514+  get missing(): number | undefined {515+    return this.filled < PADDED_FRAME_HEADER_BYTES516+      ? undefined517+      : this.declared - this.filled;518+  }519+511520   /** The frame bytes held for the unpadder. */512521   get assembled(): Uint8Array {513522     return this.bytes.subarray(0, this.filled);@@ -593,9 +602,10 @@593602       // single transport message at 65535 bytes (see crypto-core/src/lib.rs —594603       // 65519 plaintext, 16-byte tag). We split a too-large padded frame into595604       // Noise-sized messages, tagging every chunk but the last with a 1-byte596-      // continuation marker. The receiver keeps reading until a chunk arrives597-      // without the marker, and uses the padded frame's 4-byte length header to598-      // bound how much of those bytes it stores.605+      // continuation marker. Once the receiver has the padded frame's 4-byte606+      // length header it compares how many frame bytes are still missing with607+      // the message length, so the one final chunk that would otherwise look608+      // like another continuation is still read as data.599609       const padded = padSyncFrame(plaintext);600610       if (padded.byteLength > SYNC_LIMITS.MAX_FRAME_BYTES) {601611         throw new SyncFrameTooLargeError(@@ -639,13 +649,13 @@639649             ? Promise.race([receiveCiphertext(), deadline])640650             : receiveCiphertext());641651           const pt = session.decrypt(ct);642-          const continued =643-            pt.byteLength > 0 && pt[0] === FRAME_CONTINUATION_MARKER;644-          if (reassembly === null && !continued) {645-            // Single-chunk frame, exactly as before.646-            return unpadSyncFrame(pt);647-          }648652           if (reassembly === null) {653+            // The length header lives inside the first chunk's payload, so654+            // only the marker can say whether this frame is split.655+            if (pt.byteLength === 0 || pt[0] !== FRAME_CONTINUATION_MARKER) {656+              // Single-chunk frame, exactly as before.657+              return unpadSyncFrame(pt);658+            }649659             reassembly = new PaddedFrameReassembly();650660             deadline = new Promise<never>((_resolve, reject) => {651661               deadlineTimer = setTimeout(() => {@@ -655,29 +665,37 @@655665               }, reassemblyTimeoutMs);656666             });657667           }668+          // PB-26-950 marker ambiguity: a final chunk may itself begin with669+          // the marker byte, so the marker alone cannot say whether a message670+          // ends the frame. Once the declared size is known, exactly one671+          // length is ambiguous: a message as long as the bytes still missing672+          // may be that final chunk, whose first byte is data, or a673+          // continuation plus a one-byte final chunk. The receiver reads it as674+          // the final chunk. Every other marked message keeps the marker rule675+          // — longer than the bytes missing stays a continuation, which is676+          // also what a peer gets for marking a message after the frame is677+          // already full. Bytes past the declared frame are padding.678+          const missing = reassembly.missing;679+          const completesFrame =680+            missing !== undefined && pt.byteLength === missing;681+          const continued =682+            pt.byteLength > 0 &&683+            pt[0] === FRAME_CONTINUATION_MARKER &&684+            !completesFrame;658685           // A continuation chunk spends its first byte on the marker.659686           reassembly.write(continued ? pt.subarray(1) : pt);660-          if (continued) {661-            // PB-26-002: the frame bound must hold DURING reassembly. The662-            // post-receive check in the coordinator runs only after the full663-            // frame is in memory, so a peer that completes the Noise KK664-            // handshake could otherwise stream continuation chunks without665-            // bound inside the session window.666-            if (reassembly.received > SYNC_LIMITS.MAX_FRAME_BYTES) {667-              throw new SyncFrameTooLargeError(668-                `sync frame exceeds ${SYNC_LIMITS.MAX_FRAME_BYTES} bytes`,669-              );670-            }671-            continue;672-          }673-          // Final chunk. PB-26-602: the continuation loop only bounds what is674-          // buffered, so the last chunk could push the frame up to 64 KiB past675-          // the cap. Bound the concatenation, not just the buffer.687+          // PB-26-002 / PB-26-602: the frame bound must hold DURING688+          // reassembly, and must cover the final chunk too. The post-receive689+          // check in the coordinator runs only after the full frame is in690+          // memory, so a peer that completes the Noise KK handshake could691+          // otherwise stream past the cap inside the session window, or push692+          // the concatenation one chunk past it with the last message.676693           if (reassembly.received > SYNC_LIMITS.MAX_FRAME_BYTES) {677694             throw new SyncFrameTooLargeError(678695               `sync frame exceeds ${SYNC_LIMITS.MAX_FRAME_BYTES} bytes`,679696             );680697           }698+          if (continued) continue;681699           return unpadSyncFrame(reassembly.assembled);682700         }683701       } finally {

view file