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/lib/cheapcgi.c src/lib/cheapcgi.c index b502e3c32cf..63c26ff7489 100644 --- src/lib/cheapcgi.c +++ src/lib/cheapcgi.c @@ -818,63 +818,99 @@ } freez(&cdMain); freez(&cdName); freez(&cdFileName); } slReverse(&list); *retList = list; *retHash = hash; } #endif /* GBROWSE */ +static boolean skipMalformedPairs = FALSE; + +void cgiSkipMalformedPairs(boolean on) +/* Tell the cookie parser to step over a malformed pair instead of losing the + * pair after it or aborting the request. These libraries cannot read hg.conf + * themselves, so hgConfig.c pushes the setting in, the same way + * cfgSetLogCgiVars pushes cgiSetMaxLogLen. refs #38340 */ +{ +skipMalformedPairs = on; +} + static void parseCookies(struct hash **retHash, struct cgiVar **retList) /* parses any cookies and puts them into the given hash and list */ { char* str; char *namePt, *dataPt, *nextNamePt; struct hash *hash; struct cgiVar *list = NULL, *el; /* don't build the hash table again */ if(haveCookiesHash == TRUE) return; str = cloneString(getenv("HTTP_COOKIE")); if(str == NULL) /* don't have a cookie */ return; hash = newHash(6); namePt = str; while (isNotEmpty(namePt)) + { + if (skipMalformedPairs) + { + /* Step over the separators of an empty pair, then confine the search + * for the '=' to this pair. Without both, a cookie with a name and no + * value swallows the cookie after it, and the same cookie at the end + * of the string aborts the CGI. The browser then sends that cookie + * again on every request, so the reader cannot get a page back until + * they clear it by hand. 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("Mangled Cookie input string: no = in '%s' (offset %d in complete cookie string: '%s')", namePt, (int)(namePt - str), getenv("HTTP_COOKIE")); *dataPt++ = 0; nextNamePt = strchr(dataPt, ';'); if (nextNamePt != NULL) { *nextNamePt++ = 0; if (*nextNamePt == ' ') nextNamePt++; } + } cgiDecode(dataPt,dataPt,strlen(dataPt)); AllocVar(el); el->val = dataPt; slAddHead(&list, el); hashAddSaveName(hash, namePt, el, &el->name); namePt = nextNamePt; } haveCookiesHash = TRUE; slReverse(&list); *retList = list; *retHash = hash; }