import fs from './read-boundary.js'; import os from 'node:os'; import path from 'node:path'; import { gitLsFiles } from './git.js'; // Malware planted in sample projects and take-home tests that target developers ("fake job // interview" campaigns). Found in four repositories of our corpus in October 2026, all with the // same three tricks: a VS Code task that runs on folder open, code hidden after hundreds of // spaces on the last line of babel.config.js (Babel loads it on every start and build), and a // JavaScript file named like a font. The scan only reads these files; NativeKeel never runs // anything from the project, so it is safe to point at a repository you do not trust yet. const SKIP_DIRS = new Set(['node_modules', '.git', 'Pods', 'build', '.gradle', 'DerivedData', '.expo', 'dist', 'coverage']); const CODE_EXT = new Set(['.js', '.cjs', '.mjs', '.jsx', '.ts', '.tsx']); // Leading bytes of the real formats. A text file with one of these names is not that format. const MAGIC = { '.woff2': ['wOF2'], '.woff': ['wOFF'], '.otf': ['OTTO'], '.ttf': ['\x00\x01\x00\x00', 'true', 'OTTO'], '.eot': null, '.png': ['\x89PNG'], '.jpg': ['\xff\xd8'], '.jpeg': ['\xff\xd8'], '.gif': ['GIF8'], '.ico': ['\x00\x00\x01\x00'], '.llf': null, }; const LOOKS_LIKE_CODE = /^(?:global\s*\[|globalThis\s*\[|function[\s(]|(?:var|let|const)\s|\(\s*function|!function|\(\s*\(\s*\)\s*=>|eval\s*\(|require\s*\(|module\.exports|_0x[0-9a-f]{3})/; const PAD = /[ \t]{100,}/g; const MAX_BYTES = 4 * 1024 * 1024; function listFiles(root) { const tracked = gitLsFiles(root); if (tracked) return tracked; const out = []; const walk = (dir, depth) => { let entries = []; try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; } for (const e of entries) { const p = path.join(dir, e.name); if (e.isDirectory()) { if (depth < 12 && !SKIP_DIRS.has(e.name)) walk(p, depth + 1); } else if (e.isFile()) out.push(path.relative(root, p).split(path.sep).join('/')); } }; walk(root, 0); return out; } function readHead(file, bytes) { let fd; try { fd = fs.openSync(file, 'r'); const buf = Buffer.alloc(bytes); const n = fs.readSync(fd, buf, 0, bytes, 0); return buf.subarray(0, n).toString('latin1'); } catch { return ''; } finally { if (fd !== undefined) fs.closeSync(fd); } } // Code that starts after a long run of spaces or tabs on a line, where an editor without word // wrap shows nothing. Formatted tables and indentation end the line soon after; the hidden // payloads are thousands of characters of code. export function hiddenAfterWhitespace(text) { for (const m of text.matchAll(PAD)) { const start = m.index + m[0].length; const end = text.indexOf('\n', start); const rest = text.slice(start, end === -1 ? undefined : end); if (rest.length >= 500 && LOOKS_LIKE_CODE.test(rest)) return { line: text.slice(0, m.index).split('\n').length, column: start }; } return null; } // A font or image that is really a text file of code. Broken assets (an S3 error page saved as // a .png, an SVG or HTML file) are text too, so the content has to look like JavaScript. export function fakeBinary(head, ext) { if (!(ext in MAGIC)) return false; const magic = MAGIC[ext]; if (magic && magic.some((m) => head.startsWith(m))) return false; if (!/^[\t\n\r\x20-\x7e]*$/.test(head.slice(0, 2048))) return false; return LOOKS_LIKE_CODE.test(head.trimStart()); } // .vscode/tasks.json tasks with "runOn": "folderOpen" run as soon as the folder is opened in a // trusted VS Code window (Cursor and other forks too). Legitimate ones exist (a watcher), so the // finding names the command; it becomes critical when the command runs a file that is not code // or downloads and pipes something into a shell. function autoRunTasks(text) { let json; try { // tasks.json allows comments and trailing commas. json = JSON.parse(text.replace(/\/\*[\s\S]*?\*\//g, '').replace(/(^|[^:"])\/\/[^\n]*/g, '$1').replace(/,(\s*[}\]])/g, '$1')); } catch { return /"runOn"\s*:\s*"folderOpen"/.test(text) ? [{ command: '(unparsed tasks.json)', suspicious: true }] : []; } const tasks = Array.isArray(json && json.tasks) ? json.tasks : []; const out = []; for (const t of tasks) { if (!t || !t.runOptions || t.runOptions.runOn !== 'folderOpen') continue; const parts = [t.command, ...(Array.isArray(t.args) ? t.args : [])]; for (const os of ['osx', 'linux', 'windows']) if (t[os] && t[os].command) parts.push(t[os].command); const command = parts.filter((p) => typeof p === 'string').join(' ').trim(); const hidden = t.hide === true || (t.presentation && t.presentation.reveal === 'never'); const runsData = /\bnode\s+(?:"[^"]*|'[^']*|\S*?)\.(?:woff2?|ttf|otf|eot|llf|png|jpe?g|gif|ico|svg|txt|md|json|css|map|lock|dat|bin)\b/i.test(command); const download = /\b(?:curl|wget|iwr|Invoke-WebRequest|irm|Invoke-RestMethod)\b[^\n]*(?:\|\s*(?:ba|z)?sh\b|\|\s*iex\b|\|\s*node\b)|\bbase64\s+(?:-d|--decode)\b|\beval\s*[("$]/i.test(command); out.push({ command, suspicious: runsData || download, hidden }); } return out; } function vscodeTaskFiles(root, files) { const found = files.filter((f) => /(^|\/)\.vscode\/tasks\.json$/.test(f)).map((f) => path.join(root, f)); // In a monorepo the scan may start in an app folder while .vscode sits at the repository root. let dir = path.resolve(root); for (let i = 0; i < 4; i++) { if (fs.existsSync(path.join(dir, '.git'))) break; const parent = path.dirname(dir); if (parent === dir || parent === os.homedir()) break; dir = parent; const p = path.join(dir, '.vscode', 'tasks.json'); if (fs.existsSync(p)) found.push(p); } return [...new Set(found)]; } const DO_NOT_RUN = 'Do not run npm/yarn install, start or build, and do not open the folder in VS Code (or Cursor) with workspace trust, until this is removed. If you already did, treat the machine as compromised: rotate GitHub, npm, SSH and cloud credentials and browser-saved passwords, and check crypto wallets.'; export function maliciousCode(root) { const files = listFiles(root); const hidden = []; const fakes = []; const install = []; for (const rel of files) { if (/(^|\/)node_modules\//.test(rel)) continue; const ext = path.extname(rel).toLowerCase(); const file = path.join(root, rel); let st; try { st = fs.lstatSync(file); } catch { continue; } if (!st.isFile() || st.size > MAX_BYTES) continue; if (ext in MAGIC) { if (st.size > 100 && fakeBinary(readHead(file, 64 * 1024), ext)) fakes.push(rel); continue; } if (!CODE_EXT.has(ext) && !rel.endsWith('package.json')) continue; const text = readHead(file, st.size); const at = hiddenAfterWhitespace(text); if (at) hidden.push(`${rel}:${at.line}`); if (rel.endsWith('package.json')) { let scripts = {}; try { scripts = JSON.parse(text).scripts || {}; } catch {} for (const k of ['preinstall', 'install', 'postinstall', 'prepare']) { const s = scripts[k]; if (typeof s !== 'string') continue; if (/\bnode\s+(?:"[^"]*|'[^']*|\S*?)\.(?:woff2?|ttf|otf|eot|llf|png|jpe?g|gif|ico|txt|md|css|dat|bin)\b|\b(?:curl|wget)\b[^\n]*\|\s*(?:ba|z)?sh\b|\bnode\s+-e\b[^\n]*(?:eval|Buffer\.from\([^)]*base64|atob)/i.test(s)) install.push(`${rel} "${k}": ${s.slice(0, 120)}`); } } } const tasks = []; for (const f of vscodeTaskFiles(root, files)) { let text = ''; try { text = fs.readFileSync(f, 'utf8'); } catch { continue; } for (const t of autoRunTasks(text)) tasks.push({ ...t, file: path.relative(root, f).split(path.sep).join('/') || f }); } const findings = []; const evidence = [ ...hidden.map((h) => `code hidden after a long run of spaces at ${h}`), ...fakes.map((f) => `${f} is named like a font or image but contains JavaScript`), ...tasks.filter((t) => t.suspicious).map((t) => `${t.file} runs \`${t.command.slice(0, 100)}\` when the folder is opened${t.hidden ? ' (hidden from the terminal)' : ''}`), ...install.map((s) => `install script ${s}`), ]; if (evidence.length) { findings.push({ id: 'malicious-code', severity: 'critical', area: 'security', title: `This project contains code that looks like malware: ${evidence[0]}${evidence.length > 1 ? ` (and ${evidence.length - 1} more sign${evidence.length > 2 ? 's' : ''})` : ''}`, detail: `These are the tricks used by malware planted in sample projects and coding tests sent to developers: a VS Code task that runs when the folder is opened, code hidden after a long run of spaces on a config line (Babel and Metro load babel.config.js and metro.config.js on every start and build), and JavaScript disguised as a font or image file. The hidden code typically downloads a second stage that steals credentials and crypto wallets. NativeKeel only read these files. ${DO_NOT_RUN}`, fix: { kind: 'security', step: [ hidden.length ? `delete the code after the long run of spaces at ${hidden.slice(0, 3).join(', ')}` : '', fakes.length || tasks.some((t) => t.suspicious) ? `delete ${[...fakes, ...tasks.filter((t) => t.suspicious).map((t) => t.file)].filter((v, i, a) => a.indexOf(v) === i).slice(0, 4).join(', ')}` : '', install.length ? 'remove the install script' : '', ].filter(Boolean).join(', ').replace(/^./, (c) => c.toUpperCase()) + ' (or restore them from a commit before the change), find out how the code got in (a compromised account, or a commit with a harmless title), and report the repository if it is not yours.' }, evidence, }); } const plain = tasks.filter((t) => !t.suspicious); if (plain.length && !evidence.length) { findings.push({ id: 'vscode-autorun-task', severity: 'medium', area: 'security', title: `A VS Code task runs automatically when the folder is opened (${plain[0].file}: \`${plain[0].command.slice(0, 80)}\`)`, detail: `Tasks with "runOn": "folderOpen" run as soon as the folder is opened in a trusted VS Code or Cursor window${plain.some((t) => t.hidden) ? ', and this one is hidden from the terminal' : ''}. That is how malware in shared sample projects starts, so anyone who clones the repository should be able to see what runs. If this is your own watcher, keep it visible and document it; if you did not add it, remove it.`, fix: { kind: 'security', step: `Check that the command in ${plain[0].file} is yours. Remove "runOn": "folderOpen" if nobody needs it, or make the task visible (no "hide", reveal not "never").` }, }); } return findings; }