← Back to Pull requests

CXH-2196: stop dropping SendGrid teammate permissions on scope drift #40

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 29, 2026 07:53 UTC
Author

CXH-2196: stop dropping SendGrid teammate permissions on scope drift

Linear: CXH-2196

What was asked

teammateBuilder.Grants resolved every teammate scope against a hand-maintained 197-entry
SendGridScopes map and silently skipped any scope the map did not contain (Debug log +
continue). On the audited account the connector dropped 70 genuine permissions (an admin
teammate reports 255 granted scopes; C1 emitted 178) — including sso.settings. and
teammates.
— with nothing above debug level to indicate it, because the map's premise
("Sendgrid does not provide an api to list scopes") was false: GET /v3/scopes exists. The
ticket also appended a second defect to fix in the same change: the error surface mangled
or dropped vendor error detail (literal .json in field error text, flat {"message"}
envelopes silently ignored, error-status bodies decoded into success targets).

What changed

  • Grant emission is gated by nothing (pkg/connector/teammates.go). Every scope string

  • SendGrid reports on a teammate is emitted as a grant — including scopes absent from both
    the static floor and GET /v3/scopes. The only non-emission path is an explicit,
    documented rule for SendGrid's non-grantable account/UI state flags (2fa_exempt,
    2fa_required, sender_verification_eligible, signup.trigger_confirmation, ui.),
    logged once per teammate at Debug naming the rule. The skipping unknown scope drop path
    no longer exists.
  • Scope resource catalog = live ∪ immutable floor (pkg/connector/scopes.go,

  • client/client.go, models/models.go, connector.go). New client method GetScopes
    (GET v3/scopes; response shape per the Twilio doc: a single-page string array).
    scopeBuilder.List emits the sorted union of the compatibility floor with the live
    response, so the catalog is additive-only by construction: /v3/scopes is key-scoped, and
    on a restricted key the floor guarantees today's 197 resources never disappear. A
    GetScopes failure fails the sync with a wrapped baton-sendgrid: error — no floor
    fallback, no partial catalog with a nil error. The floor's false-premise comment is
    rewritten to state its new role.
  • Error surface rework at its single root in doRequest (pkg/connector/client/client.go).

  • The response body is read once; error-status bodies are parsed as errors and never decoded
    into the call's success target; a 2xx with an undecodable body returns a wrapped error,
    never a silent empty result; the vendor envelope (flat {"message"} and fielded
    {"errors":[…]}) is parsed for EVERY non-2xx status and joined onto the uhttp-classified
    error — preserving status.Code(err) (pinned SDK mapping verified in source: 429 and all
    5xx → codes.Unavailable, 501 → Unimplemented). The literal .json no longer appears
    in field error text; CustomErr now implements error correctly.
  • Docs (docs/connector.mdx): teammate grants mirror all reported scopes; the scope

  • catalog is refreshed from GET /v3/scopes unioned with the built-in baseline.
  • Tests (table-driven, house style): client_test.go (error surface: flat/fielded

  • envelopes, 401 body never lands in the success struct, 429/503 detail joined with correct
    gRPC codes, 2xx decode failure), scopes_test.go (union semantics, floor survives a
    narrow live list, GetScopes failure propagates), teammates_test.go (fixture teammate
    carrying the documented dropped families — billing.create (in the compatibility floor,
    absent from live GET /v3/scopes) and email_testing (absent from both the floor and
    the live response) — emits grants, with the grant-discovered off-catalog scope resource
    inserted via the InsertResourceGrants response annotation; pseudo-flags excluded with
    the documented message; zero skipping unknown scope entries).

    Breaking-change assessment (BP4)

  • Identifiers C1 matches on are unchanged. The scope resource ID remains the vendor

  • scope string (derived by scopeResource); the assignment entitlement slug assigned is
    unchanged; no resource type, entitlement, or identifier is removed or renamed.
  • Existing grants keep matching on deploy. All grants emitted today are still emitted;

  • previously-dropped scopes appear as NEW resources and grants. No migration path needed.
  • The catalog only grows (live ∪ immutable floor, sorted) and GET /v3/scopes becomes

  • a hard sync dependency
    : an API key that cannot call it now fails a sync that previously
    "succeeded" with a silently wrong catalog. That is the requested trade (fail loudly, never
    drift silently), and it is documented in docs/connector.mdx.

    How far this is verified

  • Targeted validation is green: validate-changes.sh (scoped build/vet/test over the

  • touched packages) passes on the final head; check-kind-rules.sh reports 0 violations,
    0 hints.
  • Adversarial review: SHIP (zero fix commits; four advisory findings dispositioned,

  • none blocking).
  • connector-eval (advisory): clean — 0 lost / +23 resources / +23 entitlements /

  • +5 grants vs main, replayed against a doc-derived mock recording (the mock is
    deliberately thin, so the gains prove the mechanism, not the audited account's numbers).
  • Smoke: cmd/baton-sendgrid builds and starts. NOTE: this repo declares no mock, so

  • no sync was exercised by the smoke gate — the change is unproven against a live or mocked
    upstream
    by the smoke itself; the advisory eval replay and the tester's real mock syncs
    (below) are the sync evidence this batch has, and no live SendGrid account was exercised
    in this batch.
  • Independent tester (VERA): PASS — all 9 acceptance criteria WORKS, with behavioral

  • evidence from real syncs against the doc-derived mock (scope grants 8 → 13 on the
    fixtures with 0 lost; union catalog 197 → 220; pseudo-flags excluded with the documented
    log; crafted 401/400 error scenarios carrying the vendor reason with zero .json
    occurrences; zero skipping unknown scope lines). Mock-based only; the tester's clone
    shares this repo's CI toolchain state, not a clean room.
  • Independent deep review: CLEAR after 2 rounds. Round 1 (a fresh reviewer that had

  • seen none of the batch) found two real defects — sender_verification_legacy missing
    from the exclusion set (the Twilio doc lists eight auto-added non-grantable flags; the
    set covered seven) and a silent-success path on a degenerate empty-body 200 (the
    mass-revocation class) — both fixed, with the fix's own regression caught and repaired
    (res != nil decode guard restored for the nil-target provisioning calls). Round 2
    verified the fixes and one doc-citation nit was corrected.
  • CI is green (all 11 checks) and the repository judge passes at the final head, with

  • every review thread answered and resolved.

    What to look at hardest

    1. GET /v3/scopes as a new hard sync dependency — scoped-down API keys (403 on the
    endpoint) now fail the sync by design. If that turns out to be common in production,
    that is a new decision with new evidence, not something to pre-hedge here.
    2. The pseudo-flag exclusion set is hand-maintained (2fa_
    , ui.*,
    sender_verification_eligible, signup.trigger_confirmation). The failure direction
    after this change is "one extra visible grant", never a missing permission; extending the
    set is a one-line map edit.
    3. Error-text changes — vendor detail is now joined onto error messages for every
    non-2xx status (previously dropped for 429/5xx); anything matching exact error strings
    will see new text. status.Code(err) behavior is preserved.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: c1-squire-dev[bot] Review duration: 27793m
    All Vouch review confidence checks passed
    Residual risk 0.40 = Change Risk (0.75) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.75 · 650 lines diff across 8 files
    0.75
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.075
    Real PR Conversation Timeline
    1 comments
    "Review active on #40"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 46.7% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 75.2% (HIGH) · Higher = larger blast radius
    75.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 40.1% · Moderate risk
    40.1%