Re:Server-Side JavaScript Injection
by firefoxman1 on Friday October 19, 2012 @12:47AM
(#4672309)
It's funny how we've been taught for years not to use eval for "security reasons" and I always thought "eh, how much harm can someone do in their own browser? But I won't use eval anyway."
...then Node came along and I vowed to never consider eval even an option. I mean, that's a HUGE risk if you eval any user input on the server side. In fact, the only valid use I've seen for eval is template engines like Jade, but they don't touch user input.
Re:Server-Side JavaScript Injection
by justjohn on Friday October 19, 2012 @12:09AM
(#4672223)
It's interesting to see SQL injection techniques reworked to attack Mongo, but not really surprising. Not following good coding practices is going to cause problems regardless the language. Nosql, SSJS and the like won't protect you from yourself if you don't sanitize user input.
Re:Server-Side JavaScript Injection
by kevincennis on Thursday October 18, 2012 @11:49PM
(#4672195)
It can be nice for quick and dirty things like this:
function sum(numericArray){return eval(numericArray.join('+'))}
Of course, that assumes you're smart enough not to use it on arrays that might contain user input, or you'll end up being susceptible to things like sum(['alert("pwned")']).
Re:Server-Side JavaScript Injection
by wladimir on Friday October 19, 2012 @01:55AM
(#4672439)
That code takes a terrible detour. To do a simple reduction, you build a STRING, hand it over to the parser, which creates an expression, which is converted to bytecode and then it can be interpreted/JIT-ed.
I can't imagine it actually being faster than normal array functions, and if it's the case it yet again proved what an abysmal language JS is :-)
But seriously, don't use eval this way. The array may not seem to contain user input on first glance, but it's very fragile: a small bug, a change to the code (for example, by another programmer, or by you when you're not paying attention) can easily cause it to be.
You're never "smart enough" to go around basic security guidelines to gain some (questionable) speed.
I think it's a good question by the grandparent what eval is good for. Arguably it allows for some powerful paradigms such as generating code at run time, but similar things can be done with function closures and eval is just too often abused for parsing (wasn't there window.JSON.parse for that?)
Re:Server-Side JavaScript Injection
by kevincennis on Friday October 19, 2012 @01:53PM
(#4674636)
All I'm saying is that if I'm putting together a quick demo or proof of concept or reduced test case, I'm always going to favor "easy" over "secure". And sometimes using eval is easier.
...then Node came along and I vowed to never consider eval even an option. I mean, that's a HUGE risk if you eval any user input on the server side. In fact, the only valid use I've seen for eval is template engines like Jade, but they don't touch user input.
Does node provide anything similar?
Why use map/reduce for user interaction queries. Since we have mongo's JSON based querying.
Btw. Mongo can also vurnable to JSON queries with taking query JSON directly from the user.
function sum(numericArray){return eval(numericArray.join('+'))}
Of course, that assumes you're smart enough not to use it on arrays that might contain user input, or you'll end up being susceptible to things like sum(['alert("pwned")']).
I can't imagine it actually being faster than normal array functions, and if it's the case it yet again proved what an abysmal language JS is :-)
But seriously, don't use eval this way. The array may not seem to contain user input on first glance, but it's very fragile: a small bug, a change to the code (for example, by another programmer, or by you when you're not paying attention) can easily cause it to be.
You're never "smart enough" to go around basic security guidelines to gain some (questionable) speed.
I think it's a good question by the grandparent what eval is good for. Arguably it allows for some powerful paradigms such as generating code at run time, but similar things can be done with function closures and eval is just too often abused for parsing (wasn't there window.JSON.parse for that?)
By the way, the performance of evaling an array that way is abysmal: http://jsperf.com/array-summing-loop-vs-eval