Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(themes): sniff SVG bytes in one pass

Sebastien Tardifd5b7dcf7437d
diff --git a/server/themes/store.test.ts b/server/themes/store.test.ts
index ce6243be0a..3dd244445e 100644
--- a/server/themes/store.test.ts
+++ b/server/themes/store.test.ts
@@ -4,7 +4,7 @@ import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { LOOK_HISTORY_LIMIT } from "../../shared/theme.ts";
-import { isRefusal, createThemeStore } from "./store.ts";
+import { isRefusal, createThemeStore, pictureKind } from "./store.ts";
const PNG = Buffer.from("89504e470d0a1a0a0000000d49484452", "hex");
const GIF = Buffer.from("GIF89a\x01\x00\x01\x00", "latin1");
@@ -56,6 +56,21 @@ describe("theme store", () => {
expect(store.asset("someone-else", "neon", "stars.png")).toBeNull();
});
+ it("refuses an unclosed comment prefix without stalling the svg sniff", () => {
+ const { store } = fixture();
+ store.save("owner", "neon", { name: "Neon" });
+ const stall = Buffer.from(`<!--${"--><!--".repeat(26)}`);
+ const started = Date.now();
+ expect(pictureKind(stall)).toBeNull();
+ expect(Date.now() - started).toBeLessThan(500);
+ const saving = Date.now();
+ expect(isRefusal(store.addAsset("owner", "neon", "mark.svg", stall))).toBe(true);
+ expect(Date.now() - saving).toBeLessThan(500);
+ const wrapped = Buffer.from('<?xml version="1.0"?>\n<!-- icon -->\n<!DOCTYPE svg>\n<svg></svg>');
+ expect(pictureKind(wrapped)).toBe("svg");
+ expect(store.addAsset("owner", "neon", "icon.svg", wrapped)).toEqual({ url: "/api/theme-assets/neon/icon.svg" });
+ });
+
it("exports with files inlined and imports them again under a free id, CSS pointed at the new copy", () => {
const { store } = fixture();
store.save("owner", "neon", { name: "Neon", css: '[data-look="app"] { background: url("/api/theme-assets/neon/stars.png") }' });
diff --git a/server/themes/store.ts b/server/themes/store.ts
index cd22c72248..68647f4bf6 100644
--- a/server/themes/store.ts
+++ b/server/themes/store.ts
@@ -48,6 +48,37 @@ function ftypBrands(bytes: Buffer): string[] | null {
return brands;
}
+// One forward pass. A nested match over an unclosed `<!--` retries until the server stalls.
+function svgDocument(head: string): boolean {
+ let i = 0;
+ const space = () => { while (i < head.length && /\s/u.test(head[i]!)) i += 1; };
+ const opens = (text: string) => head.slice(i, i + text.length).toLowerCase() === text;
+ const close = () => {
+ const end = head.indexOf(">", i);
+ if (end < 0) return false;
+ i = end + 1;
+ return true;
+ };
+ space();
+ if (opens("<?xml")) {
+ if (!close()) return false;
+ space();
+ }
+ while (opens("<!--")) {
+ const end = head.indexOf("-->", i + 4);
+ if (end < 0) return false;
+ i = end + 3;
+ space();
+ }
+ if (opens("<!doctype")) {
+ const end = head.indexOf(">", i);
+ if (end < 0 || !/^<!doctype svg/i.test(head.slice(i, end))) return false;
+ i = end + 1;
+ space();
+ }
+ return /^<svg[\s>]/i.test(head.slice(i));
+}
+
/** What picture the bytes are, by their first bytes: one a window can show, "heic" for a phone photo it cannot, or null. */
export function pictureKind(bytes: Buffer): PictureKind | "heic" | null {
const starts = (...values: number[]) => values.every((value, index) => bytes[index] === value);
@@ -59,7 +90,7 @@ export function pictureKind(bytes: Buffer): PictureKind | "heic" | null {
const brands = ftypBrands(bytes);
if (brands) return brands.some((brand) => brand === "avif" || brand === "avis") ? "avif" : brands.some((brand) => HEIF_BRANDS.has(brand)) ? "heic" : null;
const head = bytes.subarray(0, 4096).toString("utf8").replace(/^\uFEFF/, "").trimStart();
- return /^(<\?xml[^>]*>\s*)?(<!--[\s\S]*?-->\s*)*(<!DOCTYPE svg[^>]*>\s*)?<svg[\s>]/i.test(head) ? "svg" : null;
+ return svgDocument(head) ? "svg" : null;
}
/** Security: a file's first bytes must match its extension, so a theme file is only ever the image or font it claims. */
@@ -78,8 +109,8 @@ function looksLike(name: string, bytes: Buffer): boolean {
case "ttf": return starts(0x00, 0x01, 0x00, 0x00) || ascii(0, "true");
case "otf": return ascii(0, "OTTO");
case "svg": {
- const head = bytes.subarray(0, 4096).toString("utf8").replace(/^/, "").trimStart();
- return /^(<\?xml[^>]*>\s*)?(<!--[\s\S]*?-->\s*)*(<!DOCTYPE svg[^>]*>\s*)?<svg[\s>]/i.test(head);
+ const head = bytes.subarray(0, 4096).toString("utf8").replace(/^\uFEFF/, "").trimStart();
+ return svgDocument(head);
}
default: return false;
}