Lesson 5 of 20 · 18m
Mass assignment and prototype pollution
Request binders write every key the body carries, and `__proto__` merges write onto Object.prototype — allowlists and merge hygiene are the only fixes.
Object.assign(user, req.body) doesn't ask which keys belong. Neither does User.new(params[:user]), new User(req.body) in Mongoose, Model.create(request.data) in Sequelize, or Spring's @ModelAttribute. The binder's job is to map request keys onto object fields, and it does that job for every key the request carries — role, isAdmin, balance, verified — whether or not your form ever rendered it.
GitHub ran this in March 2012: Egor Homakov used mass assignment on the public-key form and committed to the Rails repository itself. The fallout is why Rails grew attr_accessible and why Rails 4 settled on params.require(:user).permit(:name, :email) — strong parameters are an allowlist you state per action, not a filter on "dangerous" keys.
Allowlist or lose
Blocklists lose to the future. The column added next quarter — subscription_tier, quota_override, stripe_customer_id — starts writable until somebody remembers to guard it. Laravel's $guarded = [] is the standing joke for a reason: it's the empty blocklist, it unguards everything, and it sits in shipped apps today.
Allowlist per write path:
params.require(:user).permit(:name, :email, :avatar)protected $fillable = ['name', 'email', 'avatar']; // never $guarded = []class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ["name", "email", "avatar"] # fields, never exclude[Bind(Include = "Name,Email,Avatar")] // or TryUpdateModel against a DTOA DTO per write path is the grown-up version of the same idea: the binder can only reach fields the DTO declares, and isAdmin simply isn't on it. The allowlist is also per action, not per model — the fields a profile update may touch and the fields signup may touch are different lists, and reusing one across both is how username stays editable forever. PUT/PATCH handlers that replace the whole object are the sneaky variant: whole-object replace re-exposes every field the GET showed, so the write list has to be narrower than the read shape. GraphQL is the same question one layer up: the schema resolves fields, not models, so isAdmin on the type is a per-field authorization decision, not something the query binder inherits.
The JavaScript twin: prototype pollution
JavaScript adds a second bug with the same shape. JSON.parse('{"__proto__": {"isAdmin": true}}') yields an object whose own key is literally __proto__. Hand it to a naive deep-merge and target["__proto__"].isAdmin = true doesn't set a data property — it invokes the setter and writes onto Object.prototype, after which {isAdmin: undefined} reads true on every object in the process. Object.assign, $.extend(true), and lodash's merge/defaultsDeep/set each had exploitable variants: CVE-2019-11358 (jQuery), CVE-2019-10744 and CVE-2020-8203 (lodash), and CVE-2019-7609, where Kibana's Timelion turned prototype pollution into remote code execution.
The tells are specific:
for...initerates inherited enumerable properties;Object.keysdoesn't. Afor...inmerge loop is the classic pollution vector.target[key] = valuewithkey === "__proto__"fires the setter;Object.definePropertyor aMapdoesn't.if (user.admin)andobj.hasRole("x")now answer for the prototype — any property read that isn't own-property is pollution-readable.
The fix is threefold: merge into Object.create(null) targets or Map, reject __proto__, constructor, and prototype explicitly in merge loops (Object.hasOwn(target, key) gates), and actually update lodash — those CVEs are years old and still top every npm audit report.
Practice
Bind through allowlists — permit, $fillable, fields, DTOs — and grep for Model.create(input) / Object.assign(model, input) call sites, which is where the pattern survives. In JavaScript, own-property checks, null-prototype merge targets, and a CI step on npm audit cover the twin. The review question is always "which fields can this request reach", and the right answer is a list you wrote — not every column the model happens to have.
Keep reading
Related articles from the SudoSecurity blog.
Building a Production REST API With Node.js and Express
Build a REST API in Node.js and Express the way production systems need: input validation, JWT auth, refresh token rotation, status codes, rate limiting, idempotency keys, and centralized error handling.
RSA in JavaScript Part 2: The JWT Is Where You'll Actually Meet It
Most JavaScript developers never call an RSA function directly. They verify an RS256 token, and two classic attacks turn that into an authentication bypass — both demonstrated here, both fixed by one line.
RSA Signatures: The Version in Most Tutorials Is Forgeable
Signing is encryption with the keys swapped, and that symmetry is exactly what makes the naive implementation breakable. Here are two forgeries executed against textbook RSA signatures, and what PSS does that hashing alone cannot.
Ask Java for AES and You Get ECB. Nobody Warns You.
Cipher.getInstance("AES") compiles, runs, encrypts, decrypts, and hands you the weakest mode in the box. Here is the OpenJDK source that makes that decision, and the line where ECB gets chosen for you.