fix(cli): route JavaScript applications from analyze
This commit is contained in:
@@ -13,8 +13,9 @@ import {
|
||||
PermissionRequiredError,
|
||||
type AnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import { projectInputIssues } from "../domain/inputIssueProjection.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import type { ExecutionOptions } from "./AnalysisProvider.js";
|
||||
import { createJavaScriptApplicationEvidence } from "./JavaScriptApplicationEvidence.js";
|
||||
import { reconstructJavaScriptArtifact } from "./JavaScriptArtifactReconstruction.js";
|
||||
@@ -29,7 +30,14 @@ export const analyzeJavaScriptApplication = async (
|
||||
options: ExecutionOptions = {},
|
||||
): Promise<Result<Evidence, AnalysisError>> => {
|
||||
const parsed = analyzeJavaScriptApplicationInputSchema.safeParse(rawInput);
|
||||
if (!parsed.success) return err(new AnalysisInputError(OPERATION));
|
||||
if (!parsed.success)
|
||||
return err(
|
||||
new AnalysisInputError(
|
||||
OPERATION,
|
||||
undefined,
|
||||
projectInputIssues(parsed.error.issues, rawInput),
|
||||
),
|
||||
);
|
||||
return analyzeJavaScriptApplicationValidated(authority, parsed.data, options);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { z } from "incur";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
import { runDirectAnalysis } from "../application/DirectAnalysis.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
@@ -6,6 +8,7 @@ import { logCliCommand } from "../cliLogging.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { directAnalysisOptions, providerSelectionOption } from "./options.js";
|
||||
import type { CliInstance } from "./types.js";
|
||||
import { runCliJavaScriptApplicationAnalysis } from "./javascriptApplicationAnalysis.js";
|
||||
|
||||
export const registerCoreAnalysisCommands = (
|
||||
cli: CliInstance,
|
||||
@@ -38,20 +41,23 @@ const registerCoreCommands = (cli: CliInstance, logger: Logger): void => {
|
||||
.describe("Load and update a local analysis snapshot"),
|
||||
provider: providerSelectionOption,
|
||||
});
|
||||
const analyzeOptions = overviewOptions.extend({
|
||||
approved: z
|
||||
.boolean()
|
||||
.default(false)
|
||||
.describe(
|
||||
"Approve static analysis when the path is a JavaScript directory or ASAR",
|
||||
),
|
||||
});
|
||||
cli.command(CLI_COMMANDS.analyze, {
|
||||
description: "Get an overview of an app",
|
||||
args: z.object({
|
||||
path: z.string().describe("App, program, or analysis database path"),
|
||||
}),
|
||||
options: overviewOptions,
|
||||
options: analyzeOptions,
|
||||
run: ({ args, options }) =>
|
||||
logCliCommand(logger, "analyze", () =>
|
||||
runDirectAnalysis(
|
||||
args.path,
|
||||
"binary_overview",
|
||||
{ detail: options.detail, limit: options.limit },
|
||||
directAnalysisOptions(logger, options.snapshot, options.provider),
|
||||
),
|
||||
runRoutedOverview(args.path, options, logger),
|
||||
),
|
||||
});
|
||||
cli.command(CLI_COMMANDS.inspect, {
|
||||
@@ -96,6 +102,45 @@ const registerCoreCommands = (cli: CliInstance, logger: Logger): void => {
|
||||
});
|
||||
};
|
||||
|
||||
const runRoutedOverview = async (
|
||||
path: string,
|
||||
options: {
|
||||
readonly approved: boolean;
|
||||
readonly detail: "concise" | "detailed";
|
||||
readonly limit: number;
|
||||
readonly snapshot?: string | undefined;
|
||||
readonly provider?: string | undefined;
|
||||
},
|
||||
logger: Logger,
|
||||
) => {
|
||||
if (
|
||||
options.provider === undefined &&
|
||||
options.snapshot === undefined &&
|
||||
(await isJavaScriptApplicationPath(path))
|
||||
)
|
||||
return runCliJavaScriptApplicationAnalysis({
|
||||
input_path: resolve(path),
|
||||
approved: options.approved,
|
||||
});
|
||||
return runDirectAnalysis(
|
||||
path,
|
||||
"binary_overview",
|
||||
{ detail: options.detail, limit: options.limit },
|
||||
directAnalysisOptions(logger, options.snapshot, options.provider),
|
||||
);
|
||||
};
|
||||
|
||||
const isJavaScriptApplicationPath = async (path: string): Promise<boolean> => {
|
||||
const lower = path.toLowerCase();
|
||||
if (lower.endsWith(".asar")) return true;
|
||||
if (lower.endsWith(".app")) return false;
|
||||
try {
|
||||
return (await stat(path)).isDirectory();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const registerXrefsCommand = (cli: CliInstance, logger: Logger): void => {
|
||||
cli.command(CLI_COMMANDS.xrefs, {
|
||||
description: "List bounded references to an analyzed address",
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { analyzeJavaScriptApplication } from "../application/JavaScriptApplicationService.js";
|
||||
import { loadConfiguredPermissionAuthority } from "../application/PermissionConfiguration.js";
|
||||
import { parseConfig } from "../config.js";
|
||||
import { projectAnalysisError } from "../domain/errors.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
|
||||
/** Execute the shared one-shot CLI boundary for static JavaScript analysis. */
|
||||
export const runCliJavaScriptApplicationAnalysis = async (
|
||||
input: unknown,
|
||||
): Promise<JsonValue> => {
|
||||
const config = parseConfig(process.env);
|
||||
if (!config.ok) return cliError(config.error);
|
||||
const authority = await loadConfiguredPermissionAuthority(config.value);
|
||||
if (!authority.ok) return cliError(authority.error);
|
||||
const result = await analyzeJavaScriptApplication(authority.value, input);
|
||||
return result.ok ? result.value : cliError(result.error);
|
||||
};
|
||||
|
||||
const cliError = (
|
||||
error: Parameters<typeof projectAnalysisError>[0],
|
||||
): JsonValue => ({
|
||||
error: "JavaScript application analysis failed",
|
||||
...projectAnalysisError(error),
|
||||
});
|
||||
+24
-38
@@ -4,7 +4,6 @@ import {
|
||||
inspectElectronPage,
|
||||
listElectronTargets,
|
||||
} from "./application/ElectronObservationService.js";
|
||||
import { analyzeJavaScriptApplication } from "./application/JavaScriptApplicationService.js";
|
||||
import { reconcileJavaScriptRuntimeEvidence } from "./application/JavaScriptRuntimeReconciliationService.js";
|
||||
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
|
||||
import { CdpElectronProvider } from "./browser/CdpElectronProvider.js";
|
||||
@@ -14,7 +13,6 @@ import {
|
||||
inspectElectronPageInputSchema,
|
||||
listElectronTargetsInputSchema,
|
||||
} from "./domain/electronObservation.js";
|
||||
import { analyzeJavaScriptApplicationInputSchema } from "./domain/javascriptApplicationAnalysis.js";
|
||||
import { AnalysisInputError, projectAnalysisError } from "./domain/errors.js";
|
||||
import type { JsonValue } from "./domain/jsonValue.js";
|
||||
import type { Logger } from "./logger.js";
|
||||
@@ -24,6 +22,7 @@ import {
|
||||
electronPageInspectionOptions,
|
||||
javascriptApplicationOptions,
|
||||
} from "./cliObservationOptions.js";
|
||||
import { runCliJavaScriptApplicationAnalysis } from "./cli/javascriptApplicationAnalysis.js";
|
||||
|
||||
const scopeOptions = {
|
||||
allowedFileRoots: z
|
||||
@@ -191,42 +190,29 @@ const registerJavaScriptApplicationCommand = (
|
||||
}),
|
||||
options: javascriptApplicationOptions,
|
||||
run: ({ args, options }) =>
|
||||
logCliCommand(
|
||||
logger,
|
||||
CLI_COMMANDS.analyzeJavaScriptApplication,
|
||||
async () => {
|
||||
const context = await electronContext();
|
||||
if (!context.ok) return context.error;
|
||||
const parsed = analyzeJavaScriptApplicationInputSchema.safeParse({
|
||||
input_path: args.path,
|
||||
approved: options.approved,
|
||||
format: options.format,
|
||||
source_map_read_approved: options.sourceMapReadApproved,
|
||||
limits: {
|
||||
max_entries: options.maxEntries,
|
||||
max_total_artifact_bytes: options.maxTotalArtifactBytes,
|
||||
max_artifact_entry_bytes: options.maxArtifactEntryBytes,
|
||||
max_compression_ratio: options.maxCompressionRatio,
|
||||
max_depth: options.maxDepth,
|
||||
max_path_bytes: options.maxPathBytes,
|
||||
max_text_files: options.maxTextFiles,
|
||||
max_total_text_bytes: options.maxTotalTextBytes,
|
||||
max_text_file_bytes: options.maxTextFileBytes,
|
||||
max_ast_nodes: options.maxAstNodes,
|
||||
max_findings: options.maxFindings,
|
||||
max_modules: options.maxModules,
|
||||
max_source_map_sources: options.maxSourceMapSources,
|
||||
max_parse_milliseconds: options.maxParseMilliseconds,
|
||||
},
|
||||
});
|
||||
if (!parsed.success)
|
||||
return inputError("analyze_javascript_application");
|
||||
const result = await analyzeJavaScriptApplication(
|
||||
context.authority,
|
||||
parsed.data,
|
||||
);
|
||||
return result.ok ? result.value : cliError(result.error);
|
||||
},
|
||||
logCliCommand(logger, CLI_COMMANDS.analyzeJavaScriptApplication, () =>
|
||||
runCliJavaScriptApplicationAnalysis({
|
||||
input_path: args.path,
|
||||
approved: options.approved,
|
||||
format: options.format,
|
||||
source_map_read_approved: options.sourceMapReadApproved,
|
||||
limits: {
|
||||
max_entries: options.maxEntries,
|
||||
max_total_artifact_bytes: options.maxTotalArtifactBytes,
|
||||
max_artifact_entry_bytes: options.maxArtifactEntryBytes,
|
||||
max_compression_ratio: options.maxCompressionRatio,
|
||||
max_depth: options.maxDepth,
|
||||
max_path_bytes: options.maxPathBytes,
|
||||
max_text_files: options.maxTextFiles,
|
||||
max_total_text_bytes: options.maxTotalTextBytes,
|
||||
max_text_file_bytes: options.maxTextFileBytes,
|
||||
max_ast_nodes: options.maxAstNodes,
|
||||
max_findings: options.maxFindings,
|
||||
max_modules: options.maxModules,
|
||||
max_source_map_sources: options.maxSourceMapSources,
|
||||
max_parse_milliseconds: options.maxParseMilliseconds,
|
||||
},
|
||||
}),
|
||||
),
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user