69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
|
|
|
/**
|
|
* Proof of Concept: Static Analysis Payloads (Gen 2)
|
|
*
|
|
* Simulates a real CI/CD integration where we spawn Deno.Command to run a local
|
|
* static analysis tool (like deno lint), capture its JSON output, and feed it
|
|
* as structured data for the Adversary agent.
|
|
*/
|
|
|
|
async function runStaticAnalysis(code: string): Promise<any> {
|
|
// Write the temp code to a file
|
|
const tempFile = await Deno.makeTempFile({ suffix: ".ts" });
|
|
await Deno.writeTextFile(tempFile, code);
|
|
|
|
// We use deno lint as our real "static analysis tool" for this PoC
|
|
const command = new Deno.Command("deno", {
|
|
args: ["lint", "--json", tempFile],
|
|
stdout: "piped",
|
|
stderr: "piped",
|
|
});
|
|
|
|
const { stdout } = await command.output();
|
|
const outputStr = new TextDecoder().decode(stdout);
|
|
|
|
// Cleanup
|
|
await Deno.remove(tempFile);
|
|
|
|
try {
|
|
return JSON.parse(outputStr);
|
|
} catch (_e) {
|
|
return { diagnostics: [] }; // Empty if no output or parse error
|
|
}
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
console.log("Running Static Analysis Payloads PoC (Gen 2) tests...");
|
|
|
|
// We write some intentionally bad code that triggers deno lint
|
|
const badCode = `
|
|
const unusedVar = 42;
|
|
function anyFunc(a: any) {
|
|
return a == null;
|
|
}
|
|
`;
|
|
|
|
try {
|
|
const analysisReport = await runStaticAnalysis(badCode) as any;
|
|
|
|
console.log("Agent received real structured static analysis report:");
|
|
console.log(`Found ${analysisReport.diagnostics.length} lint issues.`);
|
|
|
|
// We expect deno lint to catch 'no-unused-vars'
|
|
assertEquals(analysisReport.diagnostics.length > 0, true);
|
|
|
|
const hasUnusedVar = analysisReport.diagnostics.some((e: any) =>
|
|
e.code === "no-unused-vars"
|
|
);
|
|
assertEquals(hasUnusedVar, true);
|
|
|
|
console.log(
|
|
"✅ Static Analysis Payloads PoC (Gen 2) successful: Spawned real tool (deno lint) and parsed JSON payload.",
|
|
);
|
|
} catch (err) {
|
|
console.error("❌ Static Analysis Payloads PoC (Gen 2) failed:", err);
|
|
Deno.exit(1);
|
|
}
|
|
}
|