|
1 | 1 | import { existsSync, renameSync, symlinkSync } from "node:fs"; |
2 | 2 | import { |
3 | 3 | chmod, |
| 4 | + copyFile, |
4 | 5 | lstat, |
5 | 6 | mkdir, |
6 | 7 | mkdtemp, |
@@ -169,6 +170,151 @@ describe("plugin runtime preparation", () => { |
169 | 170 | ).toBeDefined(); |
170 | 171 | }); |
171 | 172 |
|
| 173 | + testPosix( |
| 174 | + "preserves literal POSIX candidate paths in the bundled plugin", |
| 175 | + async () => { |
| 176 | + const root = await temporaryDirectory(); |
| 177 | + await mkdir(join(root, "source")); |
| 178 | + const cases = [ |
| 179 | + { path: "source\\candidate.py", contents: "literal candidate\n" }, |
| 180 | + { path: " leading.py", contents: "leading whitespace\n" }, |
| 181 | + { path: "trailing.py ", contents: "trailing whitespace\n" }, |
| 182 | + { path: " ", contents: "single whitespace filename\n" }, |
| 183 | + { path: " ", contents: "multiple whitespace filename\n" }, |
| 184 | + { path: "C:candidate.py", contents: "literal colon\n" }, |
| 185 | + { path: "carriage\rreturn.py", contents: "literal carriage return\n" }, |
| 186 | + { path: "vertical\vtab.py", contents: "literal vertical tab\n" }, |
| 187 | + { path: "form\ffeed.py", contents: "literal form feed\n" }, |
| 188 | + { path: "next\u0085line.py", contents: "literal next line\n" }, |
| 189 | + { |
| 190 | + path: "unicode\u2028separator.py", |
| 191 | + contents: "literal line separator\n", |
| 192 | + }, |
| 193 | + { |
| 194 | + path: "paragraph\u2029separator.py", |
| 195 | + contents: "literal paragraph separator\n", |
| 196 | + }, |
| 197 | + ]; |
| 198 | + await Promise.all([ |
| 199 | + ...cases.map((item) => writeFile(join(root, item.path), item.contents)), |
| 200 | + writeFile(join(root, "source", "candidate.py"), "wrong candidate\n"), |
| 201 | + writeFile(join(root, "leading.py"), "wrong leading candidate\n"), |
| 202 | + writeFile(join(root, "trailing.py"), "wrong trailing candidate\n"), |
| 203 | + ]); |
| 204 | + const scopePath = join(root, "in-scope-files.txt"); |
| 205 | + await writeFile( |
| 206 | + scopePath, |
| 207 | + `${cases.map((item) => item.path).join("\n")}\n`, |
| 208 | + ); |
| 209 | + |
| 210 | + const python = Bun.which("python3") ?? Bun.which("python"); |
| 211 | + expect(python).not.toBeNull(); |
| 212 | + const sourcePlugin = await bundledPluginRoot(); |
| 213 | + const projector = new URL( |
| 214 | + "../scripts/project-plugin.mjs", |
| 215 | + import.meta.url, |
| 216 | + ); |
| 217 | + const publicManifest = new URL( |
| 218 | + "../public-repo/sdk/typescript/plugin.public.json", |
| 219 | + import.meta.url, |
| 220 | + ); |
| 221 | + let bundledPlugin = sourcePlugin; |
| 222 | + if (existsSync(projector) && existsSync(publicManifest)) { |
| 223 | + const packageRoot = join(root, "package"); |
| 224 | + const isolatedProjector = join( |
| 225 | + packageRoot, |
| 226 | + "scripts", |
| 227 | + "project-plugin.mjs", |
| 228 | + ); |
| 229 | + const isolatedManifest = join( |
| 230 | + packageRoot, |
| 231 | + "public-repo", |
| 232 | + "sdk", |
| 233 | + "typescript", |
| 234 | + "plugin.public.json", |
| 235 | + ); |
| 236 | + await Promise.all([ |
| 237 | + mkdir(dirname(isolatedProjector), { recursive: true }), |
| 238 | + mkdir(dirname(isolatedManifest), { recursive: true }), |
| 239 | + ]); |
| 240 | + await Promise.all([ |
| 241 | + copyFile(projector, isolatedProjector), |
| 242 | + copyFile(publicManifest, isolatedManifest), |
| 243 | + ]); |
| 244 | + const projection = Bun.spawnSync( |
| 245 | + [process.execPath, isolatedProjector], |
| 246 | + { |
| 247 | + cwd: packageRoot, |
| 248 | + env: { |
| 249 | + ...process.env, |
| 250 | + CODEX_SECURITY_PLUGIN_ROOT: sourcePlugin, |
| 251 | + }, |
| 252 | + stdout: "pipe", |
| 253 | + stderr: "pipe", |
| 254 | + }, |
| 255 | + ); |
| 256 | + expect(new TextDecoder().decode(projection.stderr)).toBe(""); |
| 257 | + expect(projection.exitCode).toBe(0); |
| 258 | + bundledPlugin = join(packageRoot, "_bundled_plugin"); |
| 259 | + } |
| 260 | + const normalizer = join( |
| 261 | + bundledPlugin, |
| 262 | + "scripts", |
| 263 | + "normalize_candidates.py", |
| 264 | + ); |
| 265 | + expect(await readFile(normalizer, "utf8")).toBe( |
| 266 | + await readFile( |
| 267 | + join(sourcePlugin, "scripts", "normalize_candidates.py"), |
| 268 | + "utf8", |
| 269 | + ), |
| 270 | + ); |
| 271 | + const result = Bun.spawnSync([ |
| 272 | + python!, |
| 273 | + "-I", |
| 274 | + "-B", |
| 275 | + "-c", |
| 276 | + [ |
| 277 | + "import json, pathlib, runpy, sys", |
| 278 | + "module = runpy.run_path(sys.argv[1])", |
| 279 | + "root = pathlib.Path(sys.argv[2])", |
| 280 | + "scope = module['read_scope'](pathlib.Path(sys.argv[3]), root)", |
| 281 | + "finalizer = runpy.run_path(sys.argv[5])", |
| 282 | + "results = []", |
| 283 | + "for value in json.loads(sys.argv[4]):", |
| 284 | + " path, source = module['relative_file'](value, root)", |
| 285 | + " candidate = {'cwe_ids': ['CWE-89'], 'locations': [{'path': value, 'start_line': 1, 'role': 'entrypoint'}], 'summary': 'Test finding', 'evidence': 'Test evidence'}", |
| 286 | + " try:", |
| 287 | + " normalized = module['normalize_candidate'](candidate, root, scope, {})", |
| 288 | + " location = normalized['locations'][0]", |
| 289 | + " finalizer['_validate_location']({'path': location['path'], 'startLine': location['start_line'], 'endLine': location['end_line'], 'role': location['role']}, 'candidate.locations[0]')", |
| 290 | + " except ValueError:", |
| 291 | + " contract_valid = False", |
| 292 | + " else:", |
| 293 | + " contract_valid = True", |
| 294 | + " results.append({'path': path, 'contents': source.read_text(encoding='utf-8'), 'inScope': path in scope, 'contractValid': contract_valid})", |
| 295 | + "print(json.dumps(results))", |
| 296 | + ].join("\n"), |
| 297 | + normalizer, |
| 298 | + root, |
| 299 | + scopePath, |
| 300 | + JSON.stringify(cases.map((item) => item.path)), |
| 301 | + join(bundledPlugin, "scripts", "finalize_scan_contract.py"), |
| 302 | + ]); |
| 303 | + |
| 304 | + expect(result.exitCode).toBe(0); |
| 305 | + expect(JSON.parse(new TextDecoder().decode(result.stdout))).toEqual( |
| 306 | + cases.map((item) => ({ |
| 307 | + ...item, |
| 308 | + inScope: true, |
| 309 | + contractValid: |
| 310 | + item.path.trim().length > 0 && |
| 311 | + !item.path.includes("\\") && |
| 312 | + !item.path.includes(":"), |
| 313 | + })), |
| 314 | + ); |
| 315 | + }, |
| 316 | + ); |
| 317 | + |
172 | 318 | test("uses a configured plugin directory directly", async () => { |
173 | 319 | const root = await temporaryDirectory(); |
174 | 320 | const ambientHome = join(root, ".codex", "plugins", "cache"); |
|
0 commit comments