Conversation
|
QHelp previews: unified/ql/src/queries/security/CWE-022/PathInjection.qhelpUncontrolled data used in path expressionAccessing paths controlled by users can expose resources to attackers. Paths that are naively constructed from data controlled by a user may contain unexpected special characters, such as RecommendationValidate user input before using it to construct a file path. Ideally, follow these rules:
ExampleThe following code shows two bad examples. let fm = FileManager.default
let path = try String(contentsOf: URL(string: "http://example.com/")!)
// BAD
return fm.contents(atPath: path)
// BAD
if (path.hasPrefix(NSHomeDirectory() + "/Library/Caches")) {
return fm.contents(atPath: path)
}In the first, a file name is read from an HTTP request and then used to access a file. In this case, a malicious response could include a file name that is an absolute path, such as In the second bad example, it appears that the user is restricted to opening a file within the In the following (good) example, the path used to access the file system is normalized before being checked against a known prefix. This ensures that regardless of the user input, the resulting path is safe. let fm = FileManager.default
let path = try String(contentsOf: URL(string: "http://example.com/")!)
// GOOD
let filePath = FilePath(stringLiteral: path)
if (filePath.lexicallyNormalized().starts(with: FilePath(stringLiteral: NSHomeDirectory() + "/Library/Caches"))) {
return fm.contents(atPath: path)
}References |
e49176b to
46dc2be
Compare
46dc2be to
ce5fb70
Compare
No description provided.