Skip to content

fix(core): reject invalid UTF-8 directory paths in serve#38314

Open
extencil wants to merge 1 commit into
anomalyco:devfrom
extencil:directory-path-encoding
Open

fix(core): reject invalid UTF-8 directory paths in serve#38314
extencil wants to merge 1 commit into
anomalyco:devfrom
extencil:directory-path-encoding

Conversation

@extencil

@extencil extencil commented Jul 22, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #38235
Closes #37764

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

While running opencode serve, I got a session whose directory ended with two U+FFFD replacement characters: .../bin/��. The file picker rejected that path, and prompt_async failed because FileSystem.realPath tried to resolve it and got ENOENT.

I could reproduce the same path shape with a malformed route segment. For example, __8 decodes to the bytes FF FF, and the non-fatal TextDecoder turns those into ��. The app uses that as the directory, and the SDK sends it as %EF%BF%BD%EF%BF%BD in x-opencode-directory. InstanceContextMiddleware decodes the header, then InstanceStore calls path.resolve() on the relative �� value and ends up with <cwd>/��. That matches what showed up in my log.

I do not have the original browser URL from that run, so __8 is only a minimal reproduction of the bug, not a claim that it was the exact route segment. The database also had older session directories with literal U+FFFD characters, so values like this had already been persisted before.

The fix makes base64Decode use a fatal UTF-8 decoder. The app's decode64 wrapper already handles decode errors, so malformed route segments now go through the existing invalid-route behavior. For values that already contain U+FFFD, the HTTP routing, location, decoded instance context, and InstanceStore.load/reload paths fall back to process.cwd() instead of initializing that directory.

How did you verify your code works?

  • bun test test/server/httpapi-workspace-routing.test.ts test/project/instance.test.ts: 18 passed, 0 failed
  • bun turbo typecheck: 30 tasks passed
  • bun run script/build.ts --single --skip-embed-web-ui --skip-install: built the Linux x64 binary and passed its --version smoke test
  • Reproduced __8 -> �� -> %EF%BF%BD%EF%BF%BD -> <cwd>/�� with the old decoder, then confirmed the fatal decoder rejects the same bytes
  • Manually checked a valid non-ASCII base64 round trip

I reproduced the original failure with ./opencode serve --log-level DEBUG --pure --print-logs. I have not rerun that browser flow after adding the final instance-loading guard.

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

base64Decode silently produced U+FFFD replacement characters for invalid
UTF-8 bytes, which then poisoned workspace directories and broke the web UI.
Use TextDecoder fatal mode and fall back to cwd when paths contain U+FFFD.
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Jul 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

Web interface working directory path corruption chat fails with directory=/home/<username>/��

1 participant