3eedbb7636648978b0371f2c54378e75fd1ac78c
braney
  Fri Sep 11 12:20:14 2026 -0700
Skip a CGI or cookie pair with no =value in three more parsers, refs #38340

The loop that #38335 fixed in the query string parsers is copied in three more
places, and each one still looks for the '=' across the whole rest of the string
instead of inside the pair it is reading.  A pair with no value therefore runs
into the pair after it and takes its value, and the same pair at the end of the
string has no '=' left to find and aborts.

lib/cheapcgi.c parseCookies         one bad cookie aborts every CGI for that
browser, on every request, until the
reader clears the cookie by hand
hg/hgSession/backup.c               a session backup silently leaves out a
custom track
hg/utils/refreshNamedSessionCustomTracks
one bad session aborts the child, the
parent exits non-zero, and every session
after it goes unscanned, so the trash
cleaner removes their custom track files

All three are behind the hg.conf flag skipMalformedCgiPairs, off by default, and
registered as a release gate in hgConfCatalog.  The kent libraries do not read
hg.conf, so hgConfig.c hands the setting to cheapcgi the way cfgSetLogCgiVars
already hands it cgiSetMaxLogLen.  The query string parsers do not read the
flag; they were fixed unconditionally under #38335.

refreshNamedSessionCustomTracks rebuilds the session contents as it walks, so it
copies a malformed pair through untouched rather than stepping over it.  A
session carrying one comes back byte for byte the same.

Adds lib/tests/cgiCookieTest and hg/hgSession/tests/backupParseTest.  Both read
every case with the flag off and on, so they pin the old behavior as well as the
new one.  The nightly tool has no seam for a unit test; its loop sits inside a
function that runs its own query.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

diff --git src/lib/tests/expected/cgiCookieTest src/lib/tests/expected/cgiCookieTest
new file mode 100644
index 00000000000..cbc886307a9
--- /dev/null
+++ src/lib/tests/expected/cgiCookieTest
@@ -0,0 +1,44 @@
+cookie: first=1; second=2; third=3
+off : [first=1][second=2][third=3]
+on  : [first=1][second=2][third=3]
+
+cookie: first=1;second=2;third=3
+off : [first=1][second=2][third=3]
+on  : [first=1][second=2][third=3]
+
+cookie: ;first=1;second=2
+off : [first=(not found)][second=2][third=(not found)]
+on  : [first=1][second=2][third=(not found)]
+
+cookie: first=1;;second=2;third=3
+off : [first=1][second=(not found)][third=3]
+on  : [first=1][second=2][third=3]
+
+cookie: first=1;second=2;;
+off : aborted: Mangled Cookie input string: no = in ';' (offset 17 in complete cookie string: 'first=1;second=2;;')
+on  : [first=1][second=2][third=(not found)]
+
+cookie: first=1; broken; second=2
+off : [first=1][second=(not found)][third=(not found)]
+on  : [first=1][second=2][third=(not found)]
+
+cookie: first=1; second=2; broken
+off : aborted: Mangled Cookie input string: no = in 'broken' (offset 19 in complete cookie string: 'first=1; second=2; broken')
+on  : [first=1][second=2][third=(not found)]
+
+cookie: broken
+off : aborted: Mangled Cookie input string: no = in 'broken' (offset 0 in complete cookie string: 'broken')
+on  : [first=(not found)][second=(not found)][third=(not found)]
+
+cookie: first=; second=2
+off : [first=][second=2][third=(not found)]
+on  : [first=][second=2][third=(not found)]
+
+cookie: 
+off : [first=(not found)][second=(not found)][third=(not found)]
+on  : [first=(not found)][second=(not found)][third=(not found)]
+
+cookie: ;
+off : aborted: Mangled Cookie input string: no = in ';' (offset 0 in complete cookie string: ';')
+on  : [first=(not found)][second=(not found)][third=(not found)]
+