Skip to content

Fix Shift resource drops into desktop webviews - #344153

Open
YuriHead (ylzz1997) wants to merge 4 commits into
microsoft:mainfrom
ylzz1997:codex/fix-webview-shift-drag
Open

YuriHead (ylzz1997) wants to merge 4 commits into
microsoft:mainfrom
ylzz1997:codex/fix-webview-shift-drag

Conversation

@ylzz1997

@ylzz1997 YuriHead (ylzz1997) commented Oct 25, 2026 •

Copy link
Copy Markdown

Fixes #339036

Internal resource drags from the Explorer can be rejected by Electron before dragover or drop reaches a cross-site webview iframe, even while Shift is held. This restores Shift-dropping files and folders into desktop webviews without disabling Electron's cross-site drag restriction.

Keep internal resource drags in the workbench and forward them through the existing webview messaging channel while Shift is held over the visible webview. The preload reconstructs DOM drag events, including enter/leave transitions, transfer types, relative coordinates, modifiers and drop-effect feedback. Transfer contents are only read on drop; hovering exposes types with empty values. Cancellation, leaving the target and releasing Shift clear the forwarded drag state, and stale feedback from a previous drag is ignored. The preload independently rejects queued drops unless the current target accepted the latest hover of that drag. Resource detection includes internal URI lists for remote folders, and target checks accept nodes adopted into auxiliary windows.

The existing Shift gesture is retained. Unmodified workbench drags, external file drags and browser builds keep their existing routing. Forwarded DOM events are synthetic (isTrusted === false); no native security restriction is bypassed.

Validation:

  • 67 targeted tests passed across webviewElement.test, webviewWindowDragMonitor.test, webviewWindowDrag.test and webviewShellMessaging.test, including the preload CSP hash check.
  • Client type check, targeted ESLint, client build and pre-commit hygiene passed.
  • 22 native drag scenarios passed on the initial patch on macOS arm64 with the source-built Code - OSS 1.144 / Electron 43.8.3, using a minimal extension with both a WebviewPanel and a WebviewView. Mouse drags and modifier keys were sent through macOS input events, rather than injecting drag events into the iframe. Coverage includes repeated drops, holding Shift after entering the target, Unicode/spaces/quotes in filenames, folders, multiple selections, nested targets, rejection and recovery, Escape cancellation, releasing Shift and unmodified drags. An additional 10-case native WebviewView regression run passed after the queued-drop, auxiliary-window and remote-folder corrections. The new regression tests first reproduced all three defects (7 failures) and then passed with the fixes. Windows and Linux native UI behavior has not been verified locally.

To verify manually, create a script-enabled webview with a dragover handler that calls preventDefault() and a drop handler that reads text/uri-list. Drag an Explorer file onto it while holding Shift. The handler should receive the URI, including on repeated drops. Repeat in a WebviewView; then set dataTransfer.dropEffect = 'none' in its dragover handler and confirm that the drop is rejected. Releasing Shift should return the drag to the workbench's normal handling.

Copilot AI balanced review requested due to automatic review settings October 25, 2026 18:20
@ylzz1997

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Auxiliary-window targeting, remote-folder detection, and queued-drop rejection need correction before approval.

3 open issues
What changed in this PR

This PR addresses #339036 by forwarding Shift-held internal resource drags through desktop webview messaging, without disabling Electron’s cross-site drag restriction.

Changes:

  • Adds host-side resource drag detection, forwarding, and feedback handling.
  • Reconstructs synthetic drag events in the preload, exposing transfer contents only on drop.
  • Adds tests for routing, metadata, target transitions, cancellation, and stale feedback.
File Description
src/​vs/​workbench/​contrib/​webview/​test/​electron-browser/​webviewWindowDragMonitor.test.ts Tests internal and native drag routing.
src/​vs/​workbench/​contrib/​webview/​test/​electron-browser/​webviewWindowDrag.test.ts Tests preload drag events and feedback.
src/​vs/​workbench/​contrib/​webview/​test/​browser/​webviewElement.test.ts Tests host forwarding and state cleanup.
src/​vs/​workbench/​contrib/​webview/​browser/​webviewWindowDragMonitor.ts Detects and routes desktop resource drags.
src/​vs/​workbench/​contrib/​webview/​browser/​webviewMessages.d.ts Defines drag and feedback messages.
src/​vs/​workbench/​contrib/​webview/​browser/​webviewElement.ts Adds target checks, serialization, and feedback handling.
src/​vs/​workbench/​contrib/​webview/​browser/​webview.ts Extends the webview drag interface.
src/​vs/​workbench/​contrib/​webview/​browser/​pre/​index.html Reconstructs drag events and updates the CSP hash.
src/​vs/​workbench/​contrib/​webview/​browser/​overlayWebview.ts Delegates forwarded drag events.

Review effort: Balanced

@Jajaho

Copy link
Copy Markdown

Tested on Windows 11 with a dev build of 78a481c (Electron 43.8.6, scripts\code.bat).

The CSP hash in pre/index.html is stale after the merge of 11-09. In 78a481c, script-src lists sha256-29YeDbmHnhN2BgMJKVq7vDCMiuya6rpEctI6SkJFfJc=, the hash of the inline script on main at the time of the merge. The merged script (LF line endings) hashes to sha256-3RBijb0KK1hDwzwBYJaL0iqenFZzbdPT+1euncCdteg=. Chromium blocks the script, and every webview stays blank, so none of the forwarding runs. 48fc622 and 5463a5a had updated the hash; 78a481c took main's. main has changed the hash again since (mJlW7V1H…), so it needs to be regenerated after the next merge or rebase.

With only the hash corrected, Shift-drops into a custom editor webview (registerCustomEditorProvider, retainContextWhenHidden) work:

Drag source (Shift held) 78a481c with the hash corrected Same build with the PR's diff reversed
Explorer file forwarded; dragover and drop arrive (isTrusted: false, types text/plain, text/uri-list, ResourceURLs, CodeFiles, chromium/x-drag-id, CodeEditors, application/vnd.code.uri-list) no event reaches the webview
Item of an extension's tree view (TreeDragAndDropController, dragMimeTypes = own type, text/plain, text/uri-list) forwarded; dragover and drop arrive no event reaches the webview
File from the Windows File Explorer arrives natively (isTrusted: true) arrives natively

So this also covers the tree-view case reported in #339036, as long as the controller lists text/uri-list: addExtensionProvidedTransferTypes in treeView.ts sets ResourceURLs only then, and WebviewWindowDragMonitor detects a resource drag by ResourceURLs, CodeFiles or application/vnd.code.uri-list. A tree whose dragMimeTypes lack text/uri-list is not forwarded. Also matching the application/vnd.code.tree.* types in that check would cover it (not tested).

@ntrogh Nick Trogh (ntrogh) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for putting this together—the overall host-mediated forwarding approach and security posture (hiding DataTransfer values until drop, auxiliary-window handling, remote folders, and test coverage) look solid.

Before this can be merged, a few changes are needed:

Required changes

  1. Regenerate the CSP hash in src/vs/workbench/contrib/webview/browser/pre/index.html
    After merging main, the script-src 'sha256-...' hash no longer matches the inline <script>, so Chromium blocks the preload script and webviews render blank (and webviewShellMessaging.test.ts fails). Please rebase/merge main and regenerate the hash.

  2. Prevent handleInnerDragEvent from calling preventDefault() on forwarded dragover events
    pre/index.html already attaches handleInnerDragEvent to contentWindow's dragover event and unconditionally calls e.preventDefault(). Because forwarded dragover events are dispatched with bubbles: true, cancelable: true, they bubble up to contentWindow and always end up with event.defaultPrevented === true—even in webviews with no drag handler at all (e.g. Markdown preview, READMEs), causing them to falsely report dropEffect = 'copy' and accept drops.
    Please guard handleInnerDragEvent (and handleInnerDragStartEvent / handleInnerDropEvent) so they ignore forwarded/synthetic drag events (for example via an isDispatchingWindowDrag flag or if (!e.isTrusted) { return; }), and add a test with handleInnerDragEvent attached to contentWindow.

  3. Fix target === windowDragTarget silently discarding valid drops
    In pre/index.html, acceptsDrop requires target === windowDragTarget. If the pointer shifts even 1px between the last dragover and drop onto a child/sibling element inside the same drop zone (e.g. from a container <div> onto a <textarea>, <span>, or icon), or if the webview renders a drop overlay on hover, elementFromPoint returns a different element and the drop is silently discarded.
    When target changes on drop for the same active drag, consider dispatching a synthetic dragover on the new target right before drop to verify acceptance rather than rejecting whenever target !== windowDragTarget.

  4. Broaden internal drag detection in src/vs/workbench/contrib/webview/browser/webviewWindowDragMonitor.ts
    isDraggingResource currently only matches ResourceURLs, CodeFiles, and application/vnd.code.uri-list. This leaves other internal workbench drags broken on Electron 43.7+, notably extension TreeDragAndDropController drags (application/vnd.code.tree.*) that don't include text/uri-list (as reported in #339036), as well as editor/text drags. Please broaden the detection (e.g. include application/vnd.code.tree. and editor/text drags, or forward internal workbench drags more generally).

Recommended improvements

  • Skip reading/sending drag payload on drop when _windowDragDropEffect === 'none': In WebviewElement.windowDidDrag, if the host already knows the webview rejected the hover (this._windowDragDropEffect === 'none'), short-circuiting before calling transfer.getData(mime) avoids sending the drag payload over the MessagePort altogether.
  • Construct DataTransfer and DragEvent from frame.contentWindow: Currently new DataTransfer() and new DragEvent(...) use the outer shell frame's constructors, so e instanceof DragEvent and e.dataTransfer instanceof DataTransfer evaluate to false inside the extension's iframe. Constructing from frame.contentWindow (with a fallback) avoids breaking instanceof checks in webview code.
  • Pierce Shadow DOM during hit-testing: contentDocument.elementFromPoint(x, y) stops at the shadow host for web components; walking el.shadowRoot?.elementFromPoint(x, y) ensures events reach the inner target.

This branch has not been deployed

No deployments
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.

Regression - VS Code Secondary Sidebar Plugin Drop-down

5 participants