Files
kandev/apps/backend/internal/common/httpmw/origin_test.go
T
João Salavisa 3746a30126 fix: enforce origin validation on websocket upgrades (#1698)
* fix: enforce origin validation on websocket upgrades

The gateway /ws upgrader hardcoded CheckOrigin to true, so any web page
could open a socket to the local backend and drive session/shell actions
(cross-site WebSocket hijacking → RCE on the host).

Both upgraders now share checkWebSocketOrigin: no Origin header is
allowed (non-browser clients), same-hostname and loopback↔loopback
origins are allowed, everything else is rejected. Replaces the terminal
handler's prefix-based check, which accepted origins like
http://localhost.attacker.tld.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor: share origin trust policy between cors and websocket checks

Review feedback on #1698: the CORS middleware and the WebSocket upgrader
carried two copies of the same origin policy, which could silently
diverge. Both now delegate to httpmw.AllowedOrigin, which also rejects
URL components that never appear in a browser Origin header (userinfo,
path, query, fragment, opaque data).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-15 01:13:46 +01:00

67 lines
3.0 KiB
Go

package httpmw
import "testing"
func TestAllowedOrigin(t *testing.T) {
tests := []struct {
name string
origin string
host string
want bool
}{
// Loopback origin ↔ loopback host (dev servers, desktop shell)
{"localhost", "http://localhost", "localhost", true},
{"localhost different ports", "http://localhost:3000", "localhost:8080", true},
{"https localhost", "https://localhost", "localhost", true},
{"127.0.0.1", "http://127.0.0.1", "127.0.0.1", true},
{"127.0.0.1 different ports", "http://127.0.0.1:3000", "127.0.0.1:8080", true},
{"localhost origin to 127.0.0.1 host", "http://localhost:3000", "127.0.0.1:8080", true},
{"ipv6 loopback origin to ipv4 loopback host", "http://[::1]:3000", "127.0.0.1:8080", true},
{"ipv6 loopback host", "http://localhost:3000", "[::1]:8080", true},
// Same hostname (ports ignored — see AllowedOrigin doc)
{"same origin", "https://example.com", "example.com", true},
{"same origin with port", "https://example.com:443", "example.com:8080", true},
{"same origin case insensitive", "https://Example.COM", "example.com", true},
{"uppercase scheme", "HTTPS://example.com", "example.com", true},
// Cross-origin — reject
{"cross origin", "https://evil.com", "example.com", false},
{"cross origin similar", "https://notexample.com", "example.com", false},
{"ipv6 loopback origin to public host", "http://[::1]:3000", "example.com:8080", false},
{"loopback origin to public host", "http://localhost:3000", "example.com", false},
// Loopback prefix-match bypass attempts — hostname must match exactly
{"localhost subdomain bypass", "http://localhost.attacker.tld", "localhost:8080", false},
{"127.0.0.1 subdomain bypass", "http://127.0.0.1.attacker.tld:80", "127.0.0.1:8080", false},
// Malformed / non-http origins
{"empty origin", "", "example.com", false},
{"malformed origin", "not-a-url", "example.com", false},
{"null origin", "null", "localhost:8080", false},
{"file scheme", "file:///etc/passwd", "localhost:8080", false},
{"ws scheme", "ws://example.com", "example.com", false},
// Components that never appear in a browser Origin header — reject
{"userinfo", "https://user@example.com", "example.com", false},
{"userinfo with password", "https://user:pass@example.com", "example.com", false},
{"path", "https://example.com/path", "example.com", false},
{"trailing slash", "https://example.com/", "example.com", false},
{"query", "https://example.com?x=1", "example.com", false},
{"empty query", "https://example.com?", "example.com", false},
{"fragment", "https://example.com#frag", "example.com", false},
{"opaque", "https:example.com", "example.com", false},
// Empty host — no match possible
{"empty request host", "https://example.com", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AllowedOrigin(tt.origin, tt.host); got != tt.want {
t.Errorf("AllowedOrigin(%q, %q) = %v, want %v", tt.origin, tt.host, got, tt.want)
}
})
}
}