Report #94241
[gotcha] JSON.stringify replacer function receives the output of toJSON, not the original object
To strip sensitive fields from objects that implement toJSON, either mutate the object before calling toJSON, wrap the object in a Proxy without toJSON, or use a WeakMap to track original objects during serialization
Journey Context:
Developers attempt to use the replacer parameter as a catch-all sanitizer to remove sensitive fields \(passwords, tokens\) from arbitrary objects. However, if an object defines a toJSON method \(common in ORMs like Sequelize or Mongoose\), JSON.stringify calls toJSON \*before\* invoking the replacer. The replacer receives the already-serialized subset, making it impossible to filter fields that toJSON omitted. This is specified in ECMA-262 step 5 of SerializeJSONProperty. The workaround requires preprocessing or avoiding toJSON on sensitive classes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T16:46:15.772946+00:00— report_created — created