Why is string_literal() not safe? #106
Replies: 4 comments
-
BTW: Some frameworks have a special case for |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
-
Added links to this issue: dbt-labs/dbt-core#2986 (comment) |
Beta Was this translation helpful? Give feedback.
-
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_quotes
would 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.
Beta Was this translation helpful? Give feedback.
All reactions