fix(order-handler): correct reversed position-manager delegation - #704
Merged
IbrahimIjai merged 1 commit intoAug 31, 2026
Conversation
SO4-Markets#535) Issue SO4-Markets#385 ("order_handler: create_order position-manager lookup is reversed, hijacks owner's own orders") was closed, but the vulnerable logic it described was still present verbatim, marked with a TODO(SO4-Markets#385) comment admitting it had never actually been fixed. The lookup direction was backwards: create_order and create_orders called get_position_manager(&caller, market) — "who is caller's own manager" — instead of checking whether caller is a manager acting FOR someone else. Any trader who delegated to a bot via set_position_manager lost the ability to trade for themselves: every direct create_order call they made afterward was silently rewritten so account = bot and receiver = bot, redirecting their own deposited collateral to the delegate. Add on_behalf_of: Option<Address> to CreateOrderParams. When present, require get_position_manager(on_behalf_of, market) == Some(caller) before opening the position for the named owner; when absent, the caller always acts for themselves regardless of whether they've registered their own manager. Apply the same fix to both create_order and create_orders so the two entrypoints agree. Replaces the prior test that asserted the reversed (buggy) redirection with regression tests covering: a trader with a registered manager can still create orders for themselves (both entrypoints); a registered manager can create an order on_behalf_of the owner who registered them; and naming an owner who never registered the caller reverts.
|
@dev-debbie-umoh Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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
Issue #385 ("order_handler: create_order position-manager lookup is reversed, hijacks owner's own orders") was closed, but the exact vulnerable logic it described was still present, unchanged, in
contracts/order_handler/src/lib.rs, with aTODO(#385)comment in the code explicitly admitting it had never actually been fixed:get_position_manager(&caller, market)looks up "who is the manager for caller" — the code then used that to redirect the order to the manager. But this is backwards: any trader who callsexchange_router.set_position_manager(self, market, bot)to delegate trade execution to a bot loses the ability to trade for themselves — every directcreate_ordercall they make afterward is silently rewritten soaccount = botandreceiver = bot, redirecting their own already-deposited collateral to the delegate.create_ordersmirrored the same bug (issue #620 had it copycreate_order's behavior "as-is, not a fix for #385").Fix
on_behalf_of: Option<Address>toCreateOrderParams.Some(owner)is given, requireget_position_manager(owner, market) == Some(caller)before opening the position for the named owner (receiver is forced to the owner — no redirect).None, the caller always acts for themselves, regardless of whether they have their own registered manager elsewhere.create_orderandcreate_ordersso both entrypoints agree.on_behalf_ofthe owner who registered themCloses #535 (and lands the fix #385 was closed without actually shipping)
Test plan
grep -n "TODO(#385)" contracts/order_handler/src/lib.rs— no matchescargo test -p order-handler --lib— 65 passed, 0 failed (3 unrelated pre-existing failures on unmodifiedmaintoo:custom_heartbeat_timeout_is_respected,execute_order_records_keeper_heartbeat,keeper_goes_stale_after_timeout_and_role_is_revocable— oracle price-lookup issue unrelated to this change, verified independently)