Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(config): show why configuration files fail to load (#1634)

ianw-oaie8f11d47e403
diff --git a/sdk/typescript/src/project-config.ts b/sdk/typescript/src/project-config.ts
index 33d25721d..97f369314 100644
--- a/sdk/typescript/src/project-config.ts
+++ b/sdk/typescript/src/project-config.ts
@@ -15,7 +15,7 @@ import {
mergeCodexOverrides,
type JsonObject,
} from "./config.js";
-import { ConfigurationError } from "./errors.js";
+import { ConfigurationError, errorMessage } from "./errors.js";
import { DEFAULT_DEEP_SCAN_SETTINGS } from "./deep-scan-defaults.js";
import { resolveConfigPath, type AbsolutePath } from "./config-path.js";
import { deepScanOptions, type DeepScanSources } from "./deep-config.js";
@@ -138,7 +138,7 @@ export async function readProjectConfig(
text = await readFile(path, "utf8");
} catch (error) {
throw new ConfigurationError(
- `Cannot read project configuration at ${path}.`,
+ `Cannot read project configuration at ${path}: ${errorMessage(error)}`,
{ cause: error },
);
}
@@ -154,7 +154,7 @@ export async function readProjectConfig(
}
} catch (error) {
throw new ConfigurationError(
- `Cannot parse project configuration at ${path}.`,
+ `Cannot parse project configuration at ${path}: ${errorMessage(error)}`,
{ cause: error },
);
}
diff --git a/sdk/typescript/tests-ts/cli-project-config.test.ts b/sdk/typescript/tests-ts/cli-project-config.test.ts
index 36e46ae6a..f004016dd 100644
--- a/sdk/typescript/tests-ts/cli-project-config.test.ts
+++ b/sdk/typescript/tests-ts/cli-project-config.test.ts
@@ -981,11 +981,14 @@ test("a malformed selected file fails before constructing a client", async () =>
),
).toBe(2);
expect(onConfig).not.toHaveBeenCalled();
- expect(stderr.text()).toContain("Cannot parse project configuration");
+ expect(stderr.text()).toContain(
+ `Cannot parse project configuration at ${input.config}: `,
+ );
+ expect(stderr.text()).toContain("end with a ]");
expect(JSON.parse(stdout.text())).toEqual({
status: "failed",
code: "SCAN_FAILED",
- message: `Cannot parse project configuration at ${input.config}.`,
+ message: expect.stringContaining("end with a ]"),
});
});
diff --git a/sdk/typescript/tests-ts/project-config.test.ts b/sdk/typescript/tests-ts/project-config.test.ts
index 4c0535cdf..bf1460a5d 100644
--- a/sdk/typescript/tests-ts/project-config.test.ts
+++ b/sdk/typescript/tests-ts/project-config.test.ts
@@ -1,4 +1,5 @@
-import { readFile, writeFile } from "node:fs/promises";
+import assert from "node:assert/strict";
+import { mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, describe, expect, test } from "bun:test";
import Ajv from "ajv";
@@ -12,6 +13,7 @@ import {
type ProjectConfigInput,
} from "../src/project-config-schema.js";
import { DEFAULT_DEEP_SCAN_SETTINGS } from "../src/deep-scan-defaults.js";
+import { ConfigurationError } from "../src/errors.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
const { temporaryDirectory, cleanup } =
@@ -248,10 +250,8 @@ describe("project configuration input contract", () => {
});
test.each([
- ["invalid.yaml", "scan: [\n"],
["duplicate.yaml", "scan: {}\nscan: {}\n"],
["stream.yaml", "scan: {}\n---\nscan: {}\n"],
- ["invalid.json", '{"scan":'],
["invalid.ts", "export default {};"],
["unknown.yaml", "scan:\n workres: 2\n"],
])("rejects an invalid selected file: %s", async (name, contents) => {
@@ -261,10 +261,35 @@ describe("project configuration input contract", () => {
await expect(readProjectConfig(path)).rejects.toThrow();
});
- test("reports a missing selected file", async () => {
- await expect(
- readProjectConfig("missing.yaml", await temporaryDirectory()),
- ).rejects.toThrow("Cannot read project configuration");
+ test.each([
+ ["scan.yaml", "scan: [\n"],
+ ["scan.json", '{"scan":'],
+ ])(
+ "includes the parser message when %s cannot be parsed",
+ async (name, contents) => {
+ const path = join(await temporaryDirectory(), name);
+ await writeFile(path, contents);
+ const error = await readProjectConfig(path).catch(
+ (cause: unknown) => cause,
+ );
+ assert(error instanceof ConfigurationError);
+ assert(error.cause instanceof Error);
+ expect(error.message).toBe(
+ `Cannot parse project configuration at ${path}: ${error.cause.message}`,
+ );
+ },
+ );
+
+ test.each([
+ ["missing.yaml", "ENOENT"],
+ ["directory.yaml", "EISDIR"],
+ ])("includes the system error for %s", async (name, code) => {
+ const path = join(await temporaryDirectory(), name);
+ if (code === "EISDIR") await mkdir(path);
+ await expect(readProjectConfig(path)).rejects.toMatchObject({
+ message: expect.stringContaining(`${path}: ${code}`),
+ cause: expect.objectContaining({ code }),
+ });
});
});