Skip to content

feat: Pass through data URLs directly and gracefully handle unknown U…#12592

Open
yxshee wants to merge 3 commits intoanomalyco:devfrom
yxshee:clipboard-paste-fix
Open

feat: Pass through data URLs directly and gracefully handle unknown U…#12592
yxshee wants to merge 3 commits intoanomalyco:devfrom
yxshee:clipboard-paste-fix

Conversation

@yxshee
Copy link
Contributor

@yxshee yxshee commented Feb 7, 2026

Summary

Fixes #12547

Error: TypeError: The URL must be of scheme file (ERR_INVALID_URL_SCHEME)

Root Cause

In prompt.ts, the switch statement at line 946 handles URL protocols:

  • case "data:" only processes text/plain mime types, then breaks
  • For clipboard images (image/png), the condition fails and falls through to case "file:"
  • fileURLToPath() is called on a data: URL → crash

Changes

packages/opencode/src/session/prompt.ts

  • Added proper handling for non-text data: URLs (images, PDFs) - returns them as-is for the model
  • Added default case to prevent silent fall-through for unknown protocols

Why This Happens on Windows

Windows clipboard paste encodes images as data:image/png;base64,... URLs via FileReader.readAsDataURL(). The server-side switch statement wasn't handling this case properly.

Testing

  • Paste image from clipboard on Windows 11 - no crash, image attaches correctly
  • File drop still works
  • File picker still works
  • Paste text still works

Checklist

  • Minimal fix addressing root cause
  • No try-catch band-aids
  • Follows existing code patterns

Copilot AI review requested due to automatic review settings February 7, 2026 09:30
@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2026

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
Copy link
Contributor

github-actions bot commented Feb 7, 2026

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate Found

PR #12549: fix(desktop): windows clipboard images come thru as data not file
#12549

This PR is directly related to the current PR #12592. Both address the same issue: handling clipboard images on Windows that come through as data: URLs instead of file URLs. PR #12549 appears to be a prior fix for the same problem, so you should check if it's already merged or if it needs to be superseded/combined with PR #12592.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a crash when handling clipboard-pasted images (e.g., Windows 11 data:image/png;base64,...) by preventing data: URLs from falling through to file: handling and by adding a safe fallback for previously-unhandled URL protocols in SessionPrompt.

Changes:

  • Pass through non-text/plain data: URL file parts (e.g., images/PDFs) without attempting fileURLToPath().
  • Add a default branch in the URL protocol switch to avoid unhandled protocols causing failures/dropped parts.
  • Add warning log for unknown protocols (with URL prefix logging).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default-case log includes a prefix of the full URL (part.url.slice(0, 100)), which can leak sensitive user data (e.g., signed URLs/tokens in query strings). Consider logging only the protocol plus a redacted/sanitized identifier (e.g., hostname for http(s), or just url.protocol/mime/length), and avoid logging the URL itself at warn level.

Suggested change
log.warn("unsupported URL protocol", { protocol: url.protocol, url: part.url.slice(0, 100) })
log.warn("unsupported URL protocol", {
protocol: url.protocol,
mime: part.mime,
filename: part.filename,
})

Copilot uses AI. Check for mistakes.
Comment on lines 1169 to 1172
default:
// Handle unknown protocols gracefully to prevent crashes
log.warn("unsupported URL protocol", { protocol: url.protocol, url: part.url.slice(0, 100) })
return [
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default branch labels any non-data:/file: URL as "unsupported" and emits a warn. The codebase can create http: URLs for image parts (see packages/opencode/src/acp/agent.ts where part.uri.startsWith("http:") becomes a file part), and those may be valid to pass through to providers. Consider adding explicit case "http:":/"https:": to pass through without warning, or change the log message/level to reflect "unhandled" rather than "unsupported" to avoid noisy/misleading logs.

Copilot uses AI. Check for mistakes.
Comment on lines 974 to 983
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change introduces new behavior for non-text data: URLs and unknown protocols. There are existing SessionPrompt tests under packages/opencode/test/session/; please add coverage for (1) data:image/png;base64,... parts to ensure they no longer crash/are preserved, and (2) a non-file:/data: URL (e.g., http:) to ensure it’s handled consistently (and not dropped).

Copilot uses AI. Check for mistakes.
When pasting images from clipboard on Windows 11, the app crashed with:
TypeError: The URL must be of scheme file (ERR_INVALID_URL_SCHEME)
Root cause: The data: case only handled text/plain and broke, causing
non-text data URLs to fall through to file: case which called
fileURLToPath() on a data: URL.
Fix:
- Return non-text data URLs directly instead of breaking
- Add default case to prevent fall-through for unknown protocols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on Windows when pasting images from clipboard (ERR_INVALID_URL_SCHEME)

1 participant

Comments