{"record":{"id":"e6b6c8e6ba91048c","repo":"BerriAI/litellm","slug":"invalid-file-path-file-path-r-contains-url-spec","errorCode":null,"errorMessage":"Invalid file path {file_path!r}: contains URL special characters","messagePattern":"Invalid file path (.+?): contains URL special characters","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/integrations/bitbucket/bitbucket_client.py","lineNumber":15,"sourceCode":"\"\"\"\nBitBucket API client for fetching .prompt files from BitBucket repositories.\n\"\"\"\n\nimport base64\nimport urllib.parse\nfrom typing import Any, Final\n\nfrom litellm.llms.custom_httpx.http_handler import HTTPHandler\n\n\ndef _sanitize_file_path(file_path: str) -> str:\n    \"\"\"Reject path traversal and URL-encode each path segment.\"\"\"\n    if \"#\" in file_path or \"?\" in file_path:\n        raise ValueError(f\"Invalid file path {file_path!r}: contains URL special characters\")\n    parts: Final = file_path.split(\"/\")\n    for part in parts:\n        if part == \"..\":\n            raise ValueError(f\"Invalid file path {file_path!r}: path traversal detected\")\n    return \"/\".join(urllib.parse.quote(part, safe=\"\") for part in parts)\n\n\nclass BitBucketClient:\n    \"\"\"\n    Client for interacting with BitBucket API to fetch .prompt files.\n\n    Supports:\n    - Authentication with access tokens\n    - Fetching file contents from repositories\n    - Team-based access control through BitBucket permissions\n    - Branch-specific file fetching\n    \"\"\"\n","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/integrations/bitbucket/bitbucket_client.py#L1-L33","documentation":"Before building the BitBucket src URL, _sanitize_file_path rejects any file path containing '#' or '?'. Those characters start a URL fragment/query respectively and would silently truncate or redirect the request to a different resource, so they are treated as invalid input rather than being percent-encoded (every other segment character IS encoded, so spaces and unicode are fine).","triggerScenarios":"Passing 'prompts/step#2.prompt' or 'triage.prompt?version=3' to get_file_content/list_files; UI or LLM-generated paths that carry query syntax; porting code from the BitBucket web UI where '?at=branch' URLs are normal and assuming the API accepts them too.","commonSituations":"Developers copy file URLs from bitbucket.org (which include ?at=refs%2Fheads%2Fmain) instead of the bare repo-relative path; templating systems that append cache-busting query strings.","solutions":["Strip the query/fragment: branch selection belongs in bitbucket_config (branch key), not the path","Rename the file if it genuinely contains '#' or '?' in its name","Pass clean repo-relative paths like 'prompts/triage.prompt'","Validate/normalize user-supplied paths before they reach the client"],"exampleFix":"// before\nclient.get_file_content(\"prompts/triage.prompt?at=feature%2Fx\")\n// after (branch goes in config, path stays bare)\nBitBucketClient({\"workspace\": \"acme\", \"repository\": \"prompts\",\n                 \"access_token\": tok, \"branch\": \"feature/x\"})\nclient.get_file_content(\"prompts/triage.prompt\")","handlingStrategy":"validation","validationCode":"def is_clean_repo_path(p: str) -> bool:\n    return bool(p) and \"#\" not in p and \"?\" not in p and \"..\" not in p.split(\"/\")\n\nif not is_clean_repo_path(file_path):\n    raise ValueError(f\"refusing to fetch path with URL special characters or traversal: {file_path!r}\")","typeGuard":"def is_clean_repo_path(p: str) -> bool:\n    \"\"\"True when the path contains no '#', '?', or '..' segments.\"\"\"\n    return bool(p) and \"#\" not in p and \"?\" not in p and \"..\" not in p.split(\"/\")","tryCatchPattern":null,"preventionTips":["Never paste URLs from the BitBucket web UI as paths — strip '?at=...' query parts","Put branch selection in bitbucket_config, never in the file path","Validate user/LLM-supplied paths against the same two rules before calling the client","Encode nothing yourself: the client percent-encodes every other character"],"tags":["bitbucket","file-path","url-validation","input-sanitization"],"backgroundTag":"invalid-file-path","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}