|
| 1 | +/* |
| 2 | + * Copyright 2026 Code Intelligence GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import type { Context } from "vm"; |
| 18 | + |
| 19 | +import { |
| 20 | + getJazzerJsGlobal, |
| 21 | + guideTowardsContainment, |
| 22 | + registerAfterEachCallback, |
| 23 | + reportAndThrowFinding, |
| 24 | + reportFinding, |
| 25 | +} from "@jazzer.js/core"; |
| 26 | +import { registerBeforeHook } from "@jazzer.js/hooking"; |
| 27 | + |
| 28 | +import { bugDetectorConfigurations } from "../configuration"; |
| 29 | +import { ensureCanary } from "../shared/code-injection-canary"; |
| 30 | +import { |
| 31 | + buildGenericSuppressionSnippet, |
| 32 | + captureStack, |
| 33 | + getUserFacingStackLines, |
| 34 | + IgnoreList, |
| 35 | + type IgnoreRule, |
| 36 | +} from "../shared/finding-suppression"; |
| 37 | + |
| 38 | +export type { IgnoreRule } from "../shared/finding-suppression"; |
| 39 | + |
| 40 | +type PendingAccess = { |
| 41 | + canaryName: string; |
| 42 | + stack: string; |
| 43 | + invoked: boolean; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Configuration for the Code Injection bug detector. |
| 48 | + * Controls the reporting and suppression of dynamic code evaluation findings. |
| 49 | + */ |
| 50 | +export interface CodeInjectionConfig { |
| 51 | + /** |
| 52 | + * Disables Stage 1 (Access) reporting entirely. |
| 53 | + * The detector will no longer report when the canary is merely read. |
| 54 | + */ |
| 55 | + disableAccessReporting(): this; |
| 56 | + /** |
| 57 | + * Disables Stage 2 (Invocation) reporting entirely. |
| 58 | + * The detector will no longer report when the canary is actually executed. |
| 59 | + */ |
| 60 | + disableInvocationReporting(): this; |
| 61 | + /** |
| 62 | + * Suppresses Stage 1 (Access) findings that match the provided rule. |
| 63 | + * Use this to silence safe heuristic reads such as template lookups. |
| 64 | + */ |
| 65 | + ignoreAccess(rule: IgnoreRule): this; |
| 66 | + /** |
| 67 | + * Suppresses Stage 2 (Invocation) findings that match the provided rule. |
| 68 | + * Use this only for known-safe execution sinks in test environments. |
| 69 | + */ |
| 70 | + ignoreInvocation(rule: IgnoreRule): this; |
| 71 | +} |
| 72 | + |
| 73 | +class CodeInjectionConfigImpl implements CodeInjectionConfig { |
| 74 | + private _reportAccess = true; |
| 75 | + private _reportInvocation = true; |
| 76 | + private readonly _ignoredAccessRules = new IgnoreList(); |
| 77 | + private readonly _ignoredInvocationRules = new IgnoreList(); |
| 78 | + |
| 79 | + disableAccessReporting(): this { |
| 80 | + this._reportAccess = false; |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + disableInvocationReporting(): this { |
| 85 | + this._reportInvocation = false; |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + ignoreAccess(rule: IgnoreRule): this { |
| 90 | + this._ignoredAccessRules.add(rule); |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + ignoreInvocation(rule: IgnoreRule): this { |
| 95 | + this._ignoredInvocationRules.add(rule); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + shouldReportAccess(stack: string): boolean { |
| 100 | + return this._reportAccess && !this._ignoredAccessRules.matches(stack); |
| 101 | + } |
| 102 | + |
| 103 | + shouldReportInvocation(stack: string): boolean { |
| 104 | + return ( |
| 105 | + this._reportInvocation && !this._ignoredInvocationRules.matches(stack) |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +const config = new CodeInjectionConfigImpl(); |
| 111 | +bugDetectorConfigurations.set("code-injection", config); |
| 112 | + |
| 113 | +const canaryCache = new WeakMap<object, string>(); |
| 114 | +const pendingAccesses: PendingAccess[] = []; |
| 115 | + |
| 116 | +ensureActiveCanary(); |
| 117 | +registerAfterEachCallback(flushPendingAccesses); |
| 118 | + |
| 119 | +registerBeforeHook( |
| 120 | + "eval", |
| 121 | + "", |
| 122 | + false, |
| 123 | + function beforeEvalHook( |
| 124 | + _thisPtr: unknown, |
| 125 | + params: unknown[], |
| 126 | + hookId: number, |
| 127 | + ) { |
| 128 | + const canaryName = ensureActiveCanary(); |
| 129 | + |
| 130 | + const code = params[0]; |
| 131 | + if (typeof code === "string") { |
| 132 | + guideTowardsContainment(code, canaryName, hookId); |
| 133 | + } |
| 134 | + }, |
| 135 | +); |
| 136 | + |
| 137 | +registerBeforeHook( |
| 138 | + "Function", |
| 139 | + "", |
| 140 | + false, |
| 141 | + function beforeFunctionHook( |
| 142 | + _thisPtr: unknown, |
| 143 | + params: unknown[], |
| 144 | + hookId: number, |
| 145 | + ) { |
| 146 | + const canaryName = ensureActiveCanary(); |
| 147 | + if (params.length === 0) return; |
| 148 | + |
| 149 | + const functionBody = params[params.length - 1]; |
| 150 | + if (functionBody == null) return; |
| 151 | + |
| 152 | + let functionBodySource: string; |
| 153 | + try { |
| 154 | + functionBodySource = String(functionBody); |
| 155 | + } catch { |
| 156 | + return; |
| 157 | + } |
| 158 | + // The hook has already performed Function's body coercion; reuse it so |
| 159 | + // user-provided toString methods are not invoked a second time. |
| 160 | + params[params.length - 1] = functionBodySource; |
| 161 | + |
| 162 | + guideTowardsContainment(functionBodySource, canaryName, hookId); |
| 163 | + }, |
| 164 | +); |
| 165 | + |
| 166 | +function getVmContext(): Context | undefined { |
| 167 | + return getJazzerJsGlobal<Context>("vmContext"); |
| 168 | +} |
| 169 | + |
| 170 | +function ensureActiveCanary(): string { |
| 171 | + return ensureCanary( |
| 172 | + [ |
| 173 | + { label: "vmContext", object: getVmContext() }, |
| 174 | + { label: "globalThis", object: globalThis }, |
| 175 | + ], |
| 176 | + canaryCache, |
| 177 | + createCanaryDescriptor, |
| 178 | + ); |
| 179 | +} |
| 180 | + |
| 181 | +function createCanaryDescriptor(canaryName: string): PropertyDescriptor { |
| 182 | + return { |
| 183 | + get() { |
| 184 | + const accessStack = captureStack(); |
| 185 | + const pendingAccess = config.shouldReportAccess(accessStack) |
| 186 | + ? { |
| 187 | + canaryName, |
| 188 | + stack: accessStack, |
| 189 | + invoked: false, |
| 190 | + } |
| 191 | + : undefined; |
| 192 | + if (pendingAccess) { |
| 193 | + pendingAccesses.push(pendingAccess); |
| 194 | + } |
| 195 | + |
| 196 | + return function canaryCall() { |
| 197 | + const invocationStack = captureStack(); |
| 198 | + if (config.shouldReportInvocation(invocationStack)) { |
| 199 | + if (pendingAccess) { |
| 200 | + pendingAccess.invoked = true; |
| 201 | + } |
| 202 | + reportAndThrowFinding( |
| 203 | + buildFindingMessage( |
| 204 | + "Confirmed Code Injection (Canary Invoked)", |
| 205 | + `invoked canary: ${canaryName}`, |
| 206 | + invocationStack, |
| 207 | + "ignoreInvocation", |
| 208 | + "If this execution sink is expected in your test environment, suppress it:", |
| 209 | + ), |
| 210 | + false, |
| 211 | + ); |
| 212 | + } |
| 213 | + }; |
| 214 | + }, |
| 215 | + enumerable: false, |
| 216 | + configurable: false, |
| 217 | + }; |
| 218 | +} |
| 219 | + |
| 220 | +function flushPendingAccesses(): void { |
| 221 | + for (const pendingAccess of pendingAccesses.splice(0)) { |
| 222 | + if (pendingAccess.invoked) { |
| 223 | + continue; |
| 224 | + } |
| 225 | + reportFinding( |
| 226 | + buildFindingMessage( |
| 227 | + "Potential Code Injection (Canary Accessed)", |
| 228 | + `accessed canary: ${pendingAccess.canaryName}`, |
| 229 | + pendingAccess.stack, |
| 230 | + "ignoreAccess", |
| 231 | + "If this is a safe heuristic read, suppress it to continue fuzzing for code execution. Add this to your custom hooks:", |
| 232 | + ), |
| 233 | + false, |
| 234 | + ); |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +function buildFindingMessage( |
| 239 | + title: string, |
| 240 | + action: string, |
| 241 | + stack: string, |
| 242 | + suppressionMethod: "ignoreAccess" | "ignoreInvocation", |
| 243 | + hint: string, |
| 244 | +): string { |
| 245 | + const relevantStackLines = getUserFacingStackLines(stack); |
| 246 | + const message = [`${title} -- ${action}`]; |
| 247 | + if (relevantStackLines.length > 0) { |
| 248 | + message.push(...relevantStackLines); |
| 249 | + } |
| 250 | + message.push( |
| 251 | + "", |
| 252 | + `[!] ${hint}`, |
| 253 | + " Example only: copy/paste it and adapt `stackPattern` to your needs.", |
| 254 | + "", |
| 255 | + buildGenericSuppressionSnippet("code-injection", suppressionMethod), |
| 256 | + ); |
| 257 | + return message.join("\n"); |
| 258 | +} |
0 commit comments