I Found Hidden Malware in a Next.js Theme: How to Check Yours
While turning an old portfolio theme into my current site, I found hidden malware in it. I never ran the project, and VS Code was in Restricted Mode, so nothing executed. But the way it was hidden is worth understanding, because every technique below works against any developer who clones a repository and runs npm run dev.
Key takeaways
- Hidden code can live in files that look harmless: an API stub, a lint config, even a font.
- Code can be pushed off-screen with thousands of spaces on a single line.
- VS Code can run a task the moment a folder opens, if you trust the workspace.
- A few cheap checks catch all of it before you run anything.
The four tricks I found
1. Code hidden after whitespace. An ordinary-looking api.js mock and an ESLint config each ended with a normal line, followed by thousands of spaces, followed by roughly 28 KB of obfuscated JavaScript on the same line. Without word wrap, an editor shows only the clean part.
2. A script disguised as a font. Next to the real fa-brands-400.woff sat a near-identical fa-brand-400.woff. It was not a font. It was plain-text JavaScript with a font extension, buried in a nested public/fonts folder.
3. A VS Code task that runs on open. A hidden .vscode/tasks.json defined a task with runOn: folderOpen that executed the fake font with Node. The workspace settings also set task.allowAutomaticTasks to true, so the task would start as soon as the folder was trusted.
4. A start script that runs the payload. The dev and start scripts in package.json were node api.js && next dev. Running the normal command executed the hidden code first.
I cannot say how it got there. It could be a compromised download or a tampered copy. The lesson is the same either way: treat every file you did not write as untrusted.
What the payload looked like
The decoded intent is not the interesting part. The structure is: a loader that decodes a hidden string and passes it to the Function constructor, so the real code never appears in plain text. Patterns like eval, new Function, child_process and a function called with (global, require) are strong warning signs in a front-end project.
A checklist you can run in five minutes
Before running or opening anything you downloaded:
- Keep VS Code in Restricted Mode. Do not click Trust until you have read
.vscode/. Look forrunOnandallowAutomaticTasks. - Read the scripts. Open
package.jsonand check every script and anypreinstall,install,postinstallorpreparehook. - Search for whitespace padding.
grep -rEn '[[:space:]]{50,}[^[:space:]]' --exclude-dir=node_modules --exclude-dir=.git .- Check that binaries are binaries.
find . -name '*.woff*' -not -path './node_modules/*' -exec file {} +A real font reports as a web font. A script reports as text.
- Install without scripts. Use
npm ci --ignore-scripts, or setignore-scripts=truein.npmrcso no dependency can run code on install. - Audit the dependency tree. Run
npm auditandnpm audit signatures, and check that the lockfile only resolves from the registry you expect.
Make it automatic
Manual checks get skipped. I turned these into a small zero-dependency script that runs in CI and in a pre-commit hook. It fails the build on very long lines, whitespace padding, text files with binary extensions, obfuscation patterns, lifecycle scripts, auto-run VS Code tasks and non-registry dependencies. I also pin every GitHub Action to a commit SHA and enable Dependabot with a cooldown, so a freshly published malicious version is not pulled in on day one.
If you want help auditing a codebase or hardening a build pipeline, see my authentication and security work or get in touch.
- npm security
- supply chain
- Next.js
- malware
- VS Code