Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(docker): read .env values the way Compose does; keep an unchanged .env at 0600

KageBinaryd8a3ac36fb2f
diff --git a/ix-cli/src/cli/__tests__/docker-compose-source.test.ts b/ix-cli/src/cli/__tests__/docker-compose-source.test.ts
index e845d582..dd15ade5 100644
--- a/ix-cli/src/cli/__tests__/docker-compose-source.test.ts
+++ b/ix-cli/src/cli/__tests__/docker-compose-source.test.ts
@@ -243,6 +243,11 @@ describe("backend .env helpers", () => {
expect(envFileLocalToken("IX_LOCAL_TOKEN=\n")).toBeUndefined();
expect(envFileLocalToken("IX_LOCAL_TOKEN=a\r\nexport IX_LOCAL_TOKEN='b'\n")).toBe("b");
expect(envFileLocalToken("# IX_LOCAL_TOKEN=commented\nX_IX_LOCAL_TOKEN=other\n")).toBeUndefined();
+ // Inline comments, read as Docker Compose reads them.
+ expect(envFileLocalToken("IX_LOCAL_TOKEN=abc # mine\n")).toBe("abc");
+ expect(envFileLocalToken('IX_LOCAL_TOKEN="abc" # mine\n')).toBe("abc");
+ expect(envFileLocalToken("IX_LOCAL_TOKEN='a#b' # note\n")).toBe("a#b");
+ expect(envFileLocalToken("IX_LOCAL_TOKEN=a#b\n")).toBe("a#b");
});
it("chooses the stored token, else the hand-set one unless clearing", async () => {
@@ -257,8 +262,13 @@ describe("backend .env helpers", () => {
const { writeBackendEnv } = await import("../backend-compose.js");
const env = join(t, "same.env");
writeFileSync(env, "IX_LOCAL_TOKEN=x\n", { mode: 0o644 });
+ const before = statSync(env);
writeBackendEnv("x", env);
- if (process.platform !== "win32") expect(statSync(env).mode & 0o777).toBe(0o644);
+ const after = statSync(env);
+ // Not rewritten (same inode, same mtime), but tightened to 0600: it holds a credential.
+ expect(after.ino).toBe(before.ino);
+ expect(after.mtimeMs).toBe(before.mtimeMs);
+ if (process.platform !== "win32") expect(after.mode & 0o777).toBe(0o600);
writeBackendEnv("y", env);
expect(readFileSync(env, "utf8")).toBe("IX_LOCAL_TOKEN=y\n");
});
diff --git a/ix-cli/src/cli/backend-compose.ts b/ix-cli/src/cli/backend-compose.ts
index 8be2386a..f3aa15da 100644
--- a/ix-cli/src/cli/backend-compose.ts
+++ b/ix-cli/src/cli/backend-compose.ts
@@ -35,11 +35,26 @@ export function envFileLocalToken(existing: string): string | undefined {
let value: string | undefined;
for (const line of existing.split(/\r?\n/)) {
const m = LOCAL_TOKEN_LINE.exec(line);
- if (m) value = m[1].trim().replace(/^(["'])(.*)\1$/, "$2").trim();
+ if (m) value = dotenvValue(m[1]);
}
return value ? value : undefined;
}
+/**
+ * A dotenv value the way Docker Compose reads it: a quoted value ends at its
+ * closing quote (anything after, such as ` # note`, is ignored); an unquoted
+ * one ends where whitespace starts a ` #` comment.
+ */
+function dotenvValue(raw: string): string {
+ const v = raw.trim();
+ const quote = v[0];
+ if (quote === '"' || quote === "'") {
+ const close = v.indexOf(quote, 1);
+ if (close > 0) return v.slice(1, close).trim();
+ }
+ return v.replace(/\s+#.*$/, "").trim();
+}
+
/**
* `.env` with IX_LOCAL_TOKEN set to `token` (empty: the backend does not
* require one). Every other line a user wrote, comments and blank lines
@@ -75,7 +90,11 @@ function readEnvFile(envFile: string): string {
export function writeBackendEnv(token: string | undefined, envFile: string = backendEnvFile()): void {
const existing = readEnvFile(envFile);
// Already says the same (however the user quoted it): leave the file as it is.
- if (existsSync(envFile) && envFileLocalToken(existing) === (token || undefined) && LOCAL_TOKEN_IN_FILE.test(existing)) return;
+ if (existsSync(envFile) && envFileLocalToken(existing) === (token || undefined) && LOCAL_TOKEN_IN_FILE.test(existing)) {
+ // Nothing to rewrite, but the file still holds a credential: keep it 0600.
+ try { chmodSync(envFile, 0o600); } catch { /* best effort */ }
+ return;
+ }
const next = backendEnvContents(existing, token);
mkdirSync(dirname(envFile), { recursive: true, mode: 0o700 });
const tmp = `${envFile}.${process.pid}.tmp`;