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/cgiCookieTest.c src/lib/tests/cgiCookieTest.c
new file mode 100644
index 00000000000..c1eb465ec74
--- /dev/null
+++ src/lib/tests/cgiCookieTest.c
@@ -0,0 +1,72 @@
+/* cgiCookieTest - check how the cookie parser treats a malformed cookie. */
+
+/* Copyright (C) 2026 The Regents of the University of California
+ * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */
+
+#include "common.h"
+#include "cheapcgi.h"
+#include "errCatch.h"
+
+static char *names[] = {"first", "second", "third"};
+
+static char *cases[] = {
+/* the ordinary shape, with and without the space a browser usually sends */
+"first=1; second=2; third=3",
+"first=1;second=2;third=3",
+/* an empty pair, at the front, in the middle and at the end.  The middle one
+ * names the next cookie "; second", so nothing looks it up and that cookie is
+ * silently lost, which is the cookie twin of #38185 */
+";first=1;second=2",
+"first=1;;second=2;third=3",
+"first=1;second=2;;",
+/* a cookie with a name and no =value.  This aborts the CGI, and because the
+ * browser sends the same cookie again on every request, the reader cannot get
+ * a page back until they clear it by hand.  The cookie twin of #38335 */
+"first=1; broken; second=2",
+"first=1; second=2; broken",
+"broken",
+/* an empty value is a value, and is kept */
+"first=; second=2",
+/* nothing at all */
+"",
+";",
+};
+
+static void readAll(char *cookie, boolean skip)
+/* Print what the three names read out of this cookie string. */
+{
+int j;
+setenv("HTTP_COOKIE", cookie, 1);
+cgiResetState();
+cgiSkipMalformedPairs(skip);
+printf("%-4s: ", skip ? "on" : "off");
+struct errCatch *errCatch = errCatchNew();
+if (errCatchStart(errCatch))
+    {
+    for (j = 0;  j < ArraySize(names);  ++j)
+	{
+	char *val = findCookieData(names[j]);
+	printf("[%s=%s]", names[j], val ? val : "(not found)");
+	}
+    }
+errCatchEnd(errCatch);
+if (errCatch->gotError)
+    printf("aborted: %s", trimSpaces(errCatch->message->string));
+errCatchFree(&errCatch);
+printf("\n");
+}
+
+int main(int argc, char *argv[])
+{
+int i;
+/* Each case is read twice, with hg.conf skipMalformedCgiPairs off and on, so
+ * the test also pins that the gate leaves the old behavior alone. */
+for (i = 0;  i < ArraySize(cases);  ++i)
+    {
+    printf("cookie: %s\n", cases[i]);
+    readAll(cases[i], FALSE);
+    readAll(cases[i], TRUE);
+    printf("\n");
+    }
+return 0;
+}