Your last comment was correct.
Query strings -- and any special characters, for that matter, are always encoded. The only possible way you could ever have this sort of injection was 1) if < > "
etc. were valid URL characters or 2) even valid filename characters. Notice that both sets exclude any HTML characters. The only time you would ever have to worry about injection was if you were using $_GET
or $_POST
variables in PHP and directly outputting them -- since those are decoded into plain text. Then, you would use htmlentities
to properly sanitize them. See this broader discussion on text injection.
Since you're directly accessing a valid URL string (through $_SERVER['REQUEST_URI']
), you will never get a non-valid (to spec) URL.
The browser should automatically handle this encoding before sending it over to the server, and if not, the server should guard against any malformed URL. Either way, your script doesn't need to worry about handling that.
EDIT
It appears you can have characters such as < >
in filenames on Mac/Linux, however those will never be properly loaded by a server since they do not follow URL guidelines.
EDIT 2
I just tested this on a Node.JS server and it appears that it does manage to serve up files with special characters. To be safe, always encode using htmlentities
or htmlspecialchars
as suggested on this page, however, I can never imagine someone having a filename with special HTML characters -- that's basically XSSing yourself -- but it's a good thing to be aware of.
EDIT 3
Curiosity got the best of me, and I deployed a file named "<test.php
to my NGINX server running on a Linux VM. Here's partial output of print_r($_SERVER)
:
[DOCUMENT_URI] => /"<test.php
[REQUEST_URI] => /%22%3Ctest.php
[SCRIPT_NAME] => /"<test.php
Notice REQUEST_URI
is still being encoded, even though the server is properly resolving the path. So, you can ignore my last edit, assuming you stick with REQUEST_URI
. To reiterate, files should never be named with special characters, so this wouldn't be an issue either way.