Zum Inhalt springen

Path traversal (ARCH303)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key dotnet.security.path-traversal · Severity error · Category security · CWE CWE-22

Joining a user-supplied file name directly into a path allows ../ sequences to escape the intended directory. The path must be normalized and constrained to a root.

var path = Path.Combine(uploadDir, userFileName);
var bytes = File.ReadAllBytes(path);
var fullRoot = Path.GetFullPath(uploadDir);
var path = Path.GetFullPath(Path.Combine(fullRoot, userFileName));
if (!path.StartsWith(fullRoot + Path.DirectorySeparatorChar))
throw new UnauthorizedAccessException();
var bytes = File.ReadAllBytes(path);

Resolve the combined path with Path.GetFullPath and verify the result stays under the allowed root directory.