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/hgTracks/extTools.c src/hg/hgTracks/extTools.c index 76d39dc..50c49d2 100644 --- src/hg/hgTracks/extTools.c +++ src/hg/hgTracks/extTools.c @@ -21,54 +21,56 @@ #include "net.h" boolean extToolsEnabled() /* Return TRUE if we can display the external tools menu. */ { return fileExists("extTools.ra"); } void printExtMenuData() /* print the external tools aka "send to" menu entries as a javascript list to stdout */ { if (!extToolsEnabled()) return; struct extTool *extTools = readExtToolRa("extTools.ra"); struct extTool *et; -hPuts("<script>\n"); -hPuts("extTools = [\n"); +struct dyString *dy = dyStringNew(1024); +dyStringAppend(dy, "extTools = [\n"); for(et = extTools; et != NULL; et = et->next) { if (et->dbs!=NULL) { if (!slNameInList(et->dbs, database)) continue; } if (et->notDbs!=NULL) { if (slNameInList(et->notDbs, database)) continue; } char* tool = jsonStringEscape(et->tool); char* shortLabel = jsonStringEscape(et->shortLabel); char* longLabel = jsonStringEscape(et->longLabel); - hPrintf(" ['%s', '%s', '%s', %d]", tool, shortLabel, longLabel, et->maxSize); + dyStringPrintf(dy, " ['%s', '%s', '%s', %d]", tool, shortLabel, longLabel, et->maxSize); if (et->next) - hPuts(","); - hPuts("\n"); + dyStringAppend(dy, ","); + dyStringAppend(dy, "\n"); } -hPuts("];\n"); -hPuts("</script>\n"); +dyStringAppend(dy, "];\n"); + +jsInline(dy->string); +dyStringFree(&dy); } static char *cloneRaSetting(struct hash *hash, char *name, struct lineFile *lf, bool mustExist) /* clone a setting out of the ra hash. errAbort if mustExit and not found, otherwise NULL. */ { char *str; if ((str = hashFindVal(hash, name)) == NULL) { if (mustExist) errAbort("missing required setting '%s' on line %d in file %s\n", name, lf->lineIx, lf->fileName); else return NULL; } @@ -155,32 +157,35 @@ } void extToolRedirect(struct cart *cart, char *tool) /* redirect to an external tool, sending the data specified in the ext tools config file */ { bool debug = cgiVarExists("debug"); struct extTool *extTools = readExtToolRa("extTools.ra"); struct extTool *et; for (et = extTools; et != NULL; et = et->next) if (sameWord(et->tool, tool)) break; if (et==NULL) errAbort("No configuration found for tool %s", tool); + // construct an invisible CGI form with the given parameters -printf("<html><body>\n"); +printf("<html>\n<head>\n"); +generateCspMetaHeader(stdout); +printf("</head>\n<body>\n"); if (debug) printf("Target URL: %s<p>", et->url); char *chromName; int winStart, winEnd; char *db = cartString(cart, "db"); char *pos = cartString(cart, "position"); // Try to deal with virt chrom position used by hgTracks. if (startsWith("virt:", cartUsualString(cart, "position", ""))) pos = cartString(cart, "nonVirtPosition"); findGenomePos(db, pos, &chromName, &winStart, &winEnd, cart); int len = winEnd-winStart; @@ -276,18 +281,24 @@ } else printf("<input type=\"hidden\" name=\"%s\" value=\"%s\">\n", slp->name, val); } } // a hidden submit button, see // http://stackoverflow.com/questions/477691/submitting-a-form-by-pressing-enter-without-a-submit-button if (debug) printf("<input type=\"submit\">\n"); else if (!submitDone) printf("<input type=\"submit\" style=\"position: absolute; left: -9999px; width: 1px; height: 1px;\">\n"); printf("</form>\n"); // a little javascript that clicks the submit button if (!debug) - printf("<script>document.getElementById(\"redirForm\").submit();</script>\n"); + { + struct dyString *dy = dyStringNew(256); + dyStringPrintf(dy, "document.getElementById(\"redirForm\").submit();"); + jsInline(dy->string); + dyStringFree(&dy); + } +jsInlineFinish(); printf("</body></html>\n"); }