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/js/utils.js src/hg/js/utils.js index 1664d71..a3f8ffe 100644 --- src/hg/js/utils.js +++ src/hg/js/utils.js @@ -662,57 +662,85 @@ $(button).attr('title', 'Expand this '+titleDesc); contents.hide(); } else { $(button).attr('title', 'Collapse this '+titleDesc); contents.show().trigger('show'); } $(hidden).val(newVal); if (doAjax) { setCartVar(hiddenPrefix+"_"+prefix+"_close", newVal); } retval = false; } return retval; } +function getNonce() +{ // Gets nonce value from page meta header +var content = $("meta[http-equiv='Content-Security-Policy']").attr("content"); +if (!content) + return ""; +// parse nonce like 'nonce-JDPiW8odQkiav4UCeXsa34ElFm7o' +var sectionBegin = "'nonce-"; +var sectionEnd = "'"; +var ix = content.indexOf(sectionBegin); +if (ix < 0) + return ""; +content = content.substring(ix+sectionBegin.length); +ix = content.indexOf(sectionEnd); +if (ix < 0) + return ""; +content = content.substring(0,ix); +return content; +} + function warnBoxJsSetup() { // Sets up warnBox if not already established. This is duplicated from htmshell.c + // alert("warnBoxJsSetup() called"); // DEBUG REMOVE GALT TODO Nonce? var html = ""; html += "
','
',cleanHtml,bounds.start,bounds.stop); if (warnMsg.length > 0) { warn(warnMsg); if (whatWeDid) whatWeDid.warnMsg = warnMsg; } cleanHtml = cleanHtml.slice(0,bounds.start) + cleanHtml.slice(bounds.stop); } return cleanHtml; } -function stripJsFiles(returnedHtml,debug) +function stripJsFiles(returnedHtml, debug, whatWeDid) { // strips javascript files from html returned by ajax var cleanHtml = returnedHtml; var shlurpPattern=/"; + var lastIx = 0; + var ix = content.indexOf(sectionBegin, lastIx); + if (ix < 0) + return results; + ix += sectionBegin.length; + var ex = content.indexOf(sectionEnd, ix); + if (ex < 0) + return results; + var jsNonce = content.substring(ix,ex); + if (debug) + alert("jsNonce:'"+jsNonce); + results.push(jsNonce); + lastIx = ex; + ex += sectionEnd.length; + return results; +} + +function charsAreHex(s) +// are all the chars found hex? +{ + var hexChars = "01234566789abcdefABCDEF"; + var d = false; + var i = 0; + if (s) { + d = true; + while (i < s.length) { + if (hexChars.indexOf(s.charAt(i++)) < 0) + d = false; + } + } + return d; +} + +function nonAlphaNumericHexDecodeText(s, prefix, postfix) +// For html tag attributes, it decodes non-alphanumeric characters +// with