Skip to content

Commit 8b87fec

Browse files
rjametchristopherthielen
authored andcommitted
test(templateFactory): Verifiy the security checks and compatibility on 1.3+
templateFactory uses templateRequest, which calls the $sce to run the usual security checks on template URLs. This change verifies that the policy is indeed enforced, and that the user can provide $sce-trusted types through templateFactory (i.e., no assumptions are made that URLs are plain strings).
1 parent 9895f94 commit 8b87fec

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

test/templateFactorySpec.js

+48-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,52 @@ describe('templateFactory', function () {
66
expect($templateFactory).toBeDefined();
77
}));
88

9-
xit('should request templates as text/html', inject(function($templateFactory, $httpBackend) {
10-
$httpBackend.expectGET('views/view.html', function(headers) {
11-
return headers.Accept === 'text/html';
12-
}).respond(200);
13-
$templateFactory.fromUrl('views/view.html');
14-
$httpBackend.flush();
15-
}));
9+
if (angular.version.major >= 1 && angular.version.minor >= 3) {
10+
// Post 1.2, there is a $templateRequest and a $sce service
11+
describe('should follow $sce policy and', function() {
12+
it('accepts relative URLs', inject(function($templateFactory, $httpBackend, $sce) {
13+
$httpBackend.expectGET('views/view.html').respond(200, 'template!');
14+
$templateFactory.fromUrl('views/view.html');
15+
$httpBackend.flush();
16+
}));
17+
18+
it('rejects untrusted URLs',
19+
inject(function($templateFactory, $httpBackend, $sce) {
20+
var error = 'No error thrown';
21+
try {
22+
$templateFactory.fromUrl('http://evil.com/views/view.html');
23+
} catch (e) {
24+
error = e.message;
25+
}
26+
expect(error).toMatch(/sce:insecurl/);
27+
}));
28+
29+
it('accepts explicitly trusted URLs',
30+
inject(function($templateFactory, $httpBackend, $sce) {
31+
$httpBackend.expectGET('http://evil.com/views/view.html').respond(200, 'template!');
32+
$templateFactory.fromUrl(
33+
$sce.trustAsResourceUrl('http://evil.com/views/view.html'));
34+
$httpBackend.flush();
35+
}));
36+
});
37+
} else { // 1.2 and before will use directly $http
38+
it('does not restrict URL loading', inject(function($templateFactory, $httpBackend) {
39+
$httpBackend.expectGET('http://evil.com/views/view.html').respond(200, 'template!');
40+
$templateFactory.fromUrl('http://evil.com/views/view.html');
41+
$httpBackend.flush();
42+
43+
$httpBackend.expectGET('data:text/html,foo').respond(200, 'template!');
44+
$templateFactory.fromUrl('data:text/html,foo');
45+
$httpBackend.flush();
46+
}));
47+
48+
// Behavior not kept in >1.2 with $templateRequest
49+
it('should request templates as text/html', inject(function($templateFactory, $httpBackend) {
50+
$httpBackend.expectGET('views/view.html', function(headers) {
51+
return headers.Accept === 'text/html';
52+
}).respond(200);
53+
$templateFactory.fromUrl('views/view.html');
54+
$httpBackend.flush();
55+
}));
56+
}
1657
});

0 commit comments

Comments
 (0)