auth-yes/forum/poc-g2/cfg_poc.ts
Tyler Gillispie e5855248e6
feat: Port all agent-forum-v4 Gen 1 PoCs to Gen 2 using real tooling (#68)
This commit ports the remaining Gen 1 proof-of-concept experiments from `forum/poc-g1` into `forum/poc-g2` while substituting naive mocks with real, production-ready mechanisms.

Key advancements include:
- `code_intelligence_poc.ts` and `cfg_poc.ts`: Swapped regex matching for actual Javascript AST traversal using `acorn`.
- `static_analysis_poc.ts`: Replaced mock payloads with real `deno lint --json` output executed via `Deno.Command`.
- `vector_db_poc.ts` and `multi_vec_poc.ts`: Replaced basic JS arrays with actual `jsr:@db/sqlite` instances utilizing User-Defined Functions (UDFs) to perform native vector cosine similarity queries in memory or on disk.
- `protobuf_poc.ts`: Implemented robust protobuf serialization/deserialization via `protobufjs`.
- Semantic/Governance PoCs (`constitution_poc.ts`, `ontology_poc.ts`, `state_machine_poc.ts`, `orphan_branch_poc.ts`, etc): Replaced string-mock I/O with absolute filesystem reads, real YAML parsing using `jsr:@std/yaml`, and isolated `Deno.Command` Git sandboxes.
- Updated `forum/poc-g2/lab.ts` to orchestrate and execute all 19 experiments, proving 100% test pass rate with Gen 2 tooling.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: mrteye <1945243+mrteye@users.noreply.github.com>
2026-08-28 22:11:53 -07:00

105 lines
3.1 KiB
TypeScript

import * as acorn from "npm:acorn";
import { assert, assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
/**
* Proof of Concept: Abstract Syntax Trees & Control Flow Graphs (Gen 2)
*
* Demonstrates the Adversary agent consuming a CFG. Instead of a hardcoded JSON,
* we dynamically generate a basic flow graph by traversing an actual AST of some
* target code, and then trace if unsanitized user input reaches a sensitive sink.
*/
// Simulated malicious or vulnerable code segment
const targetSource = `
function handleRequest(req) {
let userInput = req.query.id; // entry
// safe path
let safeInput = sanitize(userInput);
db_query(safeInput); // sink
// vulnerable path
let rawHeader = req.headers['user-agent']; // entry
db_query(rawHeader); // sink
}
`;
function generateAndAnalyzeCFG(code: string): string[] {
const ast = acorn.parse(code, { ecmaVersion: 2022 }) as any;
const vulnerabilities: string[] = [];
// A very rudimentary data-flow tracker for local variables
const variableTaints: Record<string, boolean> = {};
// Walk AST to find variable declarations and function calls
function walk(node: any) {
if (!node) return;
if (node.type === "VariableDeclarator") {
const varName = node.id.name;
// Check if it's assigned from req (our entry point)
let isTainted = false;
if (node.init && node.init.type === "MemberExpression") {
// Simplistic check for req.something
let current = node.init;
while (current.object) current = current.object;
if (current.name === "req") isTainted = true;
}
// Check if it's assigned from a sanitize call
if (node.init && node.init.type === "CallExpression") {
if (node.init.callee.name === "sanitize") {
isTainted = false; // It's clean
}
}
variableTaints[varName] = isTainted;
}
if (node.type === "CallExpression") {
if (node.callee.name === "db_query") {
const arg = node.arguments[0];
if (arg && arg.type === "Identifier") {
if (variableTaints[arg.name]) {
vulnerabilities.push(`Vulnerability: Unsanitized input '${arg.name}' reached sink 'db_query'`);
}
}
}
}
// Recurse over common blocks
for (const key in node) {
if (node[key] && typeof node[key] === "object") {
walk(node[key]);
}
}
}
walk(ast);
return vulnerabilities;
}
if (import.meta.main) {
console.log("Running CFG Security Proving PoC (Gen 2) tests...");
try {
const vulns = generateAndAnalyzeCFG(targetSource);
console.log("Adversary Agent Dynamic CFG Analysis Results:");
vulns.forEach((v) => console.log(` - ${v}`));
assertEquals(vulns.length, 1);
assert(
vulns[0].includes("rawHeader"),
"Expected rawHeader to flag a vulnerability"
);
console.log(
"✅ CFG Security Proving PoC (Gen 2) successful: Real AST traversal traced taint to a sink.",
);
} catch (err) {
console.error("❌ CFG Security Proving PoC (Gen 2) failed:", err);
Deno.exit(1);
}
}