Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat($sniffer): auto detect CSP mode
Browse files Browse the repository at this point in the history
Chrome Canary now has CSP with apis that allow auto-detection. This change
will turn on CSP mode automatically when we detect its presence.

https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html#script-interfaces--experimental
  • Loading branch information
vojtajina authored and IgorMinar committed Aug 10, 2012
1 parent 4ccd9eb commit 167aa0c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/ng/sniffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
* @name ng.$sniffer
* @requires $window
* @requires $document
*
* @property {boolean} history Does the browser support html5 history api ?
* @property {boolean} hashchange Does the browser support hashchange event ?
Expand All @@ -13,9 +14,10 @@
* This is very simple implementation of testing browser's features.
*/
function $SnifferProvider() {
this.$get = ['$window', function($window) {
this.$get = ['$window', '$document', function($window, $document) {
var eventSupport = {},
android = int((/android (\d+)/.exec(lowercase($window.navigator.userAgent)) || [])[1]);
android = int((/android (\d+)/.exec(lowercase($window.navigator.userAgent)) || [])[1]),
document = $document[0];

return {
// Android has history.pushState, but it does not update location correctly
Expand All @@ -25,22 +27,21 @@ function $SnifferProvider() {
history: !!($window.history && $window.history.pushState && !(android < 4)),
hashchange: 'onhashchange' in $window &&
// IE8 compatible mode lies
(!$window.document.documentMode || $window.document.documentMode > 7),
(!document.documentMode || document.documentMode > 7),
hasEvent: function(event) {
// IE9 implements 'input' event it's so fubared that we rather pretend that it doesn't have
// it. In particular the event is not fired when backspace or delete key are pressed or
// when cut operation is performed.
if (event == 'input' && msie == 9) return false;

if (isUndefined(eventSupport[event])) {
var divElm = $window.document.createElement('div');
var divElm = document.createElement('div');
eventSupport[event] = 'on' + event in divElm;
}

return eventSupport[event];
},
// TODO(i): currently there is no way to feature detect CSP without triggering alerts
csp: false
csp: document.SecurityPolicy ? document.SecurityPolicy.isActive() : false
};
}];
}
2 changes: 1 addition & 1 deletion test/ng/logSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('$log', function() {


beforeEach(module(function($provide){
$window = {navigator: {}};
$window = {navigator: {}, document: {}};
logger = '';
log = function() { logger+= 'log;'; };
warn = function() { logger+= 'warn;'; };
Expand Down
30 changes: 24 additions & 6 deletions test/ng/snifferSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

describe('$sniffer', function() {

function sniffer($window) {
function sniffer($window, $document) {
$window.navigator = {};
return new $SnifferProvider().$get[1]($window);
$document = jqLite($document || {});
return new $SnifferProvider().$get[2]($window, $document);
}

describe('history', function() {
Expand All @@ -20,15 +21,15 @@ describe('$sniffer', function() {

describe('hashchange', function() {
it('should be true if onhashchange property defined', function() {
expect(sniffer({onhashchange: true, document: {}}).hashchange).toBe(true);
expect(sniffer({onhashchange: true}, {}).hashchange).toBe(true);
});

it('should be false if onhashchange property not defined', function() {
expect(sniffer({document: {}}).hashchange).toBe(false);
expect(sniffer({}, {}).hashchange).toBe(false);
});

it('should be false if documentMode is 7 (IE8 comp mode)', function() {
expect(sniffer({onhashchange: true, document: {documentMode: 7}}).hashchange).toBe(false);
expect(sniffer({onhashchange: true}, {documentMode: 7}).hashchange).toBe(false);
});
});

Expand All @@ -42,7 +43,7 @@ describe('$sniffer', function() {
if (elm === 'div') return mockDivElement;
});

$sniffer = sniffer({document: mockDocument});
$sniffer = sniffer({}, mockDocument);
});


Expand Down Expand Up @@ -78,4 +79,21 @@ describe('$sniffer', function() {
expect($sniffer.hasEvent('input')).toBe((msie == 9) ? false : true);
});
});


describe('csp', function() {
it('should be false if document.SecurityPolicy.isActive not available', function() {
expect(sniffer({}, {}).csp).toBe(false);
});


it('should use document.SecurityPolicy.isActive() if available', function() {
var createDocumentWithCSP = function(csp) {
return {SecurityPolicy: {isActive: function() {return csp;}}};
};

expect(sniffer({}, createDocumentWithCSP(false)).csp).toBe(false);
expect(sniffer({}, createDocumentWithCSP(true)).csp).toBe(true);
});
});
});

0 comments on commit 167aa0c

Please sign in to comment.