fix(webhook): authenticate Linear deliveries via linear-signature HMAC (#87348)
This commit is contained in:
@@ -1066,7 +1066,7 @@ class WebhookAdapter(BasePlatformAdapter):
|
||||
def _validate_signature(
|
||||
self, request: "web.Request", body: bytes, secret: str
|
||||
) -> bool:
|
||||
"""Validate webhook signature (GitHub, GitLab, Svix, generic HMAC-SHA256)."""
|
||||
"""Validate webhook signature (GitHub, GitLab, Svix, Linear, generic HMAC-SHA256)."""
|
||||
def _header(name: str) -> str:
|
||||
return (
|
||||
request.headers.get(name, "")
|
||||
@@ -1092,6 +1092,18 @@ class WebhookAdapter(BasePlatformAdapter):
|
||||
signature_header=svix_signature,
|
||||
)
|
||||
|
||||
# Linear: linear-signature = <hex HMAC-SHA256 of the raw body, keyed
|
||||
# by the webhook signing key>. Linear's documented scheme signs the
|
||||
# body only (no timestamp binding), so this mirrors it exactly;
|
||||
# without this branch every Linear delivery to a secret-configured
|
||||
# route was rejected as unrecognized (#87348).
|
||||
linear_sig = _header("linear-signature")
|
||||
if linear_sig:
|
||||
expected_linear = hmac.new(
|
||||
secret.encode(), body, hashlib.sha256
|
||||
).hexdigest()
|
||||
return _hmac_str_equal(linear_sig, expected_linear)
|
||||
|
||||
# GitHub: X-Hub-Signature-256 = sha256=<hex>
|
||||
gh_sig = request.headers.get("X-Hub-Signature-256", "")
|
||||
if gh_sig:
|
||||
|
||||
@@ -149,11 +149,33 @@ class TestValidateSignature:
|
||||
"X-Hub-Signature-256",
|
||||
"X-Gitlab-Token",
|
||||
"X-Webhook-Signature",
|
||||
"linear-signature",
|
||||
):
|
||||
req = _mock_request(headers={header: hostile})
|
||||
# Must return False, never raise.
|
||||
assert adapter._validate_signature(req, body, secret) is False
|
||||
|
||||
def test_linear_signature_valid_accepts(self):
|
||||
"""Linear signs the raw body (hex HMAC-SHA256) in linear-signature."""
|
||||
adapter = _make_adapter()
|
||||
body = b'{"type": "Issue", "data": {"id": "abc"}}'
|
||||
secret = "linear-webhook-key"
|
||||
sig = _generic_signature(body, secret) # same math as linear-signature
|
||||
|
||||
req = _mock_request(headers={"linear-signature": sig})
|
||||
|
||||
assert adapter._validate_signature(req, body, secret) is True
|
||||
|
||||
def test_linear_signature_mismatch_rejects(self):
|
||||
"""A well-formed linear-signature computed with the wrong key fails closed."""
|
||||
adapter = _make_adapter()
|
||||
body = b'{"type": "Issue"}'
|
||||
sig = _generic_signature(body, "attacker-controlled-key")
|
||||
|
||||
req = _mock_request(headers={"linear-signature": sig})
|
||||
|
||||
assert adapter._validate_signature(req, body, "real-secret") is False
|
||||
|
||||
|
||||
def test_non_ascii_svix_signature_rejected(self):
|
||||
"""The Svix branch also runs its `v1,<sig>` comparison through the
|
||||
|
||||
Reference in New Issue
Block a user