fix: align tests with new permissive auth behavior

This commit is contained in:
Haitao Pan 2026-04-21 20:38:07 +08:00
parent fe13604703
commit 5b5ffa86cd
5 changed files with 32 additions and 19 deletions

View File

@ -272,14 +272,14 @@ func TestHandleRPCAllowsUnauthenticatedRequestsWhenBridgeAuthTokenUnset(t *testi
func TestHandleRPCRequiresBearerAuthorizationWhenBridgeAuthTokenConfigured(t *testing.T) {
t.Setenv("BRIDGE_AUTH_TOKEN", "bridge-test-token")
t.Setenv("BRIDGE_CONFIG_PATH", "../../example/config.yaml")
server := NewServer()
recorder := httptest.NewRecorder()
// session.start is a protected method that requires authentication
request := httptest.NewRequest(
http.MethodPost,
"http://127.0.0.1/acp/rpc",
strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"acp.capabilities"}`),
strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"session.start","params":{"sessionId":"test"}}`),
)
request.Header.Set("Content-Type", "application/json")

View File

@ -24,18 +24,17 @@ func (h *TokenAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
token := r.Header.Get("Authorization")
if !h.service.ValidateAuthorizationHeader(token) {
if h.service.ValidateAuthorizationHeader(token) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
// Return JSON error instead of plain text to satisfy Flutter's expectation
_ = json.NewEncoder(w).Encode(shared.ErrorEnvelope(nil, -32001, "unauthorized"))
_ = json.NewEncoder(w).Encode(map[string]any{
"jsonrpc": "2.0",
"ok": true,
"type": "res",
"payload": map[string]any{"authenticated": true},
})
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"jsonrpc": "2.0",
"ok": true,
"type": "res",
"payload": map[string]any{"authenticated": true},
})
w.WriteHeader(http.StatusUnauthorized)
_ = json.NewEncoder(w).Encode(shared.ErrorEnvelope(nil, -32001, "unauthorized"))
}

View File

@ -21,8 +21,8 @@ func TestTokenAuthHandlerServeHTTP(t *testing.T) {
}
}
func TestTokenAuthHandlerRejectsMissingBearer(t *testing.T) {
h := NewTokenAuthHandler(service.NewStaticTokenAuthService(""))
func TestTokenAuthHandlerRejectsUnauthorized(t *testing.T) {
h := NewTokenAuthHandler(service.NewStaticTokenAuthService("secret"))
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()

View File

@ -13,7 +13,11 @@ func NewStaticTokenAuthService(expectedToken string) *StaticTokenAuthService {
}
func (s *StaticTokenAuthService) ValidateToken(token string) bool {
return s.ValidateAuthorizationHeader(token)
token = strings.TrimSpace(token)
if s.expectedToken == "" {
return true
}
return token == s.expectedToken
}
func (s *StaticTokenAuthService) ValidateAuthorizationHeader(header string) bool {

View File

@ -12,15 +12,25 @@ func TestStaticTokenAuthServiceValidateToken(t *testing.T) {
}
}
func TestStaticTokenAuthServiceValidateAuthorizationHeaderAsBearer(t *testing.T) {
func TestStaticTokenAuthServiceValidateAuthorizationHeaderPermissive(t *testing.T) {
svc := NewStaticTokenAuthService("")
if !svc.ValidateAuthorizationHeader("Bearer test-token") {
t.Fatal("expected bearer header to be accepted")
}
if !svc.ValidateAuthorizationHeader("Basic abc") {
t.Fatal("expected any header to be accepted when no token is set")
}
}
func TestStaticTokenAuthServiceValidateAuthorizationHeaderStrictWhenSet(t *testing.T) {
svc := NewStaticTokenAuthService("secret")
if !svc.ValidateAuthorizationHeader("Bearer secret") {
t.Fatal("expected bearer header to be accepted")
}
if svc.ValidateAuthorizationHeader("Bearer wrong") {
t.Fatal("expected wrong bearer token to be rejected")
}
if svc.ValidateAuthorizationHeader("Basic abc") {
t.Fatal("expected non-bearer header to be rejected")
}
if svc.ValidateAuthorizationHeader("Bearer ") {
t.Fatal("expected empty bearer token to be rejected")
}
}