Why is string_literal() not safe? #106
Replies: 4 comments
|
BTW: Some frameworks have a special case for |
|
It looks like dbt-labs/dbt-utils#82 originally introduced We can see an example of the gymnastics you are talking about here. Can also see the triple quotes PR you mentioned: dbt-labs/dbt-bigquery#1089 |
|
Overall, the current behavior that omits escaping looks like an oversight rather than a design decision, and I agree that it would be most helpful if there were a "safe" macro that is dispatched to a database-adapter-specific implementation as-needed. The key activities to support that would be:
|
|
Added links to this issue: dbt-labs/dbt-core#2986 (comment) |
Uh oh!
There was an error while loading. Please reload this page.
string_literal(str)add database-adapter-specific quotes (usually'), but does not escape any occurrences of those in str.This is unexpected (does not match what most database libraries do) and could even lead to sql-injection vulnerabilities. And the documentation does not remind you, either.
The workaround seems to be:
There is a problem with this approach, though: You can't be sure that string_literal is dispatched to an (database-adapter-specific) implementation that actually wraps the string in single quotes – it might use double quotes, or triple-single-quotes (there is a PR for bigquery), ... – and then
escape_single_quoteswould not be the right function to use!Thus,
safe_string_literal(str)would have to use dispatch, too.To me, this clearly sounds like something that should already be provided out-of-the-box by dbt... either as separate function (backward compat?), or as new function.
All reactions