mirror of
https://github.com/github/codeql.git
synced 2026-04-22 15:25:18 +02:00
Add additional example.
This commit is contained in:
@@ -46,6 +46,11 @@ not contain ".." and starts with the public folder.</p>
|
||||
|
||||
<sample src="TaintedPathGood.java" />
|
||||
|
||||
<p>Alternatively, if we only want to allow simple filenames without a path component, we can remove all path
|
||||
separators ("/" or "\") and all ".." sequences from the input before using it to construct a file path.</p>
|
||||
|
||||
<sample src="TaintedPathGood2.java" />
|
||||
|
||||
</example>
|
||||
<references>
|
||||
|
||||
|
||||
13
java/ql/src/Security/CWE/CWE-022/TaintedPathGood2.java
Normal file
13
java/ql/src/Security/CWE/CWE-022/TaintedPathGood2.java
Normal file
@@ -0,0 +1,13 @@
|
||||
public void sendUserFileGood(Socket sock, String user) {
|
||||
BufferedReader filenameReader = new BufferedReader(
|
||||
new InputStreamReader(sock.getInputStream(), "UTF-8"));
|
||||
String filename = filenameReader.readLine();
|
||||
// GOOD: remove all ".." sequences and path separators from the filename
|
||||
filename = filename.replaceAll("\\.\\.|[/\\\\]", "");
|
||||
BufferedReader fileReader = new BufferedReader(new FileReader(filename));
|
||||
String fileLine = fileReader.readLine();
|
||||
while(fileLine != null) {
|
||||
sock.getOutputStream().write(fileLine.getBytes());
|
||||
fileLine = fileReader.readLine();
|
||||
}
|
||||
}
|
||||
@@ -32,4 +32,17 @@ public class TaintedPath {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void sendUserFileGood2(Socket sock, String user) throws IOException {
|
||||
BufferedReader filenameReader = new BufferedReader(new InputStreamReader(sock.getInputStream(), "UTF-8"));
|
||||
String filename = filenameReader.readLine();
|
||||
// GOOD: remove all ".." sequences and path separators from the filename
|
||||
filename = filename.replaceAll("\\.\\.|[/\\\\]", "");
|
||||
BufferedReader fileReader = new BufferedReader(new FileReader(filename));
|
||||
String fileLine = fileReader.readLine();
|
||||
while(fileLine != null) {
|
||||
sock.getOutputStream().write(fileLine.getBytes());
|
||||
fileLine = fileReader.readLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user