fix(cli): classify JSON input permission denials
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
} from "./application/RegularFileRead.js";
|
||||
import { parseUtf8Json } from "./application/Utf8JsonInput.js";
|
||||
import {
|
||||
AnalysisAccessDeniedError,
|
||||
AnalysisInputError,
|
||||
AnalysisResourceConstraintError,
|
||||
} from "./domain/analysisErrorCore.js";
|
||||
@@ -49,6 +50,9 @@ export const parseCliJsonInput = async (
|
||||
input_reason: "too-large",
|
||||
},
|
||||
};
|
||||
const systemCode = accessDeniedSystemCode(cause);
|
||||
if (systemCode !== undefined)
|
||||
return jsonAccessDeniedError(value, operation, systemCode, cause);
|
||||
if (
|
||||
["{", "["].includes(value.trimStart()[0] ?? "") &&
|
||||
cannotBeAnExistingFile(cause) &&
|
||||
@@ -59,6 +63,15 @@ export const parseCliJsonInput = async (
|
||||
}
|
||||
};
|
||||
|
||||
const accessDeniedSystemCode = (
|
||||
cause: unknown,
|
||||
): "EACCES" | "EPERM" | undefined => {
|
||||
if (!(cause instanceof Error) || !("code" in cause)) return undefined;
|
||||
return cause.code === "EACCES" || cause.code === "EPERM"
|
||||
? cause.code
|
||||
: undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve named string fields of CLI-supplied JSON against the operator
|
||||
* working directory. Each entry is a key path from the document root; only
|
||||
@@ -148,6 +161,23 @@ const jsonFileError = (
|
||||
},
|
||||
});
|
||||
|
||||
const jsonAccessDeniedError = (
|
||||
path: string,
|
||||
operation: string,
|
||||
systemCode: "EACCES" | "EPERM",
|
||||
cause: unknown,
|
||||
) => ({
|
||||
ok: false as const,
|
||||
error: {
|
||||
error: "Application workflow failed",
|
||||
...projectAnalysisError(
|
||||
new AnalysisAccessDeniedError(operation, path, systemCode, { cause }),
|
||||
),
|
||||
input_path: path,
|
||||
input_reason: "read-failed" as const,
|
||||
},
|
||||
});
|
||||
|
||||
/** Preserve file-selection and system failures alongside the requested path. */
|
||||
const readFailureIssue = (path: string | undefined, cause: unknown) => {
|
||||
const code =
|
||||
|
||||
@@ -247,9 +247,15 @@ describe("compiled CLI JSON path ambiguity", () => {
|
||||
});
|
||||
|
||||
expect(result.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
code: "access_denied",
|
||||
input_path: input,
|
||||
input_reason: "read-failed",
|
||||
details: {
|
||||
operation: "capture_browser_scenario",
|
||||
path: input,
|
||||
system_code: "EACCES",
|
||||
boundary: "filesystem-read",
|
||||
},
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
} finally {
|
||||
|
||||
@@ -13,12 +13,25 @@ import { promisify } from "node:util";
|
||||
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
|
||||
import { createCli } from "../../../src/cli.js";
|
||||
import { isCliOperationFailure } from "../../../src/cliLogging.js";
|
||||
import { parseCliJsonInput } from "../../../src/cliJsonInput.js";
|
||||
import { analysisCliErrorEnvelopeSchema } from "../../../src/contracts/errorSchemas.js";
|
||||
import { readWithoutFifoWriter } from "../../fixtures/fifoInput.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
const runCli = async (arguments_: readonly string[]): Promise<unknown> => {
|
||||
let stdout = "";
|
||||
await createCli({}).serve([...arguments_, "--json"], {
|
||||
env: {},
|
||||
stdout: (chunk) => {
|
||||
stdout += chunk;
|
||||
},
|
||||
exit: () => undefined,
|
||||
});
|
||||
return JSON.parse(stdout) as unknown;
|
||||
};
|
||||
|
||||
describe("CLI JSON file selection", () => {
|
||||
it("accepts a symlink to a regular JSON file", async () => {
|
||||
const root = await createTestTempDirectory("rea-json-input-symlink-");
|
||||
@@ -219,21 +232,27 @@ describe("CLI JSON input", () => {
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
|
||||
"lists a permission denial as the cause of an unreadable file",
|
||||
"projects a real permission denial as access_denied",
|
||||
async () => {
|
||||
const root = await createTestTempDirectory("rea-json-input-denied-");
|
||||
const path = join(root, "input.json");
|
||||
await writeFile(path, "{}");
|
||||
await chmod(path, 0o000);
|
||||
onTestFinished(() => chmod(path, 0o600));
|
||||
expect(await parseCliJsonInput(path, "test-input")).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
input_reason: "read-failed",
|
||||
details: {
|
||||
issues: [{ message: expect.stringContaining("(EACCES)") }],
|
||||
},
|
||||
const result = await runCli(["inspect-analysis-view", path]);
|
||||
expect(analysisCliErrorEnvelopeSchema.parse(result)).toMatchObject({
|
||||
error: "Application workflow failed",
|
||||
code: "access_denied",
|
||||
category: "unavailable",
|
||||
details: {
|
||||
operation: "inspect-analysis-view",
|
||||
path,
|
||||
system_code: "EACCES",
|
||||
boundary: "filesystem-read",
|
||||
},
|
||||
input_path: path,
|
||||
input_reason: "read-failed",
|
||||
remediation: { action: expect.stringContaining("read access") },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user