patches.ts
This file
Path
src/plugins/patches.ts
304 lines · 7.1 KiB · TypeScript
Last changed by
fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–26why, one step back1import {2 ImmerState,3 Patch,4 SetState,5 ES5ArrayState,6 ProxyArrayState,7 MapState,8 ES5ObjectState,9 ProxyObjectState,10 PatchPath,11 get,12 each,13 has,14 getArchtype,15 isSet,16 isMap,17 loadPlugin,18 ProxyTypeProxyObject,19 ProxyTypeES5Object,20 ProxyTypeMap,21 ProxyTypeES5Array,22 ProxyTypeProxyArray,23 ProxyTypeSet,24 ArchtypeMap,25 ArchtypeSet,26 ArchtypeArray,Immer 7Michel Weststratebefore adoption467ea5d
line 27why, one step back27 die,fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 28why, one step back28 isDraft,fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 29–30why, one step back29 isDraftable,30 ArchtypeObjectMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 31–83why, one step back31} from "../internal"3233export function enablePatches() {34 const REPLACE = "replace"35 const ADD = "add"36 const REMOVE = "remove"3738 function generatePatches_(39 state: ImmerState,40 basePath: PatchPath,41 patches: Patch[],42 inversePatches: Patch[]43 ): void {44 switch (state.type_) {45 case ProxyTypeProxyObject:46 case ProxyTypeES5Object:47 case ProxyTypeMap:48 return generatePatchesFromAssigned(49 state,50 basePath,51 patches,52 inversePatches53 )54 case ProxyTypeES5Array:55 case ProxyTypeProxyArray:56 return generateArrayPatches(state, basePath, patches, inversePatches)57 case ProxyTypeSet:58 return generateSetPatches(59 (state as any) as SetState,60 basePath,61 patches,62 inversePatches63 )64 }65 }6667 function generateArrayPatches(68 state: ES5ArrayState | ProxyArrayState,69 basePath: PatchPath,70 patches: Patch[],71 inversePatches: Patch[]72 ) {73 let {base_, assigned_} = state74 let copy_ = state.copy_!7576 // Reduce complexity by ensuring `base` is never longer.77 if (copy_.length < base_.length) {78 // @ts-ignore79 ;[base_, copy_] = [copy_, base_]80 ;[patches, inversePatches] = [inversePatches, patches]81 }8283 // Process replaced indices.Immer 7Michel Weststratebefore adoption467ea5d
line 84why, one step back84 for (let i = 0; i < base_.length; i++) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 85–89why, one step back85 if (assigned_[i] && copy_[i] !== base_[i]) {86 const path = basePath.concat([i])87 patches.push({88 op: REPLACE,89 path,fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 90–92why, one step back90 // Need to maybe clone it, as it can in fact be the original value91 // due to the base/copy inversion at the start of this function92 value: clonePatchValueIfNeeded(copy_[i])Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 93–96why, one step back93 })94 inversePatches.push({95 op: REPLACE,96 path,fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
line 97why, one step back97 value: clonePatchValueIfNeeded(base_[i])Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 98–102why, one step back98 })99 }100 }101102 // Process added indices.Immer 7Michel Weststratebefore adoption467ea5d
line 103why, one step back103 for (let i = base_.length; i < copy_.length; i++) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 104why, one step back104 const path = basePath.concat([i])Immer 7Michel Weststratebefore adoption467ea5d
line 105why, one step back105 patches.push({Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 106–107why, one step back106 op: ADD,107 path,fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 108–110why, one step back108 // Need to maybe clone it, as it can in fact be the original value109 // due to the base/copy inversion at the start of this function110 value: clonePatchValueIfNeeded(copy_[i])Immer 7Michel Weststratebefore adoption467ea5d
lines 111–113why, one step back111 })112 }113 if (base_.length < copy_.length) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 114why, one step back114 inversePatches.push({Immer 7Michel Weststratebefore adoption467ea5d
lines 115–117why, one step back115 op: REPLACE,116 path: basePath.concat(["length"]),117 value: base_.lengthMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 118–140why, one step back118 })119 }120 }121122 // This is used for both Map objects and normal objects.123 function generatePatchesFromAssigned(124 state: MapState | ES5ObjectState | ProxyObjectState,125 basePath: PatchPath,126 patches: Patch[],127 inversePatches: Patch[]128 ) {129 const {base_, copy_} = state130 each(state.assigned_!, (key, assignedValue) => {131 const origValue = get(base_, key)132 const value = get(copy_!, key)133 const op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD134 if (origValue === value && op === REPLACE) return135 const path = basePath.concat(key as any)136 patches.push(op === REMOVE ? {op, path} : {op, path, value})137 inversePatches.push(138 op === ADD139 ? {op: REMOVE, path}140 : op === REMOVEfix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 141–142why, one step back141 ? {op: ADD, path, value: clonePatchValueIfNeeded(origValue)}142 : {op: REPLACE, path, value: clonePatchValueIfNeeded(origValue)}Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 143–214why, one step back143 )144 })145 }146147 function generateSetPatches(148 state: SetState,149 basePath: PatchPath,150 patches: Patch[],151 inversePatches: Patch[]152 ) {153 let {base_, copy_} = state154155 let i = 0156 base_.forEach((value: any) => {157 if (!copy_!.has(value)) {158 const path = basePath.concat([i])159 patches.push({160 op: REMOVE,161 path,162 value163 })164 inversePatches.unshift({165 op: ADD,166 path,167 value168 })169 }170 i++171 })172 i = 0173 copy_!.forEach((value: any) => {174 if (!base_.has(value)) {175 const path = basePath.concat([i])176 patches.push({177 op: ADD,178 path,179 value180 })181 inversePatches.unshift({182 op: REMOVE,183 path,184 value185 })186 }187 i++188 })189 }190191 function generateReplacementPatches_(192 rootState: ImmerState,193 replacement: any,194 patches: Patch[],195 inversePatches: Patch[]196 ): void {197 patches.push({198 op: REPLACE,199 path: [],200 value: replacement201 })202 inversePatches.push({203 op: REPLACE,204 path: [],205 value: rootState.base_206 })207 }208209 function applyPatches_<T>(draft: T, patches: Patch[]): T {210 patches.forEach(patch => {211 const {path, op} = patch212213 let base: any = draft214 for (let i = 0; i < path.length - 1; i++) {fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 215–224why, one step back215 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)224 base = get(base, p)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 225–280why, one step back225 if (typeof base !== "object") die(15, path.join("/"))226 }227228 const type = getArchtype(base)229 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411230 const key = path[path.length - 1]231 switch (op) {232 case REPLACE:233 switch (type) {234 case ArchtypeMap:235 return base.set(key, value)236 /* istanbul ignore next */237 case ArchtypeSet:238 die(16)239 default:240 // if value is an object, then it's assigned by reference241 // in the following add or remove ops, the value field inside the patch will also be modifyed242 // so we use value from the cloned patch243 // @ts-ignore244 return (base[key] = value)245 }246 case ADD:247 switch (type) {248 case ArchtypeArray:249 return base.splice(key as any, 0, value)250 case ArchtypeMap:251 return base.set(key, value)252 case ArchtypeSet:253 return base.add(value)254 default:255 return (base[key] = value)256 }257 case REMOVE:258 switch (type) {259 case ArchtypeArray:260 return base.splice(key as any, 1)261 case ArchtypeMap:262 return base.delete(key)263 case ArchtypeSet:264 return base.delete(patch.value)265 default:266 return delete base[key]267 }268 default:269 die(17, op)270 }271 })272273 return draft274 }275276 // optimize: this is quite a performance hit, can we detect intelligently when it is needed?277 // E.g. auto-draft when new objects from outside are assigned and modified?278 // (See failing test when deepClone just returns obj)279 function deepClonePatchValue<T>(obj: T): T280 function deepClonePatchValue(obj: any) {fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 281why, one step back281 if (!isDraftable(obj)) return objMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 282–290why, one step back282 if (Array.isArray(obj)) return obj.map(deepClonePatchValue)283 if (isMap(obj))284 return new Map(285 Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])286 )287 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))288 const cloned = Object.create(Object.getPrototypeOf(obj))289 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])290 return clonedfix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 291–296why, one step back291 }292293 function clonePatchValueIfNeeded<T>(obj: T): T {294 if (isDraft(obj)) {295 return deepClonePatchValue(obj)296 } else return objMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 297–304why, one step back297 }298299 loadPlugin("Patches", {300 applyPatches_,301 generatePatches_,302 generateReplacementPatches_303 })304}