Multi-endpoint race conditions

7 min read Hard Portswigger
Race condition
Contents

On this page

The lab

here is how portswigger describes it

This lab's purchasing flow contains a race condition that enables you to purchase items for an unintended price. To solve the lab, successfully purchase a Lightweight L33t Leather Jacket. You can log into your account with the following credentials: wiener:peter.

the new pieces in this lab#

the previous race labs fired many copies of one request. this one is a multi-endpoint race, the collision is between two different requests to two different endpoints, timed so one slips into a window opened by the other. that changes both the concept and the tooling.

  • a multi-endpoint race exploits the window between two distinct operations. here: POST /cart/checkout validates the order (do you have enough store credit for what's in the cart?) and then confirms it (finalise the order and deduct credit). if those two steps aren't atomic, there's a window between "validated" and "confirmed" in which the cart's contents can still change.
  • the attack idea: send POST /cart (which adds the leather jacket) at the same time as POST /cart/checkout. if the checkout validates while the cart is cheap (just a gift card you can afford) but confirms after the jacket has been added, the order is approved against the cheap cart yet finalised with the expensive jacket in it. we are charged as if we bought the gift card but receive the jacket.
  • why the single-packet attack doesn't apply here: that technique crams one request's final bytes into a single packet, it's for firing copies of one request in perfect sync. with two different requests we instead send them as a group in parallel and rely on timing, which means network jitter matters, so we first have to rule it out.
  • connection warming: the first request on a fresh connection is consistently slower than later ones (TLS/connection setup, back-end routing), which would desynchronise our two requests. by prepending a throwaway GET / to "warm" the connection, the subsequent requests complete within a much tighter window. this proves the delay is network architecture, not per-endpoint processing, so it won't sabotage the attack. the method: confirm the cart is shared session state (collision possible), benchmark the connection and warm it so our two requests land tightly together, point POST /cart at the jacket, and fire add-jacket + checkout in parallel repeatedly until the jacket is added inside the validate→confirm window and the order goes through for the gift-card price.

why the validate-confirm window can be exploited#

checkout is a validate-then-confirm sequence: the server reads the cart, checks the total against our store credit, and if it passes, confirms the order using the cart contents. the developer assumed the cart can't change between the credit check and the confirmation, so validating once is enough. but those two steps aren't atomic, and the cart is shared, mutable state keyed on our session. that means a separate request, POST /cart adding the jacket, can land in the gap. if the checkout validates while the cart holds only the affordable gift card, the credit check passes; if the jacket is then added before the checkout confirms, the finalised order contains the jacket even though it was never validated against our credit. the flaw is that the thing being validated (cart contents at check time) can differ from the thing being committed (cart contents at confirm time), and nothing re-checks credit at confirmation. we exploit the gap by racing the two endpoints so the jacket arrives mid-window.

Step 1 - study the purchasing flow#

we log in and purchase a gift card so the full purchasing flow lands in the proxy history for study.

111

Step 2 - map the cart endpoints and confirm shared state#

from the proxy history we identify the cart endpoints: POST /cart adds items, POST /cart/checkout submits the order. we add another gift card and send GET /cart to Repeater, trying it with and without our session cookie. without the cookie we only see an empty cart, so:

  • the cart's state is stored server-side in our session.
  • cart operations are keyed on our session / user id, the same shared record for concurrent requests. that's the shared state a collision needs. we also note that submitting an order and getting confirmation happens in a single request/response cycle, hinting at a window between the order being validated and confirmed, a window in which more items might be added after the credit check.

Step 3 - benchmark the connection#

we send both POST /cart and POST /cart/checkout to Repeater and add the two tabs to a new group. sending them in sequence over a single connection a few times, we notice the first request consistently takes significantly longer than the second.

req1:

222

req2:

333

Step 4 - warm the connection#

we add a GET / (homepage) request to the start of the group and send all three in sequence over a single connection. the first request still takes longest, but by "warming" the connection this way, the second and third requests now complete within a much smaller window.

req2:

444

req3:

555

we deduce the initial delay comes from the back-end network architecture, not from the endpoints' processing time, so it won't interfere with the attack. (we then remove the GET / warming request from the group.)

Step 5 - benchmark the exploit serially#

we make sure there's a single gift card in the cart. in the group, we modify the POST /cart request so productId is set to 1 (the Lightweight L33t Leather Jacket). we send the requests in sequence. the order is rejected for insufficient funds, as expected, serially, the credit check correctly sees the jacket.

666

Step 6 - prove the concept in parallel#

we remove the jacket from the cart and add another gift card (so the cart is affordable again). we send the requests again, but this time in parallel. we check the response to POST /cart/checkout:

  • if it's still insufficient funds, we remove the jacket and repeat, this takes several attempts.
  • if it's a 200, we check whether the leather jacket was purchased, and it was. 777

how the whole attack worked, in detail#

multi-endpoint-race-diagram

the intended (serial) flow. normally you add items to the cart, then check out. POST /cart/checkout reads the cart, sums the prices, compares the total to your store credit, and, if you can afford it, confirms the order and deducts the credit. with a gift card in the cart you can afford it; with the $1337 jacket you can't, so a serial attempt to check out the jacket is rejected. the credit check and the order confirmation are assumed to see the same cart.

the race window. checkout is really two steps, validate (is the current cart affordable?) and confirm (finalise this cart as an order), with a gap between them. for a lone checkout that gap is inert. but the cart is shared, mutable state (Step 2), so another request can modify it during the gap. nothing re-validates credit at the confirm step.

the multi-endpoint collision. instead of racing copies of one request, we race two different endpoints: POST /cart (add the jacket) against POST /cart/checkout (validate + confirm). we want the add-jacket to land after checkout validates (so validation sees only the affordable gift card) but before checkout confirms (so the finalised order includes the jacket). because the two requests are different, we can't use the single-packet attack; we send them as a parallel group and depend on them arriving close together.

why warming mattered. the first request on a connection is slow due to connection/back-end setup, not endpoint processing (Steps 3–4). left unaddressed, that delay would scatter our two requests and the add-jacket would reliably land before validation (cart expensive → rejected) or after confirmation (jacket not in the order). warming the connection with a throwaway GET / proved the delay is fixed network overhead we can factor out, so the two real requests land in a tight, repeatable window, giving the collision a chance to hit the exact sweet spot.

the outcome. on a successful run, checkout validates the cart while it holds only the gift card (credit check passes), the POST /cart adds the jacket into the validate→confirm gap, and checkout confirms the order with the jacket now in it, charging us the gift-card amount we could afford. it's probabilistic, so we repeat until a 200 confirms the jacket was purchased.

the root cause in one line. the order's contents were validated at one instant and committed at another, with no re-check in between, so a concurrent add-to-cart slipped an unaffordable item into an already-approved order.

with this, the lab is solved!