Re:All JavaScript and TypeScript features of the last 3 year...
by fulafel on Thursday March 09, 2023 @01:05PM
(#35084442)
Is it really an good thing that a vulnerable sql string interpolation code pattern and this sql tagged string look and feel the same?
[ Parent
| on HN ]
Re:All JavaScript and TypeScript features of the last 3 year...
by hn_throwaway_99 on Thursday March 09, 2023 @01:36PM
(#35084862)
Actually, yes, it is. The way these libraries work, since the thing that is parsed is NOT just a plain string, in most cases it's impossible to have sql injection without doing some deliberately nasty stuff. That is, you can't just do this:
const query = `select foo from bar where zed = ${param}`; // forgot the sql tag
await runQuery(query);
In that case, the type of query is just string, but the `runQuery` method doesn't take strings, it takes a parsed query, so that wouldn't work.After using the tagged template literal pattern for SQL queries exclusively for the past couple years, I can't say enough how awesome it is to use in practice. Libraries even let you do strong typing with TypeScript to define the expected structure of the result, e.g.
sql<MyExpectedReturnType>`select foo from bar where zed = ${param}`
Re:All JavaScript and TypeScript features of the last 3 year...
by conaclos on Thursday March 09, 2023 @05:39PM
(#35087843)
> Libraries even let you do strong typing with TypeScript to define the expected structure of the result.
The tagged template does not return a string in this case?
Re:All JavaScript and TypeScript features of the last 3 year...
by TheCleric on Thursday March 09, 2023 @06:12PM
(#35088192)
No, it usually returns a parsed object. For example the gql tag in the apollo client libraries return a completely parsed query with all its various children and sub-objects.
Re:All JavaScript and TypeScript features of the last 3 year...
by sanitycheck on Thursday March 09, 2023 @01:36PM
(#35084860)
I was thinking the same thing... If I do that I'm going to forget the "sql" part at least once and nothing's going to alert me about it.
Re:All JavaScript and TypeScript features of the last 3 year...
by hn_throwaway_99 on Thursday March 09, 2023 @01:37PM
(#35084868)
The way libraries work it's impossible to forget the "sql" part and still have that query be executed - see my sibling comment.
Re:All JavaScript and TypeScript features of the last 3 year...
by runarberg on Thursday March 09, 2023 @01:43PM
(#35084950)
Why wouldn’t it be? Do you think developers get inspired by this slick API and decide to write functions that talk directly to the database using unescaped interpolated strings? I doubt it.