Skip to content

armgit / Ix

Public Imported from GitHub
0

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

fix(ingest): bound and confine tsconfig reads

KageBinary9e58df1f3ba6
diff --git a/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts b/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
index 0eedd9a7..9865e4c9 100644
--- a/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
+++ b/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
@@ -1,6 +1,6 @@
-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
-import { dirname, join } from "node:path";
+import { dirname, join, relative, sep } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
@@ -167,6 +167,64 @@ describe("createTypeScriptModuleResolver", () => {
expect(resolve("src/consumer.ts", "@core/thing")).toEqual(["src/@core/thing.ts"]);
});
+ it("ignores a config larger than the size cap instead of parsing it", () => {
+ // The resolver is built before ingestion's MAX_FILE_BYTES gate, so a repo
+ // could hand it a 64 MB JSONC file and take the process out with an
+ // uncatchable OOM. Small here; the cap is what is under test.
+ const root = workspace();
+ const config = write(
+ root,
+ "tsconfig.json",
+ `{/*${"a".repeat(1024 * 1024 + 16)}*/"compilerOptions":{"paths":{"@core":["src/worker"]}}}`,
+ );
+ const consumer = write(root, "src/consumer.ts");
+ const worker = write(root, "src/worker.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer, worker]);
+
+ // Config ignored entirely -> no mapping, and no opinion.
+ expect(resolve("src/consumer.ts", "@core")).toBeUndefined();
+ });
+
+ it("refuses an extends that escapes the workspace", () => {
+ // `startsWith(".")` is not containment: resolve() clamps at the filesystem
+ // root, so enough `..` segments name any absolute path.
+ const root = workspace();
+ const outside = mkdtempSync(join(tmpdir(), "ix-outside-"));
+ workspaces.push(outside);
+ writeFileSync(
+ join(outside, "tsconfig.json"),
+ JSON.stringify({ compilerOptions: { paths: { "@evil": ["target"] } } }),
+ );
+ const escape = relative(root, join(outside, "tsconfig.json")).split(sep).join("/");
+ const config = write(root, "tsconfig.json", JSON.stringify({ extends: `./${escape}` }));
+ const consumer = write(root, "src/consumer.ts");
+ const target = write(root, "target.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer, target]);
+
+ expect(resolve("src/consumer.ts", "@evil")).toBeUndefined();
+ });
+
+ it.skipIf(process.platform === "win32")(
+ "refuses an extends that reaches a device node through a symlink",
+ () => {
+ // Containment alone does not cover this: the link sits inside the
+ // workspace and only the *target* is a character device, which reports
+ // size 0 and would read forever.
+ const root = workspace();
+ const link = join(root, "linked.json");
+ try {
+ symlinkSync("/dev/zero", link);
+ } catch {
+ return; // no permission to symlink (e.g. restricted CI) — nothing to assert
+ }
+ const config = write(root, "tsconfig.json", JSON.stringify({ extends: "./linked.json" }));
+ const consumer = write(root, "src/consumer.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer, link]);
+
+ expect(resolve("src/consumer.ts", "@core")).toBeUndefined();
+ },
+ );
+
it("follows a relative extends that names a directory", () => {
// Previously an existsSync probe stopped at the directory itself and never
// tried <dir>/tsconfig.json, so the parent's paths were silently dropped.
diff --git a/ix-cli/src/cli/ts-module-resolution.ts b/ix-cli/src/cli/ts-module-resolution.ts
index 397dc7af..b34ddfeb 100644
--- a/ix-cli/src/cli/ts-module-resolution.ts
+++ b/ix-cli/src/cli/ts-module-resolution.ts
@@ -132,9 +132,33 @@ function parseJsonc(text: string): unknown {
return JSON.parse(normalized);
}
+/**
+ * Config files are read while the resolver is built, which happens *before*
+ * ingestion's own size gate, so they need their own. Mirrors `MAX_FILE_BYTES`
+ * in `ingest.ts`: anything larger is not a tsconfig, and parsing one is how a
+ * 64 MB JSONC file takes the process out with an uncatchable OOM.
+ */
+const MAX_CONFIG_BYTES = 1024 * 1024;
+
function readObject(filePath: string): Record<string, unknown> | undefined {
try {
- const parsed = parseJsonc(fs.readFileSync(filePath, "utf8"));
+ // Open once and fstat the same handle so the checks and the read observe the
+ // same inode — no stat-then-read window (CodeQL js/file-system-race). This is
+ // the pattern ingest.ts already uses.
+ const handle = fs.openSync(filePath, "r");
+ let text: string;
+ try {
+ const stats = fs.fstatSync(handle);
+ // `openSync` follows symlinks, so fstat describes the *target*. Refusing
+ // anything that is not a regular file is what stops a link to /dev/zero
+ // (unbounded read) or a FIFO (blocks the event loop forever) — neither of
+ // which a size check catches, since both report size 0.
+ if (!stats.isFile() || stats.size > MAX_CONFIG_BYTES) return undefined;
+ text = fs.readFileSync(handle, "utf8");
+ } finally {
+ fs.closeSync(handle);
+ }
+ const parsed = parseJsonc(text);
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: undefined;
@@ -149,10 +173,21 @@ function readObject(filePath: string): Record<string, unknown> | undefined {
* reads each one and `readObject` already reports an unreadable file as
* `undefined`, so there is no check-then-use window (CodeQL js/file-system-race).
*/
-function resolveExtends(configPath: string, value: unknown): string[] {
+function resolveExtends(workspaceRoot: string, configPath: string, value: unknown): string[] {
if (typeof value !== "string" || !value.startsWith(".")) return [];
const unresolved = nodePath.resolve(nodePath.dirname(configPath), value);
- return [unresolved, `${unresolved}.json`, nodePath.join(unresolved, "tsconfig.json")];
+ return [unresolved, `${unresolved}.json`, nodePath.join(unresolved, "tsconfig.json")].filter(
+ // `startsWith(".")` is not containment: `nodePath.resolve` clamps at the
+ // filesystem root, so "../../../../../../.." names any absolute path from
+ // any depth. The config is repo content, so without this an ingested repo
+ // chooses which files on the machine get read.
+ (candidate) => isWithinWorkspace(workspaceRoot, candidate),
+ );
+}
+
+function isWithinWorkspace(workspaceRoot: string, candidate: string): boolean {
+ const relative = nodePath.relative(nodePath.resolve(workspaceRoot), candidate);
+ return relative !== "" && !relative.startsWith("..") && !nodePath.isAbsolute(relative);
}
function loadConfig(
@@ -174,7 +209,7 @@ function loadConfig(
}
let parent: LoadedConfig | undefined;
- for (const candidate of resolveExtends(absoluteConfig, raw.extends)) {
+ for (const candidate of resolveExtends(workspaceRoot, absoluteConfig, raw.extends)) {
parent = loadConfig(workspaceRoot, candidate, cache, loading);
if (parent) break;
}