Arezgitfield notes / engineering
SecurityUPDATED NOV 23, 2026

Parse Explicit Records to Limit Prototype Pollution

Replace recursive JSON option merges with a bounded flat parser, reject unknown fields, and test that denied preferences cannot change shared object behavior.

Arezgit Editorial4 min read
AREZGIT / FIELD NOTESECURITY
A preferences endpoint that recursively merges arbitrary JSON into application options accepts a much larger operation than changing a theme. Restrict it to the fields the feature actually o
READ / VERIFY / APPLYTECHNICALLY REVIEWED

A preferences endpoint that recursively merges arbitrary JSON into application options accepts a much larger operation than changing a theme. Restrict it to the fields the feature actually owns. Parse those fields into a new record, reject unsupported keys, and keep user-controlled dictionaries away from privileged configuration objects.

The example handles a flat synthetic preferences document containing theme and pageSize. It accepts JSON text rather than arbitrary JavaScript objects, uses Node.js 24, and returns either a fresh validated record or null. Authentication, storage, and authorization remain responsibilities of the enclosing endpoint.

Locate the write that crosses the object boundary

Prototype pollution concerns changes to object behavior through prototype-related properties and unsafe writes. OWASP's prevention guidance recommends maps for dictionary-like data and null-prototype objects where an object dictionary is required. Those choices reduce inherited-property surprises; they do not authorize arbitrary configuration updates.

Parsing JSON containing an unusual key is not the same operation as recursively assigning that key into another object. Identify the later merge, path setter, or configuration consumer that gives the data authority. Blocking one spelling while retaining a generic nested merge can leave another route to the same dangerous write.

The narrow preferences feature needs no recursive merge at all. Its authoritative operation is selecting two known values after checking their types and ranges. It must not accept a role, a callback, an arbitrary nested path, or a change to the behavior of shared objects.

Parse a bounded flat record

Save this JavaScript as part of a Node.js module. The 512-code-unit input limit is a synthetic application limit; apply an independent byte limit before buffering a network body:

function parsePreferences(text) {
  if (typeof text !== "string" || text.length === 0 || text.length > 512) return null;
  let value;
  try {
    value = JSON.parse(text);
  } catch {
    return null;
  }
  if (value === null || typeof value !== "object" || Array.isArray(value)) return null;
  if (Object.keys(value).some(key => key !== "theme" && key !== "pageSize")) return null;
  const theme = Object.hasOwn(value, "theme") ? value.theme : "light";
  const pageSize = Object.hasOwn(value, "pageSize") ? value.pageSize : 20;
  if (theme !== "light" && theme !== "dark") return null;
  if (!Number.isInteger(pageSize) || pageSize < 1 || pageSize > 100) return null;
  return { theme, pageSize };
}

The parser does not spread the input into an output object or traverse nested fields. Each returned property is constructed explicitly. Own-property checks distinguish an absent input field from an inherited property, while the value checks reject arrays, objects, booleans, and numeric strings where the contract requires a specific primitive.

The function's input restriction matters. It is not a general validator for objects containing getters, proxies, or methods. Passing text through JSON.parse establishes a narrower data model. Keep that boundary intact instead of later overloading the helper to accept arbitrary objects for convenience.

Verify denial before using the options

Append these assertions in the same module and run it with Node.js. They check a useful accepted value and several denied records without modifying a global prototype:

import assert from "node:assert/strict";

assert.deepEqual(parsePreferences('{"theme":"dark","pageSize":40}'), { theme: "dark", pageSize: 40 });
assert.deepEqual(parsePreferences('{}'), { theme: "light", pageSize: 20 });
for (const text of [
  '{"__proto__":{"theme":"dark"}}',
  '{"constructor":{"prototype":{"theme":"dark"}}}',
  '{"pageSize":"40"}',
  '{"pageSize":101}',
  '{"theme":{"name":"dark"}}',
  '[]',
  'null',
  '{'
]) {
  assert.equal(parsePreferences(text), null);
}
assert.equal(Object.hasOwn(Object.prototype, "theme"), false);

The final assertion assumes a clean disposable Node process. It is evidence about this fixture, not a scanner for all prototype changes in an application. The stronger endpoint assertion is that a denied record produces no stored preference change, no new privileged option, and no downstream action.

Do not coerce a failed parse into acceptance by silently using an unchecked fallback object. Return a bounded client error and leave stored state unchanged. If unknown keys should be ignored for compatibility, make that an explicit alternate contract and still construct the output field by field.

Separate open dictionaries from executable options

Some features genuinely need arbitrary labels or counters. Store those entries in a Map or a suitable isolated dictionary and treat their values as data. Before converting them into ordinary objects for another library, inspect that library's handling of property names and nested options. A safe container can lose its protection at the next conversion boundary.

Explicit parsing also helps prevent mass assignment, but it does not supply object authorization. The server must determine whose preferences the authenticated caller may update. The tenant authorization guide addresses that separate decision.

Retain limits on body size and nesting upstream, and keep JSON parsing failures out of raw client diagnostics. If a shared process has already suffered a prototype mutation, rejecting the next request does not necessarily undo altered state. Contain the affected instance, correct the unsafe write path, and restart through the normal recovery process after assessing effects.

When adding a preference, extend the accepted schema, the explicit return record, and both accepted and denied cases together. The review question should remain small enough to answer directly: which precise input values can become which precise application options, and can a rejected document cause any mutation before that decision is complete?

READER SIGNAL

Was this guide useful?

Your rating helps us prioritize clearer, more practical technical content.

AREZGIT / DESKTOP WORKSPACEFROM FIELD NOTE TO RELEASE

Review diffs, run checks, and prepare the release in Arezgit.

Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.

Explore Arezgit