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/lib/fileUi.c src/hg/lib/fileUi.c
index 2481dc0..a730971 100644
--- src/hg/lib/fileUi.c
+++ src/hg/lib/fileUi.c
@@ -547,31 +547,31 @@
             slNameFree(&term);
             }
 
         // If there is more than one val for this var then create filterBy box for it
         if (slCount(tagLabelPairs) > 1)
             {
             // should have a list sorted on the label
             enum cvDataType eCvDataType = cvDataType(var);
             if (eCvDataType == cvInteger)
                 slPairValAtoiSort(&tagLabelPairs);
             else
                 slPairValSortCase(&tagLabelPairs);
             char extraClasses[256];
             safef(extraClasses,sizeof extraClasses,"filterTable %s",var);
             char *dropDownHtml = cgiMakeMultiSelectDropList(var,tagLabelPairs,NULL,"All",
-                      extraClasses,"onchange='filterTable.filter(this);' style='font-size:.9em;'");
+                      extraClasses, "change", "filterTable.filter(this);", "font-size:.9em;", NULL);
             // Note filterBox has classes: filterBy & {var}
             if (dropDownHtml)
                 {
                 dyStringPrintf(dyFilters,
                              "<td align='left'>\n<B>%s</B>:<BR>\n%s</td><td width=10>&nbsp;</td>\n",
                              labelWithVocabLink(var,sortOrder->title[sIx],tagLabelPairs,TRUE),
                              dropDownHtml);  // TRUE were sending tags, not values
                 freeMem(dropDownHtml);
                 count++;
                 if (sIx < 32) // avoid bit overflow but 32 filterBoxes?  I don't think so
                     filterableBits |= (0x1<<(sIx));
                 }
             }
         if (tagLabelPairs != NULL)
             slPairFreeValsAndList(&tagLabelPairs);
@@ -632,30 +632,31 @@
     printf("<BR>&#149;&nbsp;<B><A HREF='http://%s/goldenPath/%s/%s/%s%s/supplemental/' "
            "TARGET=ucscDownloads>supplemental materials</A></B> - "
            "any related files provided by the laboratory.\n",
            server,db,ENCODE_DCC_DOWNLOADS, tdb->track, subDir);
     }
 }
 
 static int filesPrintTable(char *db, struct trackDb *parentTdb, struct fileDb *fileList,
                            sortOrder_t *sortOrder,int filterable)
 // Prints filesList as a sortable table. Returns count
 {
 if (timeIt)
     uglyTime("Start table");
 // Table class=sortable
 int columnCount = 0;
+int butCount = 0;
 int restrictedColumn = 0;
 char *nowrap = (sortOrder->setting != NULL ? " nowrap":"");
       // Sort order trackDb setting found so rely on <BR> in titles for wrapping
 printf("<TABLE class='sortable' style='border: 2px outset #006600;'>\n");
 printf("<THEAD class='sortable'>\n");
 printf("<TR class='sortable' valign='bottom'>\n");
 printf("<TD align='center' valign='center'>&nbsp;");
 int filesCount = slCount(fileList);
 if (filesCount > 5)
     printf("<em><span class='filesCount'></span>%d files</em>",filesCount);
 
 // NOTE: This could be done to preserve sort order in cart.
 //       However hgFileUi would need form OR changes would need to be ajaxed over
 //       AND hgsid would be needed.
 //if (sortOrder)
@@ -711,33 +712,37 @@
     oneFile->mdb->next = NULL; // mdbs were in list for generating sortOrder,
     char *field = NULL;        // but list no longer needed
 
     printf("<TR valign='top'%s>",(filterable != 0) ?" class='filterable'":"");
     // Download button
     printf("<TD nowrap>");
     if (parentTdb)
         field = parentTdb->track;
     else
         {
         field = cloneString(mdbObjFindValue(oneFile->mdb,MDB_VAR_COMPOSITE));
         mdbObjRemoveOneVar(oneFile->mdb,MDB_VAR_COMPOSITE,NULL);
         }
     assert(field != NULL);
 
-    printf("<input type='button' value='Download' onclick=\"window.location="
-           "'http://%s/goldenPath/%s/%s/%s%s/%s';\" title='Download %s ...'>",
-           server,db,ENCODE_DCC_DOWNLOADS, field, subDir, oneFile->fileName, oneFile->fileName);
+    char id[256];
+    safef(id, sizeof id, "ftpBut_%d", butCount++);
+    printf("<input type='button' id='%s' value='Download' title='Download %s ...'>", id, oneFile->fileName);
+    char javascript[1024];
+    safef(javascript, sizeof javascript, "window.location='http://%s/goldenPath/%s/%s/%s%s/%s';"
+           ,server,db,ENCODE_DCC_DOWNLOADS, field, subDir, oneFile->fileName);
+    jsOnEventById("click", id, javascript);
 
 #define SHOW_FOLDER_FRO_COMPOSITE_DOWNLOADS
 #ifdef SHOW_FOLDER_FRO_COMPOSITE_DOWNLOADS
     if (parentTdb == NULL)
         printf("&nbsp;<A href='../cgi-bin/hgFileUi?db=%s&g=%s' title='Navigate to downloads "
                "page for %s set...'><IMG SRC='../images/folderC.png'></a>&nbsp;", db,field,field);
 #endif///def SHOW_FOLDER_FRO_COMPOSITE_DOWNLOADS
     printf("</TD>\n");
 
     // Each of the pulled out mdb vars
     if (sortOrder)
         {
         for (ix=0;ix<sortOrder->count;ix++)
             {
             if (sameString("fileSize",sortOrder->column[ix]))
@@ -836,32 +841,32 @@
        (restrictedColumn > 1 ? (restrictedColumn - 1) : columnCount));
 
 // Total
 if (filesCount > 5)
     printf("<em><span class='filesCount'></span>%d files</em>",filesCount);
 
 // Restriction policy link in later column?
 if (restrictedColumn > 1)
     printf("</TD><TH colspan=%d align='left'><A HREF='%s' TARGET=BLANK style='font-size:.9em;'>"
            "Restriction Policy</A>", columnCount,ENCODE_DATA_RELEASE_POLICY);
 
 printf("</TD></TR>\n");
 printf("</TFOOT></TABLE>\n");
 
 if (parentTdb == NULL)
-    printf("<script type='text/javascript'>{$(document).ready(function() {"
-           "sortTable.initialize($('table.sortable')[0],true,true);});}</script>\n");
+    jsInline("{$(document).ready(function() {"
+           "sortTable.initialize($('table.sortable')[0],true,true);});}\n");
 
 if (timeIt)
     uglyTime("Finished table");
 return filesCount;
 }
 
 
 static int filesFindInDir(char *db, struct mdbObj **pmdbFiles, struct fileDb **pFileList,
                       char *fileType, int limit, boolean *exceededLimit, boolean *isUnrestricted)
 // Prints list of files in downloads directories matching mdb search terms. Returns count
 {
 int fileCount = 0;
 if (isUnrestricted != NULL)
     *isUnrestricted = TRUE;