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): check containment against the file actually opened

KageBinaryadc97c1a9799
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 a36373ef..17acb80b 100644
--- a/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
+++ b/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
@@ -205,6 +205,66 @@ describe("createTypeScriptModuleResolver", () => {
expect(resolve("src/consumer.ts", "@evil")).toBeUndefined();
});
+ it.skipIf(process.platform === "win32")(
+ "refuses an extends that leaves the workspace through a symlinked directory",
+ () => {
+ // Every segment of "./linked/tsconfig.json" is inside the workspace by
+ // path arithmetic, so a lexical containment check passes it. Only the
+ // path we actually opened shows that `linked/` is a link to elsewhere.
+ const root = workspace();
+ const outside = mkdtempSync(join(tmpdir(), "ix-outside-"));
+ workspaces.push(outside);
+ writeFileSync(
+ join(outside, "tsconfig.json"),
+ JSON.stringify({ compilerOptions: { paths: { "@evil": ["target"] } } }),
+ );
+ try {
+ symlinkSync(outside, join(root, "linked"), "dir");
+ } catch {
+ return; // no permission to symlink (e.g. restricted CI) — nothing to assert
+ }
+ const config = write(root, "tsconfig.json", JSON.stringify({ extends: "./linked/tsconfig.json" }));
+ 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")(
+ "still resolves when the workspace root is itself reached through a symlink",
+ () => {
+ // Comparing a resolved file path against an unresolved root rejects every
+ // config whenever the root is a link — macOS /var -> /private/var, a home
+ // on a network mount, a ~/code symlink. No CI runner here has one, so this
+ // is the only thing standing between that and a silent no-op for those
+ // users.
+ const real = workspace();
+ const link = join(mkdtempSync(join(tmpdir(), "ix-linkroot-")), "root");
+ workspaces.push(dirname(link));
+ try {
+ symlinkSync(real, link, "dir");
+ } catch {
+ return; // no permission to symlink (e.g. restricted CI) — nothing to assert
+ }
+ write(
+ real,
+ "tsconfig.json",
+ JSON.stringify({ compilerOptions: { baseUrl: ".", paths: { "@core": ["src/worker"] } } }),
+ );
+ write(real, "src/worker.ts");
+ write(real, "src/consumer.ts");
+ const resolve = createTypeScriptModuleResolver(link, [
+ join(link, "tsconfig.json"),
+ join(link, "src/worker.ts"),
+ join(link, "src/consumer.ts"),
+ ]);
+
+ expect(resolve("src/consumer.ts", "@core")).toEqual(["src/worker.ts"]);
+ },
+ );
+
it.skipIf(process.platform === "win32")(
"refuses an extends that reaches a device node through a symlink",
() => {
diff --git a/ix-cli/src/cli/ts-module-resolution.ts b/ix-cli/src/cli/ts-module-resolution.ts
index e4c64c23..7c827c2d 100644
--- a/ix-cli/src/cli/ts-module-resolution.ts
+++ b/ix-cli/src/cli/ts-module-resolution.ts
@@ -140,7 +140,50 @@ function parseJsonc(text: string): unknown {
*/
const MAX_CONFIG_BYTES = 1024 * 1024;
-function readObject(filePath: string): Record<string, unknown> | undefined {
+/**
+ * True when the file we are *holding open* lives inside the workspace.
+ *
+ * The lexical check in {@link isWithinWorkspace} cannot see through a symlinked
+ * directory: every segment of `./linked/tsconfig.json` is inside the workspace
+ * by path arithmetic even when `linked/` points somewhere else entirely. Only
+ * the resolved path shows that, so resolve it and re-check.
+ *
+ * Resolving the name a second time reopens the question of whether it still
+ * denotes the file we hold, so the resolved path is confirmed to be the same
+ * inode as the open handle. Without that, swapping the symlink between the open
+ * and the resolve would let an outside file be read under an inside name.
+ * (`ino` is 0 on filesystems that do not report one — chiefly some Windows
+ * configurations — where this degrades to the plain resolved-path check.)
+ *
+ * The ROOT is resolved too, and that is not symmetry for its own sake: a
+ * resolved file compared against an unresolved root rejects every config
+ * whenever the workspace is itself reached through a link — macOS `/var` ->
+ * `/private/var`, a home directory on a network mount, a `~/code` symlink. That
+ * would silently switch tsconfig resolution off for those users, and no CI
+ * runner here has a symlinked root to notice it.
+ */
+function openedWithinWorkspace(
+ workspaceRoot: string,
+ filePath: string,
+ opened: fs.Stats,
+): boolean {
+ try {
+ const resolved = fs.realpathSync(filePath);
+ const viaResolved = fs.statSync(resolved);
+ if (viaResolved.dev !== opened.dev || viaResolved.ino !== opened.ino) return false;
+ let resolvedRoot: string;
+ try {
+ resolvedRoot = fs.realpathSync(workspaceRoot);
+ } catch {
+ resolvedRoot = workspaceRoot; // unreadable root: fall back to the lexical check
+ }
+ return isWithinWorkspace(resolvedRoot, resolved);
+ } catch {
+ return false;
+ }
+}
+
+function readObject(workspaceRoot: string, filePath: string): Record<string, unknown> | undefined {
try {
// 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
@@ -161,6 +204,7 @@ function readObject(filePath: string): Record<string, unknown> | undefined {
// and no EOF) — neither of which a size check catches, since both
// report size 0.
if (!stats.isFile() || stats.size > MAX_CONFIG_BYTES) return undefined;
+ if (!openedWithinWorkspace(workspaceRoot, filePath, stats)) return undefined;
text = fs.readFileSync(handle, "utf8");
} finally {
fs.closeSync(handle);
@@ -215,7 +259,7 @@ function loadConfig(
if (loading.has(absoluteConfig)) return undefined;
loading.add(absoluteConfig);
- const raw = readObject(absoluteConfig);
+ const raw = readObject(workspaceRoot, absoluteConfig);
if (!raw) {
cache.set(absoluteConfig, undefined);
loading.delete(absoluteConfig);