-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·229 lines (214 loc) · 8.05 KB
/
Copy pathindex.js
File metadata and controls
executable file
·229 lines (214 loc) · 8.05 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env node
import { Command, InvalidArgumentError, Option } from "commander";
import { loadEnv } from "./src/utils/utils.js";
import { GHSAPipelineRunner } from "./src/pipeline/ghsaPipeline.js";
import { DefaultPipelineRunner } from "./src/pipeline/defaultPipeline.js";
import { RunnerSourceNotExported } from "./src/runners/runnerSourceNotExported.js";
import { RunnerExploitUpstreamPackage } from "./src/runners/runnerExploitUpstreamPackage.js";
import { readFileSync } from "node:fs";
import fs from "fs";
import { loadVulnerabilityTypes } from "./src/models/vulnerability.js";
import { loadRefiners } from "./src/prompting/promptRefiner.js";
import { loadModels } from "./src/model/model.js";
import DefaultRefiner from "./src/prompting/refiners/default.refiner.js";
import { fileURLToPath } from "node:url";
import RunnerMiniSWEAgent from "./src/runners/runnerMiniSWEAgent.js";
function intParser(value) {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new InvalidArgumentError("Not a number.");
}
return parsedValue;
}
function floatParser(value) {
const parsedValue = parseFloat(value);
if (isNaN(parsedValue)) {
throw new InvalidArgumentError("Not a number.");
}
return parsedValue;
}
/**
* @typedef {Object} RunnerOptions
* @property {string} model
* @property {boolean} promptCache
* @property {number} temperature
* @property {number} maxCompletionTokensTotal
* @property {number} maxPromptTokensTotal
* @property {boolean} alwaysExploit
* @property {string} vulnerabilityTypeLabel
* @property {string} output
* @property {string} packageName
* @property {boolean} verbose
* @property {number} choices
* @property {number} maxRefinements
* @property {string} refiner
* @property {string} runner
* @property {string} advisoryId
* @property {string} description
*/
/**
* @typedef {RunnerOptions & {
* advisoryIds: string[],
* offset: number,
* limit: number,
* timeout: number,
* reviewed: boolean
* }} PipelineRunnerOptions
*/
export const MAX_COMPLETION_TOKENS = 100_000;
export const MAX_PROMPT_TOKENS = 300_000;
const models = await loadModels();
function addModelOptions(command) {
return command
.addOption(
new Option("-m, --model <model>", "model to use")
.choices(models.map(s => s.name))
.default("gpt-5-mini")
)
.option(
"--promptCache",
"cache the results of the model",
)
.option(
"--temperature <temperature>",
"number between 0 and 2 indicating the randomness of the model",
floatParser,
1,
)
.option(
"--maxCompletionTokensTotal <maxCompletionTokensTotal>",
"maximum number of output tokens per task",
intParser,
MAX_COMPLETION_TOKENS,
)
.option(
"--maxPromptTokensTotal <maxPromptTokensTotal>",
"maximum number of input tokens per task",
intParser,
MAX_PROMPT_TOKENS,
)
.option(
"--alwaysExploit <alwaysExploit>",
"exploit without taint path if report indicates correct source",
true,
);
}
const vulnerabilityTypes = await loadVulnerabilityTypes();
const refiners = await loadRefiners();
function addBaseOptions(command) {
addModelOptions(command)
.addOption(
new Option(
"-t, --vulnerabilityTypeLabel <vulnerabilityTypeLabel>",
`vulnerability type`,
).choices(Object.values(vulnerabilityTypes).map((v) => v.label)).argParser(
(input) => {
if (!input) {
return undefined;
}
if (!Object.values(vulnerabilityTypes).find((v) => v.label === input)) {
throw new InvalidArgumentError("Invalid vulnerability type.");
}
return input;
}),
)
.option("-o, --output <output>", "output folder", "/output")
.option("-packageName, --packageName <packageName>", "package name")
.option("-v, --verbose", "print the prompts and responses of the model")
.option("-choices, --choices <choices>", "completion choices", intParser, 1)
.option(
"-maxRefinements, --maxRefinements <maxRefinements>",
"maximum number of refinement attempts",
intParser,
30,
)
.addOption(
new Option(
"-refiner, --refiner <refiner>",
`refiner type:\n${refiners.map((v) => v.default.name + ": " + v.description).join("\n")}`,
).default(DefaultRefiner.name).argParser(
(input) => {
if (!input) {
return undefined;
}
if (!refiners.find((v) => v.default.name === input)) {
throw new InvalidArgumentError("Invalid refiner.");
}
return input;
}),
)
.addOption(
new Option(
"-runner, --runner <runner>",
`runner type`,
).default(RunnerSourceNotExported.name).choices([RunnerSourceNotExported.name, RunnerMiniSWEAgent.name])
);
return command.allowUnknownOption(false);
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const cmd = new Command();
addBaseOptions(cmd.command("create"), { isDefault: true })
.description("create an exploit for a vulnerability")
.option(
"-d, --description <description>", "description of the vulnerability",
)
.option("-upstream, --upstream <upstream>", "exploit upstream package")
.option("-exploitBaseDir, --exploitBaseDir <exploitBaseDir>", "downstream package base directory")
.argument("<advisoryId>", "GHSA/ Snyk id of the vulnerability")
.action((advisoryId, opts) => {
const runnerCls = opts.runner === RunnerMiniSWEAgent.name ? RunnerMiniSWEAgent : RunnerSourceNotExported;
opts.advisoryId = advisoryId;
if (opts.upstream && opts.exploitBaseDir) {
new runnerCls(opts).start().catch(console.error).then(console.log);
} else {
new runnerCls(opts).start().catch(console.error).then(stats => {
console.log(stats);
if (opts.upstream && stats.success) {
new RunnerExploitUpstreamPackage({
...opts,
exploitBaseDir: stats.baseDir
}).start().catch(console.error).then(console.log);
}
});
}
});
addBaseOptions(cmd.command("pipeline"))
.option("-R, --reviewed", "filter vulnerabilities that have been reviewed")
.option("-offset, --offset <offset>", "offset to start from", intParser, 0)
.option("-timeout, --timeout <offset>", "timeout in seconds", intParser, 60 * 60)
.option("-ignore, --ignore <ignore>", "file containing advisory ids to ignore")
.option(
"-limit, --limit <limit>",
"limit the number of vulnerabilities to process",
intParser,
Infinity,
)
.argument("[advisoryIds...]", "advisory ids to process")
.description("run pipeline")
.action((advisoryIds, opts) => {
if (Array.isArray(advisoryIds)) {
opts.advisoryIds = [];
for (const advisoryId of advisoryIds) {
if (fs.existsSync(advisoryId)) {
opts.advisoryIds.push(...readFileSync(advisoryId, "utf-8").split("\n"));
} else {
opts.advisoryIds.push(advisoryId);
}
}
}
const ignoreList = opts.ignore
? readFileSync(opts.ignore, "utf-8")
.split("\n")
.map((s) => s.trim())
: [];
opts.advisoryIds = opts.advisoryIds
.map((s) => s.trim())
.filter((line) => line.length > 0 && !line.startsWith("#"))
.filter((advisoryId) => !ignoreList.includes(advisoryId))
loadEnv(opts);
(opts.advisoryIds ? new DefaultPipelineRunner(opts) : new GHSAPipelineRunner(opts))
.start()
.catch(console.error);
});
cmd.parse(process.argv);
}