Skip to content

fix(serve-sim): Preserve host for exec-ws SSE streams#93

Open
mwolson wants to merge 1 commit into
EvanBacon:mainfrom
mwolson:fix-exec-ws-forward-host
Open

fix(serve-sim): Preserve host for exec-ws SSE streams#93
mwolson wants to merge 1 commit into
EvanBacon:mainfrom
mwolson:fix-exec-ws-forward-host

Conversation

@mwolson

@mwolson mwolson commented Jun 17, 2026

Copy link
Copy Markdown

Summary

Closes #92.

Preserve the viewer's Host header when /exec-ws proxies allowed SSE subscriptions back through the local serve-sim server.

Problem and Fix

Problem and Why it Happened Fix
LAN and remote viewers can initially load the UI from a host such as minipro24.lan, but /exec-ws proxies /api/events through a loopback httpRequest without the original Host header. /api/events then sees 127.0.0.1 and publishes loopback helper URLs. Capture the WebSocket upgrade Host header and forward it on the internal SSE loopback request.
The regression coverage only needs the SSE proxy path, but the bug is visible in preview config URLs rather than the raw header value. Add coverage that subscribes through /exec-ws and asserts LAN preview configs use the LAN hostname while loopback preview configs remain on 127.0.0.1.

Defensive Fixes

Problem and Why it Happened Fix
A custom client could theoretically provide duplicate Host values. Normalize the upgrade host before using it for loopback forwarding or port fallback.

Validation

  • bun test packages/serve-sim/src/__tests__/exec-ws.test.ts
  • bun run typecheck
  • bun run lint
  • git diff --check
  • bun test packages/serve-sim/src/__tests__/

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of Host headers during WebSocket upgrades and related SSE loopback subscriptions, including support for comma-separated Host values, so preview configuration URLs are consistently rewritten for LAN and loopback connections.
  • Tests
    • Added an integration test covering SSE loopback behavior to ensure the upgraded WebSocket requests preserve the correct upgrade host when generating preview config responses.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4ca7a88e-6bd5-4c71-a68a-87691cebfb3c

📥 Commits

Reviewing files that changed from the base of the PR and between bf871a9 and 4fde2ed.

📒 Files selected for processing (2)
  • packages/serve-sim/src/__tests__/exec-ws.test.ts
  • packages/serve-sim/src/exec-ws.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/serve-sim/src/exec-ws.ts
  • packages/serve-sim/src/tests/exec-ws.test.ts

📝 Walkthrough

Walkthrough

normalizeHostHeader parses incoming Host headers to handle comma-separated values. createExecUpgradeHandler extracts a normalized forwardHost, uses it for WebSocket origin validation and SSE loopback port calculation, and threads it into wireExecSocket. wireExecSocket now sets the Host header on loopback SSE proxy requests. The test file adds connectWithHeaders helper, fixtures, and an integration test that verifies SSE config reflects the originating upgrade host for LAN and loopback clients.

Changes

Host Header Forwarding in exec-ws SSE Proxy

Layer / File(s) Summary
Host normalization and forwardHost threading
packages/serve-sim/src/exec-ws.ts
normalizeHostHeader helper parses and trims the first Host value, handling comma-separated variants. createExecUpgradeHandler extracts and normalizes forwardHost from req.headers.host, uses it for WebSocket origin validation, falls back to its parsed port for SSE loopback calculations, and passes it to wireExecSocket. wireExecSocket accepts forwardHost and conditionally sets it as the Host header on loopback SSE proxy requests.
Test fixtures and integration coverage
packages/serve-sim/src/__tests__/exec-ws.test.ts
Imports expanded to include ws, createExecUpgradeHandler, and middleware helpers. HOST_FORWARD_PORT constant and PREVIEW_STATE fixture enable deterministic preview config generation. connectWithHeaders helper sequences WebSocket open/message/close with timeouts and ordered reply handling. Integration test starts a preview server with SSE and exec-ws routing, connects three times with distinct Host headers (LAN, comma-separated, loopback), and asserts streamUrl/wsUrl in SSE config match each injected host.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • EvanBacon/serve-sim#64: Fixed the original rewriteStateForRequestHost helper URL rewriting for remote viewers — this PR extends the same host-rewriting contract to the /exec-ws SSE loopback proxy path.
  • EvanBacon/serve-sim#86: Introduced the /exec-ws WebSocket control channel and SSE proxying logic that this PR builds upon by preserving the upgrade Host in loopback requests.

Poem

🐇 Hop, hop! The Host must travel true,
Through loopback tunnels, LAN shines through.
No more lost helpers on 127,
Normalize the header, reach preview heaven.
From minipro24 to ws:// anew —
This bunny forwarded the request, and flew! 🌟

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: preserving the host header for exec-ws SSE streams, which is the core fix addressing the regression described in issue #92.
Linked Issues check ✅ Passed The changes directly address all requirements from issue #92: normalizing the host header, forwarding it in SSE loopback requests, and adding comprehensive test coverage for both LAN and loopback scenarios.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the exec-ws SSE host header preservation issue; no unrelated modifications are present in the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mwolson mwolson marked this pull request as ready for review June 17, 2026 19:56

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/serve-sim/src/exec-ws.ts`:
- Around line 220-224: The forwardHost assignment at line 220 only handles
array-valued Host headers but doesn't normalize comma-separated Host strings
like "minipro24.lan:3462, 127.0.0.1:3462". Modify the forwardHost assignment to
first split the Host header by comma and take the first element (trimmed) before
checking if it's an array or string, ensuring that comma-separated Host values
are properly normalized before being used in the split operation at line 223 for
port extraction and preventing NaN values when localPort is unavailable.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0ffce813-12fb-4cdf-a0e6-a78851730468

📥 Commits

Reviewing files that changed from the base of the PR and between eb16f5c and bf871a9.

📒 Files selected for processing (2)
  • packages/serve-sim/src/__tests__/exec-ws.test.ts
  • packages/serve-sim/src/exec-ws.ts

Comment thread packages/serve-sim/src/exec-ws.ts Outdated
@mwolson mwolson force-pushed the fix-exec-ws-forward-host branch from bf871a9 to 4fde2ed Compare June 17, 2026 20:01
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.

/exec-ws SSE subscriptions publish loopback helper URLs for LAN viewers

1 participant