Summary
Banana Slides through 0.4.0 contains a path traversal flaw in its AI service backend that allows crafted markdown image references to escape the intended uploads directory.
Because the directory restriction relies on an incomplete path prefix check, unauthenticated attackers can trick the service into reading arbitrary image-format files from sibling directories on the server filesystem.
Affected context
This vulnerability appears in the Banana Slides AI service backend when the generate_image() function processes user-controlled markdown image references in page descriptions.
The issue is exposed in deployments where:
- User input is rendered into markdown that is later resolved to file paths, and
- The backend attempts to confine file access to an uploads directory using an os.path.startswith() check without a trailing path separator, and
- The backend uses PIL Image.open() to read and process image files based on the derived paths.
Under these conditions, sibling directories whose names share the uploads directory prefix can be unintentionally treated as valid targets.
Exploit preconditions
- Attack surface: Public-facing Banana Slides AI service endpoint that resolves markdown image references into local filesystem paths.
- Required access: No prior authentication is strictly required if untrusted users can create or edit page descriptions; attackers only need the ability to supply markdown content containing image references.
- Trigger point: Submission or rendering of a page description with a crafted image URL that resolves to a path beginning with the same string as the uploads directory but pointing into a different filesystem location (e.g., a sibling directory with a shared prefix).
- Why exploitation is possible: The confinement check uses os.path.startswith() against the uploads directory path without enforcing a trailing path separator or normalizing the target path robustly.
This allows paths that merely share a textual prefix with the uploads directory to pass validation, even though they reside outside the intended directory, enabling PIL Image.open() to read unintended files.
Why this happens
The root cause is an incomplete directory confinement check that relies on string prefix comparison instead of path-aware validation.
By using os.path.startswith() on the computed path without requiring a trailing separator or performing canonical path resolution, the code conflates textual prefix similarity with actual directory containment.
This aligns with CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), where user-controlled path components are insufficiently constrained before being used in filesystem operations.
Conceptually, the bug is: "trusting that a string beginning with the upload directory's path is necessarily inside that directory," rather than verifying the normalized path's parent directory structure.
Potential impact
- Attackers may read arbitrary image-format files (e.g., PNG, JPEG, GIF, TIFF) located outside the intended uploads directory but accessible to the application process.
- Sensitive information stored as or embedded in image files (such as screenshots, design assets, or exported reports) could be exposed.
- Indirect leakage of environment details or operational data if such information is stored in image form in nearby directories.
- Facilitation of further attacks by revealing information about directory layouts, file naming conventions, or deployment practices inferred from accessible files.
General mitigation guidance
Effective mitigation focuses on robust directory confinement and defensive path handling:
- Use canonical, path-aware checks (e.g., resolve and normalize the user-supplied path, then verify that the resulting absolute path is a descendant of the intended uploads directory using filesystem semantics, not string prefix comparisons).
- Explicitly enforce a trailing path separator on the base directory and use standard library helpers for joining and resolving paths to prevent ambiguity (e.g., join base and user segments rather than letting users supply absolute or parent-directory components).
- Treat all user-controlled path segments as untrusted and restrict them to a whitelist of expected patterns or identifiers (such as UUIDs or database-backed resource IDs) instead of accepting raw filenames or directory fragments.
- Keep the uploads directory and any image-processing directories isolated from configuration files, source code, logs, or other sensitive assets via directory layout and OS-level permissions.
- Run the AI backend and image processing components with the least filesystem privileges necessary, limiting the blast radius if a traversal bug occurs.
How teams detect issues like this
Detection can combine static and review-based approaches:
- Static analysis: Flag uses of string methods like startswith(), endswith(), or simple concatenation when validating or constructing filesystem paths derived from user input.
- Code review: Look for any path validation logic that:
- Fails to normalize paths (no realpath/abspath/canonicalization step),
- Relies on textual prefix checks instead of checking directory ancestry, or
- Passes user-derived paths directly into file APIs such as open() or PIL Image.open() without strong validation.
- Policy enforcement: Define coding standards that mandate:
- Use of centralized, vetted helpers for path resolution and directory confinement,
- Prohibition on direct use of user-supplied paths in IO functions.
- Automated tests: Add regression tests that attempt to access sibling or parent directories using crafted filenames and verify that such requests are consistently rejected across endpoints that touch the filesystem.

