4 ms·
But if someone omits the `safe` it may still work but allow injection.
by ZiiS 2y ago
But if someone omits the `safe` it may still work but allow injection.
- thunky 2y agoSame is true if someone forgets to use t" and uses f" instead. At least db.safe says what it does, unlike t".
- ewidar 2y agoNot really, since f"" is a string and t"" is a template, you could make `db.execute` only accept templates, maybe have `db.execute(Template)` and `db.unsafeExecute(str)`
- thunky 2y agoagreed. but then you're breaking the existing `db.execute(str)`. if you don't do that, and instead add `db.safe_execute(tpl: Template)`, then you're back to the risk that a user can forget to call the safe function. also, you're trusting that the library implementer raises a runtime exception if a string a passed where a template is expected. it's not enough to rely on type-checks/linting. and there is probably going to be a temptation to accept `db.execute(sql: Union[str, Template])` because this is non-breaking, and sql without params doesn't need to be templated - so it's breaking some stuff that doesn't need to be broken. i'm not saying templates aren't a good step forward, just that they're also susceptible to the same problems we have now if not used correctly.
- fwip 2y agoYour linter can flag the type mismatch, and/or the function can reject f"" at runtime. This is because t"" yields a Template, not a str. Template is also more powerful/concise in that the stringify function can handle the "formatting" args however it looks. Note also, that there's no requirement that the template ever become a str to be used.