mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-11-04 08:04:10 +00:00
The upgrade request passed the ban and rate-limit checks on its own headers. The backend then acted as the identity in the connection_init payload. A banned user could connect without credentials and send their token in the payload. The proxy now runs the same checks on the payload identity.
44 lines
1.7 KiB
Go
44 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"net/http"
|
|
"testing"
|
|
|
|
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func unsignedJWT(sub, role string) string {
|
|
enc := base64.RawURLEncoding.EncodeToString
|
|
return "Bearer " + enc([]byte(`{"alg":"none"}`)) + "." +
|
|
enc([]byte(`{"sub":"`+sub+`","role":"`+role+`"}`)) + "."
|
|
}
|
|
|
|
// Pins theorems/Theorems/WsIdentityGate.lean: the identity the backend acts as is the one that is gated.
|
|
func TestAuthorizePayloadIdentity(t *testing.T) {
|
|
cfg = &config{Logger: libpack_logger.New()}
|
|
cfg.Client.JWTUserClaimPath = "sub"
|
|
cfg.Client.JWTRoleClaimPath = "role"
|
|
bannedUsersIDs.Store("banned-user", "test")
|
|
defer bannedUsersIDs.Delete("banned-user")
|
|
|
|
tests := []struct {
|
|
name string
|
|
upgrade http.Header
|
|
enriched http.Header
|
|
wantErr error
|
|
}{
|
|
{"payload adds banned identity", http.Header{}, http.Header{"Authorization": {unsignedJWT("banned-user", "user")}}, errWSBanned},
|
|
{"payload replaces clean identity with banned", http.Header{"Authorization": {unsignedJWT("ok-user", "user")}}, http.Header{"Authorization": {unsignedJWT("banned-user", "user")}}, errWSBanned},
|
|
{"payload identity clean", http.Header{}, http.Header{"Authorization": {unsignedJWT("ok-user", "user")}}, nil},
|
|
{"payload does not change identity", http.Header{"Authorization": {unsignedJWT("ok-user", "user")}}, http.Header{"Authorization": {unsignedJWT("ok-user", "user")}}, nil},
|
|
{"no authorization anywhere", http.Header{}, http.Header{}, nil},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert.ErrorIs(t, authorizePayloadIdentity(tt.upgrade, tt.enriched), tt.wantErr)
|
|
})
|
|
}
|
|
}
|