Mern Stack
Table of Contents
A full-stack JavaScript framework combining MongoDB, Express.js, React, and Node.js. Widely used for modern web applications and APIs. Each component introduces its own attack surface.
Related: Web Security Web Application Testing Methodology API Testing Methodology NoSQL Injection
Components
| Layer | Technology | Role |
|---|---|---|
| Database | MongoDB | NoSQL document store |
| Backend | Express.js | Web framework running on Node.js |
| Frontend | React | Client-side UI rendering |
| Runtime | Node.js | JavaScript runtime for the backend |
Architecture
A typical MERN application flows like this:
Browser (React) → Express.js API → Node.js → MongoDB
React handles the UI and makes API calls to Express endpoints. Express routes requests and applies middleware. Node.js executes the backend logic. MongoDB stores data as JSON-like documents.
APIs are typically RESTful JSON, though GraphQL is increasingly common as a replacement for Express routing.
Attack Surface
MongoDB — NoSQL Injection
MongoDB queries use JSON operators that can be injected if user input is passed directly into queries without sanitisation.
# Authentication bypass curl -X POST http://$IP/api/login \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":{"$ne":""}}'
See NoSQL Injection for full technique detail.
Express.js — Middleware Misconfigurations
Express relies heavily on middleware for authentication, parsing, and routing. Common issues:
- Missing authentication middleware on sensitive routes — test all endpoints without a token
- CORS misconfiguration via the
corspackage — check whether arbitrary origins are reflected - Body parser accepting unexpected content types — try switching
Content-Typebetweenapplication/jsonandapplication/x-www-form-urlencoded - Debug routes left enabled in production (
/api/debug,/api/test,/api/status)
Node.js — Prototype Pollution
JavaScript's prototype chain can be polluted by injecting
properties into Object.prototype, potentially affecting
application logic.
curl -X POST http://$IP/api/data \ -H "Content-Type: application/json" \ -d '{"__proto__":{"admin":true}}'
Impact varies — can bypass authorisation checks, alter application behaviour, or in some cases lead to RCE via gadget chains.
Node.js — Command Injection
If user input is passed to Node.js functions like exec,
execSync, or spawn, command injection is possible.
curl -X POST http://$IP/api/convert \ -H "Content-Type: application/json" \ -d '{"filename":"test.pdf; whoami"}'
Look for file conversion, image processing, or any feature that likely shells out to system tools.
React — XSS
React escapes output by default but dangerouslySetInnerHTML bypasses
this entirely. Look for it in the source:
dangerouslySetInnerHTML={{ __html: userInput }}
Also check for direct DOM manipulation via document.write or
innerHTML in custom JavaScript files.
See XSS for full technique detail.
JWT Authentication
MERN applications almost universally use JWT for authentication. Tokens are often stored in localStorage — accessible via XSS — rather than HttpOnly cookies.
Common issues:
- Weak signing secret — crackable with Hashcat
- Algorithm set to
none - No token expiry validation
See JWT Attacks for full technique detail.
Dependency Vulnerabilities
MERN applications pull in large numbers of npm packages. Check for known vulnerabilities in declared dependencies:
# If you have access to the source npm audit # Check package.json for version hints curl http://$IP/package.json
Exposed package.json or package-lock.json files reveal the
exact dependency tree — search each package version against CVE
databases.
Enumeration Tips
- Check for exposed
package.json— reveals framework versions and dependencies - React apps often expose API endpoints in bundled JavaScript —
open browser devtools and search the source for
/api/ - MongoDB default port is 27017 — check if it is exposed directly
- Look for
.envfiles leaking secrets:http://$IP/.env - Stack traces in error responses often reveal file paths, package versions, and internal structure — trigger errors deliberately by sending malformed input
Tips
- MERN APIs are almost always JSON — switch
Content-Typetoapplication/jsonif a request isn't working - NoSQL injection and JWT attacks are the highest-value targets in a MERN application
- Prototype pollution is easy to miss — fuzz JSON bodies with
__proto__andconstructor.prototypepayloads - localStorage JWT storage means XSS leads directly to account takeover — chain vulnerabilities where possible