|
3 | 3 | "qhelp.dtd"> |
4 | 4 | <qhelp> |
5 | 5 | <overview> |
6 | | -<p>User supplied file paths can often pose security risks if a program does not handle them correctly. In particular, if a user |
7 | | -is meant to access files under a certain directory but does not enter a path under that directory, they can |
8 | | -unexpectedly gain access to (and potentially modify/delete) possibly sensitive resources. </p> |
9 | | - |
10 | | -<p>Suppose a program is to only accept paths that point to files/folders within directory <code>DIR</code>. |
11 | | -To ensure that a user supplied path, say <code>SUBDIR</code>, is a subdirectory of <code>DIR</code>, the |
12 | | -program verifies, using string comparisons, that <code>DIR</code> is a prefix of <code>SUBDIR</code>. |
13 | | -However, if <code>DIR</code> is not slash-terminated, such a check would not be sufficient. |
14 | | -<code>SUBDIR</code> may be allowed to also access siblings of <code>DIR</code> and not |
15 | | -just children of <code>DIR</code>, which is a security vulnerability.</p> |
16 | | - |
| 6 | +<p>A common way to check that a user-supplied path <code>SUBDIR</code> falls inside a directory <code>DIR</code> |
| 7 | +is to use <code>getCanonicalPath()</code> to remove any path-traversal elements and then check that <code>DIR</code> |
| 8 | +is a prefix. However, if <code>DIR</code> is not slash-terminated, this can unexpectedly allow accessing siblings of <code>DIR</code>.</p> |
17 | 9 | </overview> |
18 | 10 | <recommendation> |
19 | 11 |
|
20 | | -<p>If the user should only access items within a certain directory <code>DIR</code>, first ensure that <code>DIR</code> is slash-terminated, |
21 | | -and then proceed (as normal) to verify that <code>DIR</code> is a prefix of the user-provided path, <code>SUBDIR</code>. Note, Java's <code>getCanonicalPath()</code> |
22 | | -returns a <b>non</b>-slash-terminated path string, so a <code>"/"</code> must be added to <code>DIR</code> if that method is used. </p> |
| 12 | +<p>If the user should only access items within a certain directory <code>DIR</code>, ensure that <code>DIR</code> is slash-terminated |
| 13 | +before checking that <code>DIR</code> is a prefix of the user-provided path, <code>SUBDIR</code>. Note, Java's <code>getCanonicalPath()</code> |
| 14 | +returns a <b>non</b>-slash-terminated path string, so a slash must be added to <code>DIR</code> if that method is used.</p> |
23 | 15 |
|
24 | 16 | </recommendation> |
25 | 17 | <example> |
|
0 commit comments