e6ddf60465deb96e43be6738c5ca6a7a6168cac8
braney
  Sat Aug 22 15:08:59 2026 -0700
hg/lib: add an option to send the content policy as an http response header

New cspWriteResponseHeader() in hg/lib/hCommon.c, gated on hg.conf's
cspResponseHeader, which defaults off everywhere.  The policy string itself is
still built by the existing code in lib/htmshell.c, which now also knows how to
format it as a response header.  Both carry the same nonce, since getNonce() is
one per process, so a page may safely have the header and the meta tag.

Most pages pick it up from addHttpHeaders() in cart.c, the existing hook for
extra response headers, which every cart based CGI already passes through.  Six
places build their own http header block and so call it directly: the two "too
many requests" pages, the captcha and its error page, the hubApi help redirect,
and the hgSearch redirect to hgTracks.

Inline scripts on three of those pages now carry the nonce, and the policy
allows the Cloudflare script the bot check loads, so the option works when it
is turned on.  The Cloudflare entry is the only part of this that takes effect
with the option off.

diff --git src/hg/hgSearch/hgSearch.c src/hg/hgSearch/hgSearch.c
index 5c28caec17e..d781270cd66 100644
--- src/hg/hgSearch/hgSearch.c
+++ src/hg/hgSearch/hgSearch.c
@@ -1,23 +1,24 @@
 /* hgSearch - User interface to explore hgFind search results */
 #include "common.h"
 #include "linefile.h"
 #include "hash.h"
 #include "options.h"
 #include "jksql.h"
 #include "htmshell.h"
 #include "web.h"
+#include "hCommon.h"
 #include "cheapcgi.h"
 #include "cart.h"
 #include "hui.h"
 #include "udc.h"
 #include "knetUdc.h"
 #include "hgConfig.h"
 #include "jsHelper.h"
 #include "errCatch.h"
 #include "hgFind.h"
 #include "cartJson.h"
 #include "trackHub.h"
 #include "hubConnect.h"
 #include "jsonWrite.h"
 #include "hgFind.h"
 #include "trix.h"
@@ -592,34 +593,35 @@
     char *trackName = cloneString(hgp->tableList->name);
     struct trackDb *track = NULL;
     if (!sameString(trackName, "chromInfo"))
         {
         track = tdbForTrack(db, trackName, &hgFindTdbList);
         if (!track && startsWith("all_", trackName))
             track = tdbForTrack(db, trackName+strlen("all_"), &hgFindTdbList);
         if (!track)
             errAbort("no track for table \"%s\" found via a findSpec", trackName);
         }
     if (track)
         {
         trackName = cloneString(track->track);
         }
     trackHubFixName(trackName);
+    cspWriteResponseHeader();
     puts("Content-type:text/html\n");
     puts("<HTML>\n<HEAD>\n");
     printf("<script type='text/javascript' src='../js/utils.js'></script>\n");
-    printf("<script>\n");
+    printf("<script nonce='%s'>\n", getNonce());
     // we are about to redirect back to hgTracks, save the search term onto the
     // history stack so it will appear in the dropdown of auto-suggestions before
     // redirecting.  db and userSearch are user-supplied and go into a JS string literal
     // inside this inline <script>; jsonStringEscape escapes quotes and '/' so neither the
     // string literal nor a literal </script> can break out (XSS).
     char *jsDb = jsonStringEscape(db);
     char *jsSearch = jsonStringEscape(userSearch);
     printf("addRecentSearch(\"%s\", \"%s\", {\"label\": \"%s\", \"value\": \"%s\", \"id\": \"%s\"});\n",
             jsDb, jsSearch, jsSearch, jsSearch, newPosBuf);
     printf("window.location.href=\"../cgi-bin/hgTracks?");
     // db here is a URL query parameter, so cgi-encode it rather than reuse the JS-escaped jsDb
     char *urlDb = cgiEncode(db);
     printf("db=%s", urlDb);
     printf("&position=%s", newPosBuf);
     if (!sameString(trackName, "chromInfo"))