patches.ts
This file
Path
src/plugins/patches.ts
306 lines · 7.2 KiB · TypeScript
Last changed by
fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01
History of this fileTake it
fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01
line 1why, one step back1import {immerable} from "../immer"Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 2–27why, one step back2import {3 ImmerState,4 Patch,5 SetState,6 ES5ArrayState,7 ProxyArrayState,8 MapState,9 ES5ObjectState,10 ProxyObjectState,11 PatchPath,12 get,13 each,14 has,15 getArchtype,16 isSet,17 isMap,18 loadPlugin,19 ProxyTypeProxyObject,20 ProxyTypeES5Object,21 ProxyTypeMap,22 ProxyTypeES5Array,23 ProxyTypeProxyArray,24 ProxyTypeSet,25 ArchtypeMap,26 ArchtypeSet,27 ArchtypeArray,Immer 7Michel Weststratebefore adoption467ea5d
line 28why, one step back28 die,fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 29why, one step back29 isDraft,fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 30–31why, one step back30 isDraftable,31 ArchtypeObjectMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 32–84why, one step back32} from "../internal"3334export function enablePatches() {35 const REPLACE = "replace"36 const ADD = "add"37 const REMOVE = "remove"3839 function generatePatches_(40 state: ImmerState,41 basePath: PatchPath,42 patches: Patch[],43 inversePatches: Patch[]44 ): void {45 switch (state.type_) {46 case ProxyTypeProxyObject:47 case ProxyTypeES5Object:48 case ProxyTypeMap:49 return generatePatchesFromAssigned(50 state,51 basePath,52 patches,53 inversePatches54 )55 case ProxyTypeES5Array:56 case ProxyTypeProxyArray:57 return generateArrayPatches(state, basePath, patches, inversePatches)58 case ProxyTypeSet:59 return generateSetPatches(60 (state as any) as SetState,61 basePath,62 patches,63 inversePatches64 )65 }66 }6768 function generateArrayPatches(69 state: ES5ArrayState | ProxyArrayState,70 basePath: PatchPath,71 patches: Patch[],72 inversePatches: Patch[]73 ) {74 let {base_, assigned_} = state75 let copy_ = state.copy_!7677 // Reduce complexity by ensuring `base` is never longer.78 if (copy_.length < base_.length) {79 // @ts-ignore80 ;[base_, copy_] = [copy_, base_]81 ;[patches, inversePatches] = [inversePatches, patches]82 }8384 // Process replaced indices.Immer 7Michel Weststratebefore adoption467ea5d
line 85why, one step back85 for (let i = 0; i < base_.length; i++) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 86–90why, one step back86 if (assigned_[i] && copy_[i] !== base_[i]) {87 const path = basePath.concat([i])88 patches.push({89 op: REPLACE,90 path,fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 91–93why, one step back91 // Need to maybe clone it, as it can in fact be the original value92 // due to the base/copy inversion at the start of this function93 value: clonePatchValueIfNeeded(copy_[i])Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 94–97why, one step back94 })95 inversePatches.push({96 op: REPLACE,97 path,fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
line 98why, one step back98 value: clonePatchValueIfNeeded(base_[i])Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 99–103why, one step back99 })100 }101 }102103 // Process added indices.Immer 7Michel Weststratebefore adoption467ea5d
line 104why, one step back104 for (let i = base_.length; i < copy_.length; i++) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 105why, one step back105 const path = basePath.concat([i])Immer 7Michel Weststratebefore adoption467ea5d
line 106why, one step back106 patches.push({Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 107–108why, one step back107 op: ADD,108 path,fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 109–111why, one step back109 // Need to maybe clone it, as it can in fact be the original value110 // due to the base/copy inversion at the start of this function111 value: clonePatchValueIfNeeded(copy_[i])Immer 7Michel Weststratebefore adoption467ea5d
lines 112–114why, one step back112 })113 }114 if (base_.length < copy_.length) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 115why, one step back115 inversePatches.push({Immer 7Michel Weststratebefore adoption467ea5d
lines 116–118why, one step back116 op: REPLACE,117 path: basePath.concat(["length"]),118 value: base_.lengthMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 119–141why, one step back119 })120 }121 }122123 // This is used for both Map objects and normal objects.124 function generatePatchesFromAssigned(125 state: MapState | ES5ObjectState | ProxyObjectState,126 basePath: PatchPath,127 patches: Patch[],128 inversePatches: Patch[]129 ) {130 const {base_, copy_} = state131 each(state.assigned_!, (key, assignedValue) => {132 const origValue = get(base_, key)133 const value = get(copy_!, key)134 const op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD135 if (origValue === value && op === REPLACE) return136 const path = basePath.concat(key as any)137 patches.push(op === REMOVE ? {op, path} : {op, path, value})138 inversePatches.push(139 op === ADD140 ? {op: REMOVE, path}141 : op === REMOVEfix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 142–143why, one step back142 ? {op: ADD, path, value: clonePatchValueIfNeeded(origValue)}143 : {op: REPLACE, path, value: clonePatchValueIfNeeded(origValue)}Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 144–215why, one step back144 )145 })146 }147148 function generateSetPatches(149 state: SetState,150 basePath: PatchPath,151 patches: Patch[],152 inversePatches: Patch[]153 ) {154 let {base_, copy_} = state155156 let i = 0157 base_.forEach((value: any) => {158 if (!copy_!.has(value)) {159 const path = basePath.concat([i])160 patches.push({161 op: REMOVE,162 path,163 value164 })165 inversePatches.unshift({166 op: ADD,167 path,168 value169 })170 }171 i++172 })173 i = 0174 copy_!.forEach((value: any) => {175 if (!base_.has(value)) {176 const path = basePath.concat([i])177 patches.push({178 op: ADD,179 path,180 value181 })182 inversePatches.unshift({183 op: REMOVE,184 path,185 value186 })187 }188 i++189 })190 }191192 function generateReplacementPatches_(193 rootState: ImmerState,194 replacement: any,195 patches: Patch[],196 inversePatches: Patch[]197 ): void {198 patches.push({199 op: REPLACE,200 path: [],201 value: replacement202 })203 inversePatches.push({204 op: REPLACE,205 path: [],206 value: rootState.base_207 })208 }209210 function applyPatches_<T>(draft: T, patches: Patch[]): T {211 patches.forEach(patch => {212 const {path, op} = patch213214 let base: any = draft215 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 216–225why, one step back216 const parentType = getArchtype(base)217 const p = path[i]218 // See #738, avoid prototype pollution219 if (220 (parentType === ArchtypeObject || parentType === ArchtypeArray) &&221 (p === "__proto__" || p === "constructor")222 )223 die(24)224 if (typeof base === "function" && p === "prototype") die(24)225 base = get(base, p)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 226–281why, one step back226 if (typeof base !== "object") die(15, path.join("/"))227 }228229 const type = getArchtype(base)230 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411231 const key = path[path.length - 1]232 switch (op) {233 case REPLACE:234 switch (type) {235 case ArchtypeMap:236 return base.set(key, value)237 /* istanbul ignore next */238 case ArchtypeSet:239 die(16)240 default:241 // if value is an object, then it's assigned by reference242 // in the following add or remove ops, the value field inside the patch will also be modifyed243 // so we use value from the cloned patch244 // @ts-ignore245 return (base[key] = value)246 }247 case ADD:248 switch (type) {249 case ArchtypeArray:250 return base.splice(key as any, 0, value)251 case ArchtypeMap:252 return base.set(key, value)253 case ArchtypeSet:254 return base.add(value)255 default:256 return (base[key] = value)257 }258 case REMOVE:259 switch (type) {260 case ArchtypeArray:261 return base.splice(key as any, 1)262 case ArchtypeMap:263 return base.delete(key)264 case ArchtypeSet:265 return base.delete(patch.value)266 default:267 return delete base[key]268 }269 default:270 die(17, op)271 }272 })273274 return draft275 }276277 // optimize: this is quite a performance hit, can we detect intelligently when it is needed?278 // E.g. auto-draft when new objects from outside are assigned and modified?279 // (See failing test when deepClone just returns obj)280 function deepClonePatchValue<T>(obj: T): T281 function deepClonePatchValue(obj: any) {fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 282why, one step back282 if (!isDraftable(obj)) return objMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 283–290why, one step back283 if (Array.isArray(obj)) return obj.map(deepClonePatchValue)284 if (isMap(obj))285 return new Map(286 Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])287 )288 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))289 const cloned = Object.create(Object.getPrototypeOf(obj))290 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01
line 291why, one step back291 if (has(obj, immerable)) cloned[immerable] = obj[immerable]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 292why, one step back292 return clonedfix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 293–298why, one step back293 }294295 function clonePatchValueIfNeeded<T>(obj: T): T {296 if (isDraft(obj)) {297 return deepClonePatchValue(obj)298 } else return objMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 299–306why, one step back299 }300301 loadPlugin("Patches", {302 applyPatches_,303 generatePatches_,304 generateReplacementPatches_305 })306}