Blame view

天文台pc/tianwentai-ui/node_modules/lodash/_baseUnset.js 1.77 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  var castPath = require('./_castPath'),
      last = require('./last'),
      parent = require('./_parent'),
      toKey = require('./_toKey');
  
  /** Used for built-in method references. */
  var objectProto = Object.prototype;
  
  /** Used to check objects for own properties. */
  var hasOwnProperty = objectProto.hasOwnProperty;
  
  /**
   * The base implementation of `_.unset`.
   *
   * @private
   * @param {Object} object The object to modify.
   * @param {Array|string} path The property path to unset.
   * @returns {boolean} Returns `true` if the property is deleted, else `false`.
   */
  function baseUnset(object, path) {
    path = castPath(path, object);
  
    // Prevent prototype pollution, see: https://github.com/lodash/lodash/security/advisories/GHSA-xxjr-mmjv-4gpg
    var index = -1,
        length = path.length;
  
    if (!length) {
      return true;
    }
  
    var isRootPrimitive = object == null || (typeof object !== 'object' && typeof object !== 'function');
  
    while (++index < length) {
      var key = path[index];
  
      // skip non-string keys (e.g., Symbols, numbers)
      if (typeof key !== 'string') {
        continue;
      }
  
      // Always block "__proto__" anywhere in the path if it's not expected
      if (key === '__proto__' && !hasOwnProperty.call(object, '__proto__')) {
        return false;
      }
  
      // Block "constructor.prototype" chains
      if (key === 'constructor' &&
          (index + 1) < length &&
          typeof path[index + 1] === 'string' &&
          path[index + 1] === 'prototype') {
  
        // Allow ONLY when the path starts at a primitive root, e.g., _.unset(0, 'constructor.prototype.a')
        if (isRootPrimitive && index === 0) {
          continue;
        }
  
        return false;
      }
    }
  
    var obj = parent(object, path);
    return obj == null || delete obj[toKey(last(path))];
  }
  
  module.exports = baseUnset;