Re: Common-sense security hardening for the next PHP

From: Date: Thu, 19 Nov 2026 23:12:49 +0000
Subject: Re: Common-sense security hardening for the next PHP
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-133129@lists.php.net to get a copy of this message
You make great points. I take back about $interpolation inside <<<PHP et al. I misspoke there. We may need some new syntax like <<<<PHP, which treats $foo and {$foo} differently. Or has {{foo}}. But regardless — yes, it must have an actual Formats\PHP class, that implements PHP’s “Formats” interface or something, and yes it has to be aware of the context when sanitizing these interpolated variables. For <<<<PHP and <<<<SHELL it would be easy: just turn the values into fully-escaped strings. For SQL yes you need a real parser, and you could argue that PDO binding is already better. We already have escapshellarg, mysqli_real_escape_string, json_encode, htmlencode and so on. This would just be doing it under the hood. But the bigger point is — if we can get PHP 9 to simply close all the holes separating *developer-written code* from *dynamically constructed strings*, we’ll finally prevent a huge class of attacker-controlled arbitrary RCE. Using <<<< for provenance allows eval and exec, which are the most open-ended attack vectors. The remainder is structurally constrained so it can be analyzed and the dangerous things disallowed. I’d say .php and .phar files should never be written by any userland code. And userland code shouldn’t modify .ini files etc. Obviously you could kick the can down the road and require people to use external Linux or Windows tools to lock things down, but it is far better for the PHP 9 runtime to GUARANTEE these things by default. If that means you break Laravel and force them to update some dangerous things, well so be it. PHPalready requires # ReturnTypeWillChange etc hints to maintain backward compatibility with code that doesn’t declare return types etc. And this is FAR MORE serious, we’re talking about security. Tracking provenance of arbitrary code solves most of the things you brought up. There has to be a way to do that. If code was already there (eg arrived from composer) then we trust it. If it was dynamically written by userland — then we have to constrain that, by structure. You’d need to track the paths when you write to streams. This is provenance also, just at runtime. It’s just a few extra pieces of data attached to the stream for the Zend or MicroPHP engine to make use of. Yeah we’d have to map it all out: what functions can write to files, how an attacker can currently save code in files that can simply be executed willy-nilly, or variables that can be evaled() or exec()ed. If we add provenance and proper context-awere sanitizing to PHP, I feel that we can finally make that structural separation, which will close a huge class of bugs. But we would need an exhaustive graph of what goes on now and make sure we cut every possible edge for an attacker. If it were up to me I’d require *multisig* on a phar hash or any sort of change, rather than just one signature. Then we have support for PKI and fix supply chain attacks also. An organization should be able to choose which public key roots it trusts (eg auditors at RedHat or whatever). So when you include a .php file or a .phar I’d definitely want code signing… that would harden it even more. Sincerely, Greg Magarshak Qbix Inc. On Thu, Nov 19, 2026 at 1:10 PM Pratik Bhujel <prateekbhujelpb@gmail.com> wrote: > Hi Greg, > > On Thu, 19 Nov 2026 17:52:00 +0000, Gregory Magarshak <greg@qbix.com> > wrote: > > > 1) Make it so that include() and require() can only load specific > > extensions like ".php" and ".phar" > > 2) At the same time, all file functions that write to a file (e.g. fwrite > > and file_put_contents) must refuse to write to exactly these extensions.. > > 3) Any functions like rename() or move_uploaded_file() should also be > > counted among the files that "can write to files", so an attacker can't > > rename a file to have the right extension. > > I get the goal here (W^X, keeping executable code separate from writable > paths), but I'm curious how this would actually hold up once you trace it > down into php-src. > > fwrite() only sees a php_stream, not a path, and PHP has custom stream > wrappers, symlinks, hard links, copy(), ZipArchive::extractTo(), and C > extensions like SQLite or GD that open files directly without going through > ext/standard. Even if the check lives in php_stream_open_wrapper() > alongside rename() and move_uploaded_file(), how do you handle symlinks, > case-insensitive filesystems on macOS/Windows, or TOCTOU races between > checking the path and opening the file? And on the include() side, if it > checks the URI suffix, how does that interact with > phar://upload.jpg/index.php or custom stream wrappers (like the > source-rewriting wrapper in your webserver) without either leaving bypasses > or breaking stream wrappers? > > > The .ini can be used as an escape hatch for the rare cases you actually > > need this. And frankly, .ini is too coarse, I'd recommend doing it on a > > RegEx basis, where you indicate the regex of what file paths are the > > exception. > > Most modern PHP stacks legitimately write .php files and then include them > (Laravel compiled views and bootstrap/cache, Symfony var/cache, Twig, > Doctrine proxies, Composer autoloaders). Once a sysadmin adds a php.ini > regex exception for those directories so the app can boot, those paths are > writable and executable again, just guarded by regexes in php.ini instead > of read-only mounts, OS permissions, or kernel checks like the recent > AT_EXECVE_CHECK pre-RFC on this list. What threat model does a php.ini > regex close that OS-level W^X doesn't already cover for the whole process, > including extensions and subprocesses? > > > Heredoc syntax should absolutely be used for provenance of what is being > > sent to exec(), eval() and other "open-ended, dangerous functions". > > > > $code = <<<PHP > > // this specific tag, "PHP" is needed > > // in order to pass $foo to any eval() > > You can interpolate $variables here, fine. > > But this is at assignment time. > > I recommend also allowing {{interpolation}} > > that can be passed at eval time. > > PHP; > > If normal $variable interpolation is still allowed at assignment time, > doesn't that break provenance right away whenever any tainted input reaches > that variable before eval() or exec() runs? Even if $variable interpolation > were disallowed, how would provenance be tracked in the engine: only on the > AST node at compile time, or as a runtime flag on zend_string (like the > earlier is_literal() discussions)? If it's on zend_string, what happens > across concatenation, trim()/sprintf(), interned strings, or helper > functions? > > > You can even have entire autoloaded classes, like \Formats\SQL and > > \Formats\JSON, that will handle sanitization for that specific language.. > > For <<<SHELL and <<<SQL, safe escaping depends on where {{interpolation}} > sits in the syntax (an SQL identifier vs a string literal vs a LIKE > pattern, or a shell command vs an argument vs a redirect). How would > \Formats\SQL or \Formats\SHELL handle that without embedding full SQL and > shell parsers in PHP core, when prepared statements and proc_open()'s argv > array already avoid string interpolation completely? > > > 4) There's no way userland code to execute RCE anymore, frankly not even > > SQL injection, if you are conservative by default. > > Even if .php writes, eval(), and exec() were all locked down, arbitrary > file write still lets an attacker write .user.ini (auto_prepend_file), > .htaccess, or a template like .blade.php / .twig that the app itself > compiles into an allowlisted cache directory. That's mostly why this feels > to me like it shifts the problem around the way safe_mode used to, rather > than eliminating userland RCE. > > Curious how you'd approach those pieces, because from an engine > perspective it feels like the OS and parameterized APIs are already the > cleaner boundary. > > Best, > Pratik >

« previous php.internals (#133129) next »