Skip to main content

Webhook signature verification for Fygaro

Project description

fygaro-webhook

Webhook signature verification for Fygaro — pure Python stdlib, zero runtime deps

This helper validates the Fygaro-Signature header of incoming webhooks. It supports secret rotation (multiple active secrets), deterministic unit‑testing, and is ready for future hash algorithms.


Installation

pip install fygaro-webhook

Requires Python ≥ 3.8.


Quick start

from fygaro.webhook import FygaroWebhookValidator

validator = FygaroWebhookValidator(
    secrets=[
        "my-primary-secret",  # str or bytes
        # "my-previous-secret",   # include during rotation windows
    ]
)

if not validator.verify_signature(
    signature_header=request.headers["Fygaro-Signature"],
    body=request.body,  # raw bytes exactly as sent
):
    raise ValueError("Invalid signature")

# …process JSON, return 200…

Opt-in: detailed error handling

from fygaro.webhook import FygaroWebhookValidator, SignatureVerificationError

validator = FygaroWebhookValidator(
    secrets=["my-primary-secret"],
    raise_exceptions=True,  # Opt-in to detailed errors
)

try:
    validator.verify_signature(
        signature_header=request.headers["Fygaro-Signature"],
        body=request.body,
    )

except SignatureVerificationError as exc:
    logger.warning("Webhook rejected: %s", exc)
    return 400

API reference

class FygaroWebhookValidator

Parameter Type Required Default Description
secrets Sequence[str | bytes] One or more active webhook secrets. Provide all currently valid secrets during a rotation window. Each secret can be a UTF‑8 str or raw bytes.
max_age int 300 Maximum allowable clock skew (in seconds) between the timestamp in the header and the server time. A low value mitigates replay attacks
unsafe_skip_ts_validation bool False Test only. When True, the timestamp‑freshness check is skipped and a RuntimeWarning is emitted on instantiation. Never enable in production.
raise_exceptions bool False When True, verify_signature raises specific subclasses of SignatureVerificationError instead of returning False.

validator.verify_signature(signature_header: str, body: bytes) -> bool

Argument Type Description
signature_header str The exact value of the incoming Fygaro‑Signature HTTP header.
body bytes The unmodified request body (raw bytes). Do not .decode() or re‑serialize.

Return value:

  • True — signature is valid and timestamp is within max_age (unless skipped).
  • False — signature mismatch, stale timestamp, or malformed header.

Exceptions exposed at package root

  • SignatureVerificationError – base class
  • MissingHeaderError
  • TimestampInvalidError
  • TimestampExpiredError
  • SignatureMismatchError

Writing deterministic unit tests

To keep fixtures stable you can bypass the timestamp‑freshness check without touching production code:

validator = FygaroWebhookValidator(
    secrets=[b"test-secret"],
    unsafe_skip_ts_validation=True,  # ← test‑only flag
)

The first instance created with unsafe_skip_ts_validation=True emits a RuntimeWarning to remind you that this path is unsafe for live traffic.


License

MIT © Fygaro — support: support@fygaro.com

Changelog – @fygaro/webhook

[1.2.0] – 2025-07-10

Added

  • raise_exceptions constructor flag – opt-in detailed failure reasons without breaking the legacy True/False return contract.

[1.1.0] – 2025-07-10

Added

  • unsafe_skip_ts_validation constructor flag that bypasses the timestamp-freshness check. Meant only for local/unit tests; not safe for production. Emits a RuntimeWarning the first time an instance is created with the flag enabled, to minimise accidental misuse.

[1.0.0] – 2025-06-19

Added

  • Initial release with FygaroWebhookValidator class.
  • Supports multiple secrets & multiple v1= hashes.
  • Constant-time compare and configurable timestamp window.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fygaro_webhook-1.2.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fygaro_webhook-1.2.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file fygaro_webhook-1.2.0.tar.gz.

File metadata

  • Download URL: fygaro_webhook-1.2.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fygaro_webhook-1.2.0.tar.gz
Algorithm Hash digest
SHA256 abffbff5692578fe3b605ad2cc0cdadd80e257b88f21bb47efae4578c8defe5b
MD5 8438d7d35bbae004f85356c6fde05a65
BLAKE2b-256 4d1a2e486f2f43fcf3540e72055440caf1220ceb964a63f0ab3ee4ba4afe9f1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fygaro_webhook-1.2.0.tar.gz:

Publisher: webhook-python-release.yml on Fygaro/fygaro-sdks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fygaro_webhook-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: fygaro_webhook-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fygaro_webhook-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 411e32d20c39508980e89c6720feb3d9cacc2a7c0675d3ea4d347cbc574ced7e
MD5 345e49969355461a0476c4f000813c64
BLAKE2b-256 857a6f4fb60a7e8a7f864e9d2df4e655ce40f6505eb990345891699cf0b042de

See more details on using hashes here.

Provenance

The following attestation bundles were made for fygaro_webhook-1.2.0-py3-none-any.whl:

Publisher: webhook-python-release.yml on Fygaro/fygaro-sdks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page