fix(cli): quote env values with special characters
This commit is contained in:
@@ -4,7 +4,9 @@ import path from "node:path";
|
|||||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
import {
|
import {
|
||||||
ensureAgentJwtSecret,
|
ensureAgentJwtSecret,
|
||||||
|
mergePaperclipEnvEntries,
|
||||||
readAgentJwtSecretFromEnv,
|
readAgentJwtSecretFromEnv,
|
||||||
|
readPaperclipEnvEntries,
|
||||||
resolveAgentJwtEnvFile,
|
resolveAgentJwtEnvFile,
|
||||||
} from "../config/env.js";
|
} from "../config/env.js";
|
||||||
import { agentJwtSecretCheck } from "../checks/agent-jwt-secret-check.js";
|
import { agentJwtSecretCheck } from "../checks/agent-jwt-secret-check.js";
|
||||||
@@ -58,4 +60,20 @@ describe("agent jwt env helpers", () => {
|
|||||||
const result = agentJwtSecretCheck(configPath);
|
const result = agentJwtSecretCheck(configPath);
|
||||||
expect(result.status).toBe("pass");
|
expect(result.status).toBe("pass");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("quotes hash-prefixed env values so dotenv round-trips them", () => {
|
||||||
|
const configPath = tempConfigPath();
|
||||||
|
const envPath = resolveAgentJwtEnvFile(configPath);
|
||||||
|
|
||||||
|
mergePaperclipEnvEntries(
|
||||||
|
{
|
||||||
|
PAPERCLIP_WORKTREE_COLOR: "#439edb",
|
||||||
|
},
|
||||||
|
envPath,
|
||||||
|
);
|
||||||
|
|
||||||
|
const contents = fs.readFileSync(envPath, "utf-8");
|
||||||
|
expect(contents).toContain('PAPERCLIP_WORKTREE_COLOR="#439edb"');
|
||||||
|
expect(readPaperclipEnvEntries(envPath).PAPERCLIP_WORKTREE_COLOR).toBe("#439edb");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -294,7 +294,7 @@ describe("worktree helpers", () => {
|
|||||||
const envContents = fs.readFileSync(envPath, "utf8");
|
const envContents = fs.readFileSync(envPath, "utf8");
|
||||||
expect(envContents).toContain("PAPERCLIP_AGENT_JWT_SECRET=worktree-shared-secret");
|
expect(envContents).toContain("PAPERCLIP_AGENT_JWT_SECRET=worktree-shared-secret");
|
||||||
expect(envContents).toContain("PAPERCLIP_WORKTREE_NAME=repo");
|
expect(envContents).toContain("PAPERCLIP_WORKTREE_NAME=repo");
|
||||||
expect(envContents).toMatch(/PAPERCLIP_WORKTREE_COLOR=#[0-9a-f]{6}/);
|
expect(envContents).toMatch(/PAPERCLIP_WORKTREE_COLOR=\"#[0-9a-f]{6}\"/);
|
||||||
} finally {
|
} finally {
|
||||||
process.chdir(originalCwd);
|
process.chdir(originalCwd);
|
||||||
if (originalJwtSecret === undefined) {
|
if (originalJwtSecret === undefined) {
|
||||||
|
|||||||
@@ -22,11 +22,18 @@ function parseEnvFile(contents: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatEnvValue(value: string): string {
|
||||||
|
if (/^[A-Za-z0-9_./:@-]+$/.test(value)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return JSON.stringify(value);
|
||||||
|
}
|
||||||
|
|
||||||
function renderEnvFile(entries: Record<string, string>) {
|
function renderEnvFile(entries: Record<string, string>) {
|
||||||
const lines = [
|
const lines = [
|
||||||
"# Paperclip environment variables",
|
"# Paperclip environment variables",
|
||||||
"# Generated by Paperclip CLI commands",
|
"# Generated by Paperclip CLI commands",
|
||||||
...Object.entries(entries).map(([key, value]) => `${key}=${value}`),
|
...Object.entries(entries).map(([key, value]) => `${key}=${formatEnvValue(value)}`),
|
||||||
"",
|
"",
|
||||||
];
|
];
|
||||||
return lines.join("\n");
|
return lines.join("\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user