Bug: Web terminal fails when accessing via network IP instead of localhost #3691

Closed
opened 2026-02-16 17:41:07 -05:00 by yindo · 0 comments
Owner

Originally created by @ashutoshpw on GitHub (Dec 20, 2025).

Originally assigned to: @adamdotdevin on GitHub.

Description

When running opencode web --hostname 0.0.0.0 and accessing the web interface via a public/network IP address (not localhost), the terminal fails to connect. The PTY WebSocket URL is incorrectly constructed as wss://pty/pty_xxx/connect where "pty" is treated as the hostname instead of the actual server address.

Root Cause:

The issue is in packages/desktop/src/app.tsx:

const url =
  new URLSearchParams(document.location.search).get("url") ||
  (location.hostname.includes("opencode.ai") || location.hostname.includes("localhost")
    ? `http://${host}:${port}`
    : "/")

When accessing via a public IP (e.g., 192.168.1.100):

  • location.hostname = "192.168.1.100"
  • It does NOT include "opencode.ai" or "localhost"
  • So sdk.url defaults to "/" (Probably assuming the desktop app environment)

The Terminal component constructs the WebSocket URL as:

new WebSocket(sdk.url + `/pty/${local.pty.id}/connect?directory=...`)

When sdk.url = "/":

"/" + "/pty/pty_xxx/connect" = "//pty/pty_xxx/connect"

The browser interprets //pty/... as a protocol-relative URL where pty becomes the hostname.

Suggested Fix:

Replace the fallback "/" with window.location.origin:

const url =
  new URLSearchParams(document.location.search).get("url") ||
  (location.hostname.includes("opencode.ai") || location.hostname.includes("localhost")
    ? `http://${host}:${port}`
    : window.location.origin)

Safety Analysis: This fix will NOT affect the desktop app because:

  • Tauri production uses tauri://localhost protocol → location.hostname = "localhost" ✓
  • Tauri dev uses http://localhost:1420location.hostname = "localhost" ✓
  • Both cases match the existing condition and never reach the fallback branch

OpenCode version

v1.0.164 (reproducible on latest)

Steps to reproduce

  1. Start OpenCode web server bound to all interfaces:
    opencode web --hostname 0.0.0.0 --port 4096
    
  2. Access the web interface via network IP (not localhost):
    http://192.168.1.100:4096
    
  3. Open a terminal session from the web interface
  4. Observe the WebSocket connection error in browser console:
    wss://pty/pty_b3c0b4b830013ynf0aTbDhq6e7/connect
    

Expected: WebSocket connects to wss://192.168.1.100:4096/pty/pty_xxx/connect
Actual: WebSocket attempts to connect to wss://pty/pty_xxx/connect (invalid hostname)

Workaround: Pass URL explicitly via query parameter:

http://YOUR_PUBLIC_IP:4096?url=http://YOUR_PUBLIC_IP:4096

Screenshot and/or share link

Browser console showing the malformed WebSocket URL:

WebSocket connection to 'wss://pty/pty_b3c0b4b830013ynf0aTbDhq6e7/connect?directory=...' failed

Operating System

Ubuntu 24.04, also reproducible on other Linux distros (The server crashes if we try to do this on Ubuntu 22, we can ignore this as we are focusing on LTS version)

Terminal

Chrome/Firefox (any browser) - this is a web interface issue

Originally created by @ashutoshpw on GitHub (Dec 20, 2025). Originally assigned to: @adamdotdevin on GitHub. ### Description When running `opencode web --hostname 0.0.0.0` and accessing the web interface via a public/network IP address (not localhost), the terminal fails to connect. The PTY WebSocket URL is incorrectly constructed as `wss://pty/pty_xxx/connect` where "pty" is treated as the hostname instead of the actual server address. **Root Cause:** The issue is in `packages/desktop/src/app.tsx`: ```typescript const url = new URLSearchParams(document.location.search).get("url") || (location.hostname.includes("opencode.ai") || location.hostname.includes("localhost") ? `http://${host}:${port}` : "/") ``` When accessing via a public IP (e.g., `192.168.1.100`): - `location.hostname` = `"192.168.1.100"` - It does NOT include `"opencode.ai"` or `"localhost"` - So `sdk.url` defaults to `"/"` (Probably assuming the desktop app environment) The Terminal component constructs the WebSocket URL as: ```typescript new WebSocket(sdk.url + `/pty/${local.pty.id}/connect?directory=...`) ``` When `sdk.url = "/"`: ``` "/" + "/pty/pty_xxx/connect" = "//pty/pty_xxx/connect" ``` The browser interprets `//pty/...` as a protocol-relative URL where `pty` becomes the hostname. **Suggested Fix:** Replace the fallback `"/"` with `window.location.origin`: ```typescript const url = new URLSearchParams(document.location.search).get("url") || (location.hostname.includes("opencode.ai") || location.hostname.includes("localhost") ? `http://${host}:${port}` : window.location.origin) ``` **Safety Analysis:** This fix will NOT affect the desktop app because: - Tauri production uses `tauri://localhost` protocol → `location.hostname` = "localhost" ✓ - Tauri dev uses `http://localhost:1420` → `location.hostname` = "localhost" ✓ - Both cases match the existing condition and never reach the fallback branch ### OpenCode version v1.0.164 (reproducible on latest) ### Steps to reproduce 1. Start OpenCode web server bound to all interfaces: ```bash opencode web --hostname 0.0.0.0 --port 4096 ``` 2. Access the web interface via network IP (not localhost): ``` http://192.168.1.100:4096 ``` 3. Open a terminal session from the web interface 4. Observe the WebSocket connection error in browser console: ``` wss://pty/pty_b3c0b4b830013ynf0aTbDhq6e7/connect ``` **Expected:** WebSocket connects to `wss://192.168.1.100:4096/pty/pty_xxx/connect` **Actual:** WebSocket attempts to connect to `wss://pty/pty_xxx/connect` (invalid hostname) **Workaround:** Pass URL explicitly via query parameter: ``` http://YOUR_PUBLIC_IP:4096?url=http://YOUR_PUBLIC_IP:4096 ``` ### Screenshot and/or share link Browser console showing the malformed WebSocket URL: ``` WebSocket connection to 'wss://pty/pty_b3c0b4b830013ynf0aTbDhq6e7/connect?directory=...' failed ``` ### Operating System Ubuntu 24.04, also reproducible on other Linux distros (The server crashes if we try to do this on Ubuntu 22, we can ignore this as we are focusing on LTS version) ### Terminal Chrome/Firefox (any browser) - this is a web interface issue
yindo added the help-wantedweb labels 2026-02-16 17:41:07 -05:00
yindo closed this issue 2026-02-16 17:41:07 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#3691