a53b9958fa734f73aeffb9ddfe2fbad1ca65f90c
galt
  Mon Jan 30 16:18:41 2017 -0800
Check-in of CSP2 Content-Security-Policy work. All C-language CGIs should now support CSP2 in browser to stop major forms of XSS javascript injection. Javascript on pages is gathered together, and then emitted in a single script block at the end with a nonce that tells the browser, this is js that we generated instead of being injected by a hacker. Both inline script from script blocks and inline js event handlers had to be pulled out and separated. You will not see js sprinkled through-out the page now. Older browsers that support CSP1 or that do not understand CSP at all will still work, just without protection. External js libraries loaded at runtime need to be added to the CSP policy header in src/lib/htmshell.c.

diff --git src/hg/hgGateway/hgGateway.c src/hg/hgGateway/hgGateway.c
index 1cc237b..f53e749 100644
--- src/hg/hgGateway/hgGateway.c
+++ src/hg/hgGateway/hgGateway.c
@@ -231,56 +231,56 @@
 if (pix)
     jsonWriteNumber(cj->jw, "pix", pix);
 }
 
 static void doCartJson()
 /* Perform UI commands to update the cart and/or retrieve cart vars & metadata. */
 {
 struct cartJson *cj = cartJsonNew(cart);
 cartJsonRegisterHandler(cj, "setTaxId", setTaxId);
 cartJsonRegisterHandler(cj, "setDb", setDb);
 cartJsonRegisterHandler(cj, "setHubDb", setHubDb);
 cartJsonRegisterHandler(cj, "getUiState", getUiState);
 cartJsonExecute(cj);
 }
 
-static void printActiveGenomes()
+static void printActiveGenomes(struct dyString *dy)
 /* Print out JSON for an object mapping each genome that has at least one db with active=1
  * to its taxId.  */
 {
 struct jsonWrite *jw = jsonWriteNew();
 jsonWriteObjectStart(jw, NULL);
 struct sqlConnection *conn = hConnectCentral();
 // Join with defaultDb because in rare cases, different taxIds (species vs. subspecies)
 // may be used for different assemblies of the same species.  Using defaultDb means that
 // we send a taxId consistent with the taxId of the assembly that we'll change to when
 // the species is selected from the tree.
 char *query = NOSQLINJ "select dbDb.genome, taxId, dbDb.name from dbDb, defaultDb "
     "where defaultDb.name = dbDb.name and active = 1 "
     "and taxId > 1;"; // filter out experimental hgwdev-only stuff with invalid taxIds
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row;
 while ((row = sqlNextRow(sr)) != NULL)
     {
     char *genome = row[0], *db = row[2];
     int taxId = atoi(row[1]);
     if (hDbExists(db))
         jsonWriteNumber(jw, genome, taxId);
     }
 hDisconnectCentral(&conn);
 jsonWriteObjectEnd(jw);
-puts(jw->dy->string);
+dyStringAppend(dy, jw->dy->string);
 jsonWriteFree(&jw);
 }
 
 static void doMainPage()
 /* Send HTML with javascript to bootstrap the user interface. */
 {
 // Start web page with new banner
 char *db = NULL, *genome = NULL, *clade = NULL;
 getDbGenomeClade(cart, &db, &genome, &clade, oldVars);
 // If CGI has &lastDbPos=..., handle that here and save position to cart so it's in place for
 // future cartJson calls.
 char *position = cartGetPosition(cart, db, NULL);
 cartSetString(cart, "position", position);
 webStartJWest(cart, db, "Genome Browser Gateway");
 
@@ -318,54 +318,58 @@
          "use. "
          UNDER_DEV
          MAIN_SITE
          WARNING_BOX_END);
     }
 
 // The visible page elements are all in ./hgGateway.html, which is transformed into a quoted .h
 // file containing a string constant that we #include and print here (see makefile).
 puts(
 #include "hgGateway.html.h"
 );
 
 // Set global JS variables hgsid, activeGenomes, and survey* at page load time
 // We can't just use "var hgsid = " or the other scripts won't see it -- it has to be
 // "window.hgsid = ".
-puts("<script>");
-printf("window.%s = '%s';\n", cartSessionVarName(), cartSessionId(cart));
-puts("window.activeGenomes =");
-printActiveGenomes();
-puts(";");
+struct dyString *dy = dyStringNew(1024);
+
+dyStringPrintf(dy, "window.%s = '%s';\n", cartSessionVarName(), cartSessionId(cart));
+dyStringPrintf(dy, "window.activeGenomes =\n");
+printActiveGenomes(dy);
+dyStringPrintf(dy, "\n;\n");
 char *surveyLink = cfgOption("survey");
 if (isNotEmpty(surveyLink) && !sameWord(surveyLink, "off"))
     {
-    printf("window.surveyLink=\"%s\";\n", jsonStringEscape(surveyLink));
+    dyStringPrintf(dy, "window.surveyLink=\"%s\";\n", jsonStringEscape(surveyLink));
     char *surveyLabel = cfgOptionDefault("surveyLabel", "Please take our survey");
-    printf("window.surveyLabel=\"%s\";\n", jsonStringEscape(surveyLabel));
+    dyStringPrintf(dy, "window.surveyLabel=\"%s\";\n", jsonStringEscape(surveyLabel));
     char *surveyLabelImage = cfgOption("surveyLabelImage");
     if (isNotEmpty(surveyLabelImage))
-        printf("window.surveyLabelImage=\"%s\";\n", jsonStringEscape(surveyLabelImage));
+        dyStringPrintf(dy, "window.surveyLabelImage=\"%s\";\n", jsonStringEscape(surveyLabelImage));
     else
-        puts("window.surveyLabelImage=null;");
+        dyStringPrintf(dy, "window.surveyLabelImage=null;\n");
     }
 else
     {
-    puts("window.surveyLink=null;");
-    puts("window.surveyLabel=null;");
-    puts("window.surveyLabelImage=null;");
+    dyStringPrintf(dy, "window.surveyLink=null;\n");
+    dyStringPrintf(dy, "window.surveyLabel=null;\n");
+    dyStringPrintf(dy, "window.surveyLabelImage=null;\n");
     }
-puts("</script>");
+dyStringPrintf(dy, "hgGateway.init();\n");
+
+jsInline(dy->string);
+dyStringFree(&dy);
 
 puts("<script src=\"../js/es5-shim.4.0.3.min.js\"></script>");
 puts("<script src=\"../js/es5-sham.4.0.3.min.js\"></script>");
 puts("<script src=\"../js/lodash.3.10.0.compat.min.js\"></script>");
 puts("<script src=\"../js/cart.js\"></script>");
 
 webIncludeResourceFile("jquery-ui.css");
 jsIncludeFile("jquery-ui.js", NULL);
 jsIncludeFile("jquery.watermarkinput.js", NULL);
 jsIncludeFile("utils.js",NULL);
 
 // Phylogenetic tree .js file, produced by dbDbTaxonomy.pl:
 char *dbDbTree = cfgOptionDefault("hgGateway.dbDbTaxonomy", "../js/dbDbTaxonomy.js");
 if (isNotEmpty(dbDbTree))
     printf("<script src=\"%s\"></script>\n", dbDbTree);