Stop allowing cross-origin requests (closes #261)
check / check (push) Failing after 4s

The CORS middleware let every origin make credentialed requests, so a
page on another subdomain of the server's domain could read and act as
the signed-in user. Nothing needs cross-origin access, so the
middleware and the go-chi/cors dependency go. A test checks that a
request from another origin and a preflight get no
Access-Control-Allow-* headers.

Model: opus-5-5
This commit is contained in:
clawbot committed 2026-11-10 21:50:10 +00:00
1 parent f42c6214a0
commit 3587ce92cd
6 files changed
+74 -39

No files matched your search

+65
View File
@@ -8,6 +8,7 @@ import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
@@ -188,6 +189,70 @@ func TestServeAfterStopDoesNotStopApp(t *testing.T) {
}
}
// TestNoCrossOriginAccess checks that the server allows no
// cross-origin requests: neither a request from another origin
// nor a preflight gets Access-Control-Allow-Origin or
// Access-Control-Allow-Credentials, and a preflight gets 405
// like any other OPTIONS request.
func TestNoCrossOriginAccess(t *testing.T) {
var srv *server.Server
startApp(t, fx.Populate(&srv))
tests := []struct {
name string
method string
requestMethod string
status int
}{
{
name: "request",
method: http.MethodGet,
status: http.StatusOK,
},
{
name: "preflight",
method: http.MethodOptions,
requestMethod: http.MethodPost,
status: http.StatusMethodNotAllowed,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
request := httptest.NewRequestWithContext(
t.Context(), test.method,
"http://chat.example.com/api/v1/server", nil,
)
request.Header.Set("Origin", "http://blog.example.com")
if test.requestMethod != "" {
request.Header.Set(
"Access-Control-Request-Method", test.requestMethod,
)
}
recorder := httptest.NewRecorder()
srv.ServeHTTP(recorder, request)
if recorder.Code != test.status {
t.Errorf("status %d, want %d", recorder.Code, test.status)
}
for _, header := range []string{
"Access-Control-Allow-Origin",
"Access-Control-Allow-Credentials",
} {
value := recorder.Header().Get(header)
if value != "" {
t.Errorf("%s is %q, want none", header, value)
}
}
})
}
}
// startApp starts the app newApp builds, with the HTTP port
// set to 0, so the system gives the server a free port.
func startApp(