﻿# V5908\. OWASP\. Possible path traversal\. Potentially tainted data might be used to access files or folders outside a target directory\.

The analyzer detected that data from an external source is used as a file or folder path without prior validation\. This could make the application vulnerable to path traversal attacks\.

This vulnerability can be categorized under the OWASP Top 10 Application Security Risks 2025: [A01:2025 Broken Access Control](https://top10.owasp.org/2025/A01_2025-Broken_Access_Control/)\.

The example:

```cpp
func ReadFileHandler(w http.ResponseWriter, r *http.Request, userDir string) {
  userFileRelPath := r.URL.Query().Get("relPath")
  fullPath := filepath.Join(userDir, userFileRelPath)
  content, err := os.ReadFile(fullPath)
  if err != nil {
    http.Error(w, "not found", http.StatusNotFound)
    return
  }
  w.Write(content)
}
```

This function returns the contents of a file from a given user folder\. Users are meant to access only the files stored within that folder\.

The value obtained from an external source, `r.URL.Query().Get("relPath")`, is used here as a relative path\. Since the path is never checked, an attacker can access files that are not meant to be accessible\.

Let's assume that the code is running on a Windows system\. Each user's folder may contain a file named `userInfo.xml`, which holds various data, including confidential one\. To access the `admin` user's data, an attacker could pass the following string to `r.URL.Query().Get("relPath")`:

```cpp
..\admin\userInfo.xml
```

Simply checking whether a string begins with `..` is not enough to protect against such an attack. The following string, for example, retrieves the same data:`

```cpp
someFolder\..\..\admin\userInfo.xml
```

Thus, the lack of input validation allows an attacker to gain access to various files on the system\. Path traversal attacks and how they are carried out are described in more detail on the official OWASP website at the [link provided](https://community.owasp.org/attacks/Path_Traversal)\.

Protecting against path traversal requires input validation\. 

The fixed code:

```cpp
func ReadFileHandler(w http.ResponseWriter, r *http.Request, userDir string) {
  userFileRelPath := r.URL.Query().Get("relPath")

  cleanUserDir := filepath.Clean(userDir)
  fullPath := filepath.Join(cleanUserDir, userFileRelPath)

  rel, err := filepath.Rel(cleanUserDir, fullPath)
  if err != nil ||
     rel == ".." ||
     strings.HasPrefix(rel, ".." + string(os.PathSeparator)) {
    http.Error(w, "access denied", http.StatusForbidden)
    return
  }

  content, err := os.ReadFile(fullPath)
  if err != nil {
    http.Error(w, "not found", http.StatusNotFound)
    return
  }
  w.Write(content)
}
```

Here, the check with  [`filepath.Rel`](https://pkg.go.dev/path/filepath#Rel) rejects paths that go beyond the boundaries of `userDir` \(the relative path equals `..` or starts with `../`\), before `os.ReadFile` is called\.

Go 1\.24 added the [`os.OpenInRoot`](https://pkg.go.dev/os#OpenInRoot) function was added, which rejects attempts to open files outside the specified directory\.