fix(webhooks): a queue worker signed lifecycle webhooks with a stale secret - #245
Open
roncodes wants to merge 1 commit into
Open
fix(webhooks): a queue worker signed lifecycle webhooks with a stale secret#245roncodes wants to merge 1 commit into
roncodes wants to merge 1 commit into
Conversation
…secret SendResourceLifecycleWebhook only populated the session context when a key was absent, then preferred that session value over the event's own. A long running queue worker keeps its session between jobs, so once it had handled an event from one API context every later event was signed and attributed with the first one's credentials, and its company/user context leaked across jobs too. The context serialized on the event is now authoritative: it is resolved once, written to the session unconditionally for downstream code to read, and the previous session state is restored in a finally block so the next job starts clean. Fixes #244
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #245 +/- ##
===========================================
Coverage 100.00% 100.00%
Complexity 6730 6730
===========================================
Files 397 397
Lines 22448 22461 +13
===========================================
+ Hits 22448 22461 +13
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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 #244.
The bug
SendResourceLifecycleWebhook::setSessionFromEvent()only wrote a session key when it was absent, andhandle()then preferred the session value over the event's:A
queue:workprocess is long-running and its session store is a container singleton, so it survives between jobs. Once the worker handled one lifecycle event, every later event reused that first event'sapi_secret— signing outbound webhooks with the wrong HMAC key — plus itsapi_credential,api_key,api_environment,is_sandbox,companyanduser. Restarting the worker only masked it.The reporter verified this against Fleetbase API
v0.7.53/core-apiv1.6.55, and the same logic was still present onv1.6.59.The fix
The context serialized onto the event is now authoritative for the job that carries it:
resolveEventContext()resolves the event's context once, applying the defaults (console/internal/live) explicitly instead of relying onsession()->get()'s fallback argument, which never fired for a key that was present-but-stale.ApiEventrow andWebhookRequestLogare all built from that resolved context — no session reads in the send path.setSessionFromEvent()runs in afinally, putting back whatever the session held before and removing keys that were not set, so the next job starts clean and a synchronous (non-queued) run no longer leavesapi_secret/api_credentialbehind in a real user's session.setSessionFromEvent($event)stays public and keeps its signature; it now overwrites rather than backfills, and returns the restore callable.Tests
vendor/bin/pest— 1415 passed. The 6 failures inTests\Unit\Http\RequestContractsTestandRequestValidationBehaviorTestare pre-existing and reproduce identically onmain; they are unrelated to this change.php-cs-fixerandphpstanare clean on the touched files.Four tests cover the report, and all four fail against the unpatched listener:
X-Fleetbase-Signatureis asserted to equal the HMAC of its own payload under its own secret, and to not equal the HMAC under the other event's secret.api_credential,api_key,api_secret,api_environment,is_sandbox,companyanduser; the session is then asserted to be handed back exactly as it was found.ApiEventrow, the job meta, or the signature.Three existing tests asserted the old precedence (session wins) and were re-pointed at the event-authoritative behavior; their coverage of failure logging and credential attribution is unchanged, with the context moved from the session onto the event.
Note, not addressed here
ResourceLifecycleEvent::broadcastOn()readssession('company')andsession('api_credential')at broadcast time, which has the same worker-session exposure. It is left alone deliberately: the event'sapiCredentialdefaults to'console'rather than being absent, so switching it over would start adding anapi.consolechannel to every console-originated broadcast. That is a separate change with its own blast radius on the realtime layer, and worth its own issue.