Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(standalone): tolerate whitespace in the base64 fallback decoder
Elian Doranbb160d89083c
diff --git a/apps/standalone/src/lightweight/crypto_provider.spec.ts b/apps/standalone/src/lightweight/crypto_provider.spec.tsindex 08c2df8348..80466d015d 100644--- a/apps/standalone/src/lightweight/crypto_provider.spec.ts+++ b/apps/standalone/src/lightweight/crypto_provider.spec.ts@@ -223,5 +223,23 @@ describe("BrowserCryptoProvider base64", () => {for (let i = 0; i < bytes.length; i++) bytes[i] = (i * 31 + 7) & 0xff;expect(Array.from(provider.base64Decode(provider.base64Encode(bytes)))).toEqual(Array.from(bytes));});++ it("ignores embedded whitespace instead of decoding it as zero bytes (atob / native parity)", () => {+ // Line-wrapped and space-separated base64 must decode identically to the compact form,+ // not emit spurious 0x00 bytes for the newlines/spaces.+ expect(decoder.decode(provider.base64Decode("Zm9v\nYmFy"))).toBe("foobar");+ expect(decoder.decode(provider.base64Decode("Zm9v YmFy"))).toBe("foobar");+ expect(decoder.decode(provider.base64Decode(" Zm9v\r\nYmFy \n"))).toBe("foobar");+ // Whitespace around padding is tolerated too.+ expect(Array.from(provider.base64Decode("Zg =="))).toEqual([0x66]);+ expect(Array.from(provider.base64Decode("Z m 8 ="))).toEqual([0x66, 0x6f]);+ });++ it("round-trips a large buffer that is line-wrapped every 76 chars (MIME style)", () => {+ const bytes = new Uint8Array(50_000);+ for (let i = 0; i < bytes.length; i++) bytes[i] = (i * 17 + 3) & 0xff;+ const wrapped = provider.base64Encode(bytes).replace(/(.{76})/g, "$1\n");+ expect(Array.from(provider.base64Decode(wrapped))).toEqual(Array.from(bytes));+ });});});diff --git a/apps/standalone/src/lightweight/crypto_provider.ts b/apps/standalone/src/lightweight/crypto_provider.tsindex 39661aa74f..fcc499aaa0 100644--- a/apps/standalone/src/lightweight/crypto_provider.ts+++ b/apps/standalone/src/lightweight/crypto_provider.ts@@ -131,9 +131,13 @@ export default class BrowserCryptoProvider implements CryptoProvider {}}-// Base64 alphabet → 6-bit value, indexed by char code (non-alphabet bytes map to 0).+// Sentinel for a char code that is not part of the base64 alphabet. Distinct from 0 so the decoder+// can tell a real 'A' (value 0) apart from padding / whitespace / garbage, which it skips.+const B64_INVALID = 0xff;++// Base64 alphabet → 6-bit value, indexed by char code (non-alphabet bytes map to B64_INVALID).const B64_DECODE_TABLE = /* @__PURE__ */ (() => {- const table = new Uint8Array(256);+ const table = new Uint8Array(256).fill(B64_INVALID);const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for (let i = 0; i < alphabet.length; i++) {table[alphabet.charCodeAt(i)] = i;@@ -142,39 +146,41 @@ const B64_DECODE_TABLE = /* @__PURE__ */ (() => {})();/**- * Decodes standard (padded) base64 straight into a pre-sized `Uint8Array`, without `atob`'s- * intermediate binary string. On the WebView-<140 fallback path this removes a whole extra copy of- * the decoded blob from the peak — the copy that pushed sync's blob decode over the mobile heap.- * Assumes padded, whitespace-free standard base64 (what `encodeBase64` / btoa / `toBase64` emit).+ * Decodes standard base64 straight into a pre-sized `Uint8Array`, without `atob`'s intermediate+ * binary string. On the WebView-<140 fallback path this removes a whole extra copy of the decoded+ * blob from the peak — the copy that pushed sync's blob decode over the mobile heap.+ *+ * Non-alphabet characters (padding `=` and any ASCII whitespace, e.g. newlines in line-wrapped+ * base64) are skipped rather than decoded as zero bytes, matching `atob` / native `fromBase64`+ * semantics. This is done in a single streaming pass over the input via a bit accumulator, so it+ * stays allocation-free (no sanitized copy of the whole string) and preserves the memory win.*/function base64FallbackDecode(base64: string): Uint8Array {const len = base64.length;- if (len === 0) {- return new Uint8Array(0);- }-- let padding = 0;- if (base64.charCodeAt(len - 1) === 0x3d) padding++; // '='- if (len > 1 && base64.charCodeAt(len - 2) === 0x3d) padding++;-- // For padded base64 `len` is a multiple of 4, so this is exact.- const outLen = ((len * 3) >> 2) - padding;- const bytes = new Uint8Array(outLen);const table = B64_DECODE_TABLE;+ // Upper bound: every 4 alphabet symbols yield 3 bytes. Padding/whitespace only ever reduce the+ // real output, so this never under-allocates; the exact length is returned as a view at the end.+ const bytes = new Uint8Array((len * 3) >> 2);+let o = 0;- for (let i = 0; i < len; i += 4) {- const c0 = table[base64.charCodeAt(i)];- const c1 = table[base64.charCodeAt(i + 1)];- const c2 = table[base64.charCodeAt(i + 2)];- const c3 = table[base64.charCodeAt(i + 3)];-- bytes[o++] = (c0 << 2) | (c1 >> 4);- if (o < outLen) bytes[o++] = ((c1 & 0x0f) << 4) | (c2 >> 2);- if (o < outLen) bytes[o++] = ((c2 & 0x03) << 6) | c3;+ let acc = 0; // bit accumulator holding up to three pending 6-bit symbols+ let accBits = 0;+ for (let i = 0; i < len; i++) {+ const v = table[base64.charCodeAt(i)];+ if (v === B64_INVALID) {+ continue; // '=' padding or whitespace — ignore, don't emit a zero byte+ }++ acc = (acc << 6) | v;+ accBits += 6;+ if (accBits >= 8) {+ accBits -= 8;+ bytes[o++] = (acc >> accBits) & 0xff;+ }}- return bytes;+ return o === bytes.length ? bytes : bytes.subarray(0, o);}/**