Sanitize upload path traversal

Write codeMedium25 minStripe and Amazon style

Your task

Sanitize an untrusted upload path so it cannot escape a base directory. Given a base directory like '/srv/uploads' and an untrusted relative path (which may contain '..', '.', leading slashes, or empty segments), resolve it WITHOUT touching the filesystem: split on '/', drop '.' and empty segments, and for each '..' pop the last accumulated segment (never popping above the base). Return the joined absolute path as base + '/' + the safe remainder. If the remainder is empty, return the base unchanged.

Examples

safe_join(base: str, untrusted: str) -> str
Example 1
Inputsafe_join("/srv/uploads", "a/b/c.txt")
Output"/srv/uploads/a/b/c.txt"