Repository navigation
fix(desktop): shell-quote the target of open() and launch() #2144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PrabalParihar
wants to merge
1
commit into
e2b-dev:main
Choose a base branch
from
PrabalParihar:fix/desktop-quote-open-launch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@e2b/desktop': patch | ||
| '@e2b/desktop-python': patch | ||
| --- | ||
|
|
||
| `open()` and `launch()` now shell-quote the file, URL, application and URI they pass to `xdg-open` / `gtk-launch`. Before, a URL with `&` in its query string was cut at the `&` (the rest ran as a separate shell command), and a path with spaces was split into several arguments. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { execFileSync } from 'node:child_process' | ||
| import { expect, test } from 'vitest' | ||
|
|
||
| import { Sandbox } from '../src' | ||
|
|
||
| // Commands run in the sandbox through `/bin/bash -l -c <cmd>`. Run the command | ||
| // the SDK builds through bash with `xdg-open` / `gtk-launch` replaced by a | ||
| // function that prints each argument it receives, one per line. | ||
| function argvSeenBy(program: string, command: string): string[] { | ||
| const stub = `${program}() { printf '%s\\n' "$@"; }; ` | ||
| return execFileSync('bash', ['-c', stub + command], { encoding: 'utf8' }) | ||
| .split('\n') | ||
| .filter((line) => line.length > 0) | ||
| } | ||
|
|
||
| function recordingSandbox(): { sandbox: Sandbox; commands: string[] } { | ||
| const commands: string[] = [] | ||
| const sandbox = Object.create(Sandbox.prototype) as Sandbox | ||
| Object.defineProperty(sandbox, 'commands', { | ||
| value: { | ||
| run: async (cmd: string) => { | ||
| commands.push(cmd) | ||
| return { disconnect: async () => {} } | ||
| }, | ||
| }, | ||
| }) | ||
| return { sandbox, commands } | ||
| } | ||
|
|
||
| const skipOnWindows = test.skipIf(process.platform === 'win32') | ||
|
|
||
| skipOnWindows( | ||
| 'open passes a URL with a query string as one argument', | ||
| async () => { | ||
| const { sandbox, commands } = recordingSandbox() | ||
| const url = 'https://example.com/search?q=e2b&page=2#results' | ||
|
|
||
| await sandbox.open(url) | ||
|
|
||
| expect(commands).toHaveLength(1) | ||
| expect(argvSeenBy('xdg-open', commands[0])).toEqual([url]) | ||
| } | ||
| ) | ||
|
|
||
| skipOnWindows( | ||
| 'open passes a file path with spaces and quotes as one argument', | ||
| async () => { | ||
| const { sandbox, commands } = recordingSandbox() | ||
| const path = "/home/user/Q3 report (Bob's copy).pdf" | ||
|
|
||
| await sandbox.open(path) | ||
|
|
||
| expect(argvSeenBy('xdg-open', commands[0])).toEqual([path]) | ||
| } | ||
| ) | ||
|
|
||
| skipOnWindows( | ||
| 'launch passes the application and the URI as separate arguments', | ||
| async () => { | ||
| const { sandbox, commands } = recordingSandbox() | ||
| const uri = 'https://example.com/?a=1&b=2' | ||
|
|
||
| await sandbox.launch('google-chrome', uri) | ||
|
|
||
| expect(argvSeenBy('gtk-launch', commands[0])).toEqual([ | ||
| 'google-chrome', | ||
| uri, | ||
| ]) | ||
| } | ||
| ) | ||
|
|
||
| skipOnWindows('launch without a URI passes only the application', async () => { | ||
| const { sandbox, commands } = recordingSandbox() | ||
|
|
||
| await sandbox.launch('google-chrome') | ||
|
|
||
| expect(argvSeenBy('gtk-launch', commands[0])).toEqual(['google-chrome']) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| from e2b_desktop import Sandbox | ||
|
|
||
| # Commands run in the sandbox through `/bin/bash -l -c <cmd>`. Run the command | ||
| # the SDK builds through bash with `xdg-open` / `gtk-launch` replaced by a | ||
| # function that prints each argument it receives, one per line. | ||
| pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="needs bash") | ||
|
|
||
|
|
||
| def _argv_seen_by(program: str, command: str) -> list[str]: | ||
| stub = f"{program}() {{ printf '%s\\n' \"$@\"; }}; " | ||
| out = subprocess.run( | ||
| ["bash", "-c", stub + command], capture_output=True, text=True, check=True | ||
| ).stdout | ||
| return [line for line in out.split("\n") if line] | ||
|
|
||
|
|
||
| class _Handle: | ||
| def disconnect(self): | ||
| pass | ||
|
|
||
|
|
||
| class _RecordingCommands: | ||
| def __init__(self): | ||
| self.commands: list[str] = [] | ||
|
|
||
| def run(self, cmd, **kwargs): | ||
| self.commands.append(cmd) | ||
| return _Handle() | ||
|
|
||
|
|
||
| def _recording_sandbox() -> tuple[Sandbox, _RecordingCommands]: | ||
| class _FakeSandbox(Sandbox): | ||
| @property | ||
| def commands(self): | ||
| return self._fake_commands | ||
|
|
||
| sandbox = _FakeSandbox.__new__(_FakeSandbox) | ||
| sandbox._fake_commands = _RecordingCommands() | ||
| return sandbox, sandbox._fake_commands | ||
|
|
||
|
|
||
| def test_open_passes_a_url_with_a_query_string_as_one_argument(): | ||
| sandbox, commands = _recording_sandbox() | ||
| url = "https://example.com/search?q=e2b&page=2#results" | ||
|
|
||
| sandbox.open(url) | ||
|
|
||
| assert len(commands.commands) == 1 | ||
| assert _argv_seen_by("xdg-open", commands.commands[0]) == [url] | ||
|
|
||
|
|
||
| def test_open_passes_a_file_path_with_spaces_and_quotes_as_one_argument(): | ||
| sandbox, commands = _recording_sandbox() | ||
| path = "/home/user/Q3 report (Bob's copy).pdf" | ||
|
|
||
| sandbox.open(path) | ||
|
|
||
| assert _argv_seen_by("xdg-open", commands.commands[0]) == [path] | ||
|
|
||
|
|
||
| def test_launch_passes_the_application_and_the_uri_as_separate_arguments(): | ||
| sandbox, commands = _recording_sandbox() | ||
| uri = "https://example.com/?a=1&b=2" | ||
|
|
||
| sandbox.launch("google-chrome", uri) | ||
|
|
||
| assert _argv_seen_by("gtk-launch", commands.commands[0]) == ["google-chrome", uri] | ||
|
|
||
|
|
||
| def test_launch_without_a_uri_passes_only_the_application(): | ||
| sandbox, commands = _recording_sandbox() | ||
|
|
||
| sandbox.launch("google-chrome") | ||
|
|
||
| assert _argv_seen_by("gtk-launch", commands.commands[0]) == ["google-chrome"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing Cloudflare matrix leg in
.github/workflows/desktop_js_tests.yml:82-91runs the desktop suite inside workerd, andvitest.cloudflare.config.mts:75-76includes this new file—the desktop Cloudflare config excludes onlyclient.test.ts. Workerd cannot spawn Bash throughnode:child_process, and the Windows-only skip does not exclude that runtime, so these tests make the Cloudflare CI leg fail independently of the quoting behavior. Add this file to the desktop Cloudflare config's exclusions, or use Worker-compatible assertions.Useful? React with 👍 / 👎.