fix(webhook): fail loudly on unverifiable signatures, add timestamp freshness and replay protection (fixes #11) - #15
Open
Xuxchloris wants to merge 2 commits into
Conversation
…reshness and replay protection (fixes larksuite#11)
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 #11
Problem
The webhook verification path had three gaps reported in #11:
_verify_signreturned immediately when noencrypt_key/verification_tokenwas configured, so a request carrying signature headers was accepted without any verification — an integrator who believes "the SDK verifies signatures" could ship an open webhook without realizing it.X-Lark-Request-Timestampwas never checked, so a captured request verified forever.(timestamp, nonce)dedup, so a captured legitimate request could be replayed indefinitely.Changes
lark_channel/core/webhook_signature.py(new): sharedverify_webhook_signature()implementing the hardened flow for both the event path (SHA-256 + encrypt key) and the card-action path (SHA-1 + verification token), plus a bounded thread-safeReplayGuard.webhook.signature_unverifiable) and warned in compat/audit mode while keeping the legacy accepting behaviour.max_timestamp_skew_seconds(opt-in): stale/missing timestamps are blocked in strict mode, audited (webhook.timestamp_stale) otherwise.replay_protection_seconds(opt-in): duplicate(timestamp, nonce)is blocked in strict mode, audited (webhook.replay_detected) otherwise.lark_channel/channel/config.py:SecurityConfiggains the two opt-in fields (positive-int validated;Nonepreserves legacy behaviour).lark_channel/event/dispatcher_handler.py/lark_channel/card/action_handler.py:_verify_signnow delegates to the shared helper (with per-handler replay guards); plain signature mismatches still surface aswebhook.signature_invalid/card.signature_invalidas before.lark_channel/channel/tests/test_webhook_signature_hardening.py(new): 9 tests covering unverifiable (compat audit + strict block, event and card), stale timestamp (strict block + compat audit + fresh pass), replay (strict block, event and card), and config validation.Verification
python -m pytest lark_channel— 988 passed; the single failure (test_upload_error_propagation.py::test_gather_buffer_missing_local_file_raises_upload_failed) is a pre-existing Windows-only path-escaping assertion unrelated to this change (CI runs on Linux).Follow-up