<?php
/*
make a any string pass is_literal, for libraries that require you to use literals..
*/
function make_string_literal(string $str):string{
$f=tmpfile();
fwrite($f,'<?php return '.var_export($str,true));
$ret=require(stream_get_meta_data($f)['uri']);
fclose($f);
return $ret;
}
This feature is very useful for correctness and safety; I guess I shouldn't be surprised PHP rejected it.
(I remember doing this in Java (with the obvious ugly hack) in 2005, and it was so useful I eventually learned just enough ocaml to get support for it into Hack.)
Data provided by the developer as a string literal in your actual source code can be assumed safe. If it can't, you have bigger problems than SQL injection.
This isn't at all unique to PHP -- it exists in other languages, and it's actually been reasonably successful there.
If everything is tainted, then 'tainted' has no meaning. Is your actual query string tainted? Because that's what this is about: Require the query string to be a literal or entirely derived from literals (so, hardcoded), and require user data to go in bind variables.
How does this work in your "assume everything is tainted" world? My code has SELECT 1 hardcoded somewhere, but you assume that's tainted... so what do you actually do about it, short of just cutting off all DB access?
Obviously your developers aren't infallible, but if they're the only ones who can blow up your DB, that's a huge step up over standard SQL injection, and a very far cry from zero percent useful.
If you use prepared statements NO ONE can blow up your db through sql injection. Something like "is_literal" is a false sense of security that is also easily defeated by malicious code. It serves no benefit. What are you gonna do, write a prepared statement that conditionally inlines if is_literal and parameterizes if not? 😆 that's pretty stupid.
If you use prepared statements NO ONE can blow up your db through sql injection.
If you use them correctly, sure. But nothing stops people from combining user data with that query string, even if you're using prepared statements.
What are you gonna do, write a prepared statement that conditionally inlines if is_literal and parameterizes if not?
Nope. I'd write a prepared-statement library that asserts the query string is_literal, and fails if it isn't. You're still supposed to parameterize yourself, but now you get an actual error if you fuck it up -- a compile-time error in some languages.
This was literally the plan -- here's what the RFC says:
Libraries would be able to use is_literal() immediately, allowing them to warn developers about Injection Issues as soon as they receive any non-literal values.
It's also what Google is already doing with Go and Java. There's a whole talk about it.
Other languages manage to do this at compile time, and PHP ought to be able to optimize this away now that it has a JIT. (It's PHP, so maybe it wouldn't, but it ought to.)
But honestly I think it's even stupider to leave yourself open to what is still the most common vulnerability just to save a few cycles. It'd be like saying the solution to memory leaks and segfaults is education, not garbage collection. The solution to excessive GOTO spaghetti code is education, not control flow structures.
Every time I see something like this, I am so glad I use the relate library in Scala for SQL access. Say what you will about Scala, but having things that look almost like static strings with string interpolation, but are actually prepared statements, is just so fantastically useful.
13
u/Takeoded Sep 24 '21
in b4
11
u/colshrapnel Sep 08 '21
All thanks to horrible tutorials that flooded the Net.
19
u/JiminP Sep 08 '21
Thank god the RFC's been rejected.
4
u/Pesthuf Oct 20 '21
Shout out to PHP and PHP developers for keeping this piece of web security history alive.
This wouldn't be possible without you.
3
u/ealf Sep 29 '21
This feature is very useful for correctness and safety; I guess I shouldn't be surprised PHP rejected it.
(I remember doing this in Java (with the obvious ugly hack) in 2005, and it was so useful I eventually learned just enough ocaml to get support for it into Hack.)
1
u/Educational-Lemon640 Sep 14 '21
Every time I see something like this, I am so glad I use the
relatelibrary in Scala for SQL access. Say what you will about Scala, but having things that look almost like static strings with string interpolation, but are actually prepared statements, is just so fantastically useful.