Skip to content
Myr

fix: Revert "prevent prototype pollution via constructor.prototype access (CVE-2026-XXXX) (#1259)" (#1271)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This reverts commit 48fc3788609ce46dd789c313768bae4f48b00ec5. This fixes #1266 and #1268, #1265 while the security reported could not find a reasonable way to show the designated fixes was closing a real vulnerability, nor could I think of anything.

authored by Michel Weststrate · committed by GitHub · parent 60ca295 · Browse files · 3 files+1 added−140 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+1 added−140 removed

  • tests:+0 added−92 removed
  • other:+1 added−48 removed

3 files+1 added−140 removed

modified:__tests__/base.js-92 removed
2951 unmodified lines
2952 expect(result.arrConstructed).toEqual(new Array().constructor(1))2953 expect(result.objConstructed).toEqual(new Object().constructor(1))2954 })29552956 it("does not allow prototype pollution via reserved constructor access", () => {2957 const pollutedKey = "__immer_test_polluted__"2958 const original = Object.prototype[pollutedKey]29592960 delete Object.prototype[pollutedKey]29612962 try {2963 // The attack should either throw an error or silently fail2964 // but NOT pollute Object.prototype2965 let hadError = false2966 try {2967 produce({}, draft => {2968 draft.constructor.prototype[pollutedKey] = true2969 draft["__proto__"][pollutedKey] = true2970 })2971 } catch (e) {2972 // Expected: error when trying to set on undefined/frozen objects2973 hadError = true2974 }29752976 // The critical check: Object.prototype must not be polluted2977 // either because we threw an error OR because the assignment was silently blocked2978 expect(Object.prototype[pollutedKey]).toBeUndefined()2979 } finally {2980 if (original === undefined) {2981 delete Object.prototype[pollutedKey]2982 } else {2983 Object.prototype[pollutedKey] = original2984 }2985 }2986 })29872988 it("blocks prototype pollution via stored constructor reference (CVE bypass)", () => {2989 const pollutedKey = "__immer_test_ref__"2990 const original = Object.prototype[pollutedKey]29912992 delete Object.prototype[pollutedKey]29932994 try {2995 // Attack 3 from CVE: store constructor reference and mutate2996 let hadError = false2997 try {2998 produce({data: {}}, draft => {2999 const ctor = draft.data.constructor3000 ctor.prototype[pollutedKey] = true3001 })3002 } catch (e) {3003 hadError = true3004 }30053006 expect(Object.prototype[pollutedKey]).toBeUndefined()3007 } finally {3008 if (original === undefined) {3009 delete Object.prototype[pollutedKey]3010 } else {3011 Object.prototype[pollutedKey] = original3012 }3013 }3014 })30153016 it("blocks prototype pollution via Object.assign with malicious payload", () => {3017 const pollutedKey = "__immer_test_assign__"3018 const original = Object.prototype[pollutedKey]30193020 delete Object.prototype[pollutedKey]30213022 try {3023 // Simulates the real-world attack scenario where user input is Object.assign'd to draft3024 const userInput = {3025 constructor: {prototype: {[pollutedKey]: true}}3026 }30273028 let hadError = false3029 try {3030 produce({}, draft => {3031 Object.assign(draft, userInput)3032 })3033 } catch (e) {3034 hadError = true3035 }30363037 // Must NOT pollute via Object.assign path3038 expect(Object.prototype[pollutedKey]).toBeUndefined()3039 } finally {3040 if (original === undefined) {3041 delete Object.prototype[pollutedKey]3042 } else {3043 Object.prototype[pollutedKey] = original3044 }3045 }3046 })30472956 it("should handle equality correctly - 1", () => {2957 const baseState = {2958 y: 3 / 0,2959 z: NaN
1221 unmodified lines
modified:.vscode/settings.json+1 added-4 removed
modified:src/core/proxy.ts-44 removed
108 unmodified lines
109export const objectTraps: ProxyHandler<ProxyState> = {110 get(state, prop) {111 if (prop === DRAFT_STATE) return state112113 // Guard against prototype pollution via constructor and __proto__114 // We allow access but wrap in a proxy that blocks prototype chain traversal115 if (prop === "constructor" || prop === "__proto__") {116 const source = latest(state)117 const value = source[prop]118 // Return a proxy that allows calling the constructor but blocks access to prototype119 return new Proxy(value || {}, {120 get: (target, key) => {121 // Block __proto__ and prototype access chains122 if (key === "__proto__" || key === "prototype") {123 return Object.freeze(Object.create(null))124 }125 // Allow normal property access for legitimate use126 return Reflect.get(target, key)127 },128 set: () => {129 // Silently ignore writes to prevent pollution130 return true131 },132 apply: (target, thisArg, args) => {133 // Allow constructor to be called as a function (e.g., draft.arr.constructor(1))134 return Reflect.apply(target as Function, thisArg, args)135 }136 })137 }138113 let arrayPlugin = state.scope_.arrayMethodsPlugin_114 const isArrayWithStringProp =115 state.type_ === ArchType.Array && typeof prop === "string"116 // Intercept array methods so that we can override
39 unmodified lines
156 }157 return value158 },159 has(state, prop) {186 // Block reserved properties from being detected187 if (188 prop === "constructor" ||189 prop === "__proto__" ||190 prop === "prototype"191 ) {192 return false193 }160 return prop in latest(state)161 },162 ownKeys(state) {163 return Reflect.ownKeys(latest(state))
2 unmodified lines
166 state: ProxyObjectState,167 prop: string /* strictly not, but helps TS */,168 value169 ) {204 // Guard against prototype pollution - prevent assignment to reserved properties205 // that could lead to Object.prototype pollution206 if (207 prop === "constructor" ||208 prop === "__proto__" ||209 prop === "prototype"210 ) {211 return true212 }213170 const desc = getDescriptorFromProto(latest(state), prop)171 if (desc?.set) {172 // special case: if this write is captured by a setter, we have173 // to trigger it with the correct context
163 unmodified lines