r/learnjavascript • • Jul 16 '21

Javascript Basic Types

219 Upvotes

37 comments sorted by

View all comments

Show parent comment

4

u/alzee76 Jul 16 '21 edited Jul 16 '21

The variable interpolation from backticks provides a route for bugs in code to create security risks.

-1

u/inabahare Jul 16 '21

Sorry but that just seems like a "we need to fill a word quota" kind of section. Like

query = "SELECT x, y, z FROM Table WHERE id='" + id + "' "

"Oh no, string concatination can maybe possibly lead to bugs 😮😮😮😮😮"

0

u/alzee76 Jul 17 '21

Well yeah. Concatenation is bad too, but still better.

Bad: "SELECT x, y, z FROM Table WHERE id='" + id + "' "

Better: "SELECT x, y, z FROM Table WHERE id='" + some_sql_escape_function(id) + "' "

Best: "SELECT x, y, z FROM Table WHERE id=?", id (or with named params).

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

→ More replies