@devries-cameron20 / t3code Mirror · read-only / commits / 0f5ecef914
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 17:15 UTC

fix(server): refuse a Linux service for a T3 home systemd cannot load (#35968)

Julius Marminge committed Nov 12, 2026 at 20:44 UTC 0f5ecef91458454dc03b7d7e1adb43de414b20ee
2 files changed +73 -4
apps/server/src/cloud/bootService.test.ts
+47 -4
@@ -119,7 +119,7 @@ it("protects the server unit from agents under CPU and memory pressure", () => {
119 });
120
121 it.each([
122 - ["a unit", '/home/theo/T3 "Data"/100%\\x', BootService.renderBootServiceUnit],
122 + ["a unit", "/home/theo/T3 Data/100%$x", BootService.renderBootServiceUnit],
123 [
124 "a plist",
125 "/Users/theo/a&b <c>",
@@ -141,6 +141,41 @@ it.each([
141 ).toBe(program);
142 });
143
144 +// systemd rejects a unit whose executable has a control character, quote or backslash, even quoted.
145 +it.each([
146 + ["a double quote", '/home/theo/T3 "Data"'],
147 + ["a single quote", "/home/theo/Theo's T3"],
148 + ["a backslash", "/home/theo/T3\\Data"],
149 + ["a newline", "/home/theo/T3\nData"],
150 +])("renders a loadable unit, or refuses, for a T3 home with %s", (_, baseDir) => {
151 + let unit: string;
152 + try {
153 + unit = BootService.renderBootServiceUnit({
154 + program: [`${baseDir}/runtime/versions/1.2.3/t3`, "__service-launcher"],
155 + baseDir,
156 + logPath: `${baseDir}/userdata/logs/boot-service.log`,
157 + unitPath: "/home/theo/.config/systemd/user/t3code.service",
158 + });
159 + } catch {
160 + return;
161 + }
162 + for (const line of unit.split("\n")) {
163 + expect(line === "" || line.startsWith("[") || line.includes("=")).toBe(true);
164 + }
165 + const execStart = unit.split("\n").find((line) => line.startsWith("ExecStart="));
166 + const executable = execStart
167 + ?.slice("ExecStart=".length)
168 + .match(/^"((?:\\.|[^"\\])*)"|^(\S+)/)
169 + ?.slice(1)
170 + .find((group) => group !== undefined)
171 + ?.replace(/\\(.)/g, "$1");
172 + expect(executable).toBeDefined();
173 + const unsafe = [...(executable ?? "")].filter(
174 + (char) => char.charCodeAt(0) < 0x20 || `"'\\`.includes(char),
175 + );
176 + expect(unsafe).toEqual([]);
177 +});
178 +
179 const macInstallerPath =
180 "/opt/homebrew/bin:/Users/theo/.npm-global/bin:/Users/theo/.nvm/versions/node/v22.16.0/bin:/usr/bin:/bin";
181
@@ -593,7 +628,7 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
628 "preserves the custom install bin directory in the installed service on %s",
629 (platform) =>
630 Effect.gen(function* () {
596 - const installBinDir = '/custom/T3 & <100%>/"quoted"\\bin';
631 + const installBinDir = '/custom/T3\t& <100%>/"quoted"\\bin';
632 const { service, makeService, fs } = yield* makeHarness(
633 platform,
634 "",
@@ -605,8 +640,8 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
640 const unit = yield* fs.readFileString(plan.unitPath);
641 expect(unit).toContain(
642 platform === "linux"
608 - ? 'Environment=T3CODE_INSTALL_BIN_DIR="/custom/T3 & <100%%>/\\"quoted\\"\\\\bin"'
609 - : '<key>T3CODE_INSTALL_BIN_DIR</key>\n <string>/custom/T3 &amp; &lt;100%&gt;/"quoted"\\bin</string>',
643 + ? 'Environment=T3CODE_INSTALL_BIN_DIR="/custom/T3\t& <100%%>/\\"quoted\\"\\\\bin"'
644 + : '<key>T3CODE_INSTALL_BIN_DIR</key>\n <string>/custom/T3\t&amp; &lt;100%&gt;/"quoted"\\bin</string>',
645 );
646 expect((yield* service.status).current).toBe(true);
647
@@ -1185,6 +1220,14 @@ process.disconnect();
1220 expect(yield* fs.readFileString(plan.unitPath)).toBe(unit);
1221 expect(commands).toEqual([]);
1222 expect((yield* other.status).otherBaseDir).toBe(baseDir);
1223 +
1224 + const quoted = yield* makeService(undefined, "1.2.3", path.join(otherHome, 'T3 "Data"'));
1225 + commands.length = 0;
1226 + const unloadable = yield* quoted.install().pipe(Effect.flip);
1227 + expect(unloadable._tag).toBe("BootServiceBaseDirUnsupportedError");
1228 + expect(CliError.getUserMessage(unloadable)).toBe(unloadable.message);
1229 + expect(yield* fs.readFileString(plan.unitPath)).toBe(unit);
1230 + expect(commands).toEqual([]);
1231 }),
1232 );
1233
apps/server/src/cloud/bootService.ts
+26
@@ -91,8 +91,16 @@ export interface BootServicePlan {
91 readonly handedOff?: boolean;
92 }
93
94 +// systemd refuses an executable with a quote, backslash or control character even when
95 +// quoted. The executable lives under the T3 home, so this also catches a home with a newline.
96 +const systemdUnsafeValue = (plan: BootServicePlan) =>
97 + /[\x00-\x1f\x7f"'\\]/.test(plan.program[0] ?? "");
98 +
99 /** Service units cannot rely on the user's shell or PATH. */
100 export function renderBootServiceUnit(plan: BootServicePlan): string {
101 + if (systemdUnsafeValue(plan)) {
102 + throw new BootServiceBaseDirUnsupportedError({ baseDir: plan.baseDir });
103 + }
104 // The user manager has no reliable network-online target; server networking retries itself.
105 return [
106 "[Unit]",
@@ -705,8 +713,22 @@ export class BootServiceOtherBaseDirError extends Schema.TaggedError<BootService
713 }
714 }
715
716 +export class BootServiceBaseDirUnsupportedError extends Schema.TaggedError<BootServiceBaseDirUnsupportedError>()(
717 + "BootServiceBaseDirUnsupportedError",
718 + { baseDir: Schema.String },
719 +) {
720 + override get message(): string {
721 + return `systemd cannot run T3 Code from ${JSON.stringify(this.baseDir)}: the path contains a quote, backslash or control character. Choose another T3 home with --base-dir.`;
722 + }
723 +
724 + override get [CliError.userMessage]() {
725 + return this.message;
726 + }
727 +}
728 +
729 export type BootServiceError =
730 | BootServiceUnsupportedError
731 + | BootServiceBaseDirUnsupportedError
732 | BootServiceOtherBaseDirError
733 | BootServiceCommandError
734 | BootServiceInstallError
@@ -1174,6 +1196,9 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
1196 readonly start?: boolean;
1197 }) {
1198 let manager = yield* requireManager;
1199 + if (manager.kind === "systemd" && systemdUnsafeValue(plan)) {
1200 + return yield* new BootServiceBaseDirUnsupportedError({ baseDir: input.baseDir });
1201 + }
1202 // The unit is per user. Rewriting one that serves another home moves the
1203 // service onto this home's data, which looks like every thread vanished.
1204 const refuseOtherBaseDir = Effect.gen(function* () {
@@ -1537,6 +1562,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
1562 problems,
1563 current:
1564 problems.length === 0 &&
1565 + !(manager.kind === "systemd" && systemdUnsafeValue(planForUnit(unit))) &&
1566 normalizeUnit(unit) === normalizeUnit(manager.render(planForUnit(unit))) &&
1567 runtimeEntryExists &&
1568 Option.isSome(runtimeSentinel) &&