Swadhin Biswas Feature/ai code reviewer (#79) · 8/16/2026
cfd400d
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { createCipheriv, createDecipheriv, randomBytes } from "crypto";
import Docker from "dockerode";
import pino from "pino";
import { Writable } from "stream";
const logger = pino({
name: "runner-executor",
transport: {
target: "pino-pretty",
options: { colorize: true },
},
});
// Default resource limits for job containers
const DEFAULT_LIMITS = {
memoryMb: parseInt(process.env.EXECUTOR_MEMORY_MB || "2048", 10),
cpuShares: parseInt(process.env.EXECUTOR_CPU_SHARES || "1024", 10),
cpuCount: parseInt(process.env.EXECUTOR_CPU_COUNT || "2", 10), // NanoCpus
timeoutSeconds: parseInt(process.env.EXECUTOR_TIMEOUT_SECONDS || "3600", 10),
maxLogBytes: parseInt(
process.env.EXECUTOR_MAX_LOG_BYTES || String(10 * 1024 * 1024),
10,
),
};
// ── Image Allowlist ───────────────────────────────────────────────────────────
// Only these image prefixes are permitted for job containers.
// Set EXECUTOR_IMAGE_ALLOWLIST env var to override (comma-separated).
const DEFAULT_ALLOWED_IMAGES = [
"node:",
"python:",
"golang:",
"rust:",
"ruby:",
"alpine:",
"ubuntu:",
"debian:",
"ghcr.io/opencodehub/",
"docker.io/library/",
];
const IMAGE_ALLOWLIST: string[] = process.env.EXECUTOR_IMAGE_ALLOWLIST
? process.env.EXECUTOR_IMAGE_ALLOWLIST.split(",").map((s) => s.trim())
: DEFAULT_ALLOWED_IMAGES;
// ── Network Isolation ─────────────────────────────────────────────────────────
// Set to "none" to fully isolate containers, or a custom Docker network name.
const NETWORK_MODE = process.env.EXECUTOR_NETWORK_MODE || "bridge";
// ── Credential Encryption ─────────────────────────────────────────────────────
const CREDENTIAL_ENCRYPTION_KEY = process.env.EXECUTOR_CREDENTIAL_KEY || "";
// Patterns for secrets that should be redacted from logs
const SECRET_PATTERNS = [
/(?:password|secret|token|api[_-]?key|auth|credential|bearer)[\s]*[=:]\s*['"]?([^'"\s]+)['"]?/gi,
/ghp_[a-zA-Z0-9]{36}/g, // GitHub personal access tokens
/sk-[a-zA-Z0-9]{48}/g, // OpenAI API keys
/sk-ant-[a-zA-Z0-9-]{93}/g, // Anthropic API keys
/xox[baprs]-[a-zA-Z0-9-]+/g, // Slack tokens
/eyJ[a-zA-Z0-9_-]*\.eyJ[a-zA-Z0-9_-]*/g, // JWT tokens
];
export interface ExecutorOptions {
memoryMb?: number;
cpuShares?: number;
cpuCount?: number;
timeoutSeconds?: number;
maxLogBytes?: number;
env?: Record<string, string>;
workDir?: string;
}
export class Executor {
private docker: Docker;
constructor() {
this.docker = new Docker();
}
async verifyDocker() {
try {
await this.docker.ping();
logger.info("Docker is available");
return true;
} catch (e) {
logger.error(
"Docker is NOT available. Please install Docker or ensure the runner has permissions.",
);
return false;
}
}
/**
* Validate that the requested image is in the allowlist
*/
private validateImage(image: string): boolean {
return IMAGE_ALLOWLIST.some((prefix) => image.startsWith(prefix));
}
/**
* Encrypt sensitive environment variables for at-rest storage
*/
static encryptCredential(plaintext: string): string {
if (!CREDENTIAL_ENCRYPTION_KEY || CREDENTIAL_ENCRYPTION_KEY.length !== 64) {
return plaintext; // No encryption configured
}
const key = Buffer.from(CREDENTIAL_ENCRYPTION_KEY, "hex");
const iv = randomBytes(16);
const cipher = createCipheriv("aes-256-cbc", key, iv);
const encrypted = Buffer.concat([
cipher.update(plaintext, "utf-8"),
cipher.final(),
]);
return `enc:${iv.toString("hex")}:${encrypted.toString("hex")}`;
}
/**
* Decrypt credentials that were encrypted with encryptCredential
*/
static decryptCredential(ciphertext: string): string {
if (!ciphertext.startsWith("enc:")) return ciphertext;
if (!CREDENTIAL_ENCRYPTION_KEY || CREDENTIAL_ENCRYPTION_KEY.length !== 64) {
logger.warn(
"Encrypted credential found but EXECUTOR_CREDENTIAL_KEY not set",
);
return ciphertext;
}
const parts = ciphertext.split(":");
if (parts.length !== 3) return ciphertext;
const key = Buffer.from(CREDENTIAL_ENCRYPTION_KEY, "hex");
const iv = Buffer.from(parts[1], "hex");
const data = Buffer.from(parts[2], "hex");
const decipher = createDecipheriv("aes-256-cbc", key, iv);
return Buffer.concat([decipher.update(data), decipher.final()]).toString(
"utf-8",
);
}
/**
* Redact secrets from log output
*/
private redactSecrets(text: string): string {
let redacted = text;
for (const pattern of SECRET_PATTERNS) {
redacted = redacted.replace(pattern, (match) => {
// Keep first 4 chars visible for debugging
const visible = match.substring(0, 4);
return `${visible}${"*".repeat(Math.max(4, match.length - 4))}[REDACTED]`;
});
}
return redacted;
}
/**
* Run a job with resource limits, timeout, and log sanitization
*/
async runJob(
jobId: string,
script: string,
image = "node:20-slim",
options: ExecutorOptions = {},
): Promise<{
success: boolean;
logs: string;
exitCode: number;
timedOut?: boolean;
}> {
// ── Image allowlist check ─────────────────────────────────────────────
if (!this.validateImage(image)) {
logger.error(
{ image, allowlist: IMAGE_ALLOWLIST },
"Image not in allowlist",
);
return {
success: false,
exitCode: 1,
logs:
`Security Error: Image "${image}" is not in the allowed images list. ` +
`Contact your administrator to add it to EXECUTOR_IMAGE_ALLOWLIST.`,
};
}
// Decrypt env vars that may be encrypted
const decryptedEnv: Record<string, string> = {};
if (options.env) {
for (const [k, v] of Object.entries(options.env)) {
decryptedEnv[k] = Executor.decryptCredential(v);
}
}
const containerName = `och-runner-${jobId}-${Date.now()}`;
const limits = {
memoryMb: options.memoryMb ?? DEFAULT_LIMITS.memoryMb,
cpuShares: options.cpuShares ?? DEFAULT_LIMITS.cpuShares,
timeoutSeconds: options.timeoutSeconds ?? DEFAULT_LIMITS.timeoutSeconds,
maxLogBytes: options.maxLogBytes ?? DEFAULT_LIMITS.maxLogBytes,
};
let logs = "";
let logBytes = 0;
let logsTruncated = false;
let container: Docker.Container | null = null;
const logStream = new Writable({
write: (chunk, _encoding, callback) => {
const chunkStr = chunk.toString();
const redactedChunk = this.redactSecrets(chunkStr);
// Check log size limit
if (logBytes + redactedChunk.length > limits.maxLogBytes) {
if (!logsTruncated) {
logs += "\n[LOG TRUNCATED: Exceeded maximum log size]\n";
logsTruncated = true;
}
} else {
logs += redactedChunk;
logBytes += redactedChunk.length;
// Stream to stdout for visibility (also redacted)
process.stdout.write(redactedChunk);
}
callback();
},
});
// Setup timeout
const timeoutPromise = new Promise<{ timedOut: true }>((resolve) => {
setTimeout(() => {
resolve({ timedOut: true });
}, limits.timeoutSeconds * 1000);
});
try {
logger.info(
{
image,
containerName,
memoryMb: limits.memoryMb,
cpuShares: limits.cpuShares,
timeoutSeconds: limits.timeoutSeconds,
},
"Starting job container with resource limits",
);
container = await this.docker.createContainer({
Image: image,
Cmd: ["/bin/sh", "-c", script],
Tty: false,
name: containerName,
Env:
Object.keys(decryptedEnv).length > 0
? Object.entries(decryptedEnv).map(([k, v]) => `${k}=${v}`)
: options.env
? Object.entries(options.env).map(([k, v]) => `${k}=${v}`)
: undefined,
WorkingDir: options.workDir,
HostConfig: {
// Memory limit in bytes
Memory: limits.memoryMb * 1024 * 1024,
// Memory + swap limit (same as Memory to disable swap)
MemorySwap: limits.memoryMb * 1024 * 1024,
// CPU shares (relative weight)
CpuShares: limits.cpuShares,
// Limit CPUs (NanoCpus is int64, unit is 1e-9 CPUs)
NanoCpus: (options.cpuCount ?? DEFAULT_LIMITS.cpuCount) * 1e9,
// Security: prevent container from gaining new privileges
SecurityOpt: ["no-new-privileges:true"],
// Network isolation: "none" blocks all network access
NetworkMode: NETWORK_MODE,
// Drop all Linux capabilities
CapDrop: ["ALL"],
// Read-only root filesystem (can be overridden if needed)
// ReadonlyRootfs: true,
// Auto-remove on stop
AutoRemove: false, // We remove manually after getting logs
// Limit PIDs to prevent fork bombs
PidsLimit: 256,
},
});
const stream = await container.attach({
stream: true,
stdout: true,
stderr: true,
});
stream.pipe(logStream);
await container.start();
// Race between completion and timeout
const waitPromise = container
.wait()
.then((result) => ({ result, timedOut: false }));
const outcome = await Promise.race([waitPromise, timeoutPromise]);
if ("timedOut" in outcome && outcome.timedOut) {
logger.warn(
{ jobId, containerName, timeoutSeconds: limits.timeoutSeconds },
"Job timed out, killing container",
);
try {
await container.kill();
} catch (killError) {
logger.error(
{ error: killError },
"Failed to kill timed-out container",
);
}
logs += `\n[JOB TIMED OUT after ${limits.timeoutSeconds} seconds]\n`;
return {
success: false,
exitCode: 124, // Standard timeout exit code
logs,
timedOut: true,
};
}
const { result } = outcome as {
result: { StatusCode: number };
timedOut: false;
};
// Small delay to ensure logs flush
await new Promise((r) => setTimeout(r, 500));
await container.remove();
return {
success: result.StatusCode === 0,
exitCode: result.StatusCode,
logs,
};
} catch (error: any) {
logger.error(error, "Job execution failed");
// Attempt to clean up container on error
if (container) {
try {
await container.stop({ t: 5 }).catch(() => {});
await container.remove().catch(() => {});
} catch {
// Ignore cleanup errors
}
}
return {
success: false,
exitCode: 1,
logs:
logs + `\nRunner System Error: ${this.redactSecrets(error.message)}`,
};
}
}
/**
* Cancel a running job by container name pattern
*/
async cancelJob(jobId: string): Promise<boolean> {
try {
const containers = await this.docker.listContainers({
filters: { name: [`och-runner-${jobId}`] },
});
for (const containerInfo of containers) {
const container = this.docker.getContainer(containerInfo.Id);
logger.info({ jobId, containerId: containerInfo.Id }, "Cancelling job");
await container.stop({ t: 5 });
await container.remove();
}
return containers.length > 0;
} catch (error) {
logger.error({ error, jobId }, "Failed to cancel job");
return false;
}
}
}