You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.4 KiB
Go
87 lines
3.4 KiB
Go
package protocol
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"errors"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type testNonceStore map[string]time.Time
|
|
|
|
func (s testNonceStore) UseNonce(nonce string, now, expiresAt time.Time) (bool, error) {
|
|
if expiry, exists := s[nonce]; exists && expiry.After(now) {
|
|
return false, nil
|
|
}
|
|
s[nonce] = expiresAt
|
|
return true, nil
|
|
}
|
|
|
|
func TestAuthenticateBindsRequestAndRejectsReplay(t *testing.T) {
|
|
secret := []byte("0123456789abcdef0123456789abcdef")
|
|
now := time.Unix(2_000_000_000, 0)
|
|
authenticator, err := NewAuthenticator(secret, time.Minute, 2*time.Minute, testNonceStore{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
authenticator.SetClockForTesting(func() time.Time { return now })
|
|
body := []byte(`{"agent_id":"12","type":"request","id":"request_12345678","method":"GET","request_target":"/v1/health","body":"","headers":{}}`)
|
|
request, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1/internal/agent/request?trace=false", bytes.NewReader(body))
|
|
actor := Actor{Service: "php-panel"}
|
|
if err := Sign(request, body, actor, secret, now, "0123456789abcdef0123456789abcdef"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := authenticator.Authenticate(request, body)
|
|
if err != nil || got.Service != "php-panel" {
|
|
t.Fatalf("valid request rejected: actor=%#v err=%v", got, err)
|
|
}
|
|
if _, err := authenticator.Authenticate(request, body); !errors.Is(err, ErrReplay) {
|
|
t.Fatalf("replay error = %v", err)
|
|
}
|
|
|
|
request, _ = http.NewRequest(http.MethodPost, "http://127.0.0.1/internal/agent/request?trace=false", bytes.NewReader(body))
|
|
_ = Sign(request, body, actor, secret, now, "fedcba9876543210fedcba9876543210")
|
|
request.URL.RawQuery = "trace=true"
|
|
if _, err := authenticator.Authenticate(request, body); !errors.Is(err, ErrAuth) {
|
|
t.Fatalf("changed target error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSignUsesBodyDigest(t *testing.T) {
|
|
secret := []byte("0123456789abcdef0123456789abcdef")
|
|
now := time.Unix(2_000_000_000, 0)
|
|
request, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1/api/internal/cloud/session-introspection", nil)
|
|
if err := Sign(request, []byte("one"), Actor{Service: "agent-gateway"}, secret, now, "0123456789abcdef"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
authenticator, _ := NewAuthenticator(secret, time.Minute, 2*time.Minute, testNonceStore{})
|
|
authenticator.SetClockForTesting(func() time.Time { return now })
|
|
if _, err := authenticator.Authenticate(request, []byte("two")); !errors.Is(err, ErrAuth) {
|
|
t.Fatalf("changed body error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDecodeActorRejectsTrailingJSON(t *testing.T) {
|
|
encoded := base64.RawURLEncoding.EncodeToString([]byte(`{"user_id":1}{"user_id":2}`))
|
|
if _, err := decodeActor(encoded); err == nil {
|
|
t.Fatal("actor with trailing JSON was accepted")
|
|
}
|
|
}
|
|
|
|
func TestAuthenticateRejectsDuplicateSecurityHeaders(t *testing.T) {
|
|
secret := []byte("0123456789abcdef0123456789abcdef")
|
|
now := time.Unix(2_000_000_000, 0)
|
|
request, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1/api/internal/cloud/agent-auth", nil)
|
|
if err := Sign(request, nil, Actor{Service: "agent-gateway"}, secret, now, "0123456789abcdef"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request.Header.Add(HeaderTimestamp, request.Header.Get(HeaderTimestamp))
|
|
authenticator, _ := NewAuthenticator(secret, time.Minute, 2*time.Minute, testNonceStore{})
|
|
authenticator.SetClockForTesting(func() time.Time { return now })
|
|
if _, err := authenticator.Authenticate(request, nil); !errors.Is(err, ErrAuth) {
|
|
t.Fatalf("duplicate header error = %v", err)
|
|
}
|
|
}
|