Constructing path names with unsanitized user input can lead to path traversal attacks (for example, ../../..
) that allow an attacker access to file system resources.
1def verify_file_path_noncompliant():
2 from flask import request
3 file_path = request.args["file"]
4 # Noncompliant: user input file path is not sanitized.
5 file = open(file_path)
6 file.close()
1def verify_file_path_compliant():
2 from flask import request
3 base_path = "/var/data/images/"
4 file_path = request.args["file"]
5 allowed_path = ["example_path1", "example_path2"]
6 # Compliant: user input file path is sanitized.
7 if file_path in allowed_path:
8 file = open(base_path + file_path)
9 file.close()