r/ProgrammerAnimemes • • Jan 01 '20

document.all

Post image
979 Upvotes

29 comments sorted by

View all comments

Show parent comment

1

u/slakkenhuisdeur Jan 01 '20

From your example I understand that if you don't handle user input and/or properly check for illegal values (which you should always do) it is preferred to use an object. Your example could be amended with below snippet to close this specific instance of improper user input sanitation.

function registerPlugin(name, plugin) {
  if (!utils.isLegalProperty(name)) {
    throw new Error("Could not register plugin with name: " + name);
  }
  registry[name] = plugin;
}

I might underplay the issue because I consider frontend an untrusted environment where anything can happen and all payloads to the backend must be double-checked.

Also, I think you mean console.log({}.exploited);. In your example calling registerPlugin added it to the registry variable. If you meant another instance of the enclosing class, I think it should only work with newly constructed instances.

1

u/bucket3432 Jan 01 '20

From your example I understand that if you don't handle user input and/or properly check for illegal values (which you should always do) it is preferred to use an object.

If you have a whitelist, then this is easy, but blacklisting all undesired values is impractical at best. You may as well use a Map or a null-prototype object, which don't have this problem. For some cases, like this example, it may even be valid to have a key called toString or __proto__.

Like I said earlier, it gets trickier when you're doing more complex tasks like the recursive merging example in the link. It is possible to filter out some of the more dangerous keys like constructor and __proto__ like lodash does, but considering how many CVEs there have been for this kind of thing, it seems like it's easy to forget.

Whether it's preferred to use an object really depends on your use case and who will be using the dictionary. For the project that inspired my example earlier, it probably makes more sense to use a Map for its safety and because it's great for something like this, but we chose to use an object since it provided a nicer interface for the users of the registry (compare registry.pluginName for an object and registry.get("pluginName") for a Map). Of course, we backed the registry with a null-prototype object to protect against prototype pollution.

all payloads to the backend must be double-checked

Of course, but some of these objects don't make it to the back-end. Sometimes these are state objects that stay in the front-end.

Also, I think you mean console.log({}.exploited);.

I actually do mean console.log(registry.exploited);, though I realize now that my example doesn't illustrate the same kind of prototype pollution that the links I gave do. My example replaces the prototype of just the registry object and doesn't pollute Object.prototype. You'd get the latter if you were doing second-level assignment with something like obj[a][b], where a could be __proto__.

If you call the function like a normal user would, say with registerPlugin("myPlugin", myPlugin);, then you'd expect the registry to look like this: { myPlugin: { exploted: "haha" } }. Instead, because you assigned it to __proto__, the registry object continues to have no own properties and its prototype is replaced with { exploited: "haha" }.