Safeguard
Security Guides•

AI suggested path traversal fix: what should you check?

Path traversal fixes that only strip dot-dot-slash often fail. Check how an AI fix handles absolute paths, encodings and the final resolved location.

Safeguard Security Team
3 min read

A path traversal fix is sound when the code resolves the final file path and confirms it sits inside an allowed directory. Be skeptical of a fix that only removes dot-dot-slash text. OWASP notes that attackers also use variations and absolute paths.

What the attack is

OWASP describes path traversal as manipulating variables that reference files with dot-dot-slash sequences and their variations, or using absolute file paths, to reach files and directories outside the intended area. Targets can include application source, configuration and critical system files. Access is still limited by the operating system's own access controls.

Two details in that description matter for review. Variations exist, and absolute paths are in scope.

What a weak fix looks like

Assistants often propose string cleaning, such as replacing the text dot-dot-slash with nothing. Reasons that is fragile:

  • Replacement can be applied once, so overlapping input may leave a usable sequence behind.
  • It does nothing about absolute paths.
  • Encoded forms may be decoded after your check runs.
  • Different platforms use different separators.

These are review prompts to test, not a complete catalog.

What a strong fix looks like

The shape is consistent across languages:

  1. Choose a base directory the feature is allowed to read.
  2. Join the base directory and the user supplied name.
  3. Resolve the result to a canonical absolute path, following links where that matters.
  4. Confirm the resolved path starts with the base directory.
  5. Reject the request otherwise, and do not echo the path in the error.

Where the set of valid files is small, an allow-list of names or identifiers mapped to files is simpler and safer than accepting a path at all.

from pathlib import Path

BASE = Path("/srv/app/uploads").resolve()

def open_user_file(name):
    target = (BASE / name).resolve()
    if BASE not in target.parents:
        raise PermissionError("not allowed")
    return open(target, "rb")

Read the example as an illustration of the idea, and confirm the exact behavior for your language and version.

Test it

Write tests with relative sequences, an absolute path, a path that ends outside the base after joining, and a name that is a link pointing elsewhere. A good fix rejects all four. A fix that passes only the first is partial.

Error messages and logs

A fix can block the attack and still leak information. If the rejection message prints the full resolved path, an attacker learns your directory layout. Return a generic error to the client and log the detail on the server, where only your team can read it.

Check the neighbors

The same helper is often used in several handlers: download, preview, delete, export. After the AI fixes one, search for other places that join a user value to a file path. The OWASP Secure Code Review cheat sheet lists path traversal among the vulnerability patterns reviewers should look for, and file operations and path handling among the high risk code patterns.

Safeguard's scanning can surface findings of this kind and open fix pull requests, with autonomous remediation off by default. Run the tests above on any branch regardless of its origin.

Quick check

Does running as a low privilege user fix path traversal?

It limits the damage because the operating system restricts access, but the application should still reject paths outside its allowed directory.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.