let’s say I have a code as a following:
object[hash].init();
Please assume that hash comes from the website hash (www.example.com#hash).
Is there any possible security holes in the code I displayed above? For instance, is it possible that I could put any code in the #hash that injects and trigger any malicious code?
Thanks.
This will be no problem,
hashwill not be evaluated as JavaScript code, it will be treated as a string. In case the value ofhashis not a valid property name forobject,object[hash]will returnundefinedand you will get an error (not being able to callinitonundefined).On the other hand, if you use
hashin a way where strings are evaluated as JavaScript code, then you have a security problem.So this is fine:
but this is not (even with inner quotes):
For example if
hashwas a string containingThen
alert('foo');would be executed. It would be possible to inject and execute code without breaking your code.That said, I would still not do it this way. It ties the code too much together. I would probable create a map of functions (which is similar to your example, but not the same):