← Back to Pull requests

fix(webhooks): reject disconnected grants and stop returning 500 on bad requests #154

Open
unknown opened this pull request with 543 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 08, 2026 11:07 UTC
Author

Description

The VoxBento webhook receiver kept accepting validly signed webhooks after an organizer disconnected VoxBento, and several malformed requests ended in an unhandled exception (HTTP 500) instead of a 4xx. This PR hardens VoxbentoWebhookReceiverView.

Related Issue

Closes #153

Changes Made

  • Only consider grants that are not is_disconnected, so a disconnected event no longer accepts webhooks signed with its old secret. Reconnecting already resets the flag and the secret (views_oauth.py), so nothing else needs to change.

  • Pick the grant by signature. Event slugs are only unique per organizer, so one slug can belong to several grants; previously .get(event__slug=...) raised MultipleObjectsReturned. A new helper, _find_verified_grant, returns the grant whose webhook secret verifies the signature.

  • Sign over the raw request body bytes and compare signatures as bytes, so a non-ASCII v1 value no longer raises TypeError in hmac.compare_digest.

  • Catch ValueError when parsing the body, so a body that is not valid UTF-8 returns 400 instead of raising UnicodeDecodeError.

  • Keep the sanitised data after verification, so "data": null no longer crashes a handler.

  • Unknown event, disconnected grant, missing secret and wrong signature now all return the same 401, so the endpoint no longer reveals which event slugs are connected (previously 404 vs 401).
  • Testing

    Added six regression tests to tests/test_views_webhooks.py. I ran them against the unfixed code first, and all six failed with the errors listed in #153 (200 for a disconnected grant, TypeError, UnicodeDecodeError, 404 for an unknown event, AttributeError, MultipleObjectsReturned).

  • Tested locally

  • Existing tests pass: full suite 97 passed (91 existing + 6 new), against PostgreSQL 15 and Redis 7

  • Added/updated tests (if applicable)
  • pytest tests -q
    97 passed
    uvx ruff check .            -> All checks passed
    uvx ruff format --check .   -> 55 files already formatted
    

    Screenshots (if applicable)

    Not applicable (backend only).

    Checklist

  • My code follows the project's coding standards (ruff clean, 120 columns)

  • I have tested my changes

  • I have updated documentation where necessary (none needed)

  • My changes do not introduce new warnings or errors
  • Additional Notes

  • Behaviour change to be aware of: unknown event slugs now return 401 instead of 404.

  • #132 (structured logging) also edits post() in this file. Whichever PR merges second will need a small rebase; this PR does not touch logging.
  • Summary by Sourcery

    Harden the VoxBento webhook receiver to authenticate only active grants and consistently reject invalid requests with safe client errors.

    Bug Fixes:

  • Harden webhook validation so disconnected or changed grants cannot accept previously signed requests.

  • Return appropriate 4xx responses for malformed, oversized, invalid, or unauthenticated webhook requests instead of raising server errors.

  • Resolve webhook grants by verifying signatures, including when event slugs are shared across organizers.
  • Enhancements:

  • Protect webhook processing with bounded grant lookup, timing-resistant handling for unknown slugs, and concurrency checks during disconnects or reconnects.
  • Tests:

  • Add regression coverage for disconnected grants, malformed payloads and signatures, shared event slugs, request limits, timing behavior, and concurrent grant changes.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Webhook requests for unknown events, disconnected accounts, missing secrets, or invalid signatures receive a consistent unauthorized response.
    Malformed JSON, invalid text encoding, and non-string event types receive a bad-request response.
    Requests larger than 256 KiB are rejected.
    When multiple accounts have events with the same slug, the webhook updates the event whose account secret validates the request, checking up to 10 matches.
    Webhooks for accounts disconnected during processing are rejected without updating event state or sending notifications.
    * Signed webhook requests with a null data value are ignored.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 543 lines (+407 / -136) across 6 files Reviewer: Nirupon123 Review duration: 32903m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.48) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 543 lines diff across 6 files
    0.48
    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.377
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #154"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    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: 48% (MEDIUM) · Higher = larger blast radius
    48%
    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: 25.6% · Safe to merge
    25.6%