Path Traversal on Cloudflare
How Path Traversal Manifests in Cloudflare
Path Traversal in Cloudflare contexts typically arises when user-controlled input is used to construct filesystem paths for operations such as log retrieval, asset serving, or worker environment file access. Because Workers run in a sandbox, direct filesystem access is limited, but unsafe handling of URLs and headers can still lead to unintended file exposure or access bypass when Cloudflare integrations or origin fetches are involved.
Attack patterns specific to Cloudflare include manipulating URL parameters that map to internal resource identifiers, such as zone_id or script_name, to traverse directories via sequences like ../../../etc/passwd. In environments where a Worker serves files from a virtual filesystem (e.g., via ASSETS or bindings), an attacker may exploit missing normalization to read files outside the intended directory scope.
Consider a Worker that serves static assets using a path parameter without validation:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function handleRequest(event) {
const url = new URL(event.request.url)
const filePath = url.searchParams.get('file')
// Unsafe: user-controlled filePath used directly
const fullPath = `/public/${filePath}`
const file = await ASSETS.get(fullPath)
return new Response(file, { status: 200 })
}
An attacker can supply ?file=../../../secrets/config.json to reach files outside /public. While Workers restrict access to the binding’s namespace, misconfigured mappings or integrations with KV/Namespace can still expose directory traversal risks when paths are concatenated without canonicalization.
Another pattern involves log or configuration retrieval endpoints where path parameters are used to select files from a predefined set. If the selection logic does not strictly validate against a whitelist, an attacker may attempt paths like ../../../../var/log/cloudflare/diagnostic.log to access sensitive logs through a compromised or misrouted origin fetch.
Cloudflare-Specific Detection
Detecting Path Traversal in Cloudflare environments requires analyzing how URLs and parameters map to internal resource identifiers, especially within Workers and load balancer configurations. Because Workers do not expose a traditional filesystem, the risk often stems from unsafe parameter usage in bindings, header manipulation, or origin fetches that rely on constructed paths.
To identify these issues, scan endpoints that accept path or query parameters used to reference assets, logs, or configuration resources. Look for patterns where user input directly influences file paths or keys without canonicalization or strict allowlisting. MiddleBrick’s LLM/AI Security checks include active prompt injection testing and output scanning, but for Path Traversal, focus on input validation and property authorization checks that examine parameter handling.
Using the middleBrick CLI, you can scan a Cloudflare-facing endpoint to detect potential traversal indicators:
$ middlebrick scan https://your-worker.example.com?file=../../../etc/passwd
The scan will flag unsafe parameter usage and missing validation, highlighting findings related to BFLA/Privilege Escalation and Input Validation. Because middleBrick tests the unauthenticated attack surface, it can identify endpoints where traversal attempts reveal unexpected behavior or information disclosure without requiring credentials.
Additionally, review Worker bindings and KV namespace mappings for overly permissive path concatenation. If a Worker uses parameters to reference objects in a namespace without sanitizing ../ sequences, the effective access boundary may be broader than intended, increasing the risk of unauthorized data exposure.
Cloudflare-Specific Remediation
Remediation for Path Traversal in Cloudflare Workers centers on strict input validation, path normalization, and avoiding direct concatenation of user input into filesystem or namespace paths. Use allowlists for permitted resources and canonicalize paths to eliminate traversal sequences before use.
For Workers serving assets, validate and sanitize the file parameter against a known set of safe values. Reject or default on any input containing .. or absolute path indicators:
function sanitizeFilePath(input) {
if (input.includes('..') || input.startsWith('/')) {
throw new Error('Invalid path')
}
return input
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function handleRequest(event) {
const url = new URL(event.request.url)
const rawPath = url.searchParams.get('file')
const safePath = sanitizeFilePath(rawPath)
const fullPath = `assets/${safePath}`
const file = await ASSETS.get(fullPath)
if (!file) {
return new Response('Not found', { status: 404 })
}
return new Response(file, { status: 200 })
}
When using Workers KV, ensure keys are constructed from trusted identifiers rather than raw user input. If dynamic key access is necessary, validate each segment against a pattern that excludes path traversal characters:
async function getSafeKVValue(namespace, userKey) {
const segments = userKey.split('/').filter(seg => seg && !seg.includes('..'))
if (segments.length !== 2) {
throw new Error('Invalid key structure')
}
return namespace.get(`${segments[0]}:${segments[1]}`)
}
For Cloudflare Load Balancers or origin configurations, enforce strict origin path rules and avoid incorporating user-supplied paths into origin requests. Use middleware or Workers to sanitize and rewrite URLs before they reach origin servers, ensuring that any path manipulation cannot escape the intended directory context.
By combining input validation, canonicalization, and restrictive binding usage, you can mitigate Path Traversal risks while maintaining the intended functionality of Cloudflare-hosted services.
Related CWEs: inputValidation
| CWE ID | Name | Severity |
|---|---|---|
| CWE-20 | Improper Input Validation | HIGH |
| CWE-22 | Path Traversal | HIGH |
| CWE-74 | Injection | CRITICAL |
| CWE-77 | Command Injection | CRITICAL |
| CWE-78 | OS Command Injection | CRITICAL |
| CWE-79 | Cross-site Scripting (XSS) | HIGH |
| CWE-89 | SQL Injection | CRITICAL |
| CWE-90 | LDAP Injection | HIGH |
| CWE-91 | XML Injection | HIGH |
| CWE-94 | Code Injection | CRITICAL |
Frequently Asked Questions
Can Path Traversal occur through Cloudflare Workers KV bindings?
../, an attacker may traverse logical namespaces. Always validate and restrict key segments.