Note that there are some explanatory texts on larger screens.

plurals
  1. POPreventing directory traversal with web-facing application - are regular expressions bullet-proof?
    primarykey
    data
    text
    <p>I am in a situation where I need to allow a user to download a file dynamically determined from the URL. Before the download begins, I need to do some authentication, so the download has to run through a script first. All files would be stored outside of the web root to prevent manual downloading.</p> <p>For example, any of the following could be download links:</p> <ul> <li><a href="http://example.com/downloads/companyxyz/overview.pdf" rel="nofollow noreferrer">http://example.com/downloads/companyxyz/overview.pdf</a></li> <li><a href="http://example.com/downloads/companyxyz/images/logo.png" rel="nofollow noreferrer">http://example.com/downloads/companyxyz/images/logo.png</a></li> <li><a href="http://example.com/downloads/companyxyz/present/ppt/presentation.ppt" rel="nofollow noreferrer">http://example.com/downloads/companyxyz/present/ppt/presentation.ppt</a></li> </ul> <p>Basically, the folder depth can vary.</p> <p>To prevent a directory traversal, like say: <a href="http://example.com/downloads/../../../../etc/passwd" rel="nofollow noreferrer">http://example.com/downloads/../../../../etc/passwd</a> I need to obviously do some checking on the URI. (Note: I do not have the option of storing this info in a database, the URI must be used)</p> <p>Would the following regexp be bullet-proof in making sure that a user doesnt enter something fishy:</p> <pre><code>preg_match('/^\/([-_\w]+\/)*[-_\w]+\.(zip|gif|jpg|png|pdf|ppt|png)$/iD', $path) </code></pre> <p>What other options of making sure the URI is sane do I have? Possibly using realpath in PHP?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload