Skip to content

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

fix(local-vm): move each guest id only when it changes; shell-quote the chown hint path

AdityaUmale24b32455845c
diff --git a/server/container-computer.test.ts b/server/container-computer.test.ts
index 09935b90d4..39a39f7b01 100644
--- a/server/container-computer.test.ts
+++ b/server/container-computer.test.ts
@@ -1280,6 +1280,17 @@ describe("setupCommands", () => {
expect(script.indexOf(step)).toBeLessThan(script.indexOf("sed -i"));
}
expect(script).toMatch(/\|\| \{ echo "cua did not take uid \$uid gid \$gid" >&2; exit 1; \}\nfi\nexec /);
+ // chown or chgrp to an unchanged id would still clear set-id bits, so each id moves only behind its own check.
+ const uidGuard = script.indexOf('if [ "$old_uid" != "$uid" ]; then');
+ const gidGuard = script.indexOf('if [ "$old_gid" != "$gid" ]; then');
+ expect(uidGuard).toBeGreaterThan(-1);
+ expect(gidGuard).toBeGreaterThan(uidGuard);
+ const chowns = [...script.matchAll(/-exec chown /gu)].map((match) => match.index);
+ const chgrps = [...script.matchAll(/-exec chgrp /gu)].map((match) => match.index);
+ expect(chowns).toHaveLength(2);
+ expect(chgrps).toHaveLength(2);
+ for (const at of chowns) expect(at > uidGuard && at < gidGuard).toBe(true);
+ for (const at of chgrps) expect(at).toBeGreaterThan(gidGuard);
});
it("asks rootless Podman to map and privately relabel the durable workspace", () => {
diff --git a/server/container-isolation.ts b/server/container-isolation.ts
index 2600835480..4720fa0f5e 100644
--- a/server/container-isolation.ts
+++ b/server/container-isolation.ts
@@ -22,12 +22,17 @@ const GUEST_IDS_SCRIPT = [
"set -eu",
'uid="$1"; gid="$2"; old_uid="$(id -u cua)"; old_gid="$(id -g cua)"',
'if [ "$old_uid:$old_gid" != "$uid:$gid" ]; then',
+ // Each id moves only when it changes: chown or chgrp to the same id still clears set-id bits and file capabilities.
// The bind-mounted workspace is another filesystem, so -xdev from / skips it: an older desktop's files there move too.
// Best effort: a share that refuses chown must not keep the desktop from starting.
- ' [ ! -d /home/cua/workspace ] || find /home/cua/workspace -xdev -uid "$old_uid" -exec chown -h "$uid" {} + || echo "some workspace files kept uid $old_uid" >&2',
- ' [ ! -d /home/cua/workspace ] || find /home/cua/workspace -xdev -gid "$old_gid" -exec chgrp -h "$gid" {} + || echo "some workspace files kept gid $old_gid" >&2',
- ' find / -xdev -uid "$old_uid" -exec chown -h "$uid" {} +',
- ' find / -xdev -gid "$old_gid" -exec chgrp -h "$gid" {} +',
+ ' if [ "$old_uid" != "$uid" ]; then',
+ ' [ ! -d /home/cua/workspace ] || find /home/cua/workspace -xdev -uid "$old_uid" -exec chown -h "$uid" {} + || echo "some workspace files kept uid $old_uid" >&2',
+ ' find / -xdev -uid "$old_uid" -exec chown -h "$uid" {} +',
+ ' fi',
+ ' if [ "$old_gid" != "$gid" ]; then',
+ ' [ ! -d /home/cua/workspace ] || find /home/cua/workspace -xdev -gid "$old_gid" -exec chgrp -h "$gid" {} + || echo "some workspace files kept gid $old_gid" >&2',
+ ' find / -xdev -gid "$old_gid" -exec chgrp -h "$gid" {} +',
+ ' fi',
' sed -i "s/^cua:x:$old_uid:$old_gid:/cua:x:$uid:$gid:/" /etc/passwd',
' sed -i "s/^cua:x:$old_gid:/cua:x:$gid:/" /etc/group',
' [ "$(id -u cua):$(id -g cua)" = "$uid:$gid" ] || { echo "cua did not take uid $uid gid $gid" >&2; exit 1; }',
diff --git a/server/local-vm-browser-profiles.test.ts b/server/local-vm-browser-profiles.test.ts
index 44072a3e12..113586cf36 100644
--- a/server/local-vm-browser-profiles.test.ts
+++ b/server/local-vm-browser-profiles.test.ts
@@ -247,7 +247,8 @@ describe.skipIf(process.platform === "win32")("rescueFirefoxProfile", () => {
const container = join(dir, "container");
mkdirSync(join(container, "home", "cua", ".mozilla", "firefox"), { recursive: true });
writeFileSync(join(container, "home", "cua", ".mozilla", "firefox", "profiles.ini"), "old desktop");
- const workspace = join(dir, "workspace");
+ // An apostrophe in the path must still give a command the shell runs as written.
+ const workspace = join(dir, "o'brien workspace");
mkdirSync(join(workspace, ".browser-profiles"), { recursive: true });
chmodSync(join(workspace, ".browser-profiles"), 0o500);
// The fake runtime is a path; the update itself only ever passes "docker" or "podman" through to the same spawn.
@@ -259,7 +260,7 @@ describe.skipIf(process.platform === "win32")("rescueFirefoxProfile", () => {
chmodSync(join(workspace, ".browser-profiles"), 0o700);
}
expect(failure).toBeInstanceOf(Error);
- expect((failure as Error).message).toContain(`sudo chown -R ${process.getuid?.()}:${process.getgid?.()} '${workspace}'`);
+ expect((failure as Error).message).toContain(`sudo chown -R ${process.getuid?.()}:${process.getgid?.()} '${workspace.replaceAll("'", "'\\''")}' and`);
expect(readdirSync(workspace)).toEqual([".browser-profiles"]);
});
});
diff --git a/server/local-vm-profiles.ts b/server/local-vm-profiles.ts
index abb22b5d46..da32649d1c 100644
--- a/server/local-vm-profiles.ts
+++ b/server/local-vm-profiles.ts
@@ -7,6 +7,7 @@ import { lstat, mkdir, readdir, rename, rm, rmdir } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
+import { shellQuote } from "./browser/chrome/wrapper.ts";
import type { ContainerComputerStatus, LocalVmTarget } from "./container-computer.ts";
import { augmentedPath, resolveCliSpawn } from "./env-path.ts";
@@ -23,7 +24,7 @@ function withOwnershipStep(error: unknown, workspaceDir: string): Error {
const message = error instanceof Error ? error.message : String(error);
if (!PERMISSION_DENIED.test(message) || !process.getuid || !process.getgid) return error instanceof Error ? error : new Error(message);
return new Error(
- `an older desktop left files this app (uid ${process.getuid()}) cannot change; run sudo chown -R ${process.getuid()}:${process.getgid()} '${workspaceDir}' and it retries on its own (${message.slice(0, 80)})`,
+ `an older desktop left files this app (uid ${process.getuid()}) cannot change; run sudo chown -R ${process.getuid()}:${process.getgid()} ${shellQuote(workspaceDir)} and it retries on its own (${message.slice(0, 80)})`,
);
}