feat(sync): paginate Android sync with change tokens
Lint / Lint (push) Successful in 1m8s
Tests / Go backend (push) Failing after 1m23s
Tests / Dashboard checks (push) Failing after 38s
Docker / Docker Build & Push (push) Failing after 6m29s

This commit is contained in:
Gabriel20xx committed 2026-10-20 15:27:53 +00:00
1 parent 0ff94f9b10
commit 97c237be80
76 files changed
+3401 -851

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)
}
}
}
+30 -3
View File
@@ -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
}
+23 -5
View File
@@ -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)
}
}
}