> For the complete documentation index, see [llms.txt](https://ganesha-hk.gitbook.io/cybersecurity-writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ganesha-hk.gitbook.io/cybersecurity-writeups/port-swigger-path-traversal-labs/lab-5.md).

# lab-5

## File Path Traversal, Validation of Start of Path

**Difficulty:** Practitioner

### Lab Description

This lab contains a path traversal vulnerability in the display of product images. The application transmits the full file path via a request parameter and validates that the supplied path starts with the expected folder.

**Objective:** Retrieve the contents of the `/etc/passwd` file.

### Root Cause

The application validates that the user input starts with an approved base directory (e.g. `/var/www/images/`), but it performs this check on the raw, unnormalised path. Because the operating system resolves traversal sequences (`..`) after the check, an attacker can supply a path that passes the prefix check but ultimately resolves outside the intended directory. The root cause is validating the raw path instead of the canonicalised (fully resolved) path.

### Exploitation

1. Access the lab, open any product page, and open the image in a new tab.
2. Capture the request in Burp Suite.
3. Find the URL endpoint: `GET /image?filename=/var/www/images/43.jpg`
4. Replace the filename value — keep the required prefix but append traversal sequences after it:

   ```
   /var/www/images/../../../../etc/passwd
   ```

   The prefix check passes (`/var/www/images/` is present), but the OS resolves the full path to `/etc/passwd`.
5. Send the request — the contents of `/etc/passwd` are returned in the response.

### Lesson Learned

Validating that a path *starts with* an expected prefix is not sufficient. The path must first be canonicalised (all traversal sequences resolved to their final form) and then the canonical path must be checked to confirm it still falls within the intended directory. Validating the raw input before OS resolution allows traversal sequences appended after a valid prefix to escape the intended directory entirely.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ganesha-hk.gitbook.io/cybersecurity-writeups/port-swigger-path-traversal-labs/lab-5.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
