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
Provenance
- Commit
da2bd4f- Tree
da229b6- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
d75de70
3 files changed+123 added−3 removed
modified:__tests__/patch.js+109 added
10 unmodified lines11 enableAllPlugins()12 13 jest.setTimeout(1000)14 15+const isProd = process.env.NODE_ENV === "production"16+17 function runPatchTest(base, producer, patches, inversePathes) {18 let resultProxies, resultEs519 20 function runPatchTestHelper() {1127 unmodified lines1148 `"2020-11-10T08:08:08.003Z"`1149 )1150 expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))1151 })1152+1153+test("do not allow __proto__ polution - 738", () => {1154+ const obj = {}1155+1156+ // @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+})1170+1171+test("do not allow __proto__ polution using arrays - 738", () => {1172+ const obj = {}1173+ const ar = []1174+1175+ // @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+})1194+1195+test("do not allow prototype polution - 738", () => {1196+ const obj = {}1197+1198+ // @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+})1212+1213+test("do not allow constructor polution - 738", () => {1214+ const obj = {}1215+1216+ // @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+})1224+1225+test("do not allow constructor.prototype polution - 738", () => {1226+ const obj = {}1227+1228+ // @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+})1242+1243+test("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 lines25 ArchtypeSet,26 ArchtypeArray,27 die,28 isDraft,29- isDraftable29+ isDraftable,30+ ArchtypeObject31 } from "../internal"32 33 export function enablePatches() {34 const REPLACE = "replace"176 unmodified lines211 const {path, op} = patch212 213 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 }227 228 const type = getArchtype(base)76 unmodified linesmodified:src/utils/errors.ts+2 added-1 removed
36 unmodified lines373839404141424344454614 unmodified lines36 unmodified lines return `'current' expects a draft, got: ${thing}` }, 23(thing: string) { return `'original' expects a draft, got: ${thing}` } }, 24: "Patching reserved attributes like __proto__, prototype and constructor is not allowed"} as const
export function die(error: keyof typeof errors, ...args: any[]): never { if (__DEV__) {14 unmodified lines