How to add Content Security Policy to Firefox extension

后端 未结 2 1960
广开言路
广开言路 2020-12-10 09:26

I have a plugin which I have to support both on Chrome and Firefox browsers. The plugin does cross script loading.

In Chrome, by adding the content security policy i

相关标签:
2条回答
  • 2020-12-10 10:03

    There are plans in the future to add content policy natively in the SDK (bug 852297), but there is a 3rd party module that should get you close to where you want to be: policy.js

    0 讨论(0)
  • 2020-12-10 10:08

    I couldn't find a simple solution for my problem and upon looking up some firefox plugin extensions i had to come up with my own solution as below. The below solution was tested on FF 24.0 but should work on other versions as well.

    Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService)
        .addObserver(_httpExamineCallback, "http-on-examine-response", false);
    
    function _httpExamineCallback(aSubject, aTopic, aData) {
        var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
    
        if (httpChannel.responseStatus !== 200) {
            return;
        }
    
        var cspRules;
        var mycsp;
        // thre is no clean way to check the presence of csp header. an exception
        // will be thrown if it is not there.
        // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel
        try {    
            cspRules = httpChannel.getResponseHeader("Content-Security-Policy");
            mycsp = _getCspAppendingMyHostDirective(cspRules);
            httpChannel.setResponseHeader('Content-Security-Policy', mycsp, false);
        } catch (e) {
            try {
                // Fallback mechanism support             
                cspRules = httpChannel.getResponseHeader("X-Content-Security-Policy");
                mycsp = _getCspAppendingMyHostDirective(cspRules);    
                httpChannel.setResponseHeader('X-Content-Security-Policy', mycsp, false);            
            } catch (e) {
                // no csp headers defined
                return;
            }
        }
    
    };
    
    /**
     * @var cspRules : content security policy 
     * For my requirement i have to append rule just to 'script-src' directive. But you can
     * modify this function to your need.
     *
     */
    function _getCspAppendingMyHostDirective(cspRules) {
      var rules = cspRules.split(';'),
        scriptSrcDefined = false,
        defaultSrcIndex = -1;
    
      for (var ii = 0; ii < rules.length; ii++) {
        if ( rules[ii].toLowerCase().indexOf('script-src') != -1 ) {
            rules[ii] = rules[ii] + ' <My CSP Rule gets appended here>';
            scriptSrcDefined = true;
        }
    
        if (rules[ii].toLowerCase().indexOf('default-src') != -1) {
            defaultSrcIndex = ii;
        }
    }
    
      // few publishers will put every thing in the default (default-src) directive,
      // without defining script-src. We need to modify those as well.
      if ((!scriptSrcDefined) && (defaultSrcIndex != -1)) {
        rules[defaultSrcIndex] = rules[defaultSrcIndex] + ' <My CSP rule gets appended here>';
      }
    
      return rules.join(';');
    };
    
    0 讨论(0)
提交回复
热议问题