Skip to content

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 DTO

A 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...in iterates inherited enumerable properties; Object.keys doesn't. A for...in merge loop is the classic pollution vector.
  • target[key] = value with key === "__proto__" fires the setter; Object.defineProperty or a Map doesn't.
  • if (user.admin) and obj.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.