Safeguard
Application Security•

CWE-160 Leading Special Elements: Absolute Paths and Prefixes

A leading slash or other prefix can change what a value means. How CWE-160 appears in file handling, with a short code fix.

Safeguard Security Team
2 min read

CWE-160 is the failure to neutralize leading special elements, the characters at the start of a value that a downstream component reads in an unexpected way. The classic case is a leading slash that turns a relative file name into an absolute path.

File handling code is the main place it shows up, in downloaders, archive extractors and upload handlers. Clients that trust file names sent by a server are a notable group.

How it happens

The record's only observed example is CVE-2002-1345, where multiple FTP clients write arbitrary files via absolute paths in server responses. The client expected a plain name. The server sent a name that started with a slash.

Joining paths makes the mistake easy in many languages.

import os

# Vulnerable: an absolute name discards the destination directory
dest = os.path.join("/downloads", remote_name)
open(dest, "wb").write(data)

In Python, os.path.join("/downloads", "/etc/cron.d/job") returns the second argument alone. The leading slash wins.

What an attacker gains

MITRE lists integrity impact as unexpected state. The observed example is arbitrary file writes, which can overwrite configuration or startup files the user owns.

How to find it

MITRE lists no detection methods. Review any code that joins a base directory with a name from outside. Test with names starting with a slash, a backslash, a drive letter or a dot. Check clients as well as servers.

How to fix it

  1. Anticipate that leading special elements will be injected, removed or manipulated.
  2. Reduce the name to its final component, then confirm that the resolved path is inside the intended directory.
import os

base = os.path.realpath("/downloads")
name = os.path.basename(remote_name)
dest = os.path.realpath(os.path.join(base, name))
if os.path.commonpath([base, dest]) != base:
    raise ValueError("path escapes download directory")
  1. Prefer an allowlist of characters for names and reject anything else.
  2. Do not trust names sent by a remote party just because it is a server you chose.

Quick check

Do only servers need this check?

No. CVE-2002-1345 affected FTP clients that wrote files based on server responses.

Sources

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.