<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TroubieD3v</title>
    <description>The latest articles on DEV Community by TroubieD3v (@troubied3v).</description>
    <link>https://dev.to/troubied3v</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4283718%2F2596af9c-d088-4c5a-8219-38384a8bc1ce.png</url>
      <title>DEV Community: TroubieD3v</title>
      <link>https://dev.to/troubied3v</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/troubied3v"/>
    <language>en</language>
    <item>
      <title>A coupon applied three times: proving race conditions in web APIs</title>
      <dc:creator>TroubieD3v</dc:creator>
      <pubDate>Sat, 05 Dec 2026 00:04:09 +0000</pubDate>
      <link>https://dev.to/troubied3v/a-coupon-applied-three-times-proving-race-conditions-in-web-apis-1p7b</link>
      <guid>https://dev.to/troubied3v/a-coupon-applied-three-times-proving-race-conditions-in-web-apis-1p7b</guid>
      <description>&lt;p&gt;The textbook race condition is a single-use coupon that gets applied three times. A transfer that takes a balance below zero is the same bug, and so is a 2FA code with "three attempts" that lets you try nine: the server checks a condition, then acts, and a second request gets in between.&lt;/p&gt;

&lt;p&gt;You can find these by hand with Burp Repeater's "send group in parallel" and some luck. The harder part is proving the bug well enough that the developer can't reply "works on my machine", and later checking in CI that the fix still holds. I wrote PhotoFinish, an open-source Go tool, for that part.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frw3fj96x41s1ah6ssa1r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frw3fj96x41s1ah6ssa1r.gif" alt="PhotoFinish runs a coupon race against the demo shop: trials arrive live and the verdict is VIOLATED" width="760" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why 20 threads aren't enough
&lt;/h2&gt;

&lt;p&gt;Send 20 requests from 20 threads and they reach the server milliseconds apart, because each one goes through its own TCP, TLS and scheduler path. The race window in a typical handler (between the &lt;code&gt;SELECT&lt;/code&gt; and the &lt;code&gt;UPDATE&lt;/code&gt;) is often under a millisecond, so most of those requests just pass through it one after another.&lt;/p&gt;

&lt;p&gt;In 2023 James Kettle of PortSwigger published the HTTP/2 single-packet attack ("Smashing the state machine"):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open one HTTP/2 connection.&lt;/li&gt;
&lt;li&gt;For every request, send HEADERS and all of the body except the last byte, without END_STREAM. The server holds each request open.&lt;/li&gt;
&lt;li&gt;Wait briefly.&lt;/li&gt;
&lt;li&gt;Write the final frames of every stream (the last body byte, or an empty DATA frame with END_STREAM) in one write call. That is one TLS record and usually one TCP packet.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All the requests complete at the server at the same moment, and network jitter drops out of the picture. For HTTP/1.1 the older last-byte sync does the same thing with one connection per request.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I was missing
&lt;/h2&gt;

&lt;p&gt;Tools for sending these races already exist: Turbo Intruder in Burp and the h2spacex Python library. They handle the sending. After that you have 20 responses, you decide yourself whether a race happened, and there's no way to repeat it in CI.&lt;/p&gt;

&lt;p&gt;I wanted something that would:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prepare state before every trial (reset data, create a cart);&lt;/li&gt;
&lt;li&gt;check a business rule after the race instead of looking at status codes;&lt;/li&gt;
&lt;li&gt;repeat the trial and report honestly how often the rule broke;&lt;/li&gt;
&lt;li&gt;keep the evidence, meaning per-stream timings and the server-side arrival time;&lt;/li&gt;
&lt;li&gt;exit with a code CI understands.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A scenario
&lt;/h2&gt;

&lt;p&gt;Scenarios are YAML. This is a shortened version of the coupon scenario for the demo shop that ships with the tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;coupon-vuln&lt;/span&gt;
&lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://127.0.0.1:8443/vuln/api&lt;/span&gt;
&lt;span class="na"&gt;tls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;insecure:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;allow_hosts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:8443&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;POST /reset&lt;/span&gt;
  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cart&lt;/span&gt;
    &lt;span class="na"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;POST /carts&lt;/span&gt;
    &lt;span class="na"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;cart_id:&lt;/span&gt; &lt;span class="s2"&gt;"$.id"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;race&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;auto&lt;/span&gt;
  &lt;span class="na"&gt;copies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
  &lt;span class="na"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
      &lt;span class="s"&gt;POST /carts/{{cart_id}}/coupons HTTP/2&lt;/span&gt;
      &lt;span class="na"&gt;Content-Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;application/json&lt;/span&gt;

      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"code"&lt;/span&gt;&lt;span class="s"&gt;:"WELCOME10"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;check&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;GET /carts/{{cart_id}}&lt;/span&gt;
  &lt;span class="na"&gt;invariant&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="s"&gt;size(check.json.discounts) &amp;lt;= 1 &amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="s"&gt;size(race.filter(r, r.status == 200)) &amp;lt;= 1&lt;/span&gt;
&lt;span class="na"&gt;trials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rule is written in CEL, the expression language Kubernetes and Envoy use. PhotoFinish evaluates each conjunct separately, so the report says &lt;code&gt;size(check.json.discounts) &amp;lt;= 1 was 3 &amp;lt;= 1&lt;/code&gt; rather than a bare &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;scope.allow_hosts&lt;/code&gt; is mandatory. The tool talks only to the hosts listed there and enforces a request budget, so a typo in a URL can't send twenty payments somewhere else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verdicts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;VIOLATED: the rule broke in at least one trial. That counts as proof.&lt;/li&gt;
&lt;li&gt;HELD: it never broke. The summary says so in plain words: "not reproduced in 10 trials … this is not proof of absence".&lt;/li&gt;
&lt;li&gt;INCONCLUSIVE: the tool can't tell. Responses arrived before release (the server didn't wait for the body), 429s, network errors, or a setup step failed.&lt;/li&gt;
&lt;li&gt;ERROR: the scenario didn't run.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The exit codes, in the same order, are 1, 0, 2 and 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Numbers
&lt;/h2&gt;

&lt;p&gt;The demo shop has four classic races (coupon, transfer, 2FA attempt limit, gift card). Each comes in a vulnerable variant and a fixed one that uses a transaction with a row lock. I didn't stretch the race window artificially; it's ordinary read-check-write code over SQLite.&lt;/p&gt;

&lt;p&gt;The benchmark ran on a GitHub Actions runner with 4 CPUs, with the tool and the shop on the same machine, 20 copies and 30 trials per combination. I ran it on plain loopback first, then with delay and jitter added to loopback by &lt;code&gt;tc netem&lt;/code&gt;, which delays each packet independently the way a real network does.&lt;/p&gt;

&lt;p&gt;The first thing I wanted to know was whether the requests really arrive together. The demo puts the moment it has the whole request, body included, into an &lt;code&gt;X-Arrival-Ns&lt;/code&gt; header. Here is the median spread of that moment within one race:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Network&lt;/th&gt;
&lt;th&gt;h2&lt;/th&gt;
&lt;th&gt;h1&lt;/th&gt;
&lt;th&gt;burst&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;loopback&lt;/td&gt;
&lt;td&gt;0.05–0.14 ms&lt;/td&gt;
&lt;td&gt;0.05–0.13 ms&lt;/td&gt;
&lt;td&gt;0.49–2.37 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 ms ± 0.5 ms jitter&lt;/td&gt;
&lt;td&gt;0.05–0.17 ms&lt;/td&gt;
&lt;td&gt;1.36–1.85 ms&lt;/td&gt;
&lt;td&gt;0.76–2.09 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5 ms ± 2 ms jitter&lt;/td&gt;
&lt;td&gt;0.08–0.21 ms&lt;/td&gt;
&lt;td&gt;6.05–7.43 ms&lt;/td&gt;
&lt;td&gt;3.06–4.82 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With h2 the ends of all 20 requests travel in one TCP packet, so jitter has nothing to pull apart. With h1 the last bytes go out as 20 packets on separate connections, and each packet gets its own delay.&lt;/p&gt;

&lt;p&gt;And this is how many of the 30 trials broke the rule on the vulnerable variants:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Network&lt;/th&gt;
&lt;th&gt;h2&lt;/th&gt;
&lt;th&gt;h1&lt;/th&gt;
&lt;th&gt;burst&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;coupon&lt;/td&gt;
&lt;td&gt;loopback&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;transfer&lt;/td&gt;
&lt;td&gt;loopback&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2FA&lt;/td&gt;
&lt;td&gt;1 ms jitter&lt;/td&gt;
&lt;td&gt;29&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;gift card&lt;/td&gt;
&lt;td&gt;1 ms jitter&lt;/td&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2FA&lt;/td&gt;
&lt;td&gt;5 ms jitter&lt;/td&gt;
&lt;td&gt;29&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;coupon&lt;/td&gt;
&lt;td&gt;5 ms jitter&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last row breaks the pattern: with 5 ms of jitter, burst broke the coupon rule 14 times and h2 only 7. Requests have to arrive close together for a race to happen, but that alone doesn't guarantee one. Once every request is at the server, the server's scheduling decides whether they interleave inside the window. In the demo that means 20 goroutines taking turns on one SQLite connection. That's why the report shows the arrival spread next to the verdict: if the spread is tight and the rule held, the sync did its job and the requests just didn't interleave on the server.&lt;/p&gt;

&lt;p&gt;The fixed variants came out HELD in all 1,080 trials, with every engine and on every network. &lt;a href="https://github.com/TroubieD3v/PhotoFinish/blob/main/BENCHMARKS.md" rel="noopener noreferrer"&gt;BENCHMARKS.md&lt;/a&gt; has the full tables and caveats.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reports and CI
&lt;/h2&gt;

&lt;p&gt;Besides text, PhotoFinish writes JSON, SARIF (for GitHub code scanning), JUnit XML and a self-contained HTML page with a stream timeline. There's a GitHub Action too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;TroubieD3v/PhotoFinish@v0&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;scenarios&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;tests/race/*.yaml&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs8r8ywtgiigkitl1em1c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs8r8ywtgiigkitl1em1c.png" alt="The run screen: trials, the timeline of one race and the evaluated invariant" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's also a local web UI (&lt;code&gt;photofinish serve&lt;/code&gt;) with a scenario editor that validates as you type, plus runs, the timeline and history. Since it can send attack traffic, it listens only on 127.0.0.1 and requires a session token.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not there yet
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First sequence sync (RyotaK's technique for HTTP/1.1) is planned.&lt;/li&gt;
&lt;li&gt;No HTTP/3.&lt;/li&gt;
&lt;li&gt;It doesn't find races by itself. It proves a hypothesis you state, so you need an idea of where to look; anywhere a limit is checked before something is spent is a good start.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;Prebuilt binaries for Linux, macOS and Windows are on the release page: &lt;a href="https://github.com/TroubieD3v/PhotoFinish/releases/tag/v0.1.0" rel="noopener noreferrer"&gt;https://github.com/TroubieD3v/PhotoFinish/releases/tag/v0.1.0&lt;/a&gt;. The quickest way in is the web UI, which comes with the demo shop and the examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./photofinish serve --demo --open
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the command line, using the examples in the repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/TroubieD3v/PhotoFinish &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; PhotoFinish
go build -o bin/photofinish ./cmd/photofinish
./bin/photofinish demo &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;
./bin/photofinish run examples/coupon-vuln.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's also &lt;code&gt;go install github.com/TroubieD3v/photofinish/cmd/photofinish@latest&lt;/code&gt; (no web UI in that build) and a Docker image, &lt;code&gt;ghcr.io/troubied3v/photofinish&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Repo: &lt;a href="https://github.com/TroubieD3v/PhotoFinish" rel="noopener noreferrer"&gt;https://github.com/TroubieD3v/PhotoFinish&lt;/a&gt; (MIT). Only test systems you own or are authorized to test.&lt;/p&gt;

&lt;p&gt;If you have scenarios from real cases (with nobody's secrets in them) or measurements against other servers like nginx, Node or Python ASGI, I'd like to see them. I'm especially curious where h2 and burst diverge the most.&lt;/p&gt;

</description>
      <category>security</category>
      <category>go</category>
      <category>http2</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
