ai newsDi., 1. Dez.

Path traversal in AI-built apps: when ../ in a download link reads your .env (and a safe test)

Path traversal is when a user controls part of a file path and uses ../ to climb out of the folder you meant to serve. A route like /download?file=report.pdf becomes /download?file=../../.env , and your server hands over its secrets. AI-built apps add download, preview and "serve this upload" route

dev.toSonntag 01:40 · 29.11.1 Min. Lesezeit
Bild: dev.to

Path traversal is when a user controls part of a file path and uses../ to climb out of the folder you meant to serve. A route like /download?file=report.pdf becomes /download?file=../../.env, and your server hands over its secrets. AI-built apps add download, preview and "serve this upload" routes all the time, and the path handling is often one line.

app. get ( "/download", ( req, res ) => { res. sendFile ( path. join ( __dirname, "uploads", req. query. file ) ); } ); Enter fullscreen mode Exit fullscreen mode path.join does not stop../. Safer:

const base = path. resolve ( __dirname, "uploads" ); const target = path. resolve ( base, req. query. file ); if (! target. startsWith ( base + path. sep ) ) return res. status ( 400 ). end ( ); res. sendFile ( target ); Enter fullscreen mode Exit fullscreen mode Better still: store files under random IDs in a database, and let users ask for the ID, not a path.

An outside scan can check common exposed files, such as.env or.git, at the paths a stranger would try first. It does not probe your custom download routes, so test those by hand.

Check your own app from the outside, free. Run the free ProofForge scan on your live URL. No account needed for the first scan. It looks at your public site the way a stranger would and says pass, fail or inconclusive, so you know what is confirmed and what it could not see. One-time reports start at $9: see pricing. Building with an AI agent? There is an MCP server so it can check before it deploys.

Quelle: dev.to (dev.to). Original öffnen