Skip to content
Myr

fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477

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

See: CVE-2020-28477 / SNYK-JS-IMMER-1019369 https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-28477 https://snyk.io/vuln/SNYK-JS-IMMER-1019369

authored by Michel Weststrate · committed by GitHub · parent d75de70 · Browse files · 3 files+123 added−3 removed
This commit

State

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

Trailers

  • See: CVE-2020-28477 / SNYK-JS-IMMER-1019369
  • https: //cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-28477
  • https: //snyk.io/vuln/SNYK-JS-IMMER-1019369

Diff stats

3 files+123 added−3 removed

  • tests:+109 added−0 removed
  • other:+14 added−3 removed

3 files+123 added−3 removed

modified:__tests__/patch.js+109 added
10 unmodified lines
11enableAllPlugins()1213jest.setTimeout(1000)1415const isProd = process.env.NODE_ENV === "production"1617function runPatchTest(base, producer, patches, inversePathes) {18 let resultProxies, resultEs51920 function runPatchTestHelper() {
1127 unmodified lines
1148 `"2020-11-10T08:08:08.003Z"`1149 )1150 expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))1151})11521153test("do not allow __proto__ polution - 738", () => {1154 const obj = {}11551156 // @ts-ignore1157 expect(obj.polluted).toBe(undefined)1158 expect(() => {1159 applyPatches({}, [1160 {op: "add", path: ["__proto__", "polluted"], value: "yes"}1161 ])1162 }).toThrow(1163 isProd1164 ? "24"1165 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1166 )1167 // @ts-ignore1168 expect(obj.polluted).toBe(undefined)1169})11701171test("do not allow __proto__ polution using arrays - 738", () => {1172 const obj = {}1173 const ar = []11741175 // @ts-ignore1176 expect(obj.polluted).toBe(undefined)1177 // @ts-ignore1178 expect(ar.polluted).toBe(undefined)1179 expect(() => {1180 applyPatches(1181 [],1182 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]1183 )1184 }).toThrow(1185 isProd1186 ? "24"1187 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1188 )1189 // @ts-ignore1190 expect(obj.polluted).toBe(undefined)1191 // @ts-ignore1192 expect(ar.polluted).toBe(undefined)1193})11941195test("do not allow prototype polution - 738", () => {1196 const obj = {}11971198 // @ts-ignore1199 expect(obj.polluted).toBe(undefined)1200 expect(() => {1201 applyPatches(Object, [1202 {op: "add", path: ["prototype", "polluted"], value: "yes"}1203 ])1204 }).toThrow(1205 isProd1206 ? "24"1207 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1208 )1209 // @ts-ignore1210 expect(obj.polluted).toBe(undefined)1211})12121213test("do not allow constructor polution - 738", () => {1214 const obj = {}12151216 // @ts-ignore1217 expect(obj.polluted).toBe(undefined)1218 const t = {}1219 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1220 expect(typeof t.constructor).toBe("function")1221 // @ts-ignore1222 expect(Object.polluted).toBe(undefined)1223})12241225test("do not allow constructor.prototype polution - 738", () => {1226 const obj = {}12271228 // @ts-ignore1229 expect(obj.polluted).toBe(undefined)1230 expect(() => {1231 applyPatches({}, [1232 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1233 ])1234 }).toThrow(1235 isProd1236 ? "24"1237 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1238 )1239 // @ts-ignore1240 expect(Object.polluted).toBe(undefined)1241})12421243test("maps can store __proto__, prototype and constructor props", () => {1244 const obj = {}1245 const map = new Map()1246 map.set("__proto__", {})1247 map.set("constructor", {})1248 map.set("prototype", {})1249 const newMap = applyPatches(map, [1250 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1251 {op: "add", path: ["constructor", "polluted"], value: "yes"},1252 {op: "add", path: ["prototype", "polluted"], value: "yes"}1253 ])1254 expect(newMap.get("__proto__").polluted).toBe("yes")1255 expect(newMap.get("constructor").polluted).toBe("yes")1256 expect(newMap.get("prototype").polluted).toBe("yes")1257 expect(obj.polluted).toBe(undefined)1258})
modified:src/plugins/patches.ts+12 added-2 removed
24 unmodified lines
25 ArchtypeSet,26 ArchtypeArray,27 die,28 isDraft,29 isDraftable29 isDraftable,30 ArchtypeObject31} from "../internal"3233export function enablePatches() {34 const REPLACE = "replace"
176 unmodified lines
211 const {path, op} = patch212213 let base: any = draft214 for (let i = 0; i < path.length - 1; i++) {215 const parentType = getArchtype(base)216 const p = path[i]217 // See #738, avoid prototype pollution218 if (219 (parentType === ArchtypeObject || parentType === ArchtypeArray) &&220 (p === "__proto__" || p === "constructor")221 )222 die(24)223 if (typeof base === "function" && p === "prototype") die(24)214 base = get(base, path[i])224 base = get(base, p)225 if (typeof base !== "object") die(15, path.join("/"))226 }227228 const type = getArchtype(base)
76 unmodified lines
modified:src/utils/errors.ts+2 added-1 removed