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) diff --git src/hg/hgSession/backup.c src/hg/hgSession/backup.c index ef103f13ce5..aceba145466 100644 --- src/hg/hgSession/backup.c +++ src/hg/hgSession/backup.c @@ -458,40 +458,67 @@ struct downloadResults *processCtsForDownloadInternals(char *contents, char **pTrackHubsVar) /* Process the saved session cart contents, * looking for custom-tracks which are db-specific. */ { struct downloadResults *resultList = NULL; if (!contents) return resultList; char *contentsToChop = cloneString(contents); char *namePt = contentsToChop; struct sqlConnection *ctConn = hAllocConn(CUSTOM_TRASH); +boolean skipMalformed = cfgOptionBooleanDefault("skipMalformedCgiPairs", FALSE); + while (isNotEmpty(namePt)) { - char *dataPt = strchr(namePt, '='); + char *dataPt; char *nextNamePt; + if (skipMalformed) + { + /* Confine the search for the '=' to this pair. Otherwise a pair with + * no value runs into the pair after it and renames it, so a ctfile_ + * setting right behind one stops matching the prefix and the custom + * track is left out of the backup with no warning. The same pair at + * the end aborts the whole backup. refs #38340 */ + namePt += strspn(namePt, "&"); + if (namePt[0] == 0) + break; + nextNamePt = strchr(namePt, '&'); + if (nextNamePt != NULL) + *nextNamePt++ = 0; + dataPt = strchr(namePt, '='); + if (dataPt == NULL) + { + namePt = nextNamePt; + continue; + } + *dataPt++ = 0; + } + else + { + dataPt = strchr(namePt, '='); if (dataPt == NULL) errAbort("ERROR: Mangled session content string %s", namePt); *dataPt++ = 0; nextNamePt = strchr(dataPt, '&'); if (nextNamePt != NULL) *nextNamePt++ = 0; + } if (startsWith(CT_FILE_VAR_PREFIX, namePt)) { cgiDecode(dataPt, dataPt, strlen(dataPt)); struct downloadResults *result = NULL; AllocVar(result); char *db = namePt + strlen(CT_FILE_VAR_PREFIX); result->db = cloneString(db); if (fileExists(dataPt)) { result->ctPath = cloneString(dataPt);