Skip to content

fix: allow displaying images in plans from local filesystem#151

Closed
contextbotai[bot] wants to merge 2 commits into
mainfrom
cb-bot/task-1778882866544
Closed

fix: allow displaying images in plans from local filesystem#151
contextbotai[bot] wants to merge 2 commits into
mainfrom
cb-bot/task-1778882866544

Conversation

@contextbotai
Copy link
Copy Markdown
Contributor

@contextbotai contextbotai Bot commented May 15, 2026

Summary

Fixes PLAN-55 — images in plans that reference local file paths (e.g. /Users/<user>/.codex/generated_images/...) now render correctly in the plan review UI.

Problem

When a plan contains markdown images with absolute file paths like:

![diagram](/Users/alice/.codex/generated_images/019e.../image.png)

The browser resolves these relative to the localhost server origin, producing requests like http://localhost:<port>/Users/alice/.codex/generated_images/.... The annotation server had no handler for these paths and returned 404, causing broken images.

Solution

Added a serveLocalImage handler to the annotation server (packages/server/src/annotation.ts) that:

  1. Checks if an incoming GET request path has an allowed image extension (.png, .jpg, .jpeg, .gif, .svg, .webp, .avif, .ico, .bmp)
  2. Normalizes the path (handling percent-encoding and .. segments) to prevent traversal
  3. Verifies the file exists on disk
  4. Serves it via Bun.file() which automatically sets the correct Content-Type

This is placed as a fallback route — it only fires after all named routes (/, /config, /payload, /update-notice, /submit) have been checked, so it cannot shadow existing functionality.

Safety considerations

  • Only image file extensions are served (non-image paths like /etc/passwd return 404)
  • Path is normalized via node:path.normalize and verified to still be absolute after normalization
  • The server is ephemeral (lives only for the duration of a plan review) and only listens on localhost

Verification

  • All 25 server tests pass (10 new tests added for serveLocalImage + integration)
  • All 327 CLI tests pass
  • TypeScript type checking passes
  • ESLint passes with no errors
  • Tests cover: existing images, nonexistent images, non-image extensions, percent-encoded paths, path traversal, and the full server integration flow

When plans contain markdown images with absolute file paths (e.g.
![alt](/Users/.../generated_images/image.png)), the browser resolves
them relative to the localhost server origin. Previously these requests
returned 404, causing broken images.

Add a serveLocalImage handler to the annotation server that serves
files from disk when the request path has an allowed image extension
(.png, .jpg, .jpeg, .gif, .svg, .webp, .avif, .ico, .bmp) and the
file exists. Path traversal is mitigated via normalize + absolute path
check. Non-image extensions are rejected to limit exposure.

Closes PLAN-55
@contextbotai contextbotai Bot requested review from blimmer and jcarver989 as code owners May 15, 2026 22:14
@jcarver989
Copy link
Copy Markdown
Contributor

@contextbotai - review this diff, compare it against conventions and rules in this codebase and refactor to follow instructions/ rules / conventions.

Replace the synchronous existsSync from node:fs with the idiomatic
Bun.file(path).exists() API in serveLocalImage. This follows the
bun-native-apis convention which requires preferring Bun.* globals
over Node equivalents in server code.

The function becomes async (returning Promise<Response | null>) since
Bun.file().exists() is async, which is fine given it's only called
from within the async fetch handler.
@blimmer blimmer changed the title PLAN-55: fix plan images rendering as broken localhost file URLs fix: plan images rendering as broken localhost file URLs May 18, 2026
@blimmer blimmer changed the title fix: plan images rendering as broken localhost file URLs fix: allow displaying images in plans from local filesystem May 18, 2026
@blimmer
Copy link
Copy Markdown
Contributor

blimmer commented May 18, 2026

I don't know that I love this pattern of serving images from disk. This would be problematic when we allow sharing plans. I think we should explore some other paths, like having the server pull the image and directly encoding it the payload, etc.

@contextbotai
Copy link
Copy Markdown
Contributor Author

contextbotai Bot commented May 18, 2026

Thanks for your feedback! We're actively building the ability to handle comments on ContextBot PRs automatically. We'll take a look at your comment shortly. In the meantime, come chat with the founders on Slack. We'd love to hear from you!


This is an automated response from ContextBot.

@blimmer
Copy link
Copy Markdown
Contributor

blimmer commented May 18, 2026

I think we should use another approach (e.g., #165)

@blimmer blimmer closed this May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants