From 34357adf71b74a242e0017aa8d2d39a01a306efe Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:46:40 -0500 Subject: [PATCH 1/2] feat: hub per-hive consent match endpoint Co-Authored-By: Claude Opus 5.5 Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: clubanderson <407614+clubanderson@users.noreply.github.com> --- changelog.d/added-19162-hub-customer-match.md | 1 + src/pkg/hub/customer_consent.go | 5 + src/pkg/hub/customer_consent_store.go | 10 +- src/pkg/hub/customer_match.go | 456 ++++++++++++++++++ src/pkg/hub/customer_match_test.go | 344 +++++++++++++ 5 files changed, 813 insertions(+), 3 deletions(-) create mode 100644 changelog.d/added-19162-hub-customer-match.md create mode 100644 src/pkg/hub/customer_match.go create mode 100644 src/pkg/hub/customer_match_test.go diff --git a/changelog.d/added-19162-hub-customer-match.md b/changelog.d/added-19162-hub-customer-match.md new file mode 100644 index 0000000000..9e649c7a46 --- /dev/null +++ b/changelog.d/added-19162-hub-customer-match.md @@ -0,0 +1 @@ +- **Hub: per-hive customer match endpoint, consent only** ([#19162](https://github.com/hivecommons/hive/issues/19162)). `POST /api/customers/match` lets a hive, under its own per-hive heartbeat bearer, post salted hashes of its confirmed customers' GitHub logins and get back real name, organization and country only for people who consented to that hive, only for values they entered themselves, checked at answer time. Unknown and unconsented people get the same empty answer; a `cleared` entry is sent only after an earlier delivery. Batch, body, response and rate limits are named constants, deliveries are stamped on the consent record, and the route does not exist while `HIVE_HUB_CUSTOMERS_SHARE_ENABLED` is off. No spoke calls it yet. diff --git a/src/pkg/hub/customer_consent.go b/src/pkg/hub/customer_consent.go index 345fccb21b..2192780a6d 100644 --- a/src/pkg/hub/customer_consent.go +++ b/src/pkg/hub/customer_consent.go @@ -76,6 +76,9 @@ func (s *HubServer) registerCustomerSharingRoutes() { s.mux.HandleFunc("GET /api/saas/me/profile-share/consents", s.requireCustomerSharing(s.handleListCustomerConsents)) s.mux.HandleFunc("POST /api/saas/me/profile-share/consents/{id}", s.requireCustomerSharing(s.handleChangeCustomerConsent)) s.mux.HandleFunc("DELETE /api/saas/me/profile-share/consents/{id}", s.requireCustomerSharing(s.handleChangeCustomerConsent)) + // Hive-facing: per-hive bearer, not a session. The handler answers 404 + // while the sharing switch is off, like the routes above (#19162). + s.mux.HandleFunc("POST "+customerMatchPath, s.handleCustomerMatch) } type customerConsentHive struct { @@ -217,6 +220,8 @@ func (s *HubServer) handleChangeCustomerConsent(w http.ResponseWriter, r *http.R s.customerConsentError(w, err) return } + // A new grant must not wait out the match index cache. + invalidateCustomerMatchIndex() s.logger.Info("audit: customer consent "+action, "person", consent.Person, "hive", hiveID, "fields", auditFields.names()) w.WriteHeader(http.StatusNoContent) } diff --git a/src/pkg/hub/customer_consent_store.go b/src/pkg/hub/customer_consent_store.go index 3a82357c2e..2fc309b5bc 100644 --- a/src/pkg/hub/customer_consent_store.go +++ b/src/pkg/hub/customer_consent_store.go @@ -50,9 +50,13 @@ type customerConsent struct { GrantedAt time.Time `json:"granted_at"` UpdatedAt time.Time `json:"updated_at"` WithdrawnAt *time.Time `json:"withdrawn_at,omitempty"` - // Only the delivery path (phase 2) may stamp these times. - FirstDeliveredAt *time.Time `json:"first_delivered_at,omitempty"` - LastDeliveredAt *time.Time `json:"last_delivered_at,omitempty"` + // Only the delivery path (stampCustomerDelivery, customer_match.go) may + // stamp these. DeliveredFields is the union of every field ever sent to + // this hive; DeliveredRev is the rev of the last answer. + FirstDeliveredAt *time.Time `json:"first_delivered_at,omitempty"` + LastDeliveredAt *time.Time `json:"last_delivered_at,omitempty"` + DeliveredFields *customerConsentFields `json:"delivered_fields,omitempty"` + DeliveredRev string `json:"delivered_rev,omitempty"` } func (c customerConsent) discardable(now time.Time) bool { diff --git a/src/pkg/hub/customer_match.go b/src/pkg/hub/customer_match.go new file mode 100644 index 0000000000..3b60b547f3 --- /dev/null +++ b/src/pkg/hub/customer_match.go @@ -0,0 +1,456 @@ +package hub + +// Per-hive customer match endpoint (#19162, phase 2 of +// src/docs/design/customers-hub-consent.md). +// +// A hive posts salted hashes of the GitHub logins of its confirmed customers +// and the hub answers only for people who consented to share fields with that +// hive. Unknown, unconsented and never-delivered people are the same silence, +// so the reply is not an oracle for who exists on the hub. A cleared entry is +// the only statement about a past consent, and only to a hive that already +// received that person's data. + +import ( + "bytes" + "crypto/hmac" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "io" + "net/http" + "os" + "path/filepath" + "strconv" + "strings" + "sync" + "time" +) + +const ( + customerMatchPath = "/api/customers/match" + // Domain-separation label for the per-hive match key. Never the heartbeat + // label: the bearer authenticates the hive, the match key salts the hashes. + infoCustomerMatchKey = "hive-customer-match-v1" + + maxCustomerMatchBatch = 200 + maxCustomerMatchBody = 32 << 10 + maxCustomerMatchResponse = 128 << 10 + customerMatchRateWindow = time.Minute + customerMatchRateLimit = 6 + // How long the per-hive index of consented hashes is reused. Consent is + // still re-read per answer, so a withdrawal is effective immediately; a + // new grant also drops the index (invalidateCustomerMatchIndex). + customerMatchIndexTTL = 30 * time.Second + customerMatchHashLen = sha256.Size * 2 + customerMatchRevLen = 16 +) + +// customerMatchKey derives the per-hive match key from one master. It returns +// "" for an empty master or hive id, so an unconfigured hub matches nothing. +func customerMatchKey(master, hiveID string) string { + return derivePerHiveKey(master, infoCustomerMatchKey, hiveID) +} + +// customerMatchHash is HMAC-SHA256(match key, normalised GitHub login), hex. +func customerMatchHash(key, login string) string { + login = strings.ToLower(strings.TrimSpace(login)) + if key == "" || login == "" { + return "" + } + mac := hmac.New(sha256.New, []byte(key)) + mac.Write([]byte(login)) + return hex.EncodeToString(mac.Sum(nil)) +} + +// customerMatchKeys returns the match key under every master generation the +// hub still accepts, current first, mirroring verifyHeartbeatBearer so a spoke +// not yet reconciled after a rotation still matches. +func (s *HubServer) customerMatchKeys(hiveID string) []string { + var keys []string + if gs := s.currentGenerations(); gs != nil { + for _, g := range gs.acceptableGenerations(time.Now()) { + if k := customerMatchKey(g.Secret, hiveID); k != "" { + keys = append(keys, k) + } + } + return keys + } + if k := customerMatchKey(s.hubSecret, hiveID); k != "" { + keys = append(keys, k) + } + return keys +} + +// customerMatchLogin is the one identifier matched in v1: the primary github: +// identity. A LinkedGitHubLogin is not matched because no hub flow proves it +// (nothing writes it after a GitHub OAuth check), and the design requires that +// proof first. Email is never matched in v1. +func customerMatchLogin(u *SaaSUser) string { + provider, subject, ok := parseCanonical(userCanonicalID(u)) + if !ok || provider != legacyProvider { + return "" + } + return subject +} + +type customerMatchIndex struct { + built time.Time + // match hash -> consent file path. Holds only people with a record for + // this hive that is active or owes a cleared reply. + paths map[string]string +} + +type customerMatchRate struct { + start time.Time + count int +} + +type customerMatchState struct { + mu sync.Mutex + indexes map[string]customerMatchIndex + rates map[string]customerMatchRate +} + +var customerMatchStore customerMatchState + +// allow counts one request against the hive's fixed window. +func (st *customerMatchState) allow(hiveID string, now time.Time) (time.Duration, bool) { + st.mu.Lock() + defer st.mu.Unlock() + if st.rates == nil { + st.rates = make(map[string]customerMatchRate) + } + r := st.rates[hiveID] + if now.Sub(r.start) >= customerMatchRateWindow || now.Before(r.start) { + r = customerMatchRate{start: now} + } + if r.count >= customerMatchRateLimit { + return r.start.Add(customerMatchRateWindow).Sub(now), false + } + r.count++ + st.rates[hiveID] = r + return 0, true +} + +func invalidateCustomerMatchIndex() { + customerMatchStore.mu.Lock() + customerMatchStore.indexes = nil + customerMatchStore.mu.Unlock() +} + +// index returns the hive's hash index, rebuilding it when stale. The caller +// holds customerConsentMu. +func (s *HubServer) customerMatchIndexFor(hiveID string, keys []string, now time.Time) map[string]string { + st := &customerMatchStore + st.mu.Lock() + if idx, ok := st.indexes[hiveID]; ok && now.Sub(idx.built) < customerMatchIndexTTL && !now.Before(idx.built) { + st.mu.Unlock() + return idx.paths + } + st.mu.Unlock() + + paths := make(map[string]string) + dir := customerConsentDir() + entries, err := os.ReadDir(dir) + if err != nil && !errors.Is(err, os.ErrNotExist) { + s.logger.Warn("customer match: list consents failed", "error", err) + } + for _, entry := range entries { + if !entry.Type().IsRegular() || !strings.HasSuffix(entry.Name(), ".json") { + continue + } + path := filepath.Join(dir, entry.Name()) + records, err := readCustomerConsents(path) + if err != nil { + continue + } + c, ok := records[hiveID] + if !ok || c.discardable(now) || c.WithdrawnAt != nil && c.DeliveredFields == nil { + continue + } + user := loadSaaSUser(strings.TrimSuffix(entry.Name(), ".json")) + if user == nil { + continue + } + if p, err := customerConsentPath(user); err != nil || p != path { + continue + } + login := customerMatchLogin(user) + for _, key := range keys { + if h := customerMatchHash(key, login); h != "" { + paths[h] = path + } + } + } + + st.mu.Lock() + if st.indexes == nil { + st.indexes = make(map[string]customerMatchIndex) + } + st.indexes[hiveID] = customerMatchIndex{built: now, paths: paths} + st.mu.Unlock() + return paths +} + +type customerMatchRequest struct { + HiveID string `json:"hive_id"` + Hashes []string `json:"hashes"` +} + +type customerMatchEntry struct { + MatchHash string `json:"match_hash"` + Rev string `json:"rev,omitempty"` + RealName string `json:"real_name,omitempty"` + Organization string `json:"organization,omitempty"` + Country string `json:"country,omitempty"` + Cleared []string `json:"cleared,omitempty"` +} + +type customerMatchResponse struct { + Matches []customerMatchEntry `json:"matches"` + // Truncated is set when the response size cap dropped entries; the hive + // asks again with the remaining hashes. + Truncated bool `json:"truncated,omitempty"` +} + +// customerMatchEffective is the answer-time consent check: record present and +// not withdrawn, current disclosure text, grant still held, person not blocked +// or deleted. The hub switch is checked by the handler before anything else. +func customerMatchEffective(c customerConsent, user *SaaSUser, hiveID string) bool { + return c.WithdrawnAt == nil && c.TextVersion == customerConsentTextVersion && + user != nil && !user.Blocked && user.Hives[hiveID] != "" +} + +// customerMatchShareable returns the values that may cross now: consented AND +// user-entered AND non-empty. Values are read live, never from the record. +func customerMatchShareable(c customerConsent, user *SaaSUser) (customerMatchEntry, customerConsentFields) { + var e customerMatchEntry + var sent customerConsentFields + if c.Fields.RealName && user.FullNameSource == countrySourceUser { + if v := truncateRunes(strings.TrimSpace(user.FullName), maxContactNameLen); v != "" { + e.RealName, sent.RealName = v, true + } + } + if c.Fields.Organization && user.CompanySource == countrySourceUser { + if v := truncateRunes(strings.TrimSpace(user.Company), maxContactCompanyLen); v != "" { + e.Organization, sent.Organization = v, true + } + } + if c.Fields.Country && effectiveCountrySource(user) == countrySourceUser { + if v := normalizeCountryCode(user.Country); v != "" { + e.Country, sent.Country = v, true + } + } + return e, sent +} + +func customerMatchRev(e customerMatchEntry) string { + sum := sha256.Sum256([]byte("real_name\x00" + e.RealName + "\x00organization\x00" + e.Organization + "\x00country\x00" + e.Country)) + return hex.EncodeToString(sum[:])[:customerMatchRevLen] +} + +func validCustomerMatchHash(h string) bool { + if len(h) != customerMatchHashLen { + return false + } + for _, c := range h { + if (c < '0' || c > '9') && (c < 'a' || c > 'f') { + return false + } + } + return true +} + +// pendingCustomerMatch is one answer entry plus the stamp it earns if sent. +type pendingCustomerMatch struct { + entry customerMatchEntry + path string + sent customerConsentFields +} + +// fitCustomerMatches keeps entries, in order, while the encoded response +// stays within limit bytes. It reports whether any entry was dropped. +func fitCustomerMatches(pending []pendingCustomerMatch, limit int) ([]pendingCustomerMatch, bool) { + size := len(`{"matches":[],"truncated":true}`) + for i, p := range pending { + b, err := json.Marshal(p.entry) + if err != nil { + return pending[:i], true + } + size += len(b) + 1 + if size > limit { + return pending[:i], true + } + } + return pending, false +} + +func (s *HubServer) handleCustomerMatch(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-store") + // The endpoint does not exist while the hub sharing switch is off. + if !customersShareEnabled() { + http.NotFound(w, r) + return + } + auth := r.Header.Get("Authorization") + if !strings.HasPrefix(auth, "Bearer ") { + writeJSONError(w, http.StatusUnauthorized, "unauthorized") + return + } + presented := strings.TrimPrefix(auth, "Bearer ") + + var req customerMatchRequest + dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxCustomerMatchBody)) + dec.DisallowUnknownFields() + if err := dec.Decode(&req); err != nil { + var tooLarge *http.MaxBytesError + if errors.As(err, &tooLarge) { + writeJSONError(w, http.StatusRequestEntityTooLarge, "request body too large") + return + } + writeJSONError(w, http.StatusBadRequest, "invalid match JSON") + return + } + if dec.Decode(new(json.RawMessage)) != io.EOF { + writeJSONError(w, http.StatusBadRequest, "invalid match JSON") + return + } + hiveID := sanitizeHeartbeatField(req.HiveID) + if hiveID == "" || !isValidName(hiveID) { + writeJSONError(w, http.StatusBadRequest, "invalid hive_id") + return + } + // The bearer is bound to the claimed hive: a hive can only ask as itself. + if !s.verifyHeartbeatBearer(presented, hiveID) { + writeJSONError(w, http.StatusUnauthorized, "unauthorized") + return + } + if wait, ok := customerMatchStore.allow(hiveID, time.Now()); !ok { + w.Header().Set("Retry-After", strconv.Itoa(int(wait/time.Second)+1)) + writeJSONError(w, http.StatusTooManyRequests, "match rate limit exceeded") + return + } + if len(req.Hashes) > maxCustomerMatchBatch { + writeJSONError(w, http.StatusRequestEntityTooLarge, "too many hashes in one batch") + return + } + seen := make(map[string]bool, len(req.Hashes)) + hashes := make([]string, 0, len(req.Hashes)) + for _, h := range req.Hashes { + if !validCustomerMatchHash(h) { + writeJSONError(w, http.StatusBadRequest, "each hash must be lowercase hex SHA-256") + return + } + if !seen[h] { + seen[h] = true + hashes = append(hashes, h) + } + } + keys := s.customerMatchKeys(hiveID) + if len(keys) == 0 { + writeJSONError(w, http.StatusServiceUnavailable, "matching unavailable") + return + } + + customerConsentMu.Lock() + defer customerConsentMu.Unlock() + now := time.Now().UTC() + index := s.customerMatchIndexFor(hiveID, keys, now) + + // A non-matching hash is dropped here: never stored, never logged. + var pending []pendingCustomerMatch + for _, h := range hashes { + path, ok := index[h] + if !ok { + continue + } + records, err := readCustomerConsents(path) + if err != nil { + continue + } + c, ok := records[hiveID] + if !ok { + continue + } + user := loadSaaSUser(strings.TrimSuffix(filepath.Base(path), ".json")) + var entry customerMatchEntry + var sent customerConsentFields + if customerMatchEffective(c, user, hiveID) { + entry, sent = customerMatchShareable(c, user) + } + // cleared: fields this hive was sent before that do not cross now. + if d := c.DeliveredFields; d != nil { + entry.Cleared = customerConsentFields{ + RealName: d.RealName && !sent.RealName, + Organization: d.Organization && !sent.Organization, + Country: d.Country && !sent.Country, + }.names() + } + if sent == (customerConsentFields{}) && len(entry.Cleared) == 0 { + continue // silent, like an unknown person + } + entry.MatchHash = h + if sent != (customerConsentFields{}) { + entry.Rev = customerMatchRev(entry) + } + pending = append(pending, pendingCustomerMatch{entry: entry, path: path, sent: sent}) + } + pending, truncated := fitCustomerMatches(pending, maxCustomerMatchResponse) + + resp := customerMatchResponse{Matches: make([]customerMatchEntry, 0, len(pending)), Truncated: truncated} + delivered, cleared := 0, 0 + for _, p := range pending { + resp.Matches = append(resp.Matches, p.entry) + if len(p.entry.Cleared) > 0 { + cleared++ + } + if p.sent == (customerConsentFields{}) { + continue + } + delivered++ + if err := stampCustomerDelivery(p.path, hiveID, p.sent, p.entry.Rev, now); err != nil { + s.logger.Warn("customer match: delivery stamp failed", "hive", hiveID, "error", err) + } + } + var buf bytes.Buffer + if err := json.NewEncoder(&buf).Encode(resp); err != nil { + writeJSONError(w, http.StatusInternalServerError, "could not encode match reply") + return + } + // Per request: hive id and counts only. Per delivery: the record stamp. + s.logger.Info("audit: customer match", "hive", hiveID, "requested", len(hashes), + "delivered", delivered, "cleared", cleared, "truncated", truncated) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write(buf.Bytes()) +} + +// stampCustomerDelivery records a delivery on the consent record. The +// delivered field set is the union of every field ever sent to this hive, so +// a later withdrawal or lost provenance knows which fields a cleared reply +// must name, and the person's page can show what was sent. The caller holds +// customerConsentMu. +func stampCustomerDelivery(path, hiveID string, sent customerConsentFields, rev string, now time.Time) error { + records, err := readCustomerConsents(path) + if err != nil { + return err + } + c, ok := records[hiveID] + if !ok { + return nil + } + if c.FirstDeliveredAt == nil { + c.FirstDeliveredAt = &now + } + c.LastDeliveredAt = &now + union := sent + if d := c.DeliveredFields; d != nil { + union.RealName = union.RealName || d.RealName + union.Organization = union.Organization || d.Organization + union.Country = union.Country || d.Country + } + c.DeliveredFields = &union + c.DeliveredRev = rev + records[hiveID] = c + return saveCustomerConsents(path, records) +} diff --git a/src/pkg/hub/customer_match_test.go b/src/pkg/hub/customer_match_test.go new file mode 100644 index 0000000000..5ba7de3729 --- /dev/null +++ b/src/pkg/hub/customer_match_test.go @@ -0,0 +1,344 @@ +package hub + +import ( + "bytes" + "encoding/json" + "fmt" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +func resetCustomerMatchStore(t *testing.T) { + t.Helper() + reset := func() { + customerMatchStore.mu.Lock() + customerMatchStore.indexes, customerMatchStore.rates = nil, nil + customerMatchStore.mu.Unlock() + } + reset() + t.Cleanup(reset) +} + +func resetCustomerMatchRates() { + customerMatchStore.mu.Lock() + customerMatchStore.rates = nil + customerMatchStore.mu.Unlock() +} + +type matchHarness struct { + t *testing.T + s *HubServer + logs *bytes.Buffer +} + +func newMatchHarness(t *testing.T) *matchHarness { + t.Helper() + resetCustomerMatchStore(t) + s := newHandlerHub() + s.mux = http.NewServeMux() + s.registerCustomerSharingRoutes() + logs := new(bytes.Buffer) + s.logger = slog.New(slog.NewTextHandler(logs, nil)) + return &matchHarness{t: t, s: s, logs: logs} +} + +func (h *matchHarness) post(bearer, body string) *httptest.ResponseRecorder { + h.t.Helper() + r := httptest.NewRequest(http.MethodPost, customerMatchPath, strings.NewReader(body)) + r.Header.Set("Content-Type", "application/json") + if bearer != "" { + r.Header.Set("Authorization", "Bearer "+bearer) + } + w := httptest.NewRecorder() + h.s.mux.ServeHTTP(w, r) + return w +} + +func (h *matchHarness) match(asHive, claimHive string, hashes ...string) *httptest.ResponseRecorder { + h.t.Helper() + b, err := json.Marshal(customerMatchRequest{HiveID: claimHive, Hashes: hashes}) + if err != nil { + h.t.Fatal(err) + } + return h.post(h.s.heartbeatKeyFor(asHive), string(b)) +} + +func (h *matchHarness) answer(w *httptest.ResponseRecorder) customerMatchResponse { + h.t.Helper() + if w.Code != http.StatusOK { + h.t.Fatalf("status %d: %s", w.Code, w.Body) + } + var resp customerMatchResponse + if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { + h.t.Fatal(err) + } + return resp +} + +func matchHash(hive, login string) string { + return customerMatchHash(customerMatchKey(testHubSecret, hive), login) +} + +func saveMatchUser(t *testing.T, u *SaaSUser) *SaaSUser { + t.Helper() + if err := saveSaaSUser(u); err != nil { + t.Fatal(err) + } + return u +} + +func writeMatchConsent(t *testing.T, u *SaaSUser, hive string, edit func(*customerConsent)) { + t.Helper() + path, err := customerConsentPath(u) + if err != nil { + t.Fatal(err) + } + records, err := readCustomerConsents(path) + if err != nil { + t.Fatal(err) + } + c, ok := records[hive] + if !ok { + now := time.Now().UTC() + c = customerConsent{Person: u.GitHubUsername, HiveID: hive, TextVersion: customerConsentTextVersion, GrantedAt: now, UpdatedAt: now} + } + edit(&c) + records[hive] = c + if err := saveCustomerConsents(path, records); err != nil { + t.Fatal(err) + } +} + +func readMatchConsent(t *testing.T, u *SaaSUser, hive string) customerConsent { + t.Helper() + path, err := customerConsentPath(u) + if err != nil { + t.Fatal(err) + } + records, err := readCustomerConsents(path) + if err != nil { + t.Fatal(err) + } + return records[hive] +} + +func TestCustomerMatchAnswersOnlyForConsent(t *testing.T) { + defer helperSetupTempDirs(t)() + t.Setenv(customersShareEnv, "") + h := newMatchHarness(t) + one := map[string]string{"one": "viewer"} + alice := saveMatchUser(t, &SaaSUser{ + GitHubUsername: "alice", Hives: map[string]string{"one": "viewer", "two": "viewer"}, + FullName: "Alice Real", FullNameSource: countrySourceUser, + Company: "Admin Co", CompanySource: countrySourceAdmin, + Country: "GB", CountrySource: countrySourceInferred, + }) + saveMatchUser(t, &SaaSUser{GitHubUsername: "bob", Hives: one, FullName: "Bob Real", FullNameSource: countrySourceUser}) + carol := saveMatchUser(t, &SaaSUser{GitHubUsername: "carol", Hives: one, FullName: "Carol Real", FullNameSource: countrySourceUser}) + dave := saveMatchUser(t, &SaaSUser{GitHubUsername: "dave", Hives: one, FullName: "Dave Real", FullNameSource: countrySourceUser}) + erin := saveMatchUser(t, &SaaSUser{GitHubUsername: "erin", Hives: one, FullName: "Erin Real", FullNameSource: countrySourceUser}) + frank := saveMatchUser(t, &SaaSUser{GitHubUsername: "frank", Hives: one, FullName: "Frank Real", FullNameSource: countrySourceUser, Blocked: true}) + gina := saveMatchUser(t, &SaaSUser{GitHubUsername: "gina", Hives: map[string]string{}, FullName: "Gina Real", FullNameSource: countrySourceUser}) + all := customerConsentFields{RealName: true, Organization: true, Country: true} + name := customerConsentFields{RealName: true} + writeMatchConsent(t, alice, "one", func(c *customerConsent) { c.Fields = all }) + writeMatchConsent(t, carol, "one", func(c *customerConsent) { c.Fields = name }) + withdrawn := time.Now().UTC() + writeMatchConsent(t, dave, "one", func(c *customerConsent) { c.WithdrawnAt = &withdrawn }) + writeMatchConsent(t, erin, "one", func(c *customerConsent) { c.Fields, c.TextVersion = name, "0" }) + writeMatchConsent(t, frank, "one", func(c *customerConsent) { c.Fields = name }) + writeMatchConsent(t, gina, "one", func(c *customerConsent) { c.Fields = name }) // grant lost + + // Absent while the hub switch is off, even with a valid bearer. + if w := h.match("one", "one", matchHash("one", "alice")); w.Code != http.StatusNotFound { + t.Fatalf("switch off: status %d, want 404", w.Code) + } + t.Setenv(customersShareEnv, "true") + + // The bearer is bound to the claimed hive. + if w := h.post("", `{"hive_id":"one","hashes":[]}`); w.Code != http.StatusUnauthorized { + t.Fatalf("no bearer: status %d", w.Code) + } + if w := h.match("two", "one", matchHash("one", "alice")); w.Code != http.StatusUnauthorized { + t.Fatalf("hive two bearer claiming hive one: status %d", w.Code) + } + + // Unknown, unconsented, withdrawn-before-delivery, stale text, blocked and + // lost grant are all the same silence. + unknown := h.match("one", "one", matchHash("one", "nobody-here")).Body.String() + if unknown != "{\"matches\":[]}\n" { + t.Fatalf("unknown answer = %q", unknown) + } + for _, login := range []string{"bob", "dave", "erin", "frank", "gina"} { + resetCustomerMatchRates() + if got := h.match("one", "one", matchHash("one", login)).Body.String(); got != unknown { + t.Fatalf("%s answer %q differs from unknown %q", login, got, unknown) + } + } + + // Admin-sourced and inferred values never cross, even with consent. + resetCustomerMatchRates() + w := h.match("one", "one", matchHash("one", "alice"), matchHash("one", "carol"), matchHash("one", "bob")) + if body := w.Body.String(); strings.Contains(body, "Admin Co") || strings.Contains(body, "GB") { + t.Fatalf("admin or inferred value crossed: %s", body) + } + resp := h.answer(w) + if len(resp.Matches) != 2 { + t.Fatalf("want alice and carol, got %+v", resp.Matches) + } + a, c := resp.Matches[0], resp.Matches[1] + if a.MatchHash != matchHash("one", "alice") || a.RealName != "Alice Real" || a.Organization != "" || + a.Country != "" || a.Rev == "" || len(a.Cleared) != 0 { + t.Fatalf("alice entry = %+v", a) + } + if c.MatchHash != matchHash("one", "carol") || c.RealName != "Carol Real" { + t.Fatalf("carol entry = %+v", c) + } + + // Delivery stamps on the record. + ac := readMatchConsent(t, alice, "one") + if ac.FirstDeliveredAt == nil || ac.LastDeliveredAt == nil || ac.DeliveredFields == nil || + *ac.DeliveredFields != name || ac.DeliveredRev != a.Rev { + t.Fatalf("alice delivery stamp = %+v", ac) + } + firstDelivered := *ac.FirstDeliveredAt + + // A user-entered country now crosses too, and widens the delivered set. + alice.Country, alice.CountrySource = "FR", countrySourceUser + saveMatchUser(t, alice) + resetCustomerMatchRates() + resp = h.answer(h.match("one", "one", matchHash("one", "alice"))) + if len(resp.Matches) != 1 || resp.Matches[0].Country != "FR" || resp.Matches[0].Rev == a.Rev { + t.Fatalf("user country not delivered with a new rev: %+v", resp.Matches) + } + if ac = readMatchConsent(t, alice, "one"); !ac.FirstDeliveredAt.Equal(firstDelivered) || + *ac.DeliveredFields != (customerConsentFields{RealName: true, Country: true}) { + t.Fatalf("restamp = %+v", ac) + } + + // cleared only follows a delivery: carol withdraws, alice's name loses + // user provenance; dave (withdrew before any delivery) stays silent. + writeMatchConsent(t, carol, "one", func(c *customerConsent) { + now := time.Now().UTC() + c.Fields, c.WithdrawnAt = customerConsentFields{}, &now + }) + alice.FullNameSource = countrySourceAdmin + saveMatchUser(t, alice) + resetCustomerMatchRates() + resp = h.answer(h.match("one", "one", matchHash("one", "alice"), matchHash("one", "carol"), matchHash("one", "dave"))) + if len(resp.Matches) != 2 { + t.Fatalf("want alice and carol entries, got %+v", resp.Matches) + } + a, c = resp.Matches[0], resp.Matches[1] + if a.RealName != "" || a.Country != "FR" || strings.Join(a.Cleared, ",") != "real_name" { + t.Fatalf("alice after provenance loss = %+v", a) + } + if c.RealName != "" || c.Rev != "" || strings.Join(c.Cleared, ",") != "real_name" { + t.Fatalf("carol after withdrawal = %+v", c) + } + if cc := readMatchConsent(t, carol, "one"); cc.WithdrawnAt == nil || cc.FirstDeliveredAt == nil { + t.Fatalf("withdrawn carol record must be retained while a clear is owed: %+v", cc) + } + + // One hive cannot read another hive's consent, with either hive's key. + resp = h.answer(h.match("two", "two", matchHash("one", "alice"), matchHash("two", "alice"))) + if len(resp.Matches) != 0 { + t.Fatalf("hive two read hive one's consent: %+v", resp.Matches) + } + + // Audit: hive id and counts only, never values or hashes. + audit := h.logs.String() + if !strings.Contains(audit, "audit: customer match") || !strings.Contains(audit, "hive=one") || + !strings.Contains(audit, "delivered=2") { + t.Fatalf("missing match audit: %s", audit) + } + for _, leak := range []string{"Alice Real", "Carol Real", "FR", matchHash("one", "alice"), matchHash("one", "nobody-here")} { + if strings.Contains(audit, leak) { + t.Fatalf("audit leaked %q: %s", leak, audit) + } + } +} + +func TestCustomerMatchLimits(t *testing.T) { + defer helperSetupTempDirs(t)() + t.Setenv(customersShareEnv, "true") + h := newMatchHarness(t) + hex := func(i int) string { return fmt.Sprintf("%064x", i) } + status := func(w *httptest.ResponseRecorder, want int) { + t.Helper() + if w.Code != want { + t.Fatalf("status %d, want %d: %s", w.Code, want, w.Body) + } + } + + batch := make([]string, maxCustomerMatchBatch+1) + for i := range batch { + batch[i] = hex(i) + } + status(h.match("one", "one", batch...), http.StatusRequestEntityTooLarge) + status(h.match("one", "one", batch[:maxCustomerMatchBatch]...), http.StatusOK) + + huge := `{"hive_id":"one","hashes":["` + strings.Repeat("a", maxCustomerMatchBody) + `"]}` + status(h.post(h.s.heartbeatKeyFor("one"), huge), http.StatusRequestEntityTooLarge) + status(h.match("one", "one", "ABC"), http.StatusBadRequest) + status(h.match("one", "one", strings.ToUpper(hex(1))), http.StatusBadRequest) + status(h.post(h.s.heartbeatKeyFor("one"), `{"hive_id":"one","hashes":[],"emails":["a@b"]}`), http.StatusBadRequest) + + // Per-hive rate limit; another hive keeps its own budget. + resetCustomerMatchRates() + for i := 0; i < customerMatchRateLimit; i++ { + status(h.match("one", "one"), http.StatusOK) + } + w := h.match("one", "one") + status(w, http.StatusTooManyRequests) + if w.Header().Get("Retry-After") == "" { + t.Fatal("429 without Retry-After") + } + status(h.match("two", "two"), http.StatusOK) + + // Response size cap keeps a prefix and reports truncation. + pending := []pendingCustomerMatch{ + {entry: customerMatchEntry{MatchHash: hex(1), RealName: strings.Repeat("x", 40)}}, + {entry: customerMatchEntry{MatchHash: hex(2), RealName: strings.Repeat("y", 40)}}, + {entry: customerMatchEntry{MatchHash: hex(3), RealName: strings.Repeat("z", 40)}}, + } + one, _ := json.Marshal(pending[0].entry) + limit := len(`{"matches":[],"truncated":true}`) + 2*(len(one)+1) + kept, truncated := fitCustomerMatches(pending, limit) + if len(kept) != 2 || !truncated { + t.Fatalf("fit kept %d truncated=%v", len(kept), truncated) + } + if kept, truncated = fitCustomerMatches(pending, maxCustomerMatchResponse); len(kept) != 3 || truncated { + t.Fatalf("fit under cap kept %d truncated=%v", len(kept), truncated) + } +} + +// A grant made through the consent page is matched at once, not after the +// index cache expires. +func TestCustomerMatchGrantInvalidatesIndex(t *testing.T) { + defer helperSetupTempDirs(t)() + t.Setenv(customersShareEnv, "true") + t.Setenv("HIVE_HUB_PUBLIC_URL", "https://hub.example.com") + h := newMatchHarness(t) + saveMatchUser(t, &SaaSUser{GitHubUsername: "alice", Hives: map[string]string{"one": "viewer"}, + FullName: "Alice Real", FullNameSource: countrySourceUser}) + if resp := h.answer(h.match("one", "one", matchHash("one", "alice"))); len(resp.Matches) != 0 { + t.Fatalf("matched before consent: %+v", resp.Matches) + } + r := reqWithUser(http.MethodPost, consentTestAPI+"/one", + `{"text_version":"`+customerConsentTextVersion+`","fields":{"real_name":true}}`, "alice") + r.Header.Set("Origin", "https://hub.example.com") + w := httptest.NewRecorder() + h.s.mux.ServeHTTP(w, r) + if w.Code != http.StatusNoContent { + t.Fatalf("grant status %d: %s", w.Code, w.Body) + } + resp := h.answer(h.match("one", "one", matchHash("one", "alice"))) + if len(resp.Matches) != 1 || resp.Matches[0].RealName != "Alice Real" { + t.Fatalf("grant not visible to the match index: %+v", resp.Matches) + } +} From ed578337a5b99981907f61af69e7fbb66009f5bb Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:58:34 -0500 Subject: [PATCH 2/2] fix: use hash with letters in uppercase-rejection test hex(1) has no hex letters, so ToUpper left it valid; use hex(0xabc) so the uppercase branch is actually exercised. Hive-Fix-Attempt: 1/6 Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/hub/customer_match_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/hub/customer_match_test.go b/src/pkg/hub/customer_match_test.go index 5ba7de3729..0df4cf2be6 100644 --- a/src/pkg/hub/customer_match_test.go +++ b/src/pkg/hub/customer_match_test.go @@ -285,7 +285,7 @@ func TestCustomerMatchLimits(t *testing.T) { huge := `{"hive_id":"one","hashes":["` + strings.Repeat("a", maxCustomerMatchBody) + `"]}` status(h.post(h.s.heartbeatKeyFor("one"), huge), http.StatusRequestEntityTooLarge) status(h.match("one", "one", "ABC"), http.StatusBadRequest) - status(h.match("one", "one", strings.ToUpper(hex(1))), http.StatusBadRequest) + status(h.match("one", "one", strings.ToUpper(hex(0xabc))), http.StatusBadRequest) status(h.post(h.s.heartbeatKeyFor("one"), `{"hive_id":"one","hashes":[],"emails":["a@b"]}`), http.StatusBadRequest) // Per-hive rate limit; another hive keeps its own budget.