This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(ngCsp): allow CSP to be configurable #12346
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -785,43 +785,77 @@ describe('angular', function() { | |
|
||
|
||
describe('csp', function() { | ||
|
||
function mockCspElement(cspAttrName, cspAttrValue) { | ||
return spyOn(document, 'querySelector').andCallFake(function(selector) { | ||
if (selector == '[' + cspAttrName + ']') { | ||
var html = '<div ' + cspAttrName + (cspAttrValue ? ('="' + cspAttrValue + '" ') : '') + '></div>'; | ||
return jqLite(html)[0]; | ||
} | ||
}); | ||
|
||
} | ||
|
||
var originalFunction; | ||
|
||
beforeEach(function() { | ||
originalFunction = window.Function; | ||
spyOn(window, 'Function'); | ||
}); | ||
|
||
afterEach(function() { | ||
window.Function = originalFunction; | ||
delete csp.isActive_; | ||
delete csp.rules; | ||
}); | ||
|
||
|
||
it('should return the false when CSP is not enabled (the default)', function() { | ||
expect(csp()).toBe(false); | ||
it('should return the false for all rules when CSP is not enabled (the default)', function() { | ||
expect(csp()).toEqual({ noUnsafeEval: false, noInlineStyle: false }); | ||
}); | ||
|
||
|
||
it('should return true if CSP is autodetected via CSP v1.1 securityPolicy.isActive property', function() { | ||
window.Function = function() { throw new Error('CSP test'); }; | ||
expect(csp()).toBe(true); | ||
it('should return true for noUnsafeEval if eval causes a CSP security policy error', function() { | ||
window.Function.andCallFake(function() { throw new Error('CSP test'); }); | ||
expect(csp()).toEqual({ noUnsafeEval: true, noInlineStyle: false }); | ||
expect(window.Function).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
|
||
it('should return the true when CSP is enabled manually via [ng-csp]', function() { | ||
spyOn(document, 'querySelector').andCallFake(function(selector) { | ||
if (selector == '[ng-csp]') return {}; | ||
}); | ||
expect(csp()).toBe(true); | ||
it('should return true for all rules when CSP is enabled manually via empty `ng-csp` attribute', function() { | ||
var spy = mockCspElement('ng-csp'); | ||
expect(csp()).toEqual({ noUnsafeEval: true, noInlineStyle: true }); | ||
expect(spy).toHaveBeenCalledWith('[ng-csp]'); | ||
expect(window.Function).not.toHaveBeenCalled(); | ||
}); | ||
|
||
|
||
it('should return the true when CSP is enabled manually via [data-ng-csp]', function() { | ||
spyOn(document, 'querySelector').andCallFake(function(selector) { | ||
if (selector == '[data-ng-csp]') return {}; | ||
}); | ||
expect(csp()).toBe(true); | ||
expect(document.querySelector).toHaveBeenCalledWith('[data-ng-csp]'); | ||
it('should return true when CSP is enabled manually via [data-ng-csp]', function() { | ||
var spy = mockCspElement('data-ng-csp'); | ||
expect(csp()).toEqual({ noUnsafeEval: true, noInlineStyle: true }); | ||
expect(spy).toHaveBeenCalledWith('[data-ng-csp]'); | ||
expect(window.Function).not.toHaveBeenCalled(); | ||
}); | ||
|
||
|
||
it('should return true for noUnsafeEval if it is specified in the `ng-csp` attribute value', function() { | ||
var spy = mockCspElement('ng-csp', 'no-unsafe-eval'); | ||
expect(csp()).toEqual({ noUnsafeEval: true, noInlineStyle: false }); | ||
expect(spy).toHaveBeenCalledWith('[ng-csp]'); | ||
expect(window.Function).not.toHaveBeenCalled(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be also nice to test that no check is performed (i.e. spying on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, too |
||
|
||
|
||
it('should return true for noInlineStyle if it is specified in the `ng-csp` attribute value', function() { | ||
var spy = mockCspElement('ng-csp', 'no-inline-style'); | ||
expect(csp()).toEqual({ noUnsafeEval: false, noInlineStyle: true }); | ||
expect(spy).toHaveBeenCalledWith('[ng-csp]'); | ||
expect(window.Function).not.toHaveBeenCalled(); | ||
}); | ||
|
||
|
||
it('should return true for all styles if they are all specified in the `ng-csp` attribute value', function() { | ||
var spy = mockCspElement('ng-csp', 'no-inline-style;no-unsafe-eval'); | ||
expect(csp()).toEqual({ noUnsafeEval: true, noInlineStyle: true }); | ||
expect(spy).toHaveBeenCalledWith('[ng-csp]'); | ||
expect(window.Function).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the
ngCspAttribute.indexOf
checks be looking for> -1
as if they're declared then they'retrue
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking for
!== -1
has the same effect, so this is fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh! I blame it on all the double negatives 😸