feat(core): add typed policy and integrity contracts
This commit is contained in:
@@ -9,7 +9,14 @@ import { parseConfig } from "./config.js";
|
||||
import { logCliCommand } from "./cliLogging.js";
|
||||
import type { Logger } from "./logger.js";
|
||||
import type { JsonValue } from "./domain/jsonValue.js";
|
||||
import { projectAnalysisError, type AnalysisError } from "./domain/errors.js";
|
||||
import {
|
||||
AnalysisProtocolError,
|
||||
PermissionRequiredError,
|
||||
projectAnalysisError,
|
||||
type AnalysisError,
|
||||
} from "./domain/errors.js";
|
||||
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
|
||||
import type { AppConfig } from "./config.js";
|
||||
|
||||
/** Register filesystem-gated Evidence v2 commands. */
|
||||
export const registerEvidenceCommands = (
|
||||
@@ -25,6 +32,10 @@ export const registerEvidenceCommands = (
|
||||
logCliCommand(logger, "evidence-import", async () => {
|
||||
const config = parseConfig(process.env);
|
||||
if (!config.ok) return cliError(config.error);
|
||||
const denied = await authorizeEvidence(config.value, [
|
||||
{ capability: "evidence_read", path: args.path, access: "read" },
|
||||
]);
|
||||
if (denied !== undefined) return cliError(denied);
|
||||
const imported = await importEvidenceBundleCommand(
|
||||
args.path,
|
||||
config.value.evidenceFilePolicy,
|
||||
@@ -45,6 +56,11 @@ export const registerEvidenceCommands = (
|
||||
logCliCommand(logger, "evidence-export", async () => {
|
||||
const config = parseConfig(process.env);
|
||||
if (!config.ok) return cliError(config.error);
|
||||
const denied = await authorizeEvidence(config.value, [
|
||||
{ capability: "evidence_read", path: args.source, access: "read" },
|
||||
{ capability: "evidence_write", path: args.output, access: "write" },
|
||||
]);
|
||||
if (denied !== undefined) return cliError(denied);
|
||||
const exported = await exportEvidenceBundleCommand(
|
||||
args.source,
|
||||
args.output,
|
||||
@@ -69,6 +85,11 @@ export const registerEvidenceCommands = (
|
||||
logCliCommand(logger, "compare", async () => {
|
||||
const config = parseConfig(process.env);
|
||||
if (!config.ok) return cliError(config.error);
|
||||
const denied = await authorizeEvidence(config.value, [
|
||||
{ capability: "evidence_read", path: args.left, access: "read" },
|
||||
{ capability: "evidence_read", path: args.right, access: "read" },
|
||||
]);
|
||||
if (denied !== undefined) return cliError(denied);
|
||||
const compared = await compareEvidenceBundlesCommand({
|
||||
leftPath: args.left,
|
||||
rightPath: args.right,
|
||||
@@ -81,6 +102,39 @@ export const registerEvidenceCommands = (
|
||||
});
|
||||
};
|
||||
|
||||
const authorizeEvidence = async (
|
||||
config: AppConfig,
|
||||
requests: readonly {
|
||||
readonly capability: "evidence_read" | "evidence_write";
|
||||
readonly path: string;
|
||||
readonly access: "read" | "write";
|
||||
}[],
|
||||
): Promise<AnalysisError | undefined> => {
|
||||
const authority = await loadConfiguredPermissionAuthority(config);
|
||||
if (!authority.ok) return authority.error;
|
||||
for (const request of requests) {
|
||||
const result = await authority.value.authorize(
|
||||
{
|
||||
capability: request.capability,
|
||||
roots: [request.path],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
network: "none",
|
||||
mount: false,
|
||||
operation_identity: `cli:${request.capability}:${request.path}`,
|
||||
},
|
||||
request.access,
|
||||
);
|
||||
if (!result.ok)
|
||||
return result.error instanceof PermissionRequiredError
|
||||
? result.error
|
||||
: new AnalysisProtocolError(result.error.message, {
|
||||
cause: result.error,
|
||||
});
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const cliError = (error: AnalysisError): JsonValue => ({
|
||||
error: "Analysis failed",
|
||||
...projectAnalysisError(error),
|
||||
|
||||
Reference in New Issue
Block a user