@@ -5870,6 +5870,74 @@ describe('$compile', function() {
5870
5870
} ) ;
5871
5871
} ) ;
5872
5872
5873
+ describe ( 'img[srcset] sanitization' , function ( ) {
5874
+
5875
+ it ( 'should NOT require trusted values for img srcset' , inject ( function ( $rootScope , $compile , $sce ) {
5876
+ element = $compile ( '<img srcset="{{testUrl}}"></img>' ) ( $rootScope ) ;
5877
+ $rootScope . testUrl = 'http://example.com/image.png' ;
5878
+ $rootScope . $digest ( ) ;
5879
+ expect ( element . attr ( 'srcset' ) ) . toEqual ( 'http://example.com/image.png' ) ;
5880
+ // But it should accept trusted values anyway.
5881
+ $rootScope . testUrl = $sce . trustAsUrl ( 'http://example.com/image2.png' ) ;
5882
+ $rootScope . $digest ( ) ;
5883
+ expect ( element . attr ( 'srcset' ) ) . toEqual ( 'http://example.com/image2.png' ) ;
5884
+ } ) ) ;
5885
+
5886
+ it ( 'should use $$sanitizeUri' , function ( ) {
5887
+ var $$sanitizeUri = jasmine . createSpy ( '$$sanitizeUri' ) ;
5888
+ module ( function ( $provide ) {
5889
+ $provide . value ( '$$sanitizeUri' , $$sanitizeUri ) ;
5890
+ } ) ;
5891
+ inject ( function ( $compile , $rootScope ) {
5892
+ element = $compile ( '<img srcset="{{testUrl}}"></img>' ) ( $rootScope ) ;
5893
+ $rootScope . testUrl = "someUrl" ;
5894
+
5895
+ $$sanitizeUri . andReturn ( 'someSanitizedUrl' ) ;
5896
+ $rootScope . $apply ( ) ;
5897
+ expect ( element . attr ( 'srcset' ) ) . toBe ( 'someSanitizedUrl' ) ;
5898
+ expect ( $$sanitizeUri ) . toHaveBeenCalledWith ( $rootScope . testUrl , true ) ;
5899
+ } ) ;
5900
+ } ) ;
5901
+
5902
+ it ( 'should sanitize all uris in srcset' , inject ( function ( $rootScope , $compile ) {
5903
+ /*jshint scripturl:true*/
5904
+ element = $compile ( '<img srcset="{{testUrl}}"></img>' ) ( $rootScope ) ;
5905
+ var testSet = {
5906
+ 'http://example.com/image.png' :'http://example.com/image.png' ,
5907
+ ' http://example.com/image.png' :'http://example.com/image.png' ,
5908
+ 'http://example.com/image.png ' :'http://example.com/image.png' ,
5909
+ 'http://example.com/image.png 128w' :'http://example.com/image.png 128w' ,
5910
+ 'http://example.com/image.png 2x' :'http://example.com/image.png 2x' ,
5911
+ 'http://example.com/image.png 1.5x' :'http://example.com/image.png 1.5x' ,
5912
+ 'http://example.com/image1.png 1x,http://example.com/image2.png 2x' :'http://example.com/image1.png 1x,http://example.com/image2.png 2x' ,
5913
+ 'http://example.com/image1.png 1x ,http://example.com/image2.png 2x' :'http://example.com/image1.png 1x ,http://example.com/image2.png 2x' ,
5914
+ 'http://example.com/image1.png 1x, http://example.com/image2.png 2x' :'http://example.com/image1.png 1x,http://example.com/image2.png 2x' ,
5915
+ 'http://example.com/image1.png 1x , http://example.com/image2.png 2x' :'http://example.com/image1.png 1x ,http://example.com/image2.png 2x' ,
5916
+ 'http://example.com/image1.png 48w,http://example.com/image2.png 64w' :'http://example.com/image1.png 48w,http://example.com/image2.png 64w' ,
5917
+ //Test regex to make sure doesn't mistake parts of url for width descriptors
5918
+ 'http://example.com/image1.png?w=48w,http://example.com/image2.png 64w' :'http://example.com/image1.png?w=48w,http://example.com/image2.png 64w' ,
5919
+ 'http://example.com/image1.png 1x,http://example.com/image2.png 64w' :'http://example.com/image1.png 1x,http://example.com/image2.png 64w' ,
5920
+ 'http://example.com/image1.png,http://example.com/image2.png' :'http://example.com/image1.png ,http://example.com/image2.png' ,
5921
+ 'http://example.com/image1.png ,http://example.com/image2.png' :'http://example.com/image1.png ,http://example.com/image2.png' ,
5922
+ 'http://example.com/image1.png, http://example.com/image2.png' :'http://example.com/image1.png ,http://example.com/image2.png' ,
5923
+ 'http://example.com/image1.png , http://example.com/image2.png' :'http://example.com/image1.png ,http://example.com/image2.png' ,
5924
+ 'http://example.com/image1.png 1x, http://example.com/image2.png 2x, http://example.com/image3.png 3x' :
5925
+ 'http://example.com/image1.png 1x,http://example.com/image2.png 2x,http://example.com/image3.png 3x' ,
5926
+ 'javascript:doEvilStuff() 2x' : 'unsafe:javascript:doEvilStuff() 2x' ,
5927
+ 'http://example.com/image1.png 1x,javascript:doEvilStuff() 2x' :'http://example.com/image1.png 1x,unsafe:javascript:doEvilStuff() 2x' ,
5928
+ 'http://example.com/image1.jpg?x=a,b 1x,http://example.com/ima,ge2.jpg 2x' :'http://example.com/image1.jpg?x=a,b 1x,http://example.com/ima,ge2.jpg 2x' ,
5929
+ //Test regex to make sure doesn't mistake parts of url for pixel density descriptors
5930
+ 'http://example.com/image1.jpg?x=a2x,b 1x,http://example.com/ima,ge2.jpg 2x' :'http://example.com/image1.jpg?x=a2x,b 1x,http://example.com/ima,ge2.jpg 2x'
5931
+ } ;
5932
+
5933
+ forEach ( testSet , function ( ref , url ) {
5934
+ $rootScope . testUrl = url ;
5935
+ $rootScope . $digest ( ) ;
5936
+ expect ( element . attr ( 'srcset' ) ) . toEqual ( ref ) ;
5937
+ } ) ;
5938
+
5939
+ } ) ) ;
5940
+ } ) ;
5873
5941
5874
5942
describe ( 'a[href] sanitization' , function ( ) {
5875
5943
0 commit comments