path-traversal-prevention

path-traversal-prevention is a cursor rule for coding agents from matank001/cursor-security-rules. It costs 2,694 tokens per session, scanned A, original, MIT.

A set of rules for preventing path traversal, a security attack where crafted file names reach files outside the intended folder. It focuses on safely handling user-controlled file paths.

In plain words
What is it for?
Use it when writing or reviewing upload, download, attachment, or file-reading code that receives file names or paths from users.
Why use it?
It helps stop attackers from reading private files or writing over important system and application files. The rules warn against using raw user input in file operations.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/matank001/cursor-security-rules/path-traversal-prevention
Clone the repo
git clone --depth 1 https://github.com/matank001/cursor-security-rules

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for path-traversal-prevention

README.md
[![agentmods](https://agentmods.dev/badge/rules/matank001/cursor-security-rules/path-traversal-prevention.svg)](https://agentmods.dev/rules/matank001/cursor-security-rules/path-traversal-prevention)
Your own site
<a href="https://agentmods.dev/rules/matank001/cursor-security-rules/path-traversal-prevention"><img src="https://agentmods.dev/badge/rules/matank001/cursor-security-rules/path-traversal-prevention.svg" alt="Measured on agentmods" height="20"></a>
Per session 2,694 This file is loaded in full into every session.
When invoked 2,694 The same file — it is already loaded in full.
Security scan A 1 finding. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.02694 $0.02694
Opus 5 $0.01347 $0.01347
Sonnet 5 $0.00539 $0.00539
Haiku 4.5 $0.00269 $0.00269

Measured 4d ago against content hash 5fb8e807785a, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

path-traversal-prevention scanned grade A with 1 finding against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

### urllib.parse (For URL-encoded paths)
path-traversal-prevention.mdc · 404 lines

How it starts

The opening of the file, as written. The whole thing — 404 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Path Traversal Prevention

Path traversal attacks occur when user-controlled input is used to construct file paths, allowing attackers to access files outside the intended directory. This can lead to unauthorized file access, data leakage, or system compromise.

1. Do Not Use User Input in File Paths

Rule: Never use raw user input to construct file paths for read or write operations.

❌ Dangerous Examples

# File Read - DANGEROUS
filename = request.args.get('file')
with open(f"/uploads/{filename}", 'r') as f:  # User can access ../../../etc/passwd
    content = f.read()

# File Write - DANGEROUS  
user_file = request.files['file']
filename = user_file.filename
user_file.save(f"/uploads/{filename}")  # User can write to ../../../sensitive/config.json
// File Read - DANGEROUS
const filename = req.query.file;
fs.readFile(`./uploads/${filename}`, (err, data) => {  // ../../../etc/passwd
    res.send(data);
});

// File Write - DANGEROUS
const filename = req.body.filename;
fs.writeFile(`./uploads/${filename}`, data);  // ../../../system/config
// File Read - DANGEROUS
$filename = $_GET['file'];
$content = file_get_contents("/uploads/" . $filename);  // ../../../etc/passwd

// File Write - DANGEROUS
$filename = $_POST['filename'];
file_put_contents("/uploads/" . $filename, $data);  // ../../../sensitive/data

✅ Secure Alternatives

# File Read - SECURE
import os
from pathlib import Path

filename = request.args.get('file')
# Validate filename format
if not filename or not filename.replace('.', '').replace('_', '').replace('-', '').isalnum():
    abort(400, "Invalid filename")

# Use pathlib for safe path joining
upload_dir = Path("/uploads")
file_path = upload_dir / filename

# Ensure the final path is within the intended directory
if not str(file_path.resolve()).startswith(str(upload_dir.resolve())):
    abort(403, "Access denied")

with open(file_path, 'r') as f:
    content = f.read()
// File Read - SECURE
const path = require('path');
const fs = require('fs');

const filename = req.query.file;
// Validate filename
if (!filename || !/^[a-zA-Z0-9._-]+$/.test(filename)) {
    return res.status(400).send('Invalid filename');
}

const uploadDir = path.resolve('./uploads');
const filePath = path.join(uploadDir, filename);

// Ensure path is within intended directory
if (!filePath.startsWith(uploadDir)) {
    return res.status(403).send('Access denied');
}

fs.readFile(filePath, (err, data) => {
    if (err) return res.status(404).send('File not found');
    res.send(data);
});

Read the full file on GitHub · 404 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 404 lines · 2,694 tokens per session scan A 5fb8e807785a

Subscribe to this mod's changes

path-traversal-prevention is a cursor rule published in the GitHub repository matank001/cursor-security-rules (378 stars, last pushed 1y ago), licensed MIT. It adds 2,694 tokens to every session, about $0.0135 per session on Opus 5. A static security scan graded it A with 1 finding (makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.