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.
306 lines
11 KiB
Go
306 lines
11 KiB
Go
package auth
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
protocolRawKey = "0123456789abcdef0123456789abcdef"
|
|
protocolEncodedKey = "MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY="
|
|
protocolBody = `{"ok":true}`
|
|
protocolNonce = "0123456789abcdef"
|
|
protocolActor = "panel-agent"
|
|
protocolSignature = "cdaa0263ea55989caf519362660358ea67d78002ff428490d39734f0c7a1cb37"
|
|
protocolAgentID = "host-103"
|
|
protocolBoundSig = "727720b3ed11cc15b2664cdb8c4bc1c2f7befa9742666815137cb281d873b41c"
|
|
protocolTimestamp = int64(1_700_000_000)
|
|
)
|
|
|
|
func TestSignedRequestIsAcceptedOnce(t *testing.T) {
|
|
key := []byte(strings.Repeat("k", 32))
|
|
verifier, err := NewRaw(key, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := []byte(`{"operation_id":"op-1"}`)
|
|
now := time.Unix(1_700_000_000, 0)
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/operations", strings.NewReader(string(body)))
|
|
nonce := strings.Repeat("n", 16)
|
|
actor := "panel-agent"
|
|
req.Header.Set("X-Cloud-Timestamp", "1700000000")
|
|
req.Header.Set("X-Cloud-Nonce", nonce)
|
|
req.Header.Set("X-Cloud-Actor", actor)
|
|
req.Header.Set("X-Cloud-Signature", Sign(key, req.Method, req.URL.RequestURI(), body, now, nonce, actor))
|
|
if err := verifier.Verify(req, body, now); err != nil {
|
|
t.Fatalf("Verify() error = %v", err)
|
|
}
|
|
if err := verifier.Verify(req, body, now); err == nil {
|
|
t.Fatal("replayed nonce was accepted")
|
|
}
|
|
}
|
|
|
|
func TestSignedRequestRejectsBodyMutation(t *testing.T) {
|
|
key := []byte(strings.Repeat("k", 32))
|
|
verifier, _ := NewRaw(key, true)
|
|
now := time.Unix(1_700_000_000, 0)
|
|
body := []byte("original")
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/operations", strings.NewReader(string(body)))
|
|
nonce := strings.Repeat("n", 16)
|
|
actor := "panel-agent"
|
|
req.Header.Set("X-Cloud-Timestamp", "1700000000")
|
|
req.Header.Set("X-Cloud-Nonce", nonce)
|
|
req.Header.Set("X-Cloud-Actor", actor)
|
|
req.Header.Set("X-Cloud-Signature", Sign(key, req.Method, req.URL.RequestURI(), body, now, nonce, actor))
|
|
if err := verifier.Verify(req, []byte("changed"), now); err == nil {
|
|
t.Fatal("body mutation was accepted")
|
|
}
|
|
}
|
|
|
|
func TestAgentBoundVerifierRequiresMatchingIdentity(t *testing.T) {
|
|
key := []byte(strings.Repeat("k", 32))
|
|
verifier, err := NewForAgent(key, true, "host-103")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Unix(1_700_000_000, 0)
|
|
body := []byte(`{"operation_id":"op-bound"}`)
|
|
nonce := strings.Repeat("b", 16)
|
|
actor := "panel-agent"
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/operations", strings.NewReader(string(body)))
|
|
req.Header.Set("X-Cloud-Timestamp", "1700000000")
|
|
req.Header.Set("X-Cloud-Nonce", nonce)
|
|
req.Header.Set("X-Cloud-Actor", actor)
|
|
req.Header.Set("X-Cloud-Agent-ID", "host-other")
|
|
req.Header.Set("X-Cloud-Signature", SignForAgent(key, req.Method, req.URL.RequestURI(), body, now, nonce, "host-other", actor))
|
|
if err := verifier.Verify(req, body, now); err == nil {
|
|
t.Fatal("a signature for another agent was accepted")
|
|
}
|
|
req.Header.Set("X-Cloud-Agent-ID", "host-103")
|
|
req.Header.Set("X-Cloud-Signature", SignForAgent(key, req.Method, req.URL.RequestURI(), body, now, nonce, "host-103", actor))
|
|
if err := verifier.Verify(req, body, now); err != nil {
|
|
t.Fatalf("matching agent signature was rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRequiredVerifierWithoutKeyStaysNotReady(t *testing.T) {
|
|
verifier, err := New(nil, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if verifier.Ready() {
|
|
t.Fatal("missing required key reported ready")
|
|
}
|
|
}
|
|
|
|
func TestFromFileDecodesCanonicalBase64RawKey(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "request.hmac")
|
|
if err := os.WriteFile(path, []byte(protocolEncodedKey+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
verifier, err := FromFile(path, true)
|
|
if err != nil {
|
|
t.Fatalf("FromFile() error = %v", err)
|
|
}
|
|
if !verifier.Ready() {
|
|
t.Fatal("decoded verifier is not ready")
|
|
}
|
|
|
|
now := time.Unix(protocolTimestamp, 0)
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/operations", strings.NewReader(protocolBody))
|
|
req.Header.Set("X-Cloud-Timestamp", "1700000000")
|
|
req.Header.Set("X-Cloud-Nonce", protocolNonce)
|
|
req.Header.Set("X-Cloud-Actor", protocolActor)
|
|
req.Header.Set("X-Cloud-Signature", protocolSignature)
|
|
if err := verifier.Verify(req, []byte(protocolBody), now); err != nil {
|
|
t.Fatalf("known protocol vector was rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestKnownProtocolVectorMatchesPHPCloudAgent(t *testing.T) {
|
|
t.Parallel()
|
|
rawKey, err := base64.StdEncoding.Strict().DecodeString(protocolEncodedKey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(rawKey) != protocolRawKey || len(rawKey) != fileKeyBytes {
|
|
t.Fatalf("fixture key decoded incorrectly: %q (%d bytes)", rawKey, len(rawKey))
|
|
}
|
|
got := Sign(rawKey, http.MethodPost, "/v1/operations", []byte(protocolBody),
|
|
time.Unix(protocolTimestamp, 0), protocolNonce, protocolActor)
|
|
if got != protocolSignature {
|
|
t.Fatalf("known vector signature = %s, want %s", got, protocolSignature)
|
|
}
|
|
bound := SignForAgent(rawKey, http.MethodPost, "/v1/operations", []byte(protocolBody),
|
|
time.Unix(protocolTimestamp, 0), protocolNonce, protocolAgentID, protocolActor)
|
|
if bound != protocolBoundSig {
|
|
t.Fatalf("agent-bound vector signature = %s, want %s", bound, protocolBoundSig)
|
|
}
|
|
}
|
|
|
|
func TestFromFileBindsConfiguredAgentID(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "request.hmac")
|
|
if err := os.WriteFile(path, []byte(protocolEncodedKey+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
verifier, err := FromFileForAgent(path, true, protocolAgentID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Unix(protocolTimestamp, 0)
|
|
nonce := "fedcba9876543210"
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/operations", strings.NewReader(protocolBody))
|
|
req.Header.Set("X-Cloud-Timestamp", "1700000000")
|
|
req.Header.Set("X-Cloud-Nonce", nonce)
|
|
req.Header.Set("X-Cloud-Agent-ID", protocolAgentID)
|
|
req.Header.Set("X-Cloud-Actor", protocolActor)
|
|
req.Header.Set("X-Cloud-Signature", SignForAgent(
|
|
[]byte(protocolRawKey), http.MethodPost, "/v1/operations", []byte(protocolBody),
|
|
now, nonce, protocolAgentID, protocolActor,
|
|
))
|
|
if err := verifier.Verify(req, []byte(protocolBody), now); err != nil {
|
|
t.Fatalf("agent-bound request was rejected: %v", err)
|
|
}
|
|
req.Header.Set("X-Cloud-Agent-ID", "host-other")
|
|
if err := verifier.Verify(req, []byte(protocolBody), now); err == nil {
|
|
t.Fatal("request for another agent was accepted")
|
|
}
|
|
}
|
|
|
|
func TestFromFileRejectsRawOrNonCanonicalKeyText(t *testing.T) {
|
|
t.Parallel()
|
|
tests := map[string]string{
|
|
"raw bytes": protocolRawKey,
|
|
"embedded newline": protocolEncodedKey[:12] + "\n" + protocolEncodedKey[12:],
|
|
"noncanonical pad bits": protocolEncodedKey[:len(protocolEncodedKey)-2] + "Z=",
|
|
"truncated": protocolEncodedKey[:len(protocolEncodedKey)-1],
|
|
}
|
|
for name, contents := range tests {
|
|
name, contents := name, contents
|
|
t.Run(name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "request.hmac")
|
|
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
verifier, err := FromFile(path, true)
|
|
if err == nil {
|
|
t.Fatal("invalid key file was accepted")
|
|
}
|
|
if verifier == nil || verifier.Ready() {
|
|
t.Fatal("invalid required key file produced a ready verifier")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFromFileRejectsSymlinkAndGroupReadableKey(t *testing.T) {
|
|
t.Parallel()
|
|
directory := t.TempDir()
|
|
keyPath := filepath.Join(directory, "request.hmac")
|
|
if err := os.WriteFile(keyPath, []byte(protocolEncodedKey+"\n"), 0o640); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if verifier, err := FromFile(keyPath, true); err == nil || verifier == nil || verifier.Ready() {
|
|
t.Fatalf("group-readable key was accepted: verifier=%v err=%v", verifier, err)
|
|
}
|
|
if err := os.Chmod(keyPath, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
linkPath := filepath.Join(directory, "request-link")
|
|
if err := os.Symlink(keyPath, linkPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if verifier, err := FromFile(linkPath, true); err == nil || verifier == nil || verifier.Ready() {
|
|
t.Fatalf("symlink key was accepted: verifier=%v err=%v", verifier, err)
|
|
}
|
|
}
|
|
|
|
func TestFromFileRejectsUnsafeAncestorPaths(t *testing.T) {
|
|
t.Parallel()
|
|
base := t.TempDir()
|
|
keyData := []byte(protocolEncodedKey + "\n")
|
|
|
|
goodParent := filepath.Join(base, "good")
|
|
if err := os.Mkdir(goodParent, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
goodKey := filepath.Join(goodParent, "request.hmac")
|
|
if err := os.WriteFile(goodKey, keyData, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if verifier, err := FromFile(goodKey, true); err != nil || verifier == nil || !verifier.Ready() {
|
|
t.Fatalf("secure ancestor path was rejected: verifier=%v err=%v", verifier, err)
|
|
}
|
|
|
|
realParent := filepath.Join(base, "real")
|
|
if err := os.Mkdir(realParent, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(realParent, "request.hmac"), keyData, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
linkParent := filepath.Join(base, "linked-parent")
|
|
if err := os.Symlink(realParent, linkParent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if verifier, err := FromFile(filepath.Join(linkParent, "request.hmac"), true); err == nil || verifier == nil || verifier.Ready() {
|
|
t.Fatalf("symlinked ancestor was accepted: verifier=%v err=%v", verifier, err)
|
|
}
|
|
|
|
nonDir := filepath.Join(base, "not-a-directory")
|
|
if err := os.WriteFile(nonDir, []byte("x"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if verifier, err := FromFile(filepath.Join(nonDir, "request.hmac"), true); err == nil || verifier == nil || verifier.Ready() {
|
|
t.Fatalf("non-directory ancestor was accepted: verifier=%v err=%v", verifier, err)
|
|
}
|
|
|
|
writableParent := filepath.Join(base, "writable")
|
|
if err := os.Mkdir(writableParent, 0o770); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(writableParent, 0o770); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writableKey := filepath.Join(writableParent, "request.hmac")
|
|
if err := os.WriteFile(writableKey, keyData, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if verifier, err := FromFile(writableKey, true); err == nil || verifier == nil || verifier.Ready() {
|
|
t.Fatalf("group-writable ancestor was accepted: verifier=%v err=%v", verifier, err)
|
|
}
|
|
}
|
|
|
|
func TestFromFileRejectsUnsafeRegularFileModes(t *testing.T) {
|
|
t.Parallel()
|
|
for _, mode := range []os.FileMode{0o644, 0o700, 0o600 | os.ModeSticky} {
|
|
mode := mode
|
|
t.Run(modeString(mode), func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "request.hmac")
|
|
if err := os.WriteFile(path, []byte(protocolEncodedKey+"\n"), mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(path, mode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
verifier, err := FromFile(path, true)
|
|
if err == nil || verifier == nil || verifier.Ready() {
|
|
t.Fatalf("mode %o was accepted: verifier=%v err=%v", mode, verifier, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func modeString(mode os.FileMode) string {
|
|
return "mode-" + strconv.FormatUint(uint64(mode.Perm()), 8)
|
|
}
|