Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.

Commit 77c4658

Browse files
authored
refactor(tasks): share sandbox environment parsing
Extract the NUL-delimited environment parser and use its parsed object for OAuth token lookup. Add direct coverage for normal, malformed, and equals-containing entries. Generated-By: PostHog Code Task-Id: 833df099-31c5-4930-addb-305cc5fe2def
1 parent 93fc7d0 commit 77c4658

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

packages/agent/src/signed-commit-artefacts.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,27 @@ import {
1212
vi,
1313
} from "vitest";
1414
import {
15+
parseSandboxEnv,
1516
reportCommitArtefacts,
1617
reportTaskRunBranch,
1718
resolveSandboxPosthogApi,
1819
} from "./signed-commit-artefacts";
1920

21+
describe("parseSandboxEnv", () => {
22+
it("parses NUL-delimited entries into an object", () => {
23+
expect(parseSandboxEnv("FIRST=one\0SECOND=two\0")).toEqual({
24+
FIRST: "one",
25+
SECOND: "two",
26+
});
27+
});
28+
29+
it("preserves equals signs in values and ignores malformed entries", () => {
30+
expect(
31+
parseSandboxEnv("TOKEN=header.payload=signature\0invalid\0=value\0"),
32+
).toEqual({ TOKEN: "header.payload=signature" });
33+
});
34+
});
35+
2036
const ENV = {
2137
POSTHOG_API_URL: "https://us.posthog.com",
2238
POSTHOG_PERSONAL_API_KEY: "pha_test",

packages/agent/src/signed-commit-artefacts.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@ interface SandboxPosthogApi {
2424
projectId: number;
2525
}
2626

27+
export function parseSandboxEnv(raw: string): Record<string, string> {
28+
const env: Record<string, string> = {};
29+
for (const entry of raw.split("\0")) {
30+
const separator = entry.indexOf("=");
31+
if (separator > 0) {
32+
env[entry.slice(0, separator)] = entry.slice(separator + 1);
33+
}
34+
}
35+
return env;
36+
}
37+
2738
function readSandboxEnvFile(envFilePath: string): Record<string, string> {
2839
try {
29-
const raw = readFileSync(envFilePath, "utf8");
30-
const env: Record<string, string> = {};
31-
for (const entry of raw.split("\0")) {
32-
const eq = entry.indexOf("=");
33-
if (eq > 0) {
34-
env[entry.slice(0, eq)] = entry.slice(eq + 1);
35-
}
36-
}
37-
return env;
40+
return parseSandboxEnv(readFileSync(envFilePath, "utf8"));
3841
} catch {
3942
// No env file (local/desktop or test) — fall back to the process env only.
4043
return {};
@@ -44,13 +47,7 @@ function readSandboxEnvFile(envFilePath: string): Record<string, string> {
4447
function readSandboxOauthToken(oauthEnvFilePath: string): string | undefined {
4548
try {
4649
const raw = readFileSync(oauthEnvFilePath, "utf8");
47-
for (const entry of raw.split("\0")) {
48-
const prefix = "POSTHOG_PERSONAL_API_KEY=";
49-
if (entry.startsWith(prefix)) {
50-
return entry.slice(prefix.length);
51-
}
52-
}
53-
return "";
50+
return parseSandboxEnv(raw).POSTHOG_PERSONAL_API_KEY ?? "";
5451
} catch {
5552
// The dedicated credential channel is mandatory. Missing and unreadable
5653
// files both fail closed.

0 commit comments

Comments
 (0)