User tests: Successful: Unsuccessful:
InputFilter::isSafeFile() scans uploaded file contents in 131072-byte (128 KB) chunks. To catch a dangerous signature that lands across the boundary between two reads, it carried over the tail of each read into the next one — but only a fixed 10 bytes. That value was sized for <?php (5 bytes) and never updated when the longer __HALT_COMPILER() phar-stub check (17 bytes, enabled by default via phar_stub_in_content) was added to the same loop.
As a result, a phar stub aligned so that 11 or more of its 17 bytes fall before a 128 KB boundary is split across two reads and never detected, so the file passes the content scan.
This change computes the carry-over ($scanOverlap) from the longest signature actually being scanned given the current options, instead of a magic number. For the default options that is strlen('__HALT_COMPILER()') - 1 = 16. It also stays correct automatically if the forbidden-extension list or signature set changes.
Files:
libraries/src/Filter/InputFilter.php — compute $scanOverlap; replace substr($data, -10) with substr($data, -$scanOverlap).tests/Unit/Libraries/Cms/Filter/InputFilterTest.php — new regression tests.Run the bundled unit tests:
libraries/vendor/bin/phpunit tests/Unit/Libraries/Cms/Filter/InputFilterTest.php
isSafeFile() returns true (file considered safe) the phar stub spanning the read boundary is not detected and the new unit tests fail.
isSafeFile() returns false (file rejected) the phar stub is detected regardless of where it falls relative to the read boundary Large benign files containing none of the scanned signatures are still accepted (no false positives) All new unit tests pass
Please select:
Documentation link for guide.joomla.org:
No documentation changes for guide.joomla.org needed
Pull Request link for manual.joomla.org:
No documentation changes for manual.joomla.org needed
| Status | New | ⇒ | Pending |
| Category | ⇒ | Libraries Unit Tests |
| Labels |
Added:
Unit/System Tests
PR-5.4-dev
|
||
| Title |
|
||||||
Hi @saddamr3e, thank you for your contribution. This PR is still awaiting two human tests/code reviews. As with many parts of the project, reviews depend on volunteer availability.
Understood, will wait. For anyone picking this up to test: the bundled unit tests reproduce the boundary case directly (libraries/vendor/bin/phpunit tests/Unit/Libraries/Cms/Filter/InputFilterTest.php). The two phar-stub tests fail without the InputFilter change and pass with it.
Commented by maintainers as 'is a useful hardening' and 'should work'. It needs two human reviews or tests 🙏
How/Where do you upload a phar file as its a forbidden file
How/Where do you upload a phar file as its a forbidden file
Good question. @saddamr3e Can you answer that? How have you managed to upload a phar file in practice?
You don't, and that's not the case this covers. The .phar extension block works fine. phar_stub_in_content is there for the other route: a file with an allowed extension (say an image or PDF going through the Media Manager) that is also a valid phar archive. PHP's phar:// wrapper opens it by content, not by extension, so the only thing stopping it is the content scan for __HALT_COMPILER().
That scan reads 128 KB at a time and only keeps the last 10 bytes between reads. The stub is 17 bytes, so if it straddles a read boundary with 11 or more bytes before it, neither read contains the full string and the file passes. The unit tests build exactly that: an otherwise harmless file with the stub placed across the 131072 offset.
To be clear, I didn't do an end-to-end upload on a live site and then trigger it through phar://. Getting from an uploaded polyglot to actual impact still needs a phar:// sink somewhere, and PHP 8 no longer unserializes metadata on its own. So I'd call this a hardening that makes the existing check do what it already claims, not a demonstrated exploit.
any update?