Repository navigation
Expand file tree
/
Copy pathIdaInput.ts
More file actions
80 lines (79 loc) · 2.45 KB
/
Copy pathIdaInput.ts
File metadata and controls
80 lines (79 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { TOOL_CONTRACTS } from "../contracts/toolContracts.js";
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
import { jsonObjectSchema, type JsonValue } from "../domain/jsonValue.js";
import { err, ok } from "../domain/result.js";
import { idaAddressSchema } from "./IdaProtocolValues.js";
import { projectInputIssues } from "../domain/inputIssueProjection.js";
/** Apply the named public contract and IDA's explicit-address boundary before startup. */
export const parseIdaInput = (
operation: string,
parameters: Readonly<Record<string, JsonValue>>,
) => {
const contract = TOOL_CONTRACTS.find(({ name }) => name === operation);
const parsed = contract?.inputSchema.safeParse(parameters);
if (parsed?.success !== true)
return err(
new AnalysisInputError(
operation,
{ cause: parsed?.error },
parsed?.error === undefined
? []
: projectInputIssues(parsed.error.issues, parameters),
),
);
if (operation === "xrefs" && typeof parameters.address !== "string")
return err(
new AnalysisInputError(operation, undefined, [
{
path: ["address"],
reason: "missing_argument",
message: "IDA requires an explicit hexadecimal address.",
},
]),
);
if (
parameters.address !== undefined &&
!idaAddressSchema.safeParse(parameters.address).success
)
return err(
new AnalysisInputError(operation, undefined, [
{
path: ["address"],
reason: "invalid_format",
message: "IDA requires a hexadecimal address.",
},
]),
);
if (
typeof parameters.procedure === "string" &&
parameters.procedure.length === 0
)
return err(
new AnalysisInputError(operation, undefined, [
{
path: ["procedure"],
reason: "invalid_value",
message: "Supply a function name or address.",
},
]),
);
if (parameters.mode === "regex" && typeof parameters.pattern === "string") {
try {
new RegExp(
parameters.pattern,
parameters.case_sensitive === true ? "u" : "iu",
);
} catch (cause: unknown) {
return err(
new AnalysisInputError(operation, { cause }, [
{
path: ["pattern"],
reason: "invalid_format",
message: "Supply a valid regular expression.",
},
]),
);
}
}
return ok(jsonObjectSchema.parse(parsed.data));
};