3 ms·
Also be very wary of ../ or possibly ..\ in URIs. Say you have http://example.org/download?file=release/software-1.0.zip http://example.org/download?file=relea
by byuu 10y ago
Also be very wary of ../ or possibly ..\ in URIs.
Say you have http://example.org/download?file=release/software-1.0.zip http://example.org/download?file=release/software-1.0.zip so that you can log download statistics.
If you just fetch GET["download"] and return it, you're gonna have a bad time if they try http://example.org/download?file=../../../../../etc/hosts http://example.org/download?file=../../../../../etc/hosts (browsers strip it out automatically, but it's easy enough to type such a request into a telnet session.)
And don't just assume your safeguard works. Act like a hacker and try it out on yourself to make sure it works. Add an assert(validate("../") == false); at startup on your server, so it won't even run otherwise. Forbid the use of fopen() and instead go through your own file::open() function that calls validate() internally, then #define fopen ERROR_DONT_USE after (or whatever equivalent for the language you use.)
It is a hostile world, you can never be too safe by adding multiple (even seemingly redundant) layers of protection.
- shocks 10y agoAlso, you can't just find and replace ../ Consider: ....// which becomes ../
- idbehold 10y agoI've done something similar in node to ensure URIs like those cannot walk up past a base directory: function saferesolve(base, target) { var targetPath = '.' + path.posix.normalize('/' + target) return path.posix.resolve(base, targetPath) } saferesolve("./datasource", "a/b") === "./datasource/a/b" saferesolve("./datasource", "a/b/../c") === "./datasource/a/c" saferesolve("./datasource", "../..") === "./datasource" saferesolve("./datasource", "../../a/b") === "./datasource/a/b" saferesolve("./datasource", "../../a/b/..") === "./datasource/a"
- ajanuary 10y agoI've done something similar in Java, though more focused on file paths https://github.com/aJanuary/basepath https://github.com/aJanuary/basepath