Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V5908. OWASP. Possible path...
menu mobile close menu
Analyzing projects
Additional information
toggle menu Contents

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

Nov 26 2026

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.

The example:

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"):

..\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:

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.

Protecting against path traversal requires input validation.

The fixed code:

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 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 function was added, which rejects attempts to open files outside the specified directory.

This diagnostic rule is classified as: