busweb.Serve runs a <name>.web@N request (verse/web, D-200) against a UI's http.Handler; Prefix/User/FromBus tell the handler where it is mounted and who yasta says is logged in; WithPrefix serves the same handler on a TCP port. httpsec.BearerTokens is the access control of a UI without user management (a reverse proxy or yasta sends the token). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
161 lines
4.7 KiB
Go
161 lines
4.7 KiB
Go
package httpsec
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
var ok = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
func TestHeaders(t *testing.T) {
|
|
h := Headers(ok, HeaderOptions{CacheablePrefixes: []string{"/static/"}})
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/page", nil))
|
|
for k, want := range map[string]string{
|
|
"Content-Security-Policy": DefaultCSP,
|
|
"X-Content-Type-Options": "nosniff",
|
|
"X-Frame-Options": "DENY",
|
|
"Referrer-Policy": "same-origin",
|
|
"Cross-Origin-Opener-Policy": "same-origin",
|
|
"Cache-Control": "no-store",
|
|
"Strict-Transport-Security": "",
|
|
} {
|
|
if got := rec.Header().Get(k); got != want {
|
|
t.Errorf("%s = %q, want %q", k, got, want)
|
|
}
|
|
}
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/static/app.css", nil))
|
|
if got := rec.Header().Get("Cache-Control"); got != "" {
|
|
t.Errorf("static Cache-Control = %q", got)
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
Headers(ok, HeaderOptions{CSP: "default-src 'none'", TLS: true}).ServeHTTP(rec, httptest.NewRequest("GET", "/", nil))
|
|
if rec.Header().Get("Content-Security-Policy") != "default-src 'none'" || rec.Header().Get("Strict-Transport-Security") == "" {
|
|
t.Errorf("options ignored: %v", rec.Header())
|
|
}
|
|
}
|
|
|
|
func TestGateActive(t *testing.T) {
|
|
if !GateActive(false, nil) || GateActive(true, nil) || !GateActive(true, []string{"a.example"}) {
|
|
t.Fatal("GateActive rule")
|
|
}
|
|
}
|
|
|
|
func TestHostAllowed(t *testing.T) {
|
|
allowed := []string{"Ops.Example.", "b.example"}
|
|
for host, want := range map[string]bool{
|
|
"": false,
|
|
":8080": false,
|
|
"127.0.0.1:8080": true,
|
|
"[::1]:8080": true,
|
|
"::1": true,
|
|
"localhost": true,
|
|
"LOCALHOST:1": true,
|
|
"app.localhost": true,
|
|
"ops.example": true,
|
|
"ops.example.:443": true,
|
|
"b.example": true,
|
|
"evil.example": false,
|
|
"localhost.evil.com": false,
|
|
} {
|
|
if got := HostAllowed(host, allowed); got != want {
|
|
t.Errorf("HostAllowed(%q) = %v, want %v", host, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGate(t *testing.T) {
|
|
g := Gate(ok, nil, "web.allowed_hosts")
|
|
for host, code := range map[string]int{"localhost": 200, "evil.example": 421, "": 421} {
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r.Host = host
|
|
rec := httptest.NewRecorder()
|
|
g.ServeHTTP(rec, r)
|
|
if rec.Code != code {
|
|
t.Errorf("Host %q: %d, want %d", host, rec.Code, code)
|
|
}
|
|
if code == 421 && rec.Body.String() != "unknown host (see web.allowed_hosts)\n" {
|
|
t.Errorf("body %q", rec.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHostOf(t *testing.T) {
|
|
if HostOf("https://ops.example:8443/ui") != "ops.example" || HostOf("") != "" {
|
|
t.Fatal("HostOf")
|
|
}
|
|
}
|
|
|
|
func TestSafeNext(t *testing.T) {
|
|
for _, tc := range []struct{ next, base, want string }{
|
|
{"/services?x=1", "", "/services?x=1"},
|
|
{"", "", "/"},
|
|
{"services", "", "/"},
|
|
{"//evil.example", "", "/"},
|
|
{"/\\evil.example", "", "/"},
|
|
{"/a\\b", "", "/"},
|
|
{"/a\nb", "", "/"},
|
|
{"/a\x7f", "", "/"},
|
|
{"https://evil.example/", "", "/"},
|
|
{"/login", "", "/"},
|
|
{"/logout", "", "/"},
|
|
{"/login/help", "", "/login/help"},
|
|
{"/ui/repos", "/ui", "/ui/repos"},
|
|
{"/ui", "/ui", "/ui"},
|
|
{"/other", "/ui", "/ui/"},
|
|
{"/uix", "/ui", "/ui/"},
|
|
{"/ui/login", "/ui", "/ui/"},
|
|
} {
|
|
if got := SafeNext(tc.next, tc.base, "/login", "/logout"); got != tc.want {
|
|
t.Errorf("SafeNext(%q, %q) = %q, want %q", tc.next, tc.base, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCookiePath(t *testing.T) {
|
|
if CookiePath("") != "/" || CookiePath("/ui") != "/ui/" || CookiePath("/ui/") != "/ui/" {
|
|
t.Fatal("CookiePath")
|
|
}
|
|
}
|
|
|
|
func TestSecureCookies(t *testing.T) {
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
if SecureCookies(r, "") || !SecureCookies(r, "HTTPS://ops.example") {
|
|
t.Fatal("public URL")
|
|
}
|
|
r.TLS = &tls.ConnectionState{}
|
|
if !SecureCookies(r, "") {
|
|
t.Fatal("TLS")
|
|
}
|
|
}
|
|
|
|
func TestBearerTokens(t *testing.T) {
|
|
ok := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) })
|
|
h := BearerTokens(ok, []string{" one ", "", "two"})
|
|
for auth, want := range map[string]int{
|
|
"": 401, "Bearer one": 204, "bearer two": 204, "Bearer three": 401, "Basic one": 401, "Bearer": 401, "Bearer onex": 401,
|
|
} {
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
if auth != "" {
|
|
r.Header.Set("Authorization", auth)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
if w.Code != want {
|
|
t.Errorf("%q: %d, want %d", auth, w.Code, want)
|
|
}
|
|
if want == 401 && w.Header().Get("WWW-Authenticate") == "" {
|
|
t.Errorf("%q: no WWW-Authenticate", auth)
|
|
}
|
|
}
|
|
w := httptest.NewRecorder()
|
|
BearerTokens(ok, []string{" ", ""}).ServeHTTP(w, httptest.NewRequest("GET", "/", nil))
|
|
if w.Code != 204 {
|
|
t.Errorf("no tokens: %d", w.Code)
|
|
}
|
|
}
|