Skip to main content

SKY-D510 · Security in code

Prototype pollution from request data

High

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

src/settings.ts · TypeScriptReported on line 9
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

src/settings.ts · TypeScript
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

src/profile.js · JavaScriptReported on line 19
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

src/profile.js · JavaScript
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

  1. Runs with --danger on TypeScript and JavaScript files. Finds request handlers: Express, Fastify, Hapi and Next.js pages/api functions, Koa and getServerSideProps, NestJS methods with @Body() or @Query() parameters, Next.js App Router route handlers and Remix loader and action.
  2. Follows request data (req.body, req.params, req.query and the like) inside the handler through variables, destructuring, JSON.parse and helpers in the same file. Calls into other modules stop it.
  3. Nested write: reports obj[a][b] = value when a comes from the request and b is not a fixed string. Skipped when a check whose failing branch returns or throws comes first: comparison with fixed strings, a fixed list, hasOwnProperty or Object.hasOwn, a test that rejects __proto__, a strict pattern or number check, or a typeof check. Also skipped when obj was made with Object.create(null).
  4. 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…in or Object.keys(), assigns target[key] and calls itself.
  5. A merge function is safe when its body names "__proto__", "constructor" or "prototype" as a string, or uses hasOwnProperty, Object.hasOwn or Object.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.

src/settings.ts · TypeScript · the fix, against the flagged file
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 shared deepMerge), and one-level writes such as settings.theme[req.body.key] = value, which cannot reach Object.prototype on their own.
  • Missed: if (typeof section !== "string") return counts 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 --baseline accept the current findings and report only new ones.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.