File tree Expand file tree Collapse file tree 1 file changed +11
-7
lines changed
src/java/detectors/path_traversal Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Original file line number Diff line number Diff line change @@ -13,19 +13,23 @@ public class PathTraversal {
13
13
14
14
// {fact rule=path-traversal@v1.0 defects=1}
15
15
public void createFileNoncompliant (HttpServletRequest request , HttpServletResponse response ) {
16
- String basePath = "/var/example/base /" ;
17
- String relativePath = request .getParameter ("relativePath " );
18
- // Noncompliant: user-supplied relative path is not sanitized and could contain malicious special characters.
19
- File fileTarget = new File (basePath + relativePath );
16
+ String basePath = "/var/data/images /" ;
17
+ String desiredCategory = request .getParameter ("category " );
18
+ // Noncompliant: user-supplied relative path is not sanitized and could contain malicious characters.
19
+ File fileTarget = new File (basePath + desiredCategory );
20
20
}
21
21
// {/fact}
22
22
23
23
// {fact rule=path-traversal@v1.0 defects=0}
24
24
public void createFileCompliant (HttpServletRequest request ) {
25
- String basePath = "/var/example/base/" ;
25
+ String basePath = "/var/data/images/" ;
26
+ String desiredCategory = request .getParameter ("category" );
26
27
// Compliant: user-supplied relative path is sanitized before use.
27
- String relativePath = request .getParameter ("relativePath" ).replaceAll (".." , "" );
28
- File fileTarget = new File (basePath + relativePath );
28
+ if (desiredCategory .matches ("[a-z]+" )) {
29
+ File fileTarget = new File (basePath + desiredCategory );
30
+ } else {
31
+ throw new IllegalArgumentException ("Invalid category name" );
32
+ }
29
33
}
30
34
// {/fact}
31
35
}
You can’t perform that action at this time.
0 commit comments