4 ms·
No, Rust does not have the ability to access any variable in the program via a format string. Rust has this: format!("{argument}", argument = "test"); //
by Animats 10y ago
No, Rust does not have the ability to access any variable in the program via a format string. Rust has this:
format!("{argument}", argument = "test"); // => "test"
That's just named arguments to the format. Also, that's a macro; it's expanded at compile time.
Python's approach is lame. It should have used something with a limited list of named arguments, or maybe a dict.
- gizmo686 10y agoNeither does python. The problem being discussed here is that the format string can access and attribute of the object passed in. From PEP3101 [0]: " Unlike some other programming languages, you cannot embed arbitrary expressions in format strings. This is by design - the types of expressions that you can use is deliberately limited. Only two operators are supported: the '.' (getattr) operator, and the '[]' (getitem) operator. The reason for allowing these operators is that they don't normally have side effects in non-pathological code." [0] https://www.python.org/dev/peps/pep-3101/ https://www.python.org/dev/peps/pep-3101/
- echelon 10y ago> Neither does python. I would argue that being able to access any global value is a sufficient enough concern.
- deleted 10y ago[deleted]
- zardeh 10y agoThe security issue here isn't with f strings, which oddly enough, don't have this flaw (because they are literals only). This has to do with new style string formatting, which does not have access to globals.
- detaro 10y ago> don't normally have side effects in non-pathological code so, "be careful with it if you can't make sure code isn't non-pathological or friendly", what the article says.
- gizmo686 10y agoThe example is not pathalogical. The property that the PEP states is that the expression should not have side effects, which is the case. Having pathological code in this context only elavates the severity from a pure data leak to partial code execution. Of course partial code execution in an unexpected context often leads to arbitrary code execution.
- geofft 10y agoIn many webapps, there's a very easy way to go from a data leak to arbitrary code execution. For instance, if you have signed cookies that are pickles of data, the cookie signing key lets you execute arbitrary code (because pickle deserialization can execute arbitrary code).
- dom0 10y ago... which is why it's a poor idea to do that in the first place, since it has literally a single layer of protection against remote, unauthorized, arbitrary code execution, that depends on keeping a static key secret. Much better idea to use a stupid, non-ACE serialization there.
- unscaled 10y agoWhich is still not safe enough, and in effect can be used to access any global variable as described in the OP article. Rust formatting is done by macros - which expand at compile time and accept only string literals. This makes it impossible to pass user input to format!(), println!() and their ilk unless the end-user can access your compiler, in which case you have a much larger problem.
- steveklabnik 10y agoThere are crates like https://crates.io/crates/strfmt https://crates.io/crates/strfmt which let you accept a non-literal.
- kukx 10y agoAlso, in C# the string interpolation works only on literal strings at compile time. So it's not possible to inject a malicious code this way from outside of the source code.
- icebraining 10y agoSo does in Python. This isn't string interpolation, it's the equivalent of String.Format.
- progval 10y ago> It should have used something with a limited list of named arguments, or maybe a dict. Python does have that feature, called Template Strings: https://docs.python.org/3/library/string.html#template-strings https://docs.python.org/3/library/string.html#template-strin...
- tedmiston 10y agoAgree that these are a much better solution that I wish Armin would have dedicated a few sentences to.
- Manishearth 10y ago> It should have used something with a limited list of named arguments, or maybe a dict. That's what "old-style" python interpolation did :) You could do |"hi my name is %s and I live in %s" % (name, place)| or use named arguments with a dict (|"hi my name is %(name) and I live in %(place)" % {"name": name, "place": place}|). I like JS format strings -- you can write arbitrary code in them, but they are compile time only (and use a different string syntax). So you can have |`Hello my name is ${name} and I come from ${place}. My profession is ${generate_random_profession()}`|. The backtick-string isn't a different type, and can't be moved around like a value. It's a different kind of way of specifying a string literal, and will be evaluated when specified. No way of doing injection there. I suspect Python wanted to make it less verbose with new-style interpolation, and went a bit overboard with field access without realizing or caring about possible security issues like this. Of course, python has a third kind of format string; interpolation string literals (|f"hello my name is {name} ..."|), which work like JS and Ruby. This is what you should be using IMO.
- saghm 10y ago> I like JS format strings -- you can write arbitrary code in them, but they are compile time only Genuinely curious: what exactly does "compile-time" mean in the case of JS? Is it like C macros (modifying the source code before parsing)?
- Manishearth 10y agoNo, it's not that, I was using the term loosely. I meant that you cannot use a string value as a format string. It only exists in the form of a literal, and it gets evaluated when the interpreter evaluates that literal. You cannot store it and format it later.
- saghm 10y agoAh, I see. Thanks!
- eridius 10y agoFWIW, Rust's syntax is based on Python's, but yeah, you can't access value fields in Rust's syntax.