Repository navigation
Fix Shift resource drops into desktop webviews - #344153
YuriHead (ylzz1997) wants to merge 4 commits into
Conversation
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
🟡 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
|
Tested on Windows 11 with a dev build of 78a481c (Electron 43.8.6, The CSP hash in With only the hash corrected, Shift-drops into a custom editor webview (
So this also covers the tree-view case reported in #339036, as long as the controller lists |
Nick Trogh (ntrogh)
left a comment
There was a problem hiding this comment.
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
-
Regenerate the CSP hash in
src/vs/workbench/contrib/webview/browser/pre/index.html
After mergingmain, thescript-src 'sha256-...'hash no longer matches the inline<script>, so Chromium blocks the preload script and webviews render blank (andwebviewShellMessaging.test.tsfails). Please rebase/mergemainand regenerate the hash. -
Prevent
handleInnerDragEventfrom callingpreventDefault()on forwardeddragoverevents
pre/index.htmlalready attacheshandleInnerDragEventtocontentWindow'sdragoverevent and unconditionally callse.preventDefault(). Because forwardeddragoverevents are dispatched withbubbles: true, cancelable: true, they bubble up tocontentWindowand always end up withevent.defaultPrevented === true—even in webviews with no drag handler at all (e.g. Markdown preview, READMEs), causing them to falsely reportdropEffect = 'copy'and accept drops.
Please guardhandleInnerDragEvent(andhandleInnerDragStartEvent/handleInnerDropEvent) so they ignore forwarded/synthetic drag events (for example via anisDispatchingWindowDragflag orif (!e.isTrusted) { return; }), and add a test withhandleInnerDragEventattached tocontentWindow. -
Fix
target === windowDragTargetsilently discarding valid drops
Inpre/index.html,acceptsDroprequirestarget === windowDragTarget. If the pointer shifts even 1px between the lastdragoveranddroponto 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,elementFromPointreturns a different element and the drop is silently discarded.
Whentargetchanges ondropfor the same active drag, consider dispatching a syntheticdragoveron the newtargetright beforedropto verify acceptance rather than rejecting whenevertarget !== windowDragTarget. -
Broaden internal drag detection in
src/vs/workbench/contrib/webview/browser/webviewWindowDragMonitor.ts
isDraggingResourcecurrently only matchesResourceURLs,CodeFiles, andapplication/vnd.code.uri-list. This leaves other internal workbench drags broken on Electron 43.7+, notably extensionTreeDragAndDropControllerdrags (application/vnd.code.tree.*) that don't includetext/uri-list(as reported in #339036), as well as editor/text drags. Please broaden the detection (e.g. includeapplication/vnd.code.tree.and editor/text drags, or forward internal workbench drags more generally).
Recommended improvements
- Skip reading/sending drag payload on
dropwhen_windowDragDropEffect === 'none': InWebviewElement.windowDidDrag, if the host already knows the webview rejected the hover (this._windowDragDropEffect === 'none'), short-circuiting before callingtransfer.getData(mime)avoids sending the drag payload over theMessagePortaltogether. - Construct
DataTransferandDragEventfromframe.contentWindow: Currentlynew DataTransfer()andnew DragEvent(...)use the outer shell frame's constructors, soe instanceof DragEventande.dataTransfer instanceof DataTransferevaluate tofalseinside the extension's iframe. Constructing fromframe.contentWindow(with a fallback) avoids breakinginstanceofchecks in webview code. - Pierce Shadow DOM during hit-testing:
contentDocument.elementFromPoint(x, y)stops at the shadow host for web components; walkingel.shadowRoot?.elementFromPoint(x, y)ensures events reach the inner target.


Fixes #339036
Internal resource drags from the Explorer can be rejected by Electron before
dragoverordropreaches 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:
webviewElement.test,webviewWindowDragMonitor.test,webviewWindowDrag.testandwebviewShellMessaging.test, including the preload CSP hash check.WebviewPaneland aWebviewView. 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
dragoverhandler that callspreventDefault()and adrophandler that readstext/uri-list. Drag an Explorer file onto it while holding Shift. The handler should receive the URI, including on repeated drops. Repeat in aWebviewView; then setdataTransfer.dropEffect = 'none'in itsdragoverhandler and confirm that the drop is rejected. Releasing Shift should return the drag to the workbench's normal handling.