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

feat($templateRequest): trust url if it is already in cache #9745

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2144,7 +2144,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

$compileNode.empty();

$templateRequest($sce.getTrustedResourceUrl(templateUrl))
$templateRequest(templateUrl)
.then(function(content) {
var compileNode, tempTemplateAttrs, $template, childBoundTranscludeFn;

Expand Down
2 changes: 1 addition & 1 deletion src/ng/directive/ngInclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ var ngIncludeDirective = ['$templateRequest', '$anchorScroll', '$animate', '$sce
}
};

scope.$watch($sce.parseAsResourceUrl(srcExp), function ngIncludeWatchAction(src) {
scope.$watch(srcExp, function ngIncludeWatchAction(src) {
var afterAnimation = function() {
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
$anchorScroll();
Expand Down
6 changes: 5 additions & 1 deletion src/ng/templateRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var $compileMinErr = minErr('$compile');
* @property {number} totalPendingRequests total amount of pending template requests being downloaded.
*/
function $TemplateRequestProvider() {
this.$get = ['$templateCache', '$http', '$q', function($templateCache, $http, $q) {
this.$get = ['$templateCache', '$http', '$q', '$sce', function($templateCache, $http, $q, $sce) {
function handleRequestFn(tpl, ignoreRequestError) {
var self = handleRequestFn;
self.totalPendingRequests++;
Expand All @@ -40,6 +40,10 @@ function $TemplateRequestProvider() {
transformResponse: transformResponse
};

if (!isDefined($templateCache.get(tpl))) {
tpl = $sce.getTrustedResourceUrl(tpl);
}

return $http.get(tpl, httpOptions)
.then(function(response) {
self.totalPendingRequests--;
Expand Down
9 changes: 0 additions & 9 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,15 +1242,6 @@ describe('$compile', function() {
}
));

it('should not load cross domain templates by default', inject(
function($compile, $rootScope, $templateCache, $sce) {
expect(function() {
$templateCache.put('http://example.com/should-not-load.html', 'Should not load even if in cache.');
$compile('<div class="crossDomainTemplate"></div>')($rootScope);
}).toThrowMinErr('$sce', 'insecurl', 'Blocked loading resource from url not allowed by $sceDelegate policy. URL: http://example.com/should-not-load.html');
}
));

it('should load cross domain templates when trusted', inject(
function($compile, $httpBackend, $rootScope, $sce) {
$httpBackend.expect('GET', 'http://example.com/trusted-template.html').respond('<span>example.com/trusted_template_contents</span>');
Expand Down
22 changes: 22 additions & 0 deletions test/ng/templateRequestSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ describe('$templateRequest', function() {
expect(content).toBe('<div>abc</div>');
}));

it('should not download templates from untrusted urls',
inject(function($templateRequest) {
expect(function() {
$templateRequest('http://example.com/tpl.html');
}).toThrowMinErr(
'$sce', 'insecurl',
/Blocked loading resource from url not allowed by \$sceDelegate policy. URL: http:\/\/example.com\/tpl\.html.*/);
}));

it('should fetch templates from cache even if url is untrusted',
inject(function($rootScope, $templateRequest, $httpBackend, $templateCache) {

$templateCache.put('http://example.com/tpl.html', '<div>abc</div>');

var content;
$templateRequest('http://example.com/tpl.html').then(function(html) { content = html; });

$rootScope.$digest();

expect(content).toBe('<div>abc</div>');
}));

it('should cache the request to prevent extra downloads',
inject(function($rootScope, $templateRequest, $httpBackend) {

Expand Down