Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions sdk/typescript/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ const PRODUCER_NAME = "codex-security-plugin";
interface CheckedScanFile {
path: string;
metadata: BigIntStats;
parents: Array<{ path: string; metadata: Stats }>;
parents: Array<{ path: string; metadata: BigIntStats }>;
}

interface ScanRoot {
path: string;
metadata: Stats;
metadata: BigIntStats;
}

export interface ScanExpectation {
Expand Down Expand Up @@ -411,7 +411,7 @@ async function requireCheckedScanFile(
throwIfAborted(signal);
for (const part of parts.slice(0, -1)) {
current = join(current, part);
const metadata = await lstat(current);
const metadata = await lstat(current, { bigint: true });
throwIfAborted(signal);
if (!metadata.isDirectory()) {
throw new Error("unsafe parent");
Expand Down Expand Up @@ -637,13 +637,13 @@ async function requireScanRoot(
throwIfAborted(signal);
const absolute = resolve(scanDirectory);
try {
const metadata = await lstat(absolute);
const metadata = await lstat(absolute, { bigint: true });
throwIfAborted(signal);
const canonical = await realpath(absolute);
throwIfAborted(signal);
const current = await lstat(absolute);
const current = await lstat(absolute, { bigint: true });
throwIfAborted(signal);
const returned = await lstat(canonical);
const returned = await lstat(canonical, { bigint: true });
throwIfAborted(signal);
if (
!metadata.isDirectory() ||
Expand All @@ -657,7 +657,10 @@ async function requireScanRoot(
throw new Error("not a directory");
}
try {
requirePrivateOutputDirectory(returned, canonical);
requirePrivateOutputDirectory(
{ mode: Number(returned.mode), uid: Number(returned.uid) },
canonical,
);
await requireSecureOutputAncestry(canonical);
} catch (error) {
throw new ContractValidationError(
Expand All @@ -683,7 +686,7 @@ async function verifyScanRoot(
signal?: AbortSignal,
): Promise<void> {
try {
const current = await lstat(root.path);
const current = await lstat(root.path, { bigint: true });
throwIfAborted(signal);
if (
!current.isDirectory() ||
Expand All @@ -692,7 +695,10 @@ async function verifyScanRoot(
) {
throw new Error("scan directory changed while reading");
}
requirePrivateOutputDirectory(current, root.path);
requirePrivateOutputDirectory(
{ mode: Number(current.mode), uid: Number(current.uid) },
root.path,
);
await requireSecureOutputAncestry(root.path);
} catch (error) {
throwIfAborted(signal);
Expand Down Expand Up @@ -968,7 +974,7 @@ async function openCheckedScanFile(
);
}
for (const parent of checked.parents) {
const current = await lstat(parent.path);
const current = await lstat(parent.path, { bigint: true });
throwIfAborted(signal);
if (
!current.isDirectory() ||
Expand Down
2 changes: 2 additions & 0 deletions sdk/typescript/tests-ts/api-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ describe("CodexSecurity policy API", () => {
? ["--separate-git-dir", join(f.root, "other-git-data")]
: []),
);
policyGit(owner, "config", "--local", "core.autocrlf", "false");
policyGit(owner, "commit", "--allow-empty", "--quiet", "-m", "initial");
let checkout = owner;
if (kind === "linked") {
Expand Down Expand Up @@ -692,6 +693,7 @@ describe("CodexSecurity policy API", () => {
onPrepare,
});
policyGit(f.repository, "init", "--quiet");
policyGit(f.repository, "config", "--local", "core.autocrlf", "false");
const nested = await addPolicySubmodule(
f.repository,
join(f.root, "submodule-source"),
Expand Down
19 changes: 19 additions & 0 deletions sdk/typescript/tests-ts/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,14 @@ describe("CodexSecurity orchestration", () => {
const linked = join(root, "linked");
await mkdir(normal, { mode: 0o700 });
execFileSync("git", ["init", "-q", normal]);
execFileSync("git", [
"-C",
normal,
"config",
"--local",
"core.autocrlf",
"false",
]);
await writeFile(join(normal, "tracked.txt"), "tracked\n");
execFileSync("git", ["-C", normal, "add", "."]);
execFileSync("git", [
Expand Down Expand Up @@ -3112,6 +3120,14 @@ describe("CodexSecurity orchestration", () => {
test("scrubs Git overrides from direct target normalization", async () => {
const { root, repository } = await repositoryDirectories();
execFileSync("git", ["init", "-q", repository]);
execFileSync("git", [
"-C",
repository,
"config",
"--local",
"core.autocrlf",
"false",
]);
await writeFile(join(repository, "tracked.txt"), "tracked\n");
execFileSync("git", ["-C", repository, "add", "."]);
execFileSync("git", [
Expand Down Expand Up @@ -7602,6 +7618,9 @@ describe("CodexSecurity orchestration", () => {
await mkdir(ignored);
await mkdir(vendored);
execFileSync("git", ["init", "-q"], { cwd: repository });
execFileSync("git", ["config", "--local", "core.autocrlf", "false"], {
cwd: repository,
});
await Promise.all([
writeFile(
join(repository, ".gitignore"),
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/tests-ts/check-markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ test("checks tracked Markdown including leading-dash paths and skips deleted, un
if (process.platform !== "win32")
await symlink("untracked.md", join(sdk, "linked.md"));
gitText(["init", "--quiet"], { cwd: root });
gitText(["config", "--local", "core.autocrlf", "false"], { cwd: root });
gitText(["add", "--", "."], { cwd: root });
await rm(join(sdk, "deleted.md"));
await writeFile(join(sdk, "untracked.md"), invalid);
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/tests-ts/cli-project-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ test("bulk scans apply config and linked operator prompts, preserve CSV scope ov
);
for (const args of [
["init", "-q", input.repository],
["-C", input.repository, "config", "--local", "core.autocrlf", "false"],
["-C", input.repository, "add", "."],
[
"-C",
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/tests-ts/compact-diff-scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function createRepository(): { root: string; repository: string } {
const repository = join(root, "repository");
mkdirSync(repository);
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
return { root, repository };
}

Expand Down
8 changes: 8 additions & 0 deletions sdk/typescript/tests-ts/component-scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,14 @@ test("retains tracked Unicode aliases when scoped Git matching is incomplete", a
await mkdir(join(paths.repository, nonCased));
await writeFile(join(paths.repository, nonCased, "app.ts"), "export {};\n");
execFileSync("git", ["-C", paths.repository, "init", "-q"]);
execFileSync("git", [
"-C",
paths.repository,
"config",
"--local",
"core.autocrlf",
"false",
]);
execFileSync("git", ["-C", paths.repository, "add", "--force", "."]);
expect(await scopedInventory(paths, nonCased)).toEqual({
paths: [nonCased + "/app.ts"],
Expand Down
132 changes: 132 additions & 0 deletions sdk/typescript/tests-ts/custom-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
runCustomValidation,
type CustomValidationResult,
} from "../src/custom-validation.js";
import { readScanFile } from "../src/contract.js";
import {
customDiscoveryPrompt,
customValidationConfig,
Expand Down Expand Up @@ -237,6 +238,89 @@ describe("custom validation", () => {
},
);

test.each(["unchanged", "replaced"])(
"compares exact provisional file identity when numeric IDs collide: %s",
async (state) => {
const title = `compares exact provisional file identity when numeric IDs collide: ${state}`;
if (runTestInSubprocess(import.meta.path, title)) return;
const f = await fixture();
const path = join(f.scanDir, "coverage.json");
const before = 2n ** 60n;
const after = before + 1n;
expect(Number(before)).toBe(Number(after));
const files = await import("node:fs/promises");
const original = {
lstat: files.lstat,
open: files.open,
realpath: files.realpath,
};
let replaced = false;
const inspecting = spyOn(files, "lstat").mockImplementation((async (
...args: Parameters<typeof original.lstat>
) => {
const metadata = await original.lstat(...args);
if (args[0] === path) {
const identity = replaced ? after : before;
Object.defineProperty(metadata, "ino", {
value:
typeof metadata.ino === "bigint" ? identity : Number(identity),
});
}
return metadata;
}) as typeof original.lstat);
const opening = spyOn(files, "open").mockImplementation(
async (...args) => {
const file = await original.open(...args);
if (args[0] === path) {
const identity = replaced ? after : before;
const stat = file.stat.bind(file);
spyOn(file, "stat").mockImplementation((async (
...options: Parameters<typeof stat>
) => {
const metadata = await stat(...options);
Object.defineProperty(metadata, "ino", {
value:
typeof metadata.ino === "bigint"
? identity
: Number(identity),
});
return metadata;
}) as typeof stat);
}
return file;
},
);
const resolving = spyOn(files, "realpath").mockImplementation((async (
...args: Parameters<typeof original.realpath>
) => {
const canonical = await original.realpath(...args);
if (args[0] === path && state === "replaced" && !replaced) {
await rename(path, `${path}.original`);
await writeFile(path, await readFile(`${path}.original`));
replaced = true;
}
return canonical;
}) as typeof original.realpath);
const run = mock(async () => JSON.stringify(result("reportable")));
try {
if (state === "replaced") {
await expect(runCustomValidation({ ...f, run })).rejects.toThrow(
"expected the checked regular file",
);
expect(replaced).toBe(true);
expect(run).not.toHaveBeenCalled();
} else {
await runCustomValidation({ ...f, run });
expect(run).toHaveBeenCalledTimes(1);
}
} finally {
resolving.mockRestore();
opening.mockRestore();
inspecting.mockRestore();
}
},
);

test.each(["scan-manifest.json", "findings.json", "coverage.json"])(
"reads the checked %s descriptor after pathname replacement",
async (name) => {
Expand Down Expand Up @@ -276,6 +360,53 @@ describe("custom validation", () => {
},
);

test("rejects scan root replacement when numeric directory IDs collide", async () => {
const title =
"rejects scan root replacement when numeric directory IDs collide";
if (runTestInSubprocess(import.meta.path, title)) return;
const f = await fixture();
const before = 2n ** 60n;
const after = before + 1n;
expect(Number(before)).toBe(Number(after));
const files = await import("node:fs/promises");
const original = { lstat: files.lstat, realpath: files.realpath };
let replaced = false;
const inspecting = spyOn(files, "lstat").mockImplementation((async (
...args: Parameters<typeof original.lstat>
) => {
const metadata = await original.lstat(...args);
if (args[0] === f.scanDir) {
const identity = replaced ? after : before;
Object.defineProperty(metadata, "ino", {
value: typeof metadata.ino === "bigint" ? identity : Number(identity),
});
}
return metadata;
}) as typeof original.lstat);
const resolving = spyOn(files, "realpath").mockImplementation((async (
...args: Parameters<typeof original.realpath>
) => {
const canonical = await original.realpath(...args);
if (args[0] === f.scanDir && !replaced) {
replaced = true;
await rename(f.scanDir, `${f.scanDir}.original`);
await mkdir(f.scanDir, { mode: 0o700 });
}
return canonical;
}) as typeof original.realpath);
try {
await expect(
readScanFile(f.scanDir, "coverage.json", "coverage.json"),
).rejects.toThrow(
"Scan directory must be an existing non-symlink directory",
);
expect(replaced).toBe(true);
} finally {
resolving.mockRestore();
inspecting.mockRestore();
}
});

test.each(["malformed", "directory"])(
"rejects a %s provisional document before invoking validation",
async (kind) => {
Expand Down Expand Up @@ -585,6 +716,7 @@ describe("custom validation", () => {
...args,
]);
git("init", "-q");
git("config", "--local", "core.autocrlf", "false");
git("add", ".");
git("commit", "-qm", "base");
await writeFile(
Expand Down
3 changes: 3 additions & 0 deletions sdk/typescript/tests-ts/diff-rank-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test("diff previews stay inside the selected repository", () => {
const nested = join(repository, "src", "nested");
mkdirSync(nested, { recursive: true });
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
writeFileSync(join(repository, "src", "handler.py"), "value = 1\n");
writeFileSync(join(repository, "src", "deleted.py"), "removed = True\n");
writeFileSync(join(repository, "src", "entry.py"), "handler.py");
Expand Down Expand Up @@ -112,6 +113,7 @@ test("preserves Unicode Git paths and legacy-encoded commit metadata", () => {
const source = join(repository, "src", "変更.py");
mkdirSync(join(repository, "src"), { recursive: true });
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
writeFileSync(source, "value = 1\n");
git(repository, "add", ".");
git(repository, "commit", "-qm", "base");
Expand Down Expand Up @@ -204,6 +206,7 @@ testPosix(
mkdirSync(shimDirectory, { recursive: true });
mkdirSync(externalBin);
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
writeFileSync(join(repository, "source.py"), "value = 1\n");
git(repository, "add", ".");
git(repository, "commit", "-qm", "base");
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/tests-ts/hosted-remediation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function fixture(
const repository = join(root, "repository");
writeSource(repository, "guard.ts", "export const allowed = false;\n");
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
git(repository, "add", ".");
git(repository, "commit", "-qm", "fixture");
return remediationInput(
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/tests-ts/hosted-scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ async function fixture(
])
writeSource(repository, name, "export const value = 1;\n");
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
git(repository, "add", ".");
git(repository, "commit", "-qm", "fixture");
return {
Expand Down
8 changes: 8 additions & 0 deletions sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ async function fixture(git = false) {
);
if (git) {
execFileSync("git", ["init", "-q", repository]);
execFileSync("git", [
"-C",
repository,
"config",
"--local",
"core.autocrlf",
"false",
]);
execFileSync("git", ["-C", repository, "add", "."]);
execFileSync("git", [
"-C",
Expand Down
Loading
Loading