fix(webhook): case-insensitive X-Lark-* signature header lookup - #13
Open
wz-heng wants to merge 2 commits into
Open
fix(webhook): case-insensitive X-Lark-* signature header lookup#13wz-heng wants to merge 2 commits into
wz-heng wants to merge 2 commits into
Conversation
_verify_sign and _has_signature_headers in EventDispatcherHandler and CardActionHandler looked up X-Lark-Request-Timestamp, X-Lark-Request-Nonce, and X-Lark-Signature case-sensitively. ASGI servers (Starlette/FastAPI) hand handlers lowercase header names per the ASGI spec, so every legitimately signed request behind such a server hit `None + None + encrypt_key` and 500'd with a confusing TypeError instead of verifying. Add a small case-insensitive header lookup used by both handlers. Fixes larksuite#12 (bug 1 of 2).
The lowercase-ASGI-header regression coverage added for this fix exercised the event plaintext, event encrypted, and card encrypted paths, but missed CardActionHandler's plaintext-signed path even though _verify_sign there uses the same fixed lookup. Addresses review feedback from this run's required Snape pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes part 1 of #12.
Bug
EventDispatcherHandler._verify_sign/_has_signature_headers(and theidentical duplicated logic in
CardActionHandler) look upX-Lark-Request-Timestamp,X-Lark-Request-Nonce, andX-Lark-Signaturecase-sensitively via a plain
dict.get(...).ASGI servers (Starlette/FastAPI) hand the application lowercase header
names, per the ASGI spec. Behind such a server, the case-sensitive lookup
returns
Nonefor all three headers, and_verify_signimmediatelyraises
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'when it triestimestamp + nonce + encrypt_key— everylegitimately signed request 500s.
Fix
Add a small case-insensitive header lookup (
_get_header), used by bothEventDispatcherHandlerandCardActionHandler(the card callback pathhas the exact same duplicated verification code and the exact same bug).
Falls back to a case-insensitive scan only when the exact-case key isn't
found, so behavior for exact-case callers (e.g. plain WSGI) is unchanged.
Testing
Added regression tests exercising both handlers with lowercased headers
(matching what Starlette/FastAPI would hand the SDK) for both plaintext
and encrypted+signed payloads — they 500'd before this change, they pass
now. Full suite:
python -m pytest -q→ 1101 passed.🤖 Generated with Claude Code