Skip to content

feat($templateFactory): refactor to a Provider to have a $http/$templateRequest switch #3204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2016
Merged
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
58 changes: 54 additions & 4 deletions src/templateFactory.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@

/**
* @ngdoc object
* @name ui.router.util.$templateFactoryProvider
*
* @description
* Provider for $templateFactory. Manages which template-loading mechanism to
* use, and will default to the most recent one ($templateRequest on Angular
* versions starting from 1.3, $http otherwise).
*/
function TemplateFactoryProvider() {
var shouldUnsafelyUseHttp = angular.version.minor < 3;

/**
* @ngdoc function
* @name ui.router.util.$templateFactoryProvider#shouldUnsafelyUseHttp
* @methodOf ui.router.util.$templateFactoryProvider
*
* @description
* Forces $templateFactory to use $http instead of $templateRequest. This
* might cause XSS, as $http doesn't enforce the regular security checks for
* templates that have been introduced in Angular 1.3. Note that setting this
* to false on Angular older than 1.3.x will crash, as the $templateRequest
* service (and the security checks) are not implemented on these versions.
*
* See the $sce documentation, section
* <a href="https://docs.angularjs.org/api/ng/service/$sce#impact-on-loading-templates">
* Impact on loading templates</a> for more details about this mechanism.
*
* @param {boolean} value
*/
this.shouldUnsafelyUseHttp = function(value) {
shouldUnsafelyUseHttp = !!value;
};

/**
* @ngdoc object
* @name ui.router.util.$templateFactory
*
* @requires $http
* @requires $templateCache
* @requires $injector
*
* @description
* Service. Manages loading of templates.
*/
this.$get = ['$http', '$templateCache', '$injector', function($http, $templateCache, $injector){
return new TemplateFactory($http, $templateCache, $injector, shouldUnsafelyUseHttp);}];
}


/**
* @ngdoc object
* @name ui.router.util.$templateFactory
Expand All @@ -9,8 +60,7 @@
* @description
* Service. Manages loading of templates.
*/
$TemplateFactory.$inject = ['$http', '$templateCache', '$injector'];
function $TemplateFactory( $http, $templateCache, $injector) {
function TemplateFactory($http, $templateCache, $injector, shouldUnsafelyUseHttp) {

/**
* @ngdoc function
Expand Down Expand Up @@ -83,7 +133,7 @@ function $TemplateFactory( $http, $templateCache, $injector) {
if (isFunction(url)) url = url(params);
if (url == null) return null;
else {
if($injector.has && $injector.has('$templateRequest')) {
if(!shouldUnsafelyUseHttp) {
return $injector.get('$templateRequest')(url);
} else {
return $http
Expand Down Expand Up @@ -113,4 +163,4 @@ function $TemplateFactory( $http, $templateCache, $injector) {
};
}

angular.module('ui.router.util').service('$templateFactory', $TemplateFactory);
angular.module('ui.router.util').provider('$templateFactory', TemplateFactoryProvider);
23 changes: 23 additions & 0 deletions test/templateFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,26 @@ describe('templateFactory', function () {
}));
}
});

describe('templateFactory with $http use forced', function () {

beforeEach(function() {
angular
.module('forceHttpInTemplateFactory', [])
.config(function($templateFactoryProvider) {
$templateFactoryProvider.shouldUnsafelyUseHttp(true);
});
module('ui.router.util');
module('forceHttpInTemplateFactory');
});

it('does not restrict URL loading', inject(function($templateFactory, $httpBackend) {
$httpBackend.expectGET('http://evil.com/views/view.html').respond(200, 'template!');
$templateFactory.fromUrl('http://evil.com/views/view.html');
$httpBackend.flush();

$httpBackend.expectGET('data:text/html,foo').respond(200, 'template!');
$templateFactory.fromUrl('data:text/html,foo');
$httpBackend.flush();
}));
});