Security rules
NGB036: Prototype pollution
Reports attacker-controllable objects or property paths flowing into operations that can write to Object.prototype: deep merges (_.merge, mergeWith, defaultsDeep, jQuery $.extend(true, …), deepmerge), path setters (_.set(obj, path, v), setWith, zipObjectDeep) and nested computed assignments obj[a][b] = v where both keys come from postMessage data, URL parameters, window.name or the referrer..
| Property | Value |
|---|---|
| Rule ID | NGB036 |
| Category | Angular security & XSS |
| Default severity | high |
| CWE | CWE-1321 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports attacker-controllable objects or property paths flowing into operations that can write to Object.prototype: deep merges (_.merge, mergeWith, defaultsDeep, jQuery $.extend(true, …), deepmerge), path setters (_.set(obj, path, v), setWith, zipObjectDeep) and nested computed assignments obj[a][b] = v where both keys come from postMessage data, URL parameters, window.name or the referrer.
Why it matters
JSON.parse turns {"__proto__": {"isAdmin": true}} into an object with an own __proto__ key; a deep merge then copies it onto Object.prototype, and every object in the page suddenly has isAdmin === true. In browsers this commonly becomes XSS through script gadgets (innerHTML options, sanitizer configs, template settings read from plain objects) or bypasses client-side checks.
Detection behavior and supported patterns
- lodash / lodash-es / per-method packages:
merge,mergeWith,defaultsDeep(source arguments),set,setWith(path argument),zipObjectDeep(paths) $.extend(true, target, source)/jQuery.extend(true, …),deepmerge(a, b)/deepmerge.all([...])obj[k1][k2] = valuewithk1traced to an attacker source andk2(or the value) also attacker-controlled- Guards that check
__proto__/constructor/prototype,hasOwnProperty,Object.hasOwnor an allowlist suppress the computed-assignment finding
Example
window.addEventListener('message', (e) => _.merge(this.settings, JSON.parse(e.data)));Safer alternative
window.addEventListener('message', (e) => { if (e.origin !== EDITOR_ORIGIN) return; const { theme, fontSize } = SettingsSchema.parse(JSON.parse(e.data)); this.settings = { ...this.settings, theme, fontSize };});Remediation
Never deep-merge untrusted objects. Validate messages and URL-derived data against a schema and copy known fields explicitly; when dynamic keys are needed, use Map or Object.create(null) and reject __proto__, constructor and prototype. Freezing Object.prototype at startup (Object.freeze(Object.prototype)) blocks the class entirely if your dependencies allow it.
False-positive considerations
- Merging a message payload that was already validated by a schema in another function is reported; schema validation is only recognized by NGB009.
Known limitations
- Hand-written recursive merge functions are only covered when the nested assignment pattern is visible; recursion depth is not modeled.
- Library versions are not checked: patched lodash versions still pollute through
mergewithconstructor.prototypepaths in some configurations, so all calls are reported. - HTTP responses and Web Storage are not treated as attacker-controlled here.
References
Run it
ngbastion explain NGB036