Live data from Hacker News

Be Careful with Python's New-Style String Format

lucumr.pocoo.org

141–150 of 155 posts

Re: Be Careful with Python's New-Style String Format

#141

Earlier quoted context omitted.

But we don't want the string escaped. We want it interpreted.

yes, basically eval is evil

That's not even eval, the entire thing is a series of dynamic attribute lookups[0], you can trivially implement that in pure Python without needing to `eval` anything.

[0] it also supports mapping and sequence lookups IIRC but that's about the same thing

Re: Be Careful with Python's New-Style String Format

#142
If you are making a new language, the easy way to fix this is make formatting a property of your strings, not a runtime function. Ie. instead of having "foo {bar}".format(bar=bar), have "foo {bar}" be equivalent to "foo "+bar.

This sidesteps the problem because only literal strings are formatted.

Re: Be Careful with Python's New-Style String Format

#143

Earlier quoted context omitted.

Why would a template engine allow for free access to the native platforms string capabilities in the first place?

Native vs. non-native is irrelevant. A non-native formatting system could have the features of the python formatter that are dangerous. Or, if Python's native formatter lacked these dangerous features, there'd be no reason not to use it.

>Native vs. non-native is irrelevant. A non-native formatting system could have the features of the python formatter that are dangerous.

Hardly irrelevant. The idea is that a non-native formatting system is build specifically for the templating engine, and so the developers controls what it can do and gives it non-dangerous features.

>Or, if Python's native formatter lacked these dangerous features, there'd be no reason not to use it

Even if it did lack them, it's not under the control of the template platform author, so they should not rely on it for such a crucial part of the template engine as string formatting.

(Which is exactly the case here: Python lacked those features before, but got them, and bit the template engine in the back).

Re: Be Careful with Python's New-Style String Format

#144
post #117
post #18

Earlier quoted context omitted.

Shouldn't that be based not on the built-in string capabilities, but on a template engine, that must also take care for much more narrow cases of abuse (e.g. SQL injection, or access to only of a restricted number of helper functions within the template)?

What does the template engine have to do with SQL? Not really following you there.

A template engine has multiple use cases its author can not foresee, not just creating pages.

Here they talk of using it for translation, with arbitrary user provided strings for example. Another could use it to create an SQL helper (accounting themselves for injection) etc.

In any case, it should not contain dangerous helper methods of capabilities not fit for strictly templating, just because the native string class allows them.

Re: Be Careful with Python's New-Style String Format

#146

Earlier quoted context omitted.

I'd love to hear your perspectives on Ruby if this bothers you that much.

What does Ruby have to do with any of this?

See the parent comment I'm replying too. Parent is ridiculing Python "magic methods". If he thinks they are bad, just wait till he sees Ruby.

Re: Be Careful with Python's New-Style String Format

#147

Earlier quoted context omitted.

What does Ruby have to do with any of this?

Like PHP and JS's template literals, it has full on string interpolation.

Guys, this has nothing to do with interpolation. I like both Python and Ruby's string interpolation. I'm replying to the incredulous parent of my post who's griping about magic methods (which admittedly Ruby likely takes the crown).

Re: Be Careful with Python's New-Style String Format

#148
post #111

Earlier quoted context omitted.

The reasoning why printf strings in C are unsafe is not applicable in python though. You cannot access memory directly, and %n doesn't exist (nor other other fun POSIX requirements that write to random places on the stack). In fact, Python's C-style formatting strings are immune to this problem (even the extended form that allows you to name arguments).

That's pointless nitpicking. The point is that user specified format strings allow the user to access things they shouldn't be able to access. It's the same in every language that has format strings.

> That's pointless nitpicking. The point is that user specified format strings allow the user to access things they shouldn't be able to access. It's the same in every language that has format strings.

It's not nitpicking. Go's format strings don't allow you to access stuff you shouldn't be able to. In fact, Go's templating language[1] doesn't allow you to access anything that you haven't explicitly provided to the template instance.

Allowing users to format text is not an impossible problem. It's the fault of language creators that they don't make this easier.

[1]: https://golang.org/pkg/text/template/

Re: Be Careful with Python's New-Style String Format

#149

Earlier quoted context omitted.

Native vs. non-native is irrelevant. A non-native formatting system could have the features of the python formatter that are dangerous. Or, if Python's native formatter lacked these dangerous features, there'd be no reason not to use it.

> Native vs. non-native is irrelevant. A non-native formatting system could have the features of the python formatter that are dangerous. Hardly irrelevant. The idea is that a non-native formatting system is build specifically for the templating engine, and so the developers controls what it can do and gives it non-dangerous features. > Or, if Python's native formatter lacked these dangerous features, there'd be no r…

> it's not under the control of the template platform author

This doesn't really matter. If it does what you want, it's not like they're going to change the formatter's behavior from under your feet if it's part of the language's default runtime.

Re: Be Careful with Python's New-Style String Format

#150
post #107

Earlier quoted context omitted.

Which immediately becomes a security risk because classes can overload __getattr__ and __getitem__. So now you're executing random code. Of course, %s does the same thing (__str__) but at least %s doesn't take any arguments.

The assumption behind string.format and friends is that people who override __getattr__ and __getitem__ know what they're doing. Which is probably a reasonably safe tradeoff in order to allow attribute access. I'm not worried about executing arbitrary code, because it's not user-supplied and it's normal for a templating library to access attributes of objects you pass to it (if it was unsafe you shouldn't have done t…

Read the article. The danger is not __getattr__ and __getitem__ but rather things like event.__init__.__globals__[CONFIG][SECRET_KEY]. There is no such thing as an object where arbitrary attribute access is safe, because every object has a constructor function and every function references the globals dict.
Post reply on HN