Skip to content

fix(auth): caret-escape cmd metacharacters when opening the browser on WSL#61

Merged
scottlovegrove merged 2 commits into
mainfrom
fix/wsl-browser-open-url-escaping
Jun 5, 2026
Merged

fix(auth): caret-escape cmd metacharacters when opening the browser on WSL#61
scottlovegrove merged 2 commits into
mainfrom
fix/wsl-browser-open-url-escaping

Conversation

@scottlovegrove
Copy link
Copy Markdown
Collaborator

Symptom

On WSL, <cli> auth login (any DCR/OAuth consumer) pops a Windows dialog:

Windows cannot find '\https://todoist.com/oauth/authorize?resource=https%3A%2F%2F…'

instead of opening the browser. The URL is truncated at the first & and treated as a file path.

Root cause

openViaCmdExe opens the browser via cmd.exe /c start "" "<url>", wrapping the URL in double quotes to stop & from splitting the command line. Under WSL that doesn't work: the interop layer re-quotes argv entries itself, mangling the literal quotes, so & leaks through to cmd.exe as a statement separator. Only the prefix up to the first & reaches start; Windows then tries to open that fragment as a path.

Two compounding bugs, both confirmed with a real cmd.exe on WSL:

  1. The surrounding quotes don't survive interop → & was never actually protected (cmd reported 'response_type' is not recognized… for each &-separated param).
  2. url.replaceAll('%','%%') does not collapse on the cmd /c command line (only inside batch files), so %3A%%3A would have corrupted every percent-encoded byte even when the command did run.

Only OAuth consumers that override openBrowser (e.g. todoist-cli, which routes through the open package) dodged this; consumers on the default opener hit it every login.

Fix

Caret-escape cmd's metacharacters (& | < > ^ ( ) ") instead of quoting — caret escaping survives WSL interop because there are no quotes for it to mangle — and leave % untouched (OAuth URLs are RFC 3986 %HH, which never matches cmd's %NAME% env-expansion; there's no command-line caret-escape for %, and doubling corrupts %HH).

Extracted as a pure, exported escapeUrlForCmd and unit-tested. Verified end-to-end against a real cmd.exe on WSL: the escaped string round-trips to the exact URL (carets stripped), & is protected, %HH preserved.

Tests

  • Updated the WSL-routing test (it asserted the old quoted/%% behavior).
  • Added a focused escapeUrlForCmd unit test (per-metacharacter escaping + percent-encoding round-trip).
  • Full suite green (517).

🤖 Generated with Claude Code

…n WSL

`openViaCmdExe` wrapped the URL in double quotes to stop `&` from splitting the
`cmd.exe /c start` command line. Under WSL that doesn't work: the interop layer
re-quotes argv entries itself, mangling the literal quotes, so `&` leaks through
and acts as a statement separator — only the prefix up to the first `&` reaches
`start`, and Windows tries to open that fragment as a path ("Windows cannot find
'\https://…'"). OAuth authorize URLs (several `&`, percent-encoded params) hit
this every time.

Two compounding bugs:
- The surrounding quotes don't survive WSL interop, so `&` was never protected.
- `url.replaceAll('%','%%')` does not collapse on the `cmd /c` command line (only
  inside batch files), so it would have corrupted every `%HH` byte even when the
  command did run.

Fix: caret-escape cmd's metacharacters (`& | < > ^ ( ) "`) — which survives
interop since there are no quotes to mangle — and leave `%` alone (OAuth URLs are
`%HH`, which never matches `%NAME%` env-expansion). Extracted as a testable
`escapeUrlForCmd`; verified the round-trip through a real `cmd.exe` on WSL.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@scottlovegrove scottlovegrove self-assigned this Jun 5, 2026
Copy link
Copy Markdown
Member

@doistbot doistbot left a comment

Choose a reason for hiding this comment

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

Thank you Scott for thoroughly diagnosing and addressing the WSL browser-open bug 🤗.

Few things worth tightening:

  • Although caret-escaping handles command metacharacters well, cmd.exe will still treat certain valid percent-encoded sequences (like %C3% in %C3%A9) as environment variables; bypassing cmd.exe entirely to launch the browser would be a safer way to prevent URL corruption on WSL.

I also included a few optional follow-up notes in the details below.

Optional follow-up note (1)
  • [P3] src/auth/flow.test.ts:527: These assertions duplicate the escaping logic checks already covered by the dedicated escapeUrlForCmd test below. Since you are already asserting expect(args).toEqual(['/c', 'start', '""', escapeUrlForCmd(url)]) on line 524, lines 527-532 can be removed to keep this test strictly focused on the routing behavior.

Share FeedbackReview Logs

Comment thread src/auth/flow.ts Outdated
Addresses review: caret-escaping cmd metacharacters still left `%` exposed —
a percent-encoded multi-byte UTF-8 byte like `%C3%A9` (é) contains `%C3%`, which
`cmd /c` would treat as `%VAR%` env-expansion and mangle. Since runOAuthFlow is
public API for custom AuthProviders, that's a real hazard, not just a built-in
concern.

Bypass the shell entirely: launch via `rundll32.exe url.dll,FileProtocolHandler
<url>`. CreateProcess hands the single argv to the protocol handler verbatim —
no cmd parsing pass — so neither `&` (statement separator) nor `%HH`
(env-expansion) can corrupt the URL, and there's no fragile escaping to
maintain. Drops the now-unneeded escapeUrlForCmd helper.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@scottlovegrove scottlovegrove merged commit 9abcfea into main Jun 5, 2026
4 checks passed
@scottlovegrove scottlovegrove deleted the fix/wsl-browser-open-url-escaping branch June 5, 2026 14:13
doist-release-bot Bot added a commit that referenced this pull request Jun 5, 2026
## [0.25.1](v0.25.0...v0.25.1) (2026-06-05)

### Bug Fixes

* **auth:** caret-escape cmd metacharacters when opening the browser on WSL ([#61](#61)) ([9abcfea](9abcfea))
@doist-release-bot
Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.25.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants