fabro/lib/crates
Bryan Helmkamp 38695d7e89
fix(server): normalize default ports in terminal origin check (#417)
## Summary

The terminal WebSocket origin check (`origin_allowed` in
`handler/sandbox.rs`) rejected browser requests when the `Host` header
omitted the default port for the scheme. Result: clicking the
**Terminal** tab on `/runs/<id>/sandbox` returned **403 Forbidden** and
the UI showed "Terminal WebSocket connection failed." Other tabs
(Services, Filesystem, VNC) worked because their WebSockets either don't
traverse the server (VNC connects directly to Daytona's signed preview
URL) or aren't WebSocket upgrades.

## Root cause

Browsers send `Origin: https://example.com` and `Host: example.com` (no
`:443`) on default HTTPS. The previous logic always constructed the
origin authority *with* the default port, then string-compared against
the raw `Host` header:

```rust
let origin_authority = match origin_url.port_or_known_default() {
    Some(port) => format!("{origin_host}:{port}"),
    None => origin_host.to_string(),
};
origin_authority.eq_ignore_ascii_case(host)
```

So `"example.com:443"` got compared against `"example.com"` and never
matched. Every browser-driven WS upgrade to a default-port HTTPS
deployment failed.

## Fix

Parse the `Host` header through the origin's scheme into another `Url`,
then compare `host_str()` and `port_or_known_default()` on both sides.
This normalizes default ports symmetrically.

```rust
let Ok(host_url) = url::Url::parse(&format!("{}://{host}", origin_url.scheme())) else {
    return false;
};
origin_url.host_str() == host_url.host_str()
    && origin_url.port_or_known_default() == host_url.port_or_known_default()
```

Reproduced in a production deployment of the nightly image behind Caddy
doing TLS termination on a public IP. Before the fix the terminal WS
handshake returned 403 every time; with the fix the handshake completes
and the terminal session attaches.

## Tests

Added four new cases alongside the existing two:

- `origin_validation_allows_default_https_port_omitted_from_host` — the
bug case (browser-style `Origin: https://host` + `Host: host`).
- `origin_validation_allows_default_http_port_omitted_from_host` — same
for plain HTTP.
- `origin_validation_allows_explicit_default_port_in_host` — `Host:
example.com:443` still matches `Origin: https://example.com`.
- `origin_validation_rejects_scheme_mismatch_on_default_port` — `Origin:
http://example.com` + `Host: example.com:443` is still rejected
(different effective ports).

All six `origin_validation_*` tests pass; the full `fabro-server` suite
stays green (679/679).

## Test plan
- [x] `cargo nextest run -p fabro-server origin_validation` — 6 passed
- [x] `cargo nextest run -p fabro-server` — 679 passed
- [x] `cargo +nightly-2026-04-14 fmt --check --all`
- [x] `cargo +nightly-2026-04-14 clippy -p fabro-server --all-targets --
-D warnings`
- [x] Manual: terminal tab in the SPA against a TLS-terminated
default-port deployment

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-05-26 19:30:24 -04:00
..
build-support fix(build): refresh embedded git sha on branch commits 2026-05-09 14:10:04 -04:00
fabro-acp fix(workflow): capture configured artifacts once (#337) 2026-05-21 10:52:22 -04:00
fabro-agent Replace bare unwrap() with documented expect() across production runtim… (#415) 2026-05-26 17:46:39 -04:00
fabro-api feat(system): report runtime integration status (#416) 2026-05-26 19:27:32 -04:00
fabro-auth refactor: rationalize server secret scopes (vault-only for optional int… (#401) 2026-05-25 17:26:01 -04:00
fabro-checkpoint Make git metadata sandbox-native 2026-04-27 21:43:15 -07:00
fabro-cli Replace bare unwrap() with documented expect() across production runtim… (#415) 2026-05-26 17:46:39 -04:00
fabro-client feat: Add approve/deny run controls to MCP and CLI (#400) 2026-05-25 15:49:57 -04:00
fabro-config feat(system): report runtime integration status (#416) 2026-05-26 19:27:32 -04:00
fabro-core Replace bare unwrap() with documented expect() across production runtim… (#415) 2026-05-26 17:46:39 -04:00
fabro-dev feat: add [run.agent] fabro_tools opt-in for worker run tools (#348) 2026-05-22 09:41:27 -04:00
fabro-devcontainer refactor(static): centralize env var names 2026-04-24 12:29:51 -04:00
fabro-dump Add provider-backed sandbox inventory API and rename SandboxProvider to… (#409) 2026-05-25 22:41:57 -04:00
fabro-github fix(github): refresh installation tokens during workflows 2026-05-06 07:15:18 -04:00
fabro-graphviz fix(graph): support dotted Fabro graph attributes (#324) 2026-05-20 09:31:08 -04:00
fabro-hooks Add event-sourced todo tools for OpenAI and Anthropic profiles (#353) 2026-05-22 13:44:42 -04:00
fabro-http refactor(static): centralize env var names 2026-04-24 12:29:51 -04:00
fabro-install Add provider-backed sandbox inventory API and rename SandboxProvider to… (#409) 2026-05-25 22:41:57 -04:00
fabro-interview Migrate sandbox config to named environments; add InterviewOption metad… (#372) 2026-05-23 15:47:33 -04:00
fabro-llm Replace bare unwrap() with documented expect() across production runtim… (#415) 2026-05-26 17:46:39 -04:00
fabro-macros refactor(dev): simplify generated docs tooling 2026-04-24 18:41:00 -04:00
fabro-manifest Replace run-scoped sandbox config with named environments (#360) 2026-05-23 13:03:21 -04:00
fabro-mcp Add legacy SSE MCP transport support (#386) 2026-05-24 15:29:01 -04:00
fabro-mcp-server feat: Add approve/deny run controls to MCP and CLI (#400) 2026-05-25 15:49:57 -04:00
fabro-model fix(model): retire GPT-5.2 and GPT-5.3 catalog entries (#412) 2026-05-26 00:06:50 -04:00
fabro-oauth Replace bare unwrap() with documented expect() across production runtim… (#415) 2026-05-26 17:46:39 -04:00
fabro-options-metadata refactor(dev): simplify generated docs tooling 2026-04-24 18:41:00 -04:00
fabro-proc refactor(static): centralize env var names 2026-04-24 12:29:51 -04:00
fabro-redact refactor(integrations): make chat integrations Slack-only 2026-05-09 11:43:16 -04:00
fabro-sandbox Add provider-backed sandbox inventory API and rename SandboxProvider to… (#409) 2026-05-25 22:41:57 -04:00
fabro-server fix(server): normalize default ports in terminal origin check (#417) 2026-05-26 19:30:24 -04:00
fabro-slack feat(system): report runtime integration status (#416) 2026-05-26 19:27:32 -04:00
fabro-spa feat(dev): gitignore embedded spa assets 2026-04-26 21:31:11 -04:00
fabro-static refactor: rationalize server secret scopes (vault-only for optional int… (#401) 2026-05-25 17:26:01 -04:00
fabro-store Add provider-backed sandbox inventory API and rename SandboxProvider to… (#409) 2026-05-25 22:41:57 -04:00
fabro-telemetry refactor(workflow): remove retro stage (#230) 2026-05-09 10:18:20 -04:00
fabro-template feat(template): resolve template error locations (#333) 2026-05-20 20:15:04 -04:00
fabro-test fix(skills): Ensure the local server can access project skills (#383) 2026-05-24 13:22:00 -04:00
fabro-tool feat: Add approve/deny run controls to MCP and CLI (#400) 2026-05-25 15:49:57 -04:00
fabro-tracker fix(github): refresh installation tokens during workflows 2026-05-06 07:15:18 -04:00
fabro-types feat(system): report runtime integration status (#416) 2026-05-26 19:27:32 -04:00
fabro-util Replace bare unwrap() with documented expect() across production runtim… (#415) 2026-05-26 17:46:39 -04:00
fabro-validate fix(graph): support dotted Fabro graph attributes (#324) 2026-05-20 09:31:08 -04:00
fabro-vault refactor: rationalize server secret scopes (vault-only for optional int… (#401) 2026-05-25 17:26:01 -04:00
fabro-workflow Replace bare unwrap() with documented expect() across production runtim… (#415) 2026-05-26 17:46:39 -04:00