3 ms·
How would you allow user uploads with that a whitelist? Accepted uploads get automatically added to your whitelist? That sounds problematic.
by andybak 10y ago
How would you allow user uploads with that a whitelist? Accepted uploads get automatically added to your whitelist? That sounds problematic.
- wereHamster 10y agoProblematic how? It doesn't mean the whitelist has to be in the code, it could be 'generated' from a database (let whitelist be the result of select file_name from uploaded_files_table)
- paulddraper 10y agoThen file_name needs to be reasonable and not arbitrary, right? I don't care where you push your checks around to, just as long as they exist.
- wyager 10y agoA hex-encoded file hash as a file name is a safe bet. You can resolve file names (unsanitized, stored safely in a database) to hashes and load the files from disk. This general approach (whitelisting file URLs) lets us localize any path sanitation to the file upload code, rather than every single request.
- xenadu02 10y agoNever never never NEVER NEVER NEVER use user-input as any part of a path! You should generate UUIDs, bucket by the first few chars, and use a database to map from UUID to human-readable name.
- wyager 10y agoExactly. The set of acceptable files can be modified at runtime. Now you've localized the issue of sanitizing paths to a small area of your code (file upload) rather than every request. A good way to do this is to save the file on disc with the hex encoding of its SHA256 hash as its name, and then maintain a mapping from file names to hashes. This way, the only feasible attack is to overwrite a preexisting file, which would require the ability to pull off a second-preimage attack on SHA256, which is not generally thought to be feasible.