fix(MultiHookRouter): preserve hook order in removeHook#41
Merged
ariessa merged 1 commit intoMay 4, 2026
Conversation
removeHook used a swap-and-pop pattern (hooks[i] = hooks[len-1]; pop()) which silently reorders the remaining hooks. Order is semantically meaningful here: beforeAction/afterAction iterate _jobHooks in storage order, and _splitHookData binds per-hook optParams to hooks by array index. The existing reorderHooks function and HooksReordered event also indicate ordering is part of the contract. For [A, B, C, D] removing B with swap-and-pop yields [A, D, C], breaking A->C->D semantics and silently misrouting per-hook optParams (the bytes intended for C are dispatched to D and vice versa). Replace swap-and-pop with an order-preserving shift-left so removal behaves the way callers and the rest of the contract assume. Reported by FailSafe security audit (OPS2-H004). Co-Authored-By: Joshua Medvinsky <76570188+Joshua-Medvinsky@users.noreply.github.com>
ariessa
approved these changes
May 4, 2026
3 tasks
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
MultiHookRouter.removeHookuses a swap-and-pop pattern:This silently reorders the remaining hooks. Ordering is semantically meaningful in this contract:
beforeAction/afterActioniterate_jobHooks[jobId][selector]in storage order._splitHookDatabinds per-hook optParams to hooks by array index —perHookData[i]is dispatched tohooks[i].reorderHooksand aHooksReorderedevent, which is itself evidence that order is a first-class part of the configuration surface.For configured hooks
[A, B, C, D], removingBwith swap-and-pop produces[A, D, C]instead of[A, C, D]. This breaks intendedA → C → Dexecution and silently misroutes per-hook optParams (the bytes prepared forCare dispatched toDand vice versa).Fix
Replace swap-and-pop with an order-preserving shift-left, then
pop():Gas cost grows linearly with the number of hooks after the removed index, but
maxHooksPerJobalready bounds the list, so the upper bound is small and admin-controlled.Severity
Medium. Not a direct theft vector, but order-dependent hook stacks (and per-hook optParams routing) are silently broken. The contract's own ordering API (
reorderHooks/HooksReordered) implies callers may rely on the order they configured.References
contracts/hooks/MultiHookRouter.solTest plan
[A, B, C]yields[B, C][A, B, C, D]yields[A, C, D](regression test for the reported bug)[A, B, C]yields[A, B]HookNotFound_splitHookDataper-hook optParams routing remains correct after removal