Add robust webhook verification and graceful payload parsing - #571
Draft
razor-x wants to merge 2 commits into
Draft
Add robust webhook verification and graceful payload parsing#571razor-x wants to merge 2 commits into
razor-x wants to merge 2 commits into
Conversation
Seam::Webhook#verify raised NoMethodError on every call. svix/webhook calls Svix.secure_compare but does not require the file that defines it, and this SDK required only svix/webhook and svix/errors, so verification worked solely when the host application happened to load the full svix gem first. Requiring svix/util fixes it. The SDK had no webhook specs at all, which is why this went unnoticed; there are now 11. A signed but unreadable body no longer leaks JSON::ParserError, return nil, or raise NoMethodError depending on its shape. It raises the new InvalidWebhookPayloadError, kept distinct from WebhookVerificationError because the sender is genuinely Seam and retrying can never help. Reading a response no longer fails on the shape of the payload: a payload that is not an object yields a resource with unset attributes rather than raising from #each, and a malformed or non-string timestamp reads as nil rather than raising from Time.parse at read time. Waiting on an action attempt whose status is neither pending, success, nor error raises the new ActionAttemptUnknownStatusError instead of returning it as a success. Reporting success would tell the caller the action completed when the SDK cannot tell, and the previous behavior did exactly that. It subclasses ActionAttemptError, so existing handlers for that base keep working. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M2kJ4nGaM8imZCVMEKjmXA
The added comments ran to roughly double the density of the code around them and mostly restated what the line below already said. Kept the ones carrying information the code cannot: why svix/util has to be required, why both key shapes are accepted after symbolize_names, and why array_map needed replacing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M2kJ4nGaM8imZCVMEKjmXA
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.
Summary
This PR enhances the SDK's resilience by adding comprehensive webhook verification with proper error handling and implementing graceful degradation when parsing unexpected API responses.
Key Changes
Webhook Verification & Validation
InvalidWebhookPayloadErrorexception to distinguish between signature verification failures (retry-worthy) and payload parsing failures (permanent)Webhook#verifyto validate that signed payloads contain required Seam event fields (event_idandevent_type)seam_event?helper to validate payload structure, supporting both symbol and string keysGraceful Payload Parsing
BaseResource#process_data_attributesto handle non-Hash payloads without raising, allowing resources to be created with unset attributesBaseResource#parse_datetimeto catchArgumentErrorandTypeError, returningnilfor malformed timestamps instead of raisingAction Attempt Status Handling
ActionAttemptUnknownStatusErrorexception for unrecognized action attempt statusesActionAttemptResolver.wait_until_resolvedto raise this error when encountering unknown statuses, preventing false success claimsActionAttemptErrorfor backward compatibilityDependencies
require "svix/util"to ensureSvix.secure_compareis available during webhook verificationImplementation Details
https://claude.ai/code/session_01M2kJ4nGaM8imZCVMEKjmXA