Compare commits
No commits in common. "679f6c0f4747547a298f8ee35afa8bc58b90451b" and "ee01227992d561de66178ca3d642fe2790738b14" have entirely different histories.
679f6c0f47
...
ee01227992
@ -1,13 +1,5 @@
|
|||||||
import {
|
import * as acorn from "npm:acorn";
|
||||||
assert,
|
import { assert, assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
||||||
assertEquals,
|
|
||||||
} from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
|
||||||
import {
|
|
||||||
dirname,
|
|
||||||
fromFileUrl,
|
|
||||||
join,
|
|
||||||
} from "https://deno.land/std@0.224.0/path/mod.ts";
|
|
||||||
import { execTool, requireTool } from "./sys_exec.ts";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proof of Concept: Abstract Syntax Trees & Control Flow Graphs (Gen 2)
|
* Proof of Concept: Abstract Syntax Trees & Control Flow Graphs (Gen 2)
|
||||||
@ -15,8 +7,6 @@ import { execTool, requireTool } from "./sys_exec.ts";
|
|||||||
* Demonstrates the Adversary agent consuming a CFG. Instead of a hardcoded JSON,
|
* 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
|
* 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.
|
* target code, and then trace if unsanitized user input reaches a sensitive sink.
|
||||||
*
|
|
||||||
* This version uses the native `tree-sitter` CLI to produce an AST representation.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Simulated malicious or vulnerable code segment
|
// Simulated malicious or vulnerable code segment
|
||||||
@ -34,95 +24,66 @@ function handleRequest(req) {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
async function generateAndAnalyzeCFG(code: string): Promise<string[]> {
|
function generateAndAnalyzeCFG(code: string): string[] {
|
||||||
const currentDir = dirname(fromFileUrl(import.meta.url));
|
const ast = acorn.parse(code, { ecmaVersion: 2022 }) as any;
|
||||||
const TEMP_FILE = join(currentDir, "dummy_cfg_target.js");
|
|
||||||
const vulnerabilities: string[] = [];
|
const vulnerabilities: string[] = [];
|
||||||
|
|
||||||
try {
|
// A very rudimentary data-flow tracker for local variables
|
||||||
await Deno.writeTextFile(TEMP_FILE, code);
|
|
||||||
|
|
||||||
// Call native tree-sitter parser to get XML AST
|
|
||||||
const { code: exitCode, stdout, stderr } = await execTool("tree-sitter", [
|
|
||||||
"parse",
|
|
||||||
TEMP_FILE,
|
|
||||||
"-x",
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (exitCode !== 0) {
|
|
||||||
throw new Error(`Tree-sitter CLI execution failed: ${stderr || stdout}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// A very rudimentary data-flow tracker for local variables based on the tree-sitter XML output
|
|
||||||
const variableTaints: Record<string, boolean> = {};
|
const variableTaints: Record<string, boolean> = {};
|
||||||
|
|
||||||
// 1. Find variable assignments (variable_declarator)
|
// Walk AST to find variable declarations and function calls
|
||||||
const varMatches = stdout.matchAll(
|
function walk(node: any) {
|
||||||
/<variable_declarator.*?<identifier field="name".*?>(.*?)<\/identifier>.*?field="value".*?>(.*?)<\/variable_declarator>/gs,
|
if (!node) return;
|
||||||
);
|
|
||||||
for (const match of varMatches) {
|
|
||||||
const varName = match[1];
|
|
||||||
const valueBlock = match[2];
|
|
||||||
|
|
||||||
|
if (node.type === "VariableDeclarator") {
|
||||||
|
const varName = node.id.name;
|
||||||
|
// Check if it's assigned from req (our entry point)
|
||||||
let isTainted = false;
|
let isTainted = false;
|
||||||
|
if (node.init && node.init.type === "MemberExpression") {
|
||||||
// Simplistic check: is 'req' anywhere inside the value block?
|
// Simplistic check for req.something
|
||||||
if (valueBlock.includes(">req<")) {
|
let current = node.init;
|
||||||
isTainted = true;
|
while (current.object) current = current.object;
|
||||||
|
if (current.name === "req") isTainted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's assigned from a sanitize call
|
// Check if it's assigned from a sanitize call
|
||||||
if (
|
if (node.init && node.init.type === "CallExpression") {
|
||||||
valueBlock.includes("call_expression") &&
|
if (node.init.callee.name === "sanitize") {
|
||||||
valueBlock.includes(">sanitize<")
|
|
||||||
) {
|
|
||||||
isTainted = false; // It's clean
|
isTainted = false; // It's clean
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
variableTaints[varName] = isTainted;
|
variableTaints[varName] = isTainted;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Find function calls (call_expression)
|
if (node.type === "CallExpression") {
|
||||||
const callMatches = stdout.matchAll(
|
if (node.callee.name === "db_query") {
|
||||||
/<call_expression.*?<identifier field="function".*?>(.*?)<\/identifier>.*?<arguments.*?<identifier.*?>(.*?)<\/identifier>.*?<\/arguments>.*?<\/call_expression>/gs,
|
const arg = node.arguments[0];
|
||||||
);
|
if (arg && arg.type === "Identifier") {
|
||||||
for (const match of callMatches) {
|
if (variableTaints[arg.name]) {
|
||||||
const funcName = match[1];
|
vulnerabilities.push(`Vulnerability: Unsanitized input '${arg.name}' reached sink 'db_query'`);
|
||||||
const argName = match[2];
|
|
||||||
|
|
||||||
if (funcName === "db_query") {
|
|
||||||
if (variableTaints[argName]) {
|
|
||||||
vulnerabilities.push(
|
|
||||||
`Vulnerability: Unsanitized input '${argName}' reached sink 'db_query'`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
await Deno.remove(TEMP_FILE);
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Recurse over common blocks
|
||||||
|
for (const key in node) {
|
||||||
|
if (node[key] && typeof node[key] === "object") {
|
||||||
|
walk(node[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
walk(ast);
|
||||||
return vulnerabilities;
|
return vulnerabilities;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function run() {
|
if (import.meta.main) {
|
||||||
const hasTreeSitter = await requireTool(
|
console.log("Running CFG Security Proving PoC (Gen 2) tests...");
|
||||||
"tree-sitter",
|
|
||||||
"npm install -g tree-sitter-cli",
|
|
||||||
);
|
|
||||||
if (!hasTreeSitter) {
|
|
||||||
console.warn(
|
|
||||||
"⚠️ CFG Security Proving PoC skipped due to missing host dependency.",
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const vulns = await generateAndAnalyzeCFG(targetSource);
|
const vulns = generateAndAnalyzeCFG(targetSource);
|
||||||
|
|
||||||
console.log("Adversary Agent Dynamic CFG Analysis Results:");
|
console.log("Adversary Agent Dynamic CFG Analysis Results:");
|
||||||
vulns.forEach((v) => console.log(` - ${v}`));
|
vulns.forEach((v) => console.log(` - ${v}`));
|
||||||
@ -130,7 +91,7 @@ async function run() {
|
|||||||
assertEquals(vulns.length, 1);
|
assertEquals(vulns.length, 1);
|
||||||
assert(
|
assert(
|
||||||
vulns[0].includes("rawHeader"),
|
vulns[0].includes("rawHeader"),
|
||||||
"Expected rawHeader to flag a vulnerability",
|
"Expected rawHeader to flag a vulnerability"
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
@ -141,8 +102,3 @@ async function run() {
|
|||||||
Deno.exit(1);
|
Deno.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (import.meta.main) {
|
|
||||||
console.log("Running CFG Security Proving PoC (Gen 2) tests...");
|
|
||||||
run();
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,17 +1,12 @@
|
|||||||
import {
|
import * as acorn from "npm:acorn";
|
||||||
dirname,
|
|
||||||
fromFileUrl,
|
|
||||||
join,
|
|
||||||
} from "https://deno.land/std@0.224.0/path/mod.ts";
|
|
||||||
import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
||||||
import { execTool, requireTool } from "./sys_exec.ts";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proof of Concept: Local Code Intelligence (Gen 2)
|
* Proof of Concept: Local Code Intelligence (Gen 2)
|
||||||
*
|
*
|
||||||
* Replaces the naive regex extraction in Gen 1 with actual AST parsing using
|
* Replaces the naive regex extraction in Gen 1 with actual AST parsing using
|
||||||
* the tree-sitter CLI binary natively via system execution, proving that we can
|
* acorn, proving that we can extract a true Semantic
|
||||||
* extract a true Semantic Code graph structure from code files.
|
* Code graph structure from code files.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export interface ExportSymbol {
|
export interface ExportSymbol {
|
||||||
@ -20,78 +15,50 @@ export interface ExportSymbol {
|
|||||||
signature: string;
|
signature: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function extractExports(
|
export function extractExports(sourceCode: string): ExportSymbol[] {
|
||||||
sourceCode: string,
|
// Strip TypeScript annotations using a regex just to let acorn parse it as JS
|
||||||
): Promise<ExportSymbol[]> {
|
// In a real scenario we'd use a TS-capable parser like @typescript-eslint/typescript-estree or swc,
|
||||||
const currentDir = dirname(fromFileUrl(import.meta.url));
|
// but this proves the concept of AST walking vs regex scraping.
|
||||||
const TEMP_FILE = join(currentDir, "dummy_intelligence_target.js");
|
const jsCode = sourceCode
|
||||||
|
.replace(/:\s*Promise<[^>]+>/g, '')
|
||||||
|
.replace(/:\s*[a-zA-Z0-9_]+/g, '')
|
||||||
|
.replace(/<[^>]+>/g, '');
|
||||||
|
|
||||||
|
const ast = acorn.parse(jsCode, { ecmaVersion: 2022, sourceType: "module" }) as any;
|
||||||
const exports: ExportSymbol[] = [];
|
const exports: ExportSymbol[] = [];
|
||||||
|
|
||||||
try {
|
for (const node of ast.body) {
|
||||||
// Strip TypeScript annotations using a regex just to let the basic js tree-sitter parse it
|
if (node.type === "ExportNamedDeclaration") {
|
||||||
const jsCode = sourceCode
|
if (node.declaration) {
|
||||||
.replace(/:\s*Promise<[^>]+>/g, "")
|
if (node.declaration.type === "FunctionDeclaration") {
|
||||||
.replace(/:\s*[a-zA-Z0-9_]+/g, "")
|
const name = node.declaration.id.name;
|
||||||
.replace(/<[^>]+>/g, "");
|
// Simple mock signature from JS AST
|
||||||
|
const params = node.declaration.params.map((p: any) => p.name).join(", ");
|
||||||
await Deno.writeTextFile(TEMP_FILE, jsCode);
|
|
||||||
|
|
||||||
// Call native tree-sitter parser
|
|
||||||
const { code, stdout, stderr } = await execTool("tree-sitter", [
|
|
||||||
"parse",
|
|
||||||
TEMP_FILE,
|
|
||||||
"-x",
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (code !== 0) {
|
|
||||||
throw new Error(`Tree-sitter CLI execution failed: ${stderr || stdout}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// In a full implementation, we'd use a real XML or s-expression parser
|
|
||||||
// For this PoC, we will do basic extraction from the XML output format
|
|
||||||
// of tree-sitter to demonstrate the tree traversal concept.
|
|
||||||
|
|
||||||
// Look for exported functions
|
|
||||||
const funcMatches = stdout.matchAll(
|
|
||||||
/<export_statement.*?<function_declaration.*?<identifier field="name".*?>(.*?)<\/identifier>.*?<formal_parameters field="parameters".*?>(.*?)<\/formal_parameters>.*?<\/function_declaration>.*?<\/export_statement>/gs,
|
|
||||||
);
|
|
||||||
for (const match of funcMatches) {
|
|
||||||
const name = match[1];
|
|
||||||
const paramsXml = match[2];
|
|
||||||
const params = [
|
|
||||||
...paramsXml.matchAll(/<identifier.*?>(.*?)<\/identifier>/gs),
|
|
||||||
].map((m) => m[1]).join(", ");
|
|
||||||
|
|
||||||
exports.push({
|
exports.push({
|
||||||
name,
|
name,
|
||||||
type: "function",
|
type: "function",
|
||||||
signature: `(${params}) => any`,
|
signature: `(${params}) => any`,
|
||||||
});
|
});
|
||||||
}
|
} else if (node.declaration.type === "VariableDeclaration") {
|
||||||
|
for (const decl of node.declaration.declarations) {
|
||||||
// Look for exported consts
|
|
||||||
const constMatches = stdout.matchAll(
|
|
||||||
/<export_statement.*?<lexical_declaration.*?<variable_declarator.*?<identifier field="name".*?>(.*?)<\/identifier>.*?<\/variable_declarator>.*?<\/lexical_declaration>.*?<\/export_statement>/gs,
|
|
||||||
);
|
|
||||||
for (const match of constMatches) {
|
|
||||||
exports.push({
|
exports.push({
|
||||||
name: match[1],
|
name: decl.id.name,
|
||||||
type: "const",
|
type: "const",
|
||||||
signature: "const",
|
signature: "const",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} finally {
|
}
|
||||||
try {
|
}
|
||||||
await Deno.remove(TEMP_FILE);
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return exports;
|
return exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
const mockSourceCode = `
|
if (import.meta.main) {
|
||||||
|
console.log("Running Local Code Intelligence PoC (Gen 2) tests...");
|
||||||
|
|
||||||
|
const mockSourceCode = `
|
||||||
import { stuff } from "somewhere";
|
import { stuff } from "somewhere";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,20 +80,8 @@ export function doSomethingElse(): void {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
async function run() {
|
|
||||||
const hasTreeSitter = await requireTool(
|
|
||||||
"tree-sitter",
|
|
||||||
"npm install -g tree-sitter-cli",
|
|
||||||
);
|
|
||||||
if (!hasTreeSitter) {
|
|
||||||
console.warn(
|
|
||||||
"⚠️ Local Code Intelligence PoC skipped due to missing host dependency.",
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const extracted = await extractExports(mockSourceCode);
|
const extracted = extractExports(mockSourceCode);
|
||||||
|
|
||||||
assertEquals(extracted.length, 3);
|
assertEquals(extracted.length, 3);
|
||||||
|
|
||||||
@ -145,7 +100,7 @@ async function run() {
|
|||||||
assertEquals(doSomething?.signature, "() => any");
|
assertEquals(doSomething?.signature, "() => any");
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"✅ Local Code Intelligence PoC (Gen 2) successful: Extracted structured context from raw source using native tree-sitter CLI.",
|
"✅ Local Code Intelligence PoC (Gen 2) successful: Extracted structured context from raw source using AST Parser.",
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log("\n--- Agent Context Payload ---");
|
console.log("\n--- Agent Context Payload ---");
|
||||||
@ -153,11 +108,5 @@ async function run() {
|
|||||||
console.log("-----------------------------\n");
|
console.log("-----------------------------\n");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("❌ Local Code Intelligence PoC (Gen 2) failed:", err);
|
console.error("❌ Local Code Intelligence PoC (Gen 2) failed:", err);
|
||||||
Deno.exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (import.meta.main) {
|
|
||||||
console.log("Running Local Code Intelligence PoC (Gen 2) tests...");
|
|
||||||
run();
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,17 +1,12 @@
|
|||||||
|
import protobuf from "npm:protobufjs";
|
||||||
import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
||||||
import {
|
|
||||||
dirname,
|
|
||||||
fromFileUrl,
|
|
||||||
join,
|
|
||||||
} from "https://deno.land/std@0.224.0/path/mod.ts";
|
|
||||||
import { requireTool } from "./sys_exec.ts";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proof of Concept: Protocol Buffers (Gen 2)
|
* Proof of Concept: Protocol Buffers (Gen 2)
|
||||||
*
|
*
|
||||||
* Demonstrates serializing and deserializing agent state using actual
|
* Demonstrates serializing and deserializing agent state using actual
|
||||||
* protoc CLI instead of a JS library mock, showing high-performance
|
* protobufjs instead of a JSON stringifier mock, showing high-performance
|
||||||
* I/O for vector math and state passing using native host tooling.
|
* I/O for vector math and state passing.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const protoDefinition = `
|
const protoDefinition = `
|
||||||
@ -24,25 +19,12 @@ message AgentState {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
async function run() {
|
if (import.meta.main) {
|
||||||
const hasProtoc = await requireTool(
|
console.log("Running Protocol Buffers PoC (Gen 2) tests...");
|
||||||
"protoc",
|
|
||||||
"sudo apt-get install protobuf-compiler",
|
|
||||||
);
|
|
||||||
if (!hasProtoc) {
|
|
||||||
console.warn(
|
|
||||||
"⚠️ Protocol Buffers PoC skipped due to missing host dependency.",
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentDir = dirname(fromFileUrl(import.meta.url));
|
|
||||||
const TEMP_PROTO = join(currentDir, "dummy_agent.proto");
|
|
||||||
const TEMP_DATA = join(currentDir, "dummy_data.txt");
|
|
||||||
const TEMP_BIN = join(currentDir, "dummy_encoded.bin");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Deno.writeTextFile(TEMP_PROTO, protoDefinition);
|
const root = protobuf.parse(protoDefinition).root;
|
||||||
|
const AgentState = root.lookupType("AgentState");
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
agentId: "adversary-01",
|
agentId: "adversary-01",
|
||||||
@ -50,83 +32,20 @@ async function run() {
|
|||||||
memoryUsage: 1024,
|
memoryUsage: 1024,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Write text format for protoc to consume
|
const errMsg = AgentState.verify(payload);
|
||||||
const textData = `agentId: "${payload.agentId}"
|
if (errMsg) throw Error(errMsg);
|
||||||
status: "${payload.status}"
|
|
||||||
memoryUsage: ${payload.memoryUsage}
|
|
||||||
`;
|
|
||||||
await Deno.writeTextFile(TEMP_DATA, textData);
|
|
||||||
|
|
||||||
// Encode
|
const message = AgentState.create(payload);
|
||||||
const encodeCommand = new Deno.Command("protoc", {
|
const buffer = AgentState.encode(message).finish();
|
||||||
args: [
|
|
||||||
"--encode=AgentState",
|
|
||||||
`--proto_path=${currentDir}`,
|
|
||||||
"dummy_agent.proto",
|
|
||||||
],
|
|
||||||
stdin: "piped",
|
|
||||||
stdout: "piped",
|
|
||||||
stderr: "piped",
|
|
||||||
});
|
|
||||||
const encodeProcess = encodeCommand.spawn();
|
|
||||||
const encodeWriter = encodeProcess.stdin.getWriter();
|
|
||||||
await encodeWriter.write(new TextEncoder().encode(textData));
|
|
||||||
await encodeWriter.close();
|
|
||||||
const encodeOutput = await encodeProcess.output();
|
|
||||||
|
|
||||||
if (encodeOutput.code !== 0) {
|
|
||||||
throw new Error(
|
|
||||||
`protoc encode failed: ${
|
|
||||||
new TextDecoder().decode(encodeOutput.stderr)
|
|
||||||
}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const buffer = encodeOutput.stdout;
|
|
||||||
await Deno.writeFile(TEMP_BIN, buffer);
|
|
||||||
|
|
||||||
console.log(`Original Data:`, payload);
|
console.log(`Original Data:`, payload);
|
||||||
console.log(`Serialized Size: ${buffer.length} bytes (binary)`);
|
console.log(`Serialized Size: ${buffer.length} bytes (binary)`);
|
||||||
|
|
||||||
// Decode
|
const decodedMessage = AgentState.decode(buffer);
|
||||||
const decodeCommand = new Deno.Command("protoc", {
|
const deserialized = AgentState.toObject(decodedMessage, {
|
||||||
args: [
|
longs: String,
|
||||||
"--decode=AgentState",
|
enums: String,
|
||||||
`--proto_path=${currentDir}`,
|
bytes: String,
|
||||||
"dummy_agent.proto",
|
|
||||||
],
|
|
||||||
stdin: "piped",
|
|
||||||
stdout: "piped",
|
|
||||||
stderr: "piped",
|
|
||||||
});
|
|
||||||
const decodeProcess = decodeCommand.spawn();
|
|
||||||
const decodeWriter = decodeProcess.stdin.getWriter();
|
|
||||||
await decodeWriter.write(buffer);
|
|
||||||
await decodeWriter.close();
|
|
||||||
const decodeOutput = await decodeProcess.output();
|
|
||||||
|
|
||||||
if (decodeOutput.code !== 0) {
|
|
||||||
throw new Error(
|
|
||||||
`protoc decode failed: ${
|
|
||||||
new TextDecoder().decode(decodeOutput.stderr)
|
|
||||||
}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const decodedString = new TextDecoder().decode(decodeOutput.stdout);
|
|
||||||
|
|
||||||
// Parse text format back to object for assertion
|
|
||||||
// deno-lint-ignore no-explicit-any
|
|
||||||
const deserialized: any = {};
|
|
||||||
decodedString.trim().split("\n").forEach((line) => {
|
|
||||||
const [key, val] = line.split(":").map((s) => s.trim());
|
|
||||||
if (key && val) {
|
|
||||||
if (val.startsWith('"') && val.endsWith('"')) {
|
|
||||||
deserialized[key] = val.slice(1, -1);
|
|
||||||
} else {
|
|
||||||
deserialized[key] = parseInt(val, 10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Deserialized Data:", deserialized);
|
console.log("Deserialized Data:", deserialized);
|
||||||
@ -136,23 +55,10 @@ memoryUsage: ${payload.memoryUsage}
|
|||||||
assertEquals(deserialized.memoryUsage, payload.memoryUsage);
|
assertEquals(deserialized.memoryUsage, payload.memoryUsage);
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"✅ Protocol Buffers PoC (Gen 2) successful: Real protoc CLI serialization/deserialization worked.",
|
"✅ Protocol Buffers PoC (Gen 2) successful: Real protobuf serialization/deserialization worked.",
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("❌ Protocol Buffers PoC (Gen 2) failed:", err);
|
console.error("❌ Protocol Buffers PoC (Gen 2) failed:", err);
|
||||||
Deno.exit(1);
|
Deno.exit(1);
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
await Deno.remove(TEMP_PROTO);
|
|
||||||
await Deno.remove(TEMP_DATA);
|
|
||||||
await Deno.remove(TEMP_BIN);
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (import.meta.main) {
|
|
||||||
console.log("Running Protocol Buffers PoC (Gen 2) tests...");
|
|
||||||
run();
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,66 +0,0 @@
|
|||||||
export async function checkToolExists(toolName: string): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
const command = new Deno.Command("which", {
|
|
||||||
args: [toolName],
|
|
||||||
stdout: "piped",
|
|
||||||
stderr: "piped",
|
|
||||||
});
|
|
||||||
const { code } = await command.output();
|
|
||||||
return code === 0;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function requireTool(
|
|
||||||
toolName: string,
|
|
||||||
installationInstructions: string,
|
|
||||||
): Promise<boolean> {
|
|
||||||
const exists = await checkToolExists(toolName);
|
|
||||||
if (!exists) {
|
|
||||||
console.warn(`\n⚠️ [Pre-flight Check] Tool '${toolName}' is missing.`);
|
|
||||||
console.warn(` Please install it: ${installationInstructions}`);
|
|
||||||
console.warn(` Skipping execution that depends on this tool.\n`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function execTool(
|
|
||||||
toolName: string,
|
|
||||||
args: string[],
|
|
||||||
options?: { stdin?: string },
|
|
||||||
): Promise<{ code: number; stdout: string; stderr: string }> {
|
|
||||||
const commandOpts: Deno.CommandOptions = {
|
|
||||||
args,
|
|
||||||
stdout: "piped",
|
|
||||||
stderr: "piped",
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options?.stdin) {
|
|
||||||
commandOpts.stdin = "piped";
|
|
||||||
}
|
|
||||||
|
|
||||||
const command = new Deno.Command(toolName, commandOpts);
|
|
||||||
|
|
||||||
if (options?.stdin) {
|
|
||||||
const process = command.spawn();
|
|
||||||
const writer = process.stdin.getWriter();
|
|
||||||
await writer.write(new TextEncoder().encode(options.stdin));
|
|
||||||
await writer.close();
|
|
||||||
|
|
||||||
const { code, stdout, stderr } = await process.output();
|
|
||||||
return {
|
|
||||||
code,
|
|
||||||
stdout: new TextDecoder().decode(stdout),
|
|
||||||
stderr: new TextDecoder().decode(stderr),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const { code, stdout, stderr } = await command.output();
|
|
||||||
return {
|
|
||||||
code,
|
|
||||||
stdout: new TextDecoder().decode(stdout),
|
|
||||||
stderr: new TextDecoder().decode(stderr),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -2,16 +2,17 @@
|
|||||||
* Tool Sandbox PoC (Gen 2 - Production Tooling)
|
* Tool Sandbox PoC (Gen 2 - Production Tooling)
|
||||||
*
|
*
|
||||||
* This script proves that the execution environment can physically handle
|
* This script proves that the execution environment can physically handle
|
||||||
* invoking actual production-grade tooling constraints (tree-sitter CLI
|
* invoking actual production-grade tooling constraints (WASM for Tree-sitter
|
||||||
* and semgrep CLI) natively via system execution rather than Node imports.
|
* and Deno.Command for Semgrep).
|
||||||
|
*
|
||||||
|
* Dependencies required on host system for this PoC:
|
||||||
|
* 1. Semgrep: `sudo pip3 install semgrep --break-system-packages`
|
||||||
|
* 2. Tree-sitter: `npm install web-tree-sitter tree-sitter-javascript`
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import { join, dirname, fromFileUrl } from "https://deno.land/std@0.224.0/path/mod.ts";
|
||||||
dirname,
|
import * as webTreeSitter from "npm:web-tree-sitter@0.26.13";
|
||||||
fromFileUrl,
|
const Parser = webTreeSitter.default || webTreeSitter.Parser;
|
||||||
join,
|
|
||||||
} from "https://deno.land/std@0.224.0/path/mod.ts";
|
|
||||||
import { execTool, requireTool } from "./sys_exec.ts";
|
|
||||||
|
|
||||||
const currentDir = dirname(fromFileUrl(import.meta.url));
|
const currentDir = dirname(fromFileUrl(import.meta.url));
|
||||||
const TEMP_FILE = join(currentDir, "dummy_target.js");
|
const TEMP_FILE = join(currentDir, "dummy_target.js");
|
||||||
@ -24,90 +25,77 @@ function vulnerableQuery(userInput) {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
async function testTreeSitter() {
|
async function testTreeSitter() {
|
||||||
console.log("\n--- Testing Tree-sitter (CLI) ---");
|
console.log("\n--- Testing Tree-sitter (WASM) ---");
|
||||||
const hasTreeSitter = await requireTool(
|
|
||||||
"tree-sitter",
|
|
||||||
"npm install -g tree-sitter-cli",
|
|
||||||
);
|
|
||||||
if (!hasTreeSitter) return false;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Deno.writeTextFile(TEMP_FILE, DUMMY_CODE);
|
// web-tree-sitter requires initialization to load the base wasm
|
||||||
|
await Parser.init();
|
||||||
|
|
||||||
// Provide code as a file, use normal tree-sitter parse output
|
// Explicitly load the JavaScript language grammar WASM using a direct path
|
||||||
const { code, stdout, stderr } = await execTool("tree-sitter", [
|
// In a real environment, this might be copied to a known static directory.
|
||||||
"parse",
|
// For this PoC, we point directly to the npm installation path.
|
||||||
TEMP_FILE,
|
const rootDir = dirname(dirname(currentDir)); // Root of repo
|
||||||
"-q",
|
const wasmPath = join(rootDir, "node_modules", "tree-sitter-javascript", "tree-sitter-javascript.wasm");
|
||||||
]);
|
|
||||||
|
|
||||||
if (code !== 0) {
|
console.log(`[Sandbox] Loading Language WASM from: ${wasmPath}`);
|
||||||
console.error("❌ Tree-sitter CLI execution failed:", stderr || stdout);
|
const wasmBytes = await Deno.readFile(wasmPath);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't have the nice object tree structure, but we can verify it executed successfully
|
const Lang = await webTreeSitter.Language.load(wasmBytes);
|
||||||
// We would parse the sexp output from tree-sitter for full ast traversal in a real scenario
|
const parser = new Parser();
|
||||||
console.log("[Sandbox] Successfully executed native tree-sitter binary!");
|
parser.setLanguage(Lang);
|
||||||
|
|
||||||
|
const tree = parser.parse(DUMMY_CODE);
|
||||||
|
console.log("[Sandbox] Successfully parsed syntax tree!");
|
||||||
|
console.log(`[Sandbox] Root Node Type: ${tree.rootNode.type}`);
|
||||||
|
console.log(`[Sandbox] Extracted Functions: ${tree.rootNode.children.filter(n => n.type === 'function_declaration').map(n => n.childForFieldName('name')?.text).join(', ')}`);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("❌ Tree-sitter CLI execution failed:", error);
|
console.error("❌ Tree-sitter WASM execution failed:", error.message);
|
||||||
|
console.error("Please ensure you ran: `npm install web-tree-sitter tree-sitter-javascript`");
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
await Deno.remove(TEMP_FILE);
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function testSemgrep() {
|
async function testSemgrep() {
|
||||||
console.log("\n--- Testing Semgrep (Binary) ---");
|
console.log("\n--- Testing Semgrep (Binary) ---");
|
||||||
const hasSemgrep = await requireTool(
|
|
||||||
"semgrep",
|
|
||||||
"pip3 install semgrep --break-system-packages",
|
|
||||||
);
|
|
||||||
if (!hasSemgrep) return false;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Write out dummy file for semgrep to scan
|
// Write out dummy file for semgrep to scan
|
||||||
await Deno.writeTextFile(TEMP_FILE, DUMMY_CODE);
|
await Deno.writeTextFile(TEMP_FILE, DUMMY_CODE);
|
||||||
|
|
||||||
// Define a basic semgrep rule directly via CLI flag to detect our dummy issue
|
// Define a basic semgrep rule directly via CLI flag to detect our dummy issue
|
||||||
const { code, stdout, stderr } = await execTool("semgrep", [
|
const command = new Deno.Command("semgrep", {
|
||||||
|
args: [
|
||||||
"--quiet",
|
"--quiet",
|
||||||
"--json",
|
"--json",
|
||||||
"--lang",
|
"--lang", "javascript",
|
||||||
"javascript",
|
"-e", '"$SELECT ... " + $INPUT',
|
||||||
"-e",
|
TEMP_FILE
|
||||||
'"$SELECT ... " + $INPUT',
|
],
|
||||||
TEMP_FILE,
|
stdout: "piped",
|
||||||
]);
|
stderr: "piped",
|
||||||
|
});
|
||||||
|
|
||||||
|
const { code, stdout, stderr } = await command.output();
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
|
||||||
if (code !== 0 && code !== 1) { // 1 means findings found, 0 means no findings. Other codes are errors.
|
if (code !== 0 && code !== 1) { // 1 means findings found, 0 means no findings. Other codes are errors.
|
||||||
console.error("❌ Semgrep execution returned error code:", code);
|
console.error("❌ Semgrep execution returned error code:", code);
|
||||||
console.error(stderr);
|
console.error(decoder.decode(stderr));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsonResult = JSON.parse(stdout);
|
const outputString = decoder.decode(stdout);
|
||||||
|
const jsonResult = JSON.parse(outputString);
|
||||||
|
|
||||||
console.log("[Sandbox] Successfully executed native semgrep binary!");
|
console.log("[Sandbox] Successfully executed native semgrep binary!");
|
||||||
console.log(
|
console.log(`[Sandbox] Vulnerabilities found: ${jsonResult.results.length}`);
|
||||||
`[Sandbox] Vulnerabilities found: ${jsonResult.results.length}`,
|
|
||||||
);
|
|
||||||
if (jsonResult.results.length > 0) {
|
if (jsonResult.results.length > 0) {
|
||||||
console.log(
|
console.log(`[Sandbox] Details: ${jsonResult.results[0].extra.message} (Line ${jsonResult.results[0].start.line})`);
|
||||||
`[Sandbox] Details: ${jsonResult.results[0].extra.message} (Line ${
|
|
||||||
jsonResult.results[0].start.line
|
|
||||||
})`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("❌ Semgrep binary execution failed:", error);
|
console.error("❌ Semgrep binary execution failed:", error.message);
|
||||||
|
console.error("Please ensure Semgrep is installed: `sudo pip3 install semgrep --break-system-packages`");
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
@ -124,16 +112,11 @@ async function runSandbox() {
|
|||||||
const tsSuccess = await testTreeSitter();
|
const tsSuccess = await testTreeSitter();
|
||||||
const sgSuccess = await testSemgrep();
|
const sgSuccess = await testSemgrep();
|
||||||
|
|
||||||
// For PoC execution, we don't strictly fail if tools are missing, because
|
|
||||||
// the environment might be a basic docker. But we do want to record if it succeeded.
|
|
||||||
if (tsSuccess && sgSuccess) {
|
if (tsSuccess && sgSuccess) {
|
||||||
console.log(
|
console.log("\n✅ Gen 2 Sandbox execution completed successfully. Physical tools verified.");
|
||||||
"\n✅ Gen 2 Sandbox execution completed successfully. Physical tools verified.",
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
console.warn(
|
console.error("\n❌ Gen 2 Sandbox failed due to missing or malfunctioning host dependencies.");
|
||||||
"\n⚠️ Gen 2 Sandbox finished with skipped/failed host dependencies. Assuming graceful pass for PoC.",
|
Deno.exit(1);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user