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
30 changes: 25 additions & 5 deletions extensions/shared/child-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ function packageSourceValue(source: PackageSource) {

const CHILD_DISABLED_OPENPI_EXTENSION =
"-extensions/git-info/index.ts" as const;
const OPENPI_GIT_INFO_EXTENSION_PATH = realpathSync.native(
fileURLToPath(new URL("../git-info/index.ts", import.meta.url)),
);

function canonicalExistingPath(value: string) {
try {
Expand All @@ -185,15 +182,38 @@ function canonicalExistingPath(value: string) {
}
}

/**
* ENOENT means git-info is truly absent (trimmed fork, partial install):
* nothing to exclude. Any other failure (permissions, symlink loops) means we
* cannot verify the Git-polling extension is present, so fail closed instead
* of running a child with an unverifiable extension.
*/
export function resolveGitInfoPathOrThrow(
resolve: (value: string) => string = realpathSync.native,
): string | undefined {
try {
return resolve(
fileURLToPath(new URL("../git-info/index.ts", import.meta.url)),
);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
throw error;
}
}

function excludeOpenPiGitInfoExtension(
resources: LoadExtensionsResult,
resolve: (value: string) => string = realpathSync.native,
): LoadExtensionsResult {
const gitInfoPath = resolveGitInfoPathOrThrow(resolve);
// undefined = absent (ENOENT): nothing to exclude. Non-ENOENT failures
// throw from resolveGitInfoPathOrThrow and fail child creation upstream.
if (gitInfoPath === undefined) return resources;
return {
...resources,
extensions: resources.extensions.filter(
(extension) =>
canonicalExistingPath(extension.resolvedPath) !==
OPENPI_GIT_INFO_EXTENSION_PATH,
canonicalExistingPath(extension.resolvedPath) !== gitInfoPath,
),
};
}
Expand Down
31 changes: 30 additions & 1 deletion tests/extensions/shared/child-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { syncBuiltinESMExports } from "node:module";
import { tmpdir } from "node:os";
import * as path from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { fileURLToPath, pathToFileURL } from "node:url";
import {
createAgentSession,
DefaultPackageManager,
Expand All @@ -34,6 +34,7 @@ import {
createChildResources,
type DisposableChildSession,
effectiveChildToolAllowlist,
resolveGitInfoPathOrThrow,
resolveStandaloneChildProjectTrust,
shutdownAndDisposeChildSession,
} from "../../../extensions/shared/child-session.ts";
Expand Down Expand Up @@ -1470,3 +1471,31 @@ test("every registered package tool is classified child-safe or excluded (fail-c
);
}
});

test("git-info exclusion: ENOENT degrades, other errors fail closed", async () => {
const enoent: NodeJS.ErrnoException = new Error("no such file");
enoent.code = "ENOENT";
// Absent (ENOENT) -> undefined, so nothing is excluded.
assert.equal(
resolveGitInfoPathOrThrow(() => {
throw enoent;
}),
undefined,
);
// Present -> the real path is returned for exclusion matching.
assert.equal(
resolveGitInfoPathOrThrow(() => "/repo/extensions/git-info/index.ts"),
"/repo/extensions/git-info/index.ts",
);
// Unverifiable (non-ENOENT) -> must throw, not silently degrade.
const eacces: NodeJS.ErrnoException = new Error("permission denied");
eacces.code = "EACCES";
assert.throws(
() =>
resolveGitInfoPathOrThrow(() => {
throw eacces;
}),
(error: unknown) => (error as NodeJS.ErrnoException).code === "EACCES",
"non-ENOENT realpath failures must fail closed",
);
});
Loading