docs: record auth hash alignment and validation results
This commit is contained in:
1 parent
22887fef03
commit
f7819904b9
51 files changed
+1792
-542
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package coreconfig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// contractFile holds the fixtures the Go, Node and Python internal-secret checks must agree on. The
|
||||
// Node and Python suites load the same file; see the "description" field in it.
|
||||
const contractFile = "../../contract/auth-contract.json"
|
||||
|
||||
type internalSecretContract struct {
|
||||
InternalSecret struct {
|
||||
MinLength int `json:"minLength"`
|
||||
Placeholders []string `json:"placeholders"`
|
||||
Cases []struct {
|
||||
Name string `json:"name"`
|
||||
Secret string `json:"secret"`
|
||||
Provided string `json:"provided"`
|
||||
Matches bool `json:"matches"`
|
||||
} `json:"cases"`
|
||||
} `json:"internalSecret"`
|
||||
}
|
||||
|
||||
func TestContractInternalSecret(t *testing.T) {
|
||||
data, err := os.ReadFile(contractFile)
|
||||
if os.IsNotExist(err) {
|
||||
t.Skipf("%s is not part of this checkout", contractFile)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var contract internalSecretContract
|
||||
if err := json.Unmarshal(data, &contract); err != nil {
|
||||
t.Fatalf("decode %s: %v", contractFile, err)
|
||||
}
|
||||
rule := contract.InternalSecret
|
||||
if MinInternalSecretLength != rule.MinLength {
|
||||
t.Fatalf("MinInternalSecretLength = %d, contract says %d", MinInternalSecretLength, rule.MinLength)
|
||||
}
|
||||
for _, placeholder := range rule.Placeholders {
|
||||
if InternalSecretUsable(placeholder) {
|
||||
t.Errorf("placeholder %q is usable, contract says it never gates a route", placeholder)
|
||||
}
|
||||
}
|
||||
for _, entry := range rule.Cases {
|
||||
if got := InternalSecretMatches(entry.Provided, entry.Secret); got != entry.Matches {
|
||||
t.Errorf("%s: InternalSecretMatches = %v, contract says %v", entry.Name, got, entry.Matches)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,35 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// MinInternalSecretLength is the shortest SECRET a service accepts for its internal routes.
|
||||
const MinInternalSecretLength = 16
|
||||
|
||||
// exampleInternalSecrets are the placeholders shipped in .env.example files, plus the bare variable
|
||||
// name. They are public, so they never gate a route.
|
||||
var exampleInternalSecrets = map[string]bool{
|
||||
"SECRET": true,
|
||||
"change-me-to-a-long-random-value": true,
|
||||
}
|
||||
|
||||
// InternalSecretUsable reports whether secret is strong enough to gate an internal route: at least
|
||||
// MinInternalSecretLength characters and not an example placeholder. Length counts characters, as
|
||||
// the Python and Node helpers do, not bytes.
|
||||
func InternalSecretUsable(secret string) bool {
|
||||
return utf8.RuneCountInString(secret) >= MinInternalSecretLength && !exampleInternalSecrets[secret]
|
||||
}
|
||||
|
||||
// InternalSecretMatches reports whether presented is the configured secret. A secret that is not
|
||||
// usable (see InternalSecretUsable) never matches, and the comparison is constant-time.
|
||||
func InternalSecretMatches(presented, secret string) bool {
|
||||
if !InternalSecretUsable(secret) {
|
||||
return false
|
||||
}
|
||||
return subtle.ConstantTimeCompare([]byte(presented), []byte(secret)) == 1
|
||||
}
|
||||
|
||||
// Info is AuthGuard's, CarrierNode's, and ProjectConfigurator's current base URL(s), as pushed by
|
||||
// ProjectConfigurator.
|
||||
type Info struct {
|
||||
@@ -178,11 +205,11 @@ func normalizeInfo(info Info) Info {
|
||||
// ProjectConfigurator calls to push a fresh Info: mount it at that path (see
|
||||
// ProjectConfigurator/README.md) behind a secret check for this service's own
|
||||
// SECRET (the same value ProjectConfigurator and CarrierNode were configured
|
||||
// with - an empty secret refuses every caller, the same fail-closed default ProjectConfigurator's
|
||||
// own internal-auth-gated routes use).
|
||||
// with - a secret that is not usable (see InternalSecretUsable) refuses every caller, the same
|
||||
// fail-closed default ProjectConfigurator's own internal-auth-gated routes use).
|
||||
func Handler(secret string, store *Store) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if secret == "" || subtle.ConstantTimeCompare([]byte(r.Header.Get("X-Internal-Auth")), []byte(secret)) != 1 {
|
||||
if !InternalSecretMatches(r.Header.Get("X-Internal-Auth"), secret) {
|
||||
writeError(w, http.StatusUnauthorized, "internal service authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ func TestStoreOnUpdateFiresWithNewInfo(t *testing.T) {
|
||||
|
||||
func TestHandlerRejectsMissingOrWrongSecret(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
handler := Handler("s3cret", store)
|
||||
handler := Handler("test-internal-secret", store)
|
||||
body, _ := json.Marshal(Info{AuthGuardURL: "http://auth.example.com"})
|
||||
|
||||
cases := []string{"", "wrong"}
|
||||
@@ -127,14 +127,14 @@ func equalStrings(got, want []string) bool {
|
||||
|
||||
func TestHandlerAppliesPushedInfo(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
handler := Handler("s3cret", store)
|
||||
handler := Handler("test-internal-secret", store)
|
||||
body, _ := json.Marshal(Info{
|
||||
AuthGuardURL: "http://auth.example.com", CarrierNodeURL: "http://carrier.example.com", CarrierNodeSecret: "carrier-secret",
|
||||
ContentScannerURL: "http://scanner.example.com/", ContentScannerSecret: "scanner-secret",
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
|
||||
req.Header.Set("X-Internal-Auth", "s3cret")
|
||||
req.Header.Set("X-Internal-Auth", "test-internal-secret")
|
||||
rec := httptest.NewRecorder()
|
||||
handler(rec, req)
|
||||
|
||||
@@ -152,7 +152,7 @@ func TestHandlerAppliesPushedInfo(t *testing.T) {
|
||||
|
||||
func TestHandlerRejectsNonHTTPURLsWithoutChangingStore(t *testing.T) {
|
||||
store := NewStore(Info{AuthGuardURL: "http://auth.example.com"})
|
||||
handler := Handler("s3cret", store)
|
||||
handler := Handler("test-internal-secret", store)
|
||||
for _, bad := range []Info{
|
||||
{AuthGuardURL: "javascript:alert(1)"},
|
||||
{AuthGuardURL: "auth.example.com"},
|
||||
@@ -161,7 +161,7 @@ func TestHandlerRejectsNonHTTPURLsWithoutChangingStore(t *testing.T) {
|
||||
} {
|
||||
body, _ := json.Marshal(bad)
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
|
||||
req.Header.Set("X-Internal-Auth", "s3cret")
|
||||
req.Header.Set("X-Internal-Auth", "test-internal-secret")
|
||||
rec := httptest.NewRecorder()
|
||||
handler(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
@@ -172,3 +172,21 @@ func TestHandlerRejectsNonHTTPURLsWithoutChangingStore(t *testing.T) {
|
||||
t.Fatalf("store changed by a rejected push: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRefusesAnUnusableSecret(t *testing.T) {
|
||||
for _, secret := range []string{"short", "SECRET", "change-me-to-a-long-random-value"} {
|
||||
store := NewStore(Info{})
|
||||
handler := Handler(secret, store)
|
||||
body, _ := json.Marshal(Info{AuthGuardURL: "http://auth.example.com"})
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
|
||||
req.Header.Set("X-Internal-Auth", secret)
|
||||
rec := httptest.NewRecorder()
|
||||
handler(rec, req)
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("secret %q: status = %d, want %d", secret, rec.Code, http.StatusUnauthorized)
|
||||
}
|
||||
if store.Get().AuthGuardURL != "" {
|
||||
t.Fatalf("secret %q: store changed despite a refused push", secret)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user