Skip to content

fix: CSRF 403 — probe port accepts 403 + fuzzy workspace_id matching#2

Open
daocoding wants to merge 1 commit intoprofessional-ALFIE:mainfrom
daocoding:fix/csrf-probe-and-workspace-matching
Open

fix: CSRF 403 — probe port accepts 403 + fuzzy workspace_id matching#2
daocoding wants to merge 1 commit intoprofessional-ALFIE:mainfrom
daocoding:fix/csrf-probe-and-workspace-matching

Conversation

@daocoding
Copy link
Copy Markdown

Problem

Cascade creation via the CLI fails with 403 Invalid CSRF token in multi-workspace setups, particularly on macOS with paths containing special characters.

Root Cause

Two compounding issues:

1. _probePort rejects valid Language Server ports

_probePort() in ls-bridge.ts checks for statusCode === 401 || statusCode === 200, but the Language Server returns 403 (not 401) for missing/invalid CSRF tokens. This causes all valid ports to be rejected during auto-discovery, forcing a fallback to extension_server_port (wrong endpoint).

2. fixLsConnection workspace_id matching fails silently

createWorkspaceId_func() encodes workspace paths with simple regex (/[^a-zA-Z0-9]/g → '_'), but the Language Server's --workspace_id flag uses URI-component encoding (:_3A_, etc.). The exact-match comparison in findMatchingLanguageServerLine_func fails, fixLsConnection returns silently without overriding, and the SDK keeps its stale/wrong connection values.

When both issues compound: SDK picks wrong port + fixLsConnection fails to correct → every RPC call gets 403.

Fix

  1. ls-bridge.ts: Accept 403 in _probePort (line 588) — the LS uses 403 for CSRF rejection
  2. ls-process-match.ts: Add fuzzy fallback matching using last 2 path segments when exact workspace_id matching fails
  3. Added 2 new tests covering fuzzy fallback and exact-match preference

Testing

  • All 4 tests pass (node --test packages/extension/test/ls-process-match.test.ts)
  • Verified on macOS with multiple Antigravity windows and workspaces
  • Existing parent/child workspace disambiguation test still passes

Files Changed

  • packages/sdk/src/transport/ls-bridge.ts — accept 403 in port probe
  • packages/extension/src/ls-process-match.ts — fuzzy workspace matching fallback
  • packages/extension/test/ls-process-match.test.ts — 2 new test cases

Two fixes for cascade creation failures (403 Invalid CSRF token):

1. SDK _probePort: Accept 403 in addition to 401 during port discovery
   - The Language Server returns 403 (not 401) for missing/invalid CSRF tokens
   - Without this fix, _probePort rejects all valid LS ports, causing
     the SDK to fall back to extension_server_port (wrong endpoint)

2. Extension workspace_id matching: Add fuzzy fallback via path segments
   - The LS encodes workspace_id using URI-component encoding (: → _3A_)
   - createWorkspaceId_func uses simple regex replacement
   - This encoding mismatch causes exact matching to fail silently
   - fixLsConnection returns without overriding, leaving stale SDK values
   - New behavior: try exact match first, fall back to last-2-segment
     fuzzy match when exact fails

Both issues compound: if SDK auto-discovery picks the wrong port AND
fixLsConnection fails to correct it, every RPC call gets 403.

Tested on macOS with multiple Antigravity windows and workspaces.
@daocoding daocoding force-pushed the fix/csrf-probe-and-workspace-matching branch from 036c55b to 8672792 Compare March 15, 2026 01:28
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.

1 participant