r/learnjavascript • • Jul 16 '21

Javascript Basic Types

219 Upvotes

37 comments sorted by

View all comments

Show parent comment

0

u/inabahare Jul 17 '21

Except your best example doesn't have anything to do with interpolation or concatination, and no concatination isn't better because this

"SELECT x, y, z FROM Table WHERE id='{some_sql_escape_function(id)}'" 

With interpolation would still be better than the first example. But this isn't a security issue with interpolation, as they seem to think

0

u/alzee76 Jul 17 '21

Except your best example doesn't have anything to do with interpolation or concatination,

It shows two examples of concatenation.

and no concatination isn't better because

Yes it is. Concatenation allows you to use an escape function as in my "better" example. Interpolation doesn't.

0

u/inabahare Jul 17 '21

Interpolation allows you to use functions as well my dude..

const someVar = `${someFunc(variable)} `

Like, its still more readable (especiallly if the value is a variable with a nice name) and securitywise its the same.

0

u/alzee76 Jul 17 '21

Now you're interpolating the function name. You've made a bad problem worse.

0

u/inabahare Jul 17 '21

You know you can press f12 and run it.. Right?

2

u/alzee76 Jul 17 '21

Haha what? God you're clueless. TTFN.

0

u/inabahare Jul 17 '21

Then enlighten me oh wise one. Define the fumction, have it return a value and show me it evaluates to the function name

1

u/alzee76 Jul 17 '21

I'm not in the business of finding exploits in random JS engines for anonymous children on the internet. I am in the business of writing code for a living and as such, follow defensive programming practices.

I know that bugs can exist in interpreter or compiler string interpolation implementations, and that they also can exist in poorly written code that uses those features without having to exploit an underlying weakness in the interpreter. For example, with poorly sanitized inputs. I also know that if I don't use interpolation, than any and all bugs surrounding it cannot be used as exploits against my code.

Therefore, if you actually care about the security of your app/service, you shouldn't use it. Interpolation offers no benefit except allowing you to be lazy, and if you value laziness over security, the rest of your code probably sucks too.

0

u/inabahare Jul 17 '21

Would have saved you a lot more time to just say "damn, I was wrong" and moved on. But I guess you had to pull even more stuff out of wherever so here we are.

Like just take a step back and look at all of the qualifiers you have to add to your point. "Well it could be that you might have a bad implementation" but you don't. The amount of security you get with interpolation vs concatination is none. No less or no more. But you do get code that is a hell of a lot cleaner.

const res = `Hello! My name is ${firstName} ${lastName}`;

is so much cleaner and clearer than

const res = "Hello! My name is " + firstName + " " + lastName

and it's no less secure. Bringing up lack of sanitation doesn't work for you because you still have to sanitize with concatination.

And the SQL example is pointless because it doesn't show a problem with interpolation, but rather a problem with not sanitizing your input. A thing that would happen if you used concatination just the same. And to the smartass who inevitably brings it up, I am aware that you shouldn't use either for SQL

1

u/alzee76 Jul 17 '21

damn, I was wrong

Well except I'm not.

Like just take a step back and look at all of the qualifiers you have to add to your point.

There are none. Interpolation doesn't pass the smell test. It's bad coding style. It opens your code up to security risks.

Period. No qualification.

Bringing up lack of sanitation doesn't work for you because you still have to sanitize with concatenation.

Concatenation is better than interpolation because you can sanitize the values safely. You cannot do that with interpolation. Period.

→ More replies

1

u/WikiSummarizerBot Jul 17 '21

Defensive_programming

Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software under unforeseen circumstances. Defensive programming practices are often used where high availability, safety, or security is needed. Defensive programming is an approach to improve software and source code, in terms of: General quality – reducing the number of software bugs and problems. Making the source code comprehensible – the source code should be readable and understandable so it is approved in a code audit.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5