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/hg/lib/hgConfig.c src/hg/lib/hgConfig.c
index 5ee8c485a4d..0bab5e9d7b8 100644
--- src/hg/lib/hgConfig.c
+++ src/hg/lib/hgConfig.c
@@ -208,30 +208,35 @@
 {
 /* add mapping of customTrash database to profile if we have customTracks 
  * profile defined */
 if (haveProfile(CUSTOM_TRACKS_PROFILE))
     addConfigIfUndef(CUSTOM_TRASH, "profile", CUSTOM_TRACKS_PROFILE);
 }
 
 static void initConfig()
 /* create and initilize the config hash */
 {
 char filename[PATH_LEN];
 cfgOptionsHash = newHash(6);
 getConfigFile(filename);
 parseConfigFile(filename, 0);
 hackConfigProfiles();
+
+/* The kent libraries do not read hg.conf, so hand them the settings they need.
+ * Safe to call cfgOptionBooleanDefault here: the hash is built, so it will not
+ * come back through initConfig.  refs #38340 */
+cgiSkipMalformedPairs(cfgOptionBooleanDefault("skipMalformedCgiPairs", FALSE));
 }
 
 char* cfgOption(char* name)
 /* Return the option with the given name.  Return NULL if
  * it doesn't exist. */
 {
 /* initilize the config hash if it is not */
 if(cfgOptionsHash == NULL)
     initConfig();
 return hashFindVal(cfgOptionsHash, name);
 }
 
 char *cfgOptionDefault(char* name, char* def)
 /* Return the option with the given name or the given default 
  * if it doesn't exist. */