Skip to content

Commit dd69803

Browse files
fix: reject empty webhook signing keys (#4321)
## Changes being requested Raise `ValueError` when a webhook secret is empty after decoding, consistent with a missing secret. Valid raw and prefixed secrets keep their existing behavior. ## Additional context & links Updated the sync and async signature tests to expect a configuration error for empty keys. All 232 webhook tests passed. Also checked `unwrap()` with an empty environment variable on both clients. Ruff and ty passed for the changed files.
1 parent 3326749 commit dd69803

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

‎src/openai/lib/_webhooks.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def webhook_signature_matches(
5252
else:
5353
decoded_secret = secret.encode()
5454

55+
if not decoded_secret:
56+
raise ValueError("The webhook secret must not be empty after decoding")
57+
5558
body = payload.decode("utf-8") if isinstance(payload, bytes) else payload
5659

5760
# Prepare the signed payload (OpenAI uses webhookId.timestamp.payload format)

‎tests/lib/test_webhook_signature.py‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,16 @@ def webhook_resource(request: pytest.FixtureRequest) -> Webhooks | AsyncWebhooks
5252
return AsyncWebhooks(cast(openai.AsyncOpenAI, client))
5353

5454

55-
@pytest.mark.parametrize(
56-
("secret", "key"),
57-
[(RAW_SECRET, RAW_SECRET.encode()), (PREFIXED_SECRET, RAW_SECRET.encode()), ("", b"")],
58-
ids=["raw", "prefixed", "empty"],
59-
)
55+
@pytest.mark.parametrize("secret", [RAW_SECRET, PREFIXED_SECRET], ids=["raw", "prefixed"])
6056
@pytest.mark.parametrize("payload", [PAYLOAD, PAYLOAD.encode()], ids=["text", "bytes"])
6157
@pytest.mark.parametrize("signature_form", ["prefixed", "bare", "multiple"])
6258
def test_signature_forms_and_secret_encodings(
6359
webhook_resource: Webhooks | AsyncWebhooks,
6460
secret: str,
65-
key: bytes,
6661
payload: str | bytes,
6762
signature_form: str,
6863
) -> None:
69-
headers = signed_headers(payload, secret=key)
64+
headers = signed_headers(payload)
7065
signature = headers["webhook-signature"][3:]
7166
if signature_form == "bare":
7267
headers["webhook-signature"] = signature
@@ -135,12 +130,14 @@ def test_required_header_order(
135130
webhook_resource.verify_signature(PAYLOAD, headers, secret=RAW_SECRET)
136131

137132

138-
def test_client_secret_fallback_preserves_explicit_empty_secret(
139-
webhook_resource: Webhooks | AsyncWebhooks,
133+
@pytest.mark.parametrize("secret", ["", "whsec_", "whsec_!!!"])
134+
def test_rejects_empty_signing_key_even_with_client_secret(
135+
webhook_resource: Webhooks | AsyncWebhooks, secret: str
140136
) -> None:
141137
webhook_resource._client.webhook_secret = PREFIXED_SECRET
142138
webhook_resource.verify_signature(PAYLOAD, signed_headers())
143-
webhook_resource.verify_signature(PAYLOAD, signed_headers(secret=b""), secret="")
139+
with pytest.raises(ValueError, match="webhook secret must not be empty"):
140+
webhook_resource.verify_signature(PAYLOAD, signed_headers(secret=b""), secret=secret)
144141

145142

146143
def test_missing_secret_preserves_wrapper_exception_chaining(

0 commit comments

Comments
 (0)