Title: fix: run the binary directly in verify_binary instead of through a shell (CWE-78) by 07souravkunda · Pull Request #43 · browserstack/browserstack-local-ruby · GitHub
Open Graph Title: fix: run the binary directly in verify_binary instead of through a shell (CWE-78) by 07souravkunda · Pull Request #43 · browserstack/browserstack-local-ruby
X Title: fix: run the binary directly in verify_binary instead of through a shell (CWE-78) by 07souravkunda · Pull Request #43 · browserstack/browserstack-local-ruby
Description: What LocalBinary#verify_binary built its command by string concatenation: IO.popen(bin_path + " --version") The single-string form of IO.popen passes the whole string to /bin/sh, so shell metacharacters in the binary path are interpreted instead of being treated as part of a filename (CWE-78, command injection). This PR switches to the array form, which execs the binary directly and never involves a shell: IO.popen([bin_path, '--version']) Why it's reachable bin_path is assembled in binary_path from @ordered_paths — the expanded home directory, Dir.pwd, and Dir.tmpdir — and none of those is sanitised. A directory name containing ;, ` or $() anywhere in that chain turns a routine version check into arbitrary command execution. Reproduced end to end through the public LocalBinary#binary_path entry point (no private-method access, no monkey-patching): with $HOME set to a directory named h;touch /tmp/marker;echo BrowserStack Local version 9.9;#, the injected touch ran, and because the injected echo also satisfied the version regex, verify_binary returned true and binary_path handed back the poisoned path. After this change the marker is never created. Bonus: fixes a benign failure for paths with spaces The same shell splitting broke verification for any path containing a space — routine on macOS and Windows. verify_binary returned false for a perfectly good binary at …/my binary dir/BrowserStackLocal, so binary_path deleted the valid cached binary and re-downloaded it on every run. The array form fixes that too, and there's a test for it. Deliberately out of scope Each of these is tracked separately; folding them in would make this diff hard to audit: the fail-open rescue StandardError → false in this same method the TOCTOU window between verification and execution the other shell-string call sites in local.rb (logfile in system(), start_command / stop_command, verbatim extra args) A character allowlist on the path was considered and rejected: with no shell involved it adds no security on this line, and it would reject exactly the legitimate space-containing paths this change fixes. Validating the provenance of a caller-supplied binarypath is a separate concern from this internally-computed path. Also noted but unchanged: IO.popen's IO object is never closed and the child never reaped (fd + zombie leak). The block form would fix it but waits for the child to exit, which would hang on a binary that prints a line and keeps running — so the non-block form is kept. Tests Three regression tests in BrowserStackLocalBinaryTest, all verified to fail before this change (test file kept, lib/browserstack/localbinary.rb reverted to master): Test Pre-fix Post-fix test_verify_binary_does_not_interpret_shell_metacharacters_in_path Expected: false, Actual: true (marker created) pass test_verify_binary_runs_a_real_binary_at_a_path_containing_shell_metacharacters shell metacharacters in the binary path were executed pass test_verify_binary_accepts_a_path_containing_spaces Expected: true, Actual: false pass The middle one is the load-bearing guard: the first test returns false post-fix via Errno::ENOENT, so on its own it would also pass under a character-allowlist remediation rather than the array form. Putting the metacharacters in the directory name of a real executable asserts both halves at once — the injected command never runs, and the legitimate binary at that hostile-looking path still verifies. Suite, ruby 3.2.10, credentials exported — fully green: 26 runs, 50 assertions, 0 failures, 0 errors, 0 skips An earlier run on this branch showed 3 errors in test_check_pid / test_is_running / test_multiple_binary, which reproduced identically on a pristine master clone. Cause confirmed and now cleared: those three call start with no localIdentifier and contend for daemon port 45691, which was held on the test box by an unrelated BrowserStackLocal --app process. Once that process exited the same suite went green on this branch with no code change — so they were never related to this diff. CI / security gates — read this before merging semgrep/ci failed on the first push of this PR and is worth stating plainly rather than leaving for the reviewer to discover: 1 blocking finding, ruby.lang.security.dangerous-exec.dangerous-exec, code-scanning alert #13 at lib/browserstack/localbinary.rb:138 (job 94071567985, exit 1), which put the PR in mergeStateStatus: BLOCKED. It is a false positive on the fixed code: the rule fires on any non-static first argument to IO.popen and does not model the array form — which is precisely this fix, since no /bin/sh is spawned at all. Evidence that the diff did not introduce it: the identical rule has been open against master as alert #9 since 2026-06-04, on the pre-fix line 136. The diff re-anchors a pre-existing alert; it does not add one. Resolved by a rule-scoped # nosemgrep: ruby.lang.security.dangerous-exec.dangerous-exec on that one line with the reason inline (comment-only, no behaviour change — the regression tests and the injection PoC were re-run after it and are unchanged). Every other IO.popen/exec finding in this file still reports. Alternative, if you'd rather not carry an inline suppression: triage alerts #13 and #9 on the platform as false positives and drop the comment — either resolves both. Confirmed green after the fix-up: semgrep/ci=SUCCESS, plus Semgrep OSS, CodeQL, Analyze (ruby), Analyze (actions) — all 5 checks passing, mergeable: MERGEABLE. The PR still reads BLOCKED only because it is a Draft awaiting review approval (reviewDecision: REVIEW_REQUIRED), no longer because of a failing check. Live verification: a real tunnel was opened through the patched binding (BrowserStack::Local#start) and a selenium-webdriver BrowserStack session (Windows 11 / Chrome) loaded a local page over it via bs-local.com, then stopped cleanly through the binding's .stop(). The cached 36 MB binary at ~/.browserstack/BrowserStackLocal meant binary_path took the early-return branch, so the new array-form verify_binary ran against the genuine binary and returned true — the tunnel could not have come up otherwise.
Open Graph Description: What LocalBinary#verify_binary built its command by string concatenation: IO.popen(bin_path + " --version") The single-string form of IO.popen passes the whole string to /bin/sh, so shell...
X Description: What LocalBinary#verify_binary built its command by string concatenation: IO.popen(bin_path + " --version") The single-string form of IO.popen passes the whole string to /bin/sh, ...
Opengraph URL: https://github.com/browserstack/browserstack-local-ruby/pull/43
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:06a80e90-6419-acd0-e343-c7dfb6e8fd75 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E892:281ABB:757021:A0A5EB:6B04788F |
| html-safe-nonce | 63cd4644b3ae4465017a02a5e8ddafe0ec9974a9ab2e75a588fda15bf93addf8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODkyOjI4MUFCQjo3NTcwMjE6QTBBNUVCOjZCMDQ3ODhGIiwidmlzaXRvcl9pZCI6IjcyNjc2NzgwMjA2NTQzNjQ4MTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a081cd567b6882eb40f7a0a0f55488a931a8c0aaae5a229883822fc1a173e1c3 |
| hovercard-subject-tag | pull_request:4260994303 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/browserstack/browserstack-local-ruby/pull/43/files |
| twitter:image | https://avatars.githubusercontent.com/u/50537259?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/50537259?s=400&v=4 |
| og:image:alt | What LocalBinary#verify_binary built its command by string concatenation: IO.popen(bin_path + " --version") The single-string form of IO.popen passes the whole string to /bin/sh, so shell... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9c099f1b85def81ca352464c276510dfef32deaf3c305404e65b8d270284a58b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/browserstack/browserstack-local-ruby git https://github.com/browserstack/browserstack-local-ruby.git |
| octolytics-dimension-user_id | 1119453 |
| octolytics-dimension-user_login | browserstack |
| octolytics-dimension-repository_id | 51922935 |
| octolytics-dimension-repository_nwo | browserstack/browserstack-local-ruby |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 51922935 |
| octolytics-dimension-repository_network_root_nwo | browserstack/browserstack-local-ruby |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cf73f57448d52b3a7b619195e182b33d3b3454ca |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width