Path Traversal
javaCopy codeString fileName = request.getParameter("fileName");
File file = new File("/var/www/html/" + fileName);javaCopy codeString fileName = request.getParameter("fileName");
File file = new File("/var/www/html/" + Paths.get(fileName).normalize().toString());
if (!file.getCanonicalPath().startsWith("/var/www/html/")) {
throw new SecurityException("Invalid file path");
}pythonCopy code@app.route('/read_file')
def read_file():
file_name = request.args.get('file_name')
with open('/var/www/html/' + file_name, 'r') as f:
return f.read()Python Example
Vulnerable Code:
Reason for Vulnerability:
Fixed Code:
Reason for Fix:
Java Example
Vulnerable Code:
Reason for Vulnerability:
Fixed Code:
Reason for Fix:
C# Example
Vulnerable Code:
Reason for Vulnerability:
Fixed Code:
Last updated