Skip to content

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

fix(sync): reject malformed Unicode wire paths

Sebastian Bc146e17e8e27
diff --git a/electron/cloud-sync-rules.mjs b/electron/cloud-sync-rules.mjs
index 6208770376..02d0fd08dd 100644
--- a/electron/cloud-sync-rules.mjs
+++ b/electron/cloud-sync-rules.mjs
@@ -31,10 +31,10 @@ export function skipped(name, directory) {
|| /^\.~lock\..*#$/.test(name) || /\.omb-sync-[0-9a-f]+$/.test(name);
}
-/** A path as it travels: relative, "/"-separated, NFC, with no empty, "." or ".." part, and no direction override. */
+/** A path as it travels: well-formed Unicode, relative, "/"-separated, NFC, with no empty, "." or ".." part, and no direction override. */
export function validWirePath(path) {
if (typeof path !== "string" || !path || path.length > 4096 || path.includes("\0") || path.includes("\\") || path.startsWith("/") || BIDI.test(path)) return false;
- if (path !== path.normalize("NFC")) return false;
+ if (!path.isWellFormed() || path !== path.normalize("NFC")) return false;
return path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
}
diff --git a/electron/cloud-sync-rules.node-test.mjs b/electron/cloud-sync-rules.node-test.mjs
index 4cb17b3e07..5fecb3214c 100644
--- a/electron/cloud-sync-rules.node-test.mjs
+++ b/electron/cloud-sync-rules.node-test.mjs
@@ -17,6 +17,13 @@ test("a wire path is relative, slash-separated and NFC", () => {
for (const bad of ["", "/a", "a//b", "a/./b", "../a", "a/..", "a\\b", "a\0b", "Café.txt", 7, null]) assert.equal(validWirePath(bad), false, String(bad));
});
+test("wire paths reject unpaired surrogates and accept Unicode scalar values", () => {
+ for (const good of ["\ufffd.txt", "\ud83d\udcc4.txt", "docs/\ud83d\udcc4.txt"]) assert.equal(validWirePath(good), true, good);
+ for (const bad of ["\ud800.txt", "\ud801.txt", "\udbff.txt", "\udc00.txt", "\udfff.txt", "docs/\ud800.txt", "a\ud800", "\udc00\ud800.txt"]) {
+ assert.equal(validWirePath(bad), false, JSON.stringify(bad));
+ }
+});
+
test("names a Windows PC cannot hold", () => {
assert.equal(windowsProblem("a/b:c.txt"), "characters");
assert.equal(windowsProblem("notes./x"), "trailing");
diff --git a/server/cloud-sync-concurrent.test.ts b/server/cloud-sync-concurrent.test.ts
index ff67bc3a2a..400b65e7e1 100644
--- a/server/cloud-sync-concurrent.test.ts
+++ b/server/cloud-sync-concurrent.test.ts
@@ -77,6 +77,20 @@ it("accepts only one competing commit over the same uncached version", async ()
expect(await sync.commit(await upload("third", sha("first")))).toEqual({ sha256: sha("third") });
});
+it.each(["\ud800.txt", "\ud801.txt", "\udc00.txt", "\udfff.txt"])("rejects a malformed Unicode alias %j while its file has a pending commit", async (path) => {
+ const target = join(folder.cloudPath, "\ufffd.txt");
+ writeFileSync(target, "original");
+ const id = await upload("first", sha("original"), "\ufffd.txt");
+ const held = holdCurrent();
+ const committed = sync.commit(id);
+ await held.entered;
+ try {
+ await expect(upload("second", sha("original"), path).then((id) => sync.commit(id))).rejects.toMatchObject({ status: 400 });
+ expect(readFileSync(target, "utf8")).toBe("original");
+ } finally { held.release(); await committed; }
+ expect(readFileSync(target, "utf8")).toBe("first");
+});
+
it("checks a competing delete against the committed version", async () => {
writeFileSync(join(folder.cloudPath, "a.txt"), "original");
const id = await upload("first", sha("original"));