Securely set unknown property (mitigate square bracket object injection attacks) utility function

后端 未结 1 1443
-上瘾入骨i
-上瘾入骨i 2021-02-04 06:57

After setting up eslint-plugin-security, I went on to attempt to address nearly 400 uses of square brackets in our javascript codebase (flagged by the rule security/detect-objec

1条回答
  •  孤街浪徒
    2021-02-04 07:25

    It is more important to prevent a key from being accessed on the wrong object than to validate/protect object keys themselves. Designating certain object keys as 'unsafe' and avoiding accessing just these regardless of the circumstances is just another form of the 'sanitizing' anti-pattern. If the object doesn't contain sensitive data in the first place, there is no risk of it being exfiltrated or modified by untrusted inputs. You don't need to worry about accessing src or innerHTML if you don't access it on a DOM node; you don't need to worry about exposing eval if you don't perform lookups on the global object. As such:

    • Only use bracket notation with objects that either are arrays or are specifically used to contain arbitrary key-value mappings and no other things, especially no methods (the latter kind of object I'm going to call map-like below). For such objects, also ensure the following:
      • that the values you assign in a map-like object are never functions (to avoid problems with a toJSON key or anything similar);
      • that you never directly convert map-like objects to strings using the toString method, the String constructor, type coercion, Array.prototype.join or other built-in mechanism (to avoid problems with a toString key); if you ever need to convert map-like objects to strings, write a function explicitly created for the purpose, without adding it as a method to the object.
    • When accessing arrays, make sure the index is indeed an integer. Consider also using built-in methods like push, forEach, map or filter that avoid explicit indexing altogether; this will reduce the number of places you will need to audit.
    • If you ever need to break the above rule and use the bracket notation with objects with a relatively fixed set of keys, e.g. DOM nodes, window or an object you defined with class (all of which I'll call class-like below), either make sure the keys come from trusted sources or filter/validate them.
      • In particular, if you want to associate a class-like object with data whose lifetime should be tied to that object, either use WeakMap or (if that isn't available) put it on a key you control. (If you have more than one piece of such data that you want to keep together, you may put it inside a map-like object, itself stored on a key controlled by you.)

    Unfortunately, as you noticed, the prototype chain (in particular, Object.prototype) complicates things here somewhat: it defines property descriptors that are by default available on any object. Particularly worrying are constructor and various built-in methods (which can be leveraged to access the Function object, and ultimately perform arbitrary code execution), and __proto__ (which can be used to modify the prototype chain of an object).

    Below I list some strategies you may consider employing to mitigate the problem of arbitrary string keys clashing with built-ins in Object.prototype. They are not mutually exclusive, but for the sake of consistency it may be preferable to stick with just one.

    • Just transform all keys: this is probably the (conceptually) simplest option, portable even to engines from the days of ECMAScript 3, and robust even against future additions to Object.prototype (as unlikely as they are). Simply prepend a single non-identifier character to all keys in map-like objects; this will safely namespace away untrusted keys from all reasonably conceivable JavaScript built-ins (which presumably should have names that are valid identifiers). When accessing map-like objects, check for this character and strip it as appropriate. Following this strategy will even make concerns about methods like toJSON or toString mostly irrelevant. A drawback of this approach is that the prefix character is going to end up in JSON, if you ever serialise such a map-like object.

    • Enforce performing property accesses directly on the object. Read accesses can be guarded with Object.prototype.hasOwnProperty, which will stop exfiltration vulnerabilities, but will not protect you from inadvertently writing to __proto__. If you never mutate such a map-like object, this should not be a problem. You can even enforce immutability using Object.seal.

      If you insist though, you may perform property writes via Object.defineProperty, available since ECMAScript 5, which can create properties directly on the object, without involving getters and setters.

      In short, you can simply proxy all property accesses through these two functions:

    function rawget(obj, key) {
        if (Object.prototype.hasOwnProperty.call(obj, key))
            return obj[key];
    }
    
    function rawset(obj, key, val) {
        Object.defineProperty(obj, key, {
            value: val,
            writable: true,
            enumerable: true,
            configurable: true
        });
        return val;
    }
    
    • Strip away Object.prototype: make sure map-like objects have an empty prototype chain. Create them via Object.create(null) (available since ECMAScript 5) instead of {}. If you created them via direct object literals before, you can wrap them in Object.assign(Object.create(null), { /* ... */ }) (Object.assign is available since ECMAScript 6, but easily shimmable to earlier versions). If you follow this approach, you can use the bracket notation as usual; the only code you need to check is where you construct the map-like object.

      Unfortunately though, objects created by JSON.parse by default will still inherit from Object.prototype (although modern engines at least add a JSON key like __proto__ directly on the constructed object itself, bypassing the setter from the prototype's descriptor). You can either treat such objects as read-only, and guard read accesses by hasOwnProperty (as above), or strip away their prototypes by writing a reviver function that calls Object.setPrototypeOf. A reviver function can also use Object.seal to make an object immutable.

    • Use Maps instead of map-like objects: using Map (available since ECMAScript 6) allows you to use keys other than strings, which isn't possible with plain objects; but even just with string keys you have the benefit that entries of the map are completely isolated from the prototype chain of the map object itself. Items in Maps are accessed by .get and .set methods instead of the bracket notation and cannot clash with properties at all: the keys exist in a separate namespace.

      There is however the problem that a Map cannot be directly serialized into JSON. You can remedy this by writing a replacer function for JSON.stringify that converts Maps into plain, prototype-free map-like objects, and a reviver function for JSON.parse that turns plain objects back into Maps. Then again, naïvely reviving every JSON object into a Map is also going to cover class-like objects, which you probably don't want. To distinguish between them, you might need to add some kind of schema parameter to your JSON-parsing function.

    If you ask me for my preference: use Map if you don't need to worry about pre-ES6 engines or JSON serialisation; otherwise use Object.create(null); and if you need to work with legacy JS engines where neither is possible, transform all keys (first option) and hope for the best.

    Now, can all this discipline be enforced mechanically? I am not aware of a tool which does that. Presumably such a tool would allow annotating variables as containing map-like objects for which bracket accesses are valid, and probably also allow annotating properties of other objects so that map-like objects not directly contained in variables are also covered. Maybe it should even propagate such annotations through function calls. It should also ensure that such map-like objects are properly constructed, without a prototype. At this point, you're pretty much writing a type checker, so perhaps TypeScript will be worth checking out; then though, I am not aware if TypeScript is capable of this either.

    0 讨论(0)
提交回复
热议问题