SKY-D510 · Security in code
Prototype pollution from request data
In JavaScript every plain object inherits from Object.prototype, and the property name __proto__ points at it. Prototype pollution happens when request data picks a property name, so that a write meant for one object lands on Object.prototype and changes every object in the process. SKY-D510 reports two shapes in server handlers: a two-level write obj[key1][key2] = value whose first key comes from the request, and a recursive merge function that copies request data key by key.
- Languages
- TypeScript/JavaScript
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Prototype pollution
Why it matters
A request with section __proto__ and key isAdmin makes {}.isAdmin true for the whole server until it restarts. Code such as if (user.isAdmin) then lets everyone in, and other polluted properties can crash the server or, through template and child-process options, run commands.
What Skylos flags
import express from "express"; const app = express();app.use(express.json()); const settings: Record<string, Record<string, unknown>> = { theme: {}, email: {} }; app.post("/settings/:section", (req, res) => { settings[req.params.section][req.body.key] = req.body.value; res.json(settings[req.params.section]);});What Skylos leaves alone
import express from "express"; const app = express();app.use(express.json()); const SECTIONS = ["theme", "email"];const settings: Record<string, Record<string, unknown>> = { theme: {}, email: {} }; app.post("/settings/:section", (req, res) => { const section = req.params.section; if (!SECTIONS.includes(section)) { return res.status(404).json({ error: "Unknown section" }); } settings[section][req.body.key] = req.body.value; res.json(settings[section]);});Why Skylos leaves it alone
The same nested write, but the first key is checked against a fixed list and the handler returns when it is not on it. __proto__ can no longer reach the first level, so the write only changes theme or email.
More languages
JavaScript
Flagged
const express = require("express"); const router = express.Router();const defaults = { notifications: { email: true } }; function merge(target, source) { for (const key in source) { if (typeof source[key] === "object" && source[key] !== null) { merge((target[key] = target[key] || {}), source[key]); } else { target[key] = source[key]; } } return target;} router.put("/profile/preferences", (req, res) => { const preferences = merge({}, defaults); merge(preferences, req.body); res.json(preferences);}); module.exports = router;Not flagged
const express = require("express"); const router = express.Router();const defaults = { notifications: { email: true } }; function merge(target, source) { for (const key in source) { if (key === "__proto__" || key === "constructor" || key === "prototype") { continue; } if (typeof source[key] === "object" && source[key] !== null) { merge((target[key] = target[key] || {}), source[key]); } else { target[key] = source[key]; } } return target;} router.put("/profile/preferences", (req, res) => { const preferences = merge({}, defaults); merge(preferences, req.body); res.json(preferences);}); module.exports = router;Why Skylos leaves it alone
A JSON body such as {"__proto__": {"isAdmin": true}} makes the first merge recurse into Object.prototype. The second merge skips the three dangerous keys, so Skylos no longer treats it as an unsafe merge.
How Skylos decides
- Runs with
--dangeron TypeScript and JavaScript files. Finds request handlers: Express, Fastify, Hapi and Next.jspages/apifunctions, Koa andgetServerSideProps, NestJS methods with@Body()or@Query()parameters, Next.js App Router route handlers and Remixloaderandaction. - Follows request data (
req.body,req.params,req.queryand the like) inside the handler through variables, destructuring,JSON.parseand helpers in the same file. Calls into other modules stop it. - Nested write: reports
obj[a][b] = valuewhenacomes from the request andbis not a fixed string. Skipped when a check whose failing branch returns or throws comes first: comparison with fixed strings, a fixed list,hasOwnPropertyorObject.hasOwn, a test that rejects__proto__, a strict pattern or number check, or atypeofcheck. Also skipped whenobjwas made withObject.create(null). - Recursive merge: reports a call to a function in the same file that takes request data as its second or later argument and is a recursive merge: it loops over a parameter's keys with
for…inorObject.keys(), assignstarget[key]and calls itself. - A merge function is safe when its body names
"__proto__","constructor"or"prototype"as a string, or useshasOwnProperty,Object.hasOwnorObject.create(null). Merges from libraries are not checked.
How to fix it
Never let request data choose the first property name of a shared object. Check the key against the object's own keys or a fixed list first, or store user-keyed data in a Map. In merge helpers skip __proto__, constructor and prototype.
import express from "express"; const app = express();app.use(express.json()); const settings: Record<string, Record<string, unknown>> = { theme: {}, email: {} }; app.post("/settings/:section", (req, res) => {Removed: settings[req.params.section][req.body.key] = req.body.value;Removed: res.json(settings[req.params.section]);Added: const section = req.params.section;Added: if (!Object.hasOwn(settings, section)) {Added: return res.status(404).json({ error: "Unknown section" });Added: }Added: settings[section][req.body.key] = req.body.value;Added: res.json(settings[section]);});When it can be wrong
- A check done by a helper is not recognised: with
if (!isKnownSection(section)) return …imported from another file, the write is still reported. - Missed: merges done by a library or by a helper in another file (
lodash.merge, a shareddeepMerge), and one-level writes such assettings.theme[req.body.key] = value, which cannot reachObject.prototypeon their own. - Missed:
if (typeof section !== "string") returncounts as a check, although"__proto__"is a string, so the nested write after it is not reported.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark the reported line and say what limits the key.
TypeScript settings[section][key] = value; // skylos: ignore[SKY-D510] isKnownSection() checked[tool.skylos] ignore = ["SKY-D510"]turns the rule off for the whole project.skylos baseline .and then--baselineaccept the current findings and report only new ones.