Skip to content

feat: make webhook headers case insensitive #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/finch/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ._utils import is_required_type as is_required_type
from ._utils import is_annotated_type as is_annotated_type
from ._utils import maybe_coerce_float as maybe_coerce_float
from ._utils import get_required_header as get_required_header
from ._utils import maybe_coerce_boolean as maybe_coerce_boolean
from ._utils import maybe_coerce_integer as maybe_coerce_integer
from ._utils import strip_annotated_type as strip_annotated_type
Expand Down
22 changes: 21 additions & 1 deletion src/finch/_utils/_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import annotations

import os
import re
import inspect
import functools
from typing import Any, Mapping, TypeVar, Callable, Iterable, Sequence, cast, overload
from pathlib import Path
from typing_extensions import Required, Annotated, TypeGuard, get_args, get_origin

from .._types import NotGiven, FileTypes, NotGivenOr
from .._types import Headers, NotGiven, FileTypes, NotGivenOr, HeadersLike
from .._compat import is_union as _is_union
from .._compat import parse_date as parse_date
from .._compat import parse_datetime as parse_datetime
Expand Down Expand Up @@ -351,3 +352,22 @@ def file_from_path(path: str) -> FileTypes:
contents = Path(path).read_bytes()
file_name = os.path.basename(path)
return (file_name, contents)


def get_required_header(headers: HeadersLike, header: str) -> str:
lower_header = header.lower()
if isinstance(headers, Mapping):
headers = cast(Headers, headers)
for k, v in headers.items():
if k.lower() == lower_header and isinstance(v, str):
return v

""" to deal with the case where the header looks like Finch-Event-Id """
intercaps_header = re.sub(r"([^\w])(\w)", lambda pat: pat.group(1) + pat.group(2).upper(), header.capitalize())

for normalized_header in [header, lower_header, header.upper(), intercaps_header]:
value = headers.get(normalized_header)
if value:
return value

raise ValueError(f"Could not find {header} header")
27 changes: 7 additions & 20 deletions src/finch/resources/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from datetime import datetime, timezone, timedelta

from .._types import HeadersLike
from .._utils import get_required_header
from .._resource import SyncAPIResource, AsyncAPIResource

__all__ = ["Webhooks", "AsyncWebhooks"]
Expand Down Expand Up @@ -51,13 +52,8 @@ def verify_signature(
except Exception:
raise ValueError("Bad secret")

msg_id = headers.get("finch-event-id")
if not msg_id:
raise ValueError("Could not find finch-event-id header")

msg_timestamp = headers.get("finch-timestamp")
if not msg_timestamp:
raise ValueError("Could not find finch-timestamp header")
msg_id = get_required_header(headers, "finch-event-id")
msg_timestamp = get_required_header(headers, "finch-timestamp")

# validate the timestamp
webhook_tolerance = timedelta(minutes=5)
Expand Down Expand Up @@ -88,9 +84,7 @@ def verify_signature(
to_sign = f"{msg_id}.{timestamp_str}.{body}".encode()
expected_signature = hmac.new(parsedSecret, to_sign, hashlib.sha256).digest()

msg_signature = headers.get("finch-signature")
if not msg_signature:
raise ValueError("Could not find finch-signature header")
msg_signature = get_required_header(headers, "finch-signature")

# Signature header can contain multiple signatures delimited by spaces
passed_sigs = msg_signature.split(" ")
Expand Down Expand Up @@ -151,13 +145,8 @@ def verify_signature(
except Exception:
raise ValueError("Bad secret")

msg_id = headers.get("finch-event-id")
if not msg_id:
raise ValueError("Could not find finch-event-id header")

msg_timestamp = headers.get("finch-timestamp")
if not msg_timestamp:
raise ValueError("Could not find finch-timestamp header")
msg_id = get_required_header(headers, "finch-event-id")
msg_timestamp = get_required_header(headers, "finch-timestamp")

# validate the timestamp
webhook_tolerance = timedelta(minutes=5)
Expand Down Expand Up @@ -188,9 +177,7 @@ def verify_signature(
to_sign = f"{msg_id}.{timestamp_str}.{body}".encode()
expected_signature = hmac.new(parsedSecret, to_sign, hashlib.sha256).digest()

msg_signature = headers.get("finch-signature")
if not msg_signature:
raise ValueError("Could not find finch-signature header")
msg_signature = get_required_header(headers, "finch-signature")

# Signature header can contain multiple signatures delimited by spaces
passed_sigs = msg_signature.split(" ")
Expand Down
4 changes: 2 additions & 2 deletions tests/api_resources/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestWebhooks:
payload = """{"company_id":"720be419-0293-4d32-a707-32179b0827ab"}"""
signature = "m7y0TV2C+hlHxU42wCieApTSTaA8/047OAplBqxIV/s="
headers = {
"finch-event-id": "msg_2Lh9KRb0pzN4LePd3XiA4v12Axj",
"Finch-Event-Id": "msg_2Lh9KRb0pzN4LePd3XiA4v12Axj",
"finch-timestamp": timestamp,
"finch-signature": f"v1,{signature}",
}
Expand Down Expand Up @@ -127,7 +127,7 @@ class TestAsyncWebhooks:
payload = """{"company_id":"720be419-0293-4d32-a707-32179b0827ab"}"""
signature = "m7y0TV2C+hlHxU42wCieApTSTaA8/047OAplBqxIV/s="
headers = {
"finch-event-id": "msg_2Lh9KRb0pzN4LePd3XiA4v12Axj",
"Finch-Event-Id": "msg_2Lh9KRb0pzN4LePd3XiA4v12Axj",
"finch-timestamp": timestamp,
"finch-signature": f"v1,{signature}",
}
Expand Down