@@ -15,6 +15,7 @@ var $sanitizeMinErr = angular.$$minErr('$sanitize');
15
15
var bind ;
16
16
var extend ;
17
17
var forEach ;
18
+ var isArray ;
18
19
var isDefined ;
19
20
var lowercase ;
20
21
var noop ;
@@ -144,9 +145,11 @@ var htmlSanitizeWriter;
144
145
* Creates and configures {@link $sanitize} instance.
145
146
*/
146
147
function $SanitizeProvider ( ) {
148
+ var hasBeenInstantiated = false ;
147
149
var svgEnabled = false ;
148
150
149
151
this . $get = [ '$$sanitizeUri' , function ( $$sanitizeUri ) {
152
+ hasBeenInstantiated = true ;
150
153
if ( svgEnabled ) {
151
154
extend ( validElements , svgElements ) ;
152
155
}
@@ -187,7 +190,7 @@ function $SanitizeProvider() {
187
190
* </div>
188
191
*
189
192
* @param {boolean= } flag Enable or disable SVG support in the sanitizer.
190
- * @returns {boolean|ng. $sanitizeProvider } Returns the currently configured value if called
193
+ * @returns {boolean|$sanitizeProvider } Returns the currently configured value if called
191
194
* without an argument or self for chaining otherwise.
192
195
*/
193
196
this . enableSvg = function ( enableSvg ) {
@@ -199,13 +202,113 @@ function $SanitizeProvider() {
199
202
}
200
203
} ;
201
204
205
+
206
+ /**
207
+ * @ngdoc method
208
+ * @name $sanitizeProvider#addValidElements
209
+ * @kind function
210
+ *
211
+ * @description
212
+ * Extends the built-in lists of valid HTML/SVG elements, i.e. elements that are considered safe
213
+ * and are not stripped off during sanitization. You can extend the following lists of elements:
214
+ *
215
+ * - `htmlElements`: A list of elements (tag names) to extend the current list of safe HTML
216
+ * elements. HTML elements considered safe will not be removed during sanitization. All other
217
+ * elements will be stripped off.
218
+ *
219
+ * - `htmlVoidElements`: This is similar to `htmlElements`, but marks the elements as
220
+ * "void elements" (similar to HTML
221
+ * [void elements](https://rawgit.com/w3c/html/html5.1-2/single-page.html#void-elements)). These
222
+ * elements have no end tag and cannot have content.
223
+ *
224
+ * - `svgElements`: This is similar to `htmlElements`, but for SVG elements. This list is only
225
+ * taken into account if SVG is {@link ngSanitize.$sanitizeProvider#enableSvg enabled} for
226
+ * `$sanitize`.
227
+ *
228
+ * <div class="alert alert-info">
229
+ * This method must be called during the {@link angular.Module#config config} phase. Once the
230
+ * `$sanitize` service has been instantiated, this method has no effect.
231
+ * </div>
232
+ *
233
+ * <div class="alert alert-warning">
234
+ * Keep in mind that extending the built-in lists of elements may expose your app to XSS or
235
+ * other vulnerabilities. Be very mindful of the elements you add.
236
+ * </div>
237
+ *
238
+ * @param {Array<String>|Object } elements - A list of valid HTML elements or an object with one or
239
+ * more of the following properties:
240
+ * - **htmlElements** - `{Array<String>}` - A list of elements to extend the current list of
241
+ * HTML elements.
242
+ * - **htmlVoidElements** - `{Array<String>}` - A list of elements to extend the current list of
243
+ * void HTML elements; i.e. elements that do not have an end tag.
244
+ * - **svgElements** - `{Array<String>}` - A list of elements to extend the current list of SVG
245
+ * elements. The list of SVG elements is only taken into account if SVG is
246
+ * {@link ngSanitize.$sanitizeProvider#enableSvg enabled} for `$sanitize`.
247
+ *
248
+ * Passing an array (`[...]`) is equivalent to passing `{htmlElements: [...]}`.
249
+ *
250
+ * @return {$sanitizeProvider } Returns self for chaining.
251
+ */
252
+ this . addValidElements = function ( elements ) {
253
+ if ( ! hasBeenInstantiated ) {
254
+ if ( isArray ( elements ) ) {
255
+ elements = { htmlElements : elements } ;
256
+ }
257
+
258
+ addElementsTo ( svgElements , elements . svgElements ) ;
259
+ addElementsTo ( voidElements , elements . htmlVoidElements ) ;
260
+ addElementsTo ( validElements , elements . htmlVoidElements ) ;
261
+ addElementsTo ( validElements , elements . htmlElements ) ;
262
+ }
263
+
264
+ return this ;
265
+ } ;
266
+
267
+
268
+ /**
269
+ * @ngdoc method
270
+ * @name $sanitizeProvider#addValidAttrs
271
+ * @kind function
272
+ *
273
+ * @description
274
+ * Extends the built-in list of valid attributes, i.e. attributes that are considered safe and are
275
+ * not stripped off during sanitization.
276
+ *
277
+ * **Note**:
278
+ * The new attributes will not be treated as URI attributes, which means their values will not be
279
+ * sanitized as URIs using `$compileProvider`'s
280
+ * {@link ng.$compileProvider#aHrefSanitizationWhitelist aHrefSanitizationWhitelist} and
281
+ * {@link ng.$compileProvider#imgSrcSanitizationWhitelist imgSrcSanitizationWhitelist}.
282
+ *
283
+ * <div class="alert alert-info">
284
+ * This method must be called during the {@link angular.Module#config config} phase. Once the
285
+ * `$sanitize` service has been instantiated, this method has no effect.
286
+ * </div>
287
+ *
288
+ * <div class="alert alert-warning">
289
+ * Keep in mind that extending the built-in list of attributes may expose your app to XSS or
290
+ * other vulnerabilities. Be very mindful of the attributes you add.
291
+ * </div>
292
+ *
293
+ * @param {Array<String> } attrs - A list of valid attributes.
294
+ *
295
+ * @returns {$sanitizeProvider } Returns self for chaining.
296
+ */
297
+ this . addValidAttrs = function ( attrs ) {
298
+ if ( ! hasBeenInstantiated ) {
299
+ extend ( validAttrs , arrayToMap ( attrs , true ) ) ;
300
+ }
301
+ return this ;
302
+ } ;
303
+
202
304
//////////////////////////////////////////////////////////////////////////////////////////////////
203
305
// Private stuff
204
306
//////////////////////////////////////////////////////////////////////////////////////////////////
205
307
206
308
bind = angular . bind ;
207
309
extend = angular . extend ;
208
310
forEach = angular . forEach ;
311
+ isArray = angular . isArray ;
209
312
isDefined = angular . isDefined ;
210
313
lowercase = angular . $$lowercase ;
211
314
noop = angular . noop ;
@@ -230,36 +333,36 @@ function $SanitizeProvider() {
230
333
231
334
// Safe Void Elements - HTML5
232
335
// http://dev.w3.org/html5/spec/Overview.html#void-elements
233
- var voidElements = toMap ( 'area,br,col,hr,img,wbr' ) ;
336
+ var voidElements = stringToMap ( 'area,br,col,hr,img,wbr' ) ;
234
337
235
338
// Elements that you can, intentionally, leave open (and which close themselves)
236
339
// http://dev.w3.org/html5/spec/Overview.html#optional-tags
237
- var optionalEndTagBlockElements = toMap ( 'colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr' ) ,
238
- optionalEndTagInlineElements = toMap ( 'rp,rt' ) ,
340
+ var optionalEndTagBlockElements = stringToMap ( 'colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr' ) ,
341
+ optionalEndTagInlineElements = stringToMap ( 'rp,rt' ) ,
239
342
optionalEndTagElements = extend ( { } ,
240
343
optionalEndTagInlineElements ,
241
344
optionalEndTagBlockElements ) ;
242
345
243
346
// Safe Block Elements - HTML5
244
- var blockElements = extend ( { } , optionalEndTagBlockElements , toMap ( 'address,article,' +
347
+ var blockElements = extend ( { } , optionalEndTagBlockElements , stringToMap ( 'address,article,' +
245
348
'aside,blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5,' +
246
349
'h6,header,hgroup,hr,ins,map,menu,nav,ol,pre,section,table,ul' ) ) ;
247
350
248
351
// Inline Elements - HTML5
249
- var inlineElements = extend ( { } , optionalEndTagInlineElements , toMap ( 'a,abbr,acronym,b,' +
352
+ var inlineElements = extend ( { } , optionalEndTagInlineElements , stringToMap ( 'a,abbr,acronym,b,' +
250
353
'bdi,bdo,big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s,' +
251
354
'samp,small,span,strike,strong,sub,sup,time,tt,u,var' ) ) ;
252
355
253
356
// SVG Elements
254
357
// https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Elements
255
358
// Note: the elements animate,animateColor,animateMotion,animateTransform,set are intentionally omitted.
256
359
// They can potentially allow for arbitrary javascript to be executed. See #11290
257
- var svgElements = toMap ( 'circle,defs,desc,ellipse,font-face,font-face-name,font-face-src,g,glyph,' +
360
+ var svgElements = stringToMap ( 'circle,defs,desc,ellipse,font-face,font-face-name,font-face-src,g,glyph,' +
258
361
'hkern,image,linearGradient,line,marker,metadata,missing-glyph,mpath,path,polygon,polyline,' +
259
362
'radialGradient,rect,stop,svg,switch,text,title,tspan' ) ;
260
363
261
364
// Blocked Elements (will be stripped)
262
- var blockedElements = toMap ( 'script,style' ) ;
365
+ var blockedElements = stringToMap ( 'script,style' ) ;
263
366
264
367
var validElements = extend ( { } ,
265
368
voidElements ,
@@ -268,17 +371,17 @@ function $SanitizeProvider() {
268
371
optionalEndTagElements ) ;
269
372
270
373
//Attributes that have href and hence need to be sanitized
271
- var uriAttrs = toMap ( 'background,cite,href,longdesc,src,xlink:href,xml:base' ) ;
374
+ var uriAttrs = stringToMap ( 'background,cite,href,longdesc,src,xlink:href,xml:base' ) ;
272
375
273
- var htmlAttrs = toMap ( 'abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,' +
376
+ var htmlAttrs = stringToMap ( 'abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,' +
274
377
'color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,' +
275
378
'ismap,lang,language,nohref,nowrap,rel,rev,rows,rowspan,rules,' +
276
379
'scope,scrolling,shape,size,span,start,summary,tabindex,target,title,type,' +
277
380
'valign,value,vspace,width' ) ;
278
381
279
382
// SVG attributes (without "id" and "name" attributes)
280
383
// https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Attributes
281
- var svgAttrs = toMap ( 'accent-height,accumulate,additive,alphabetic,arabic-form,ascent,' +
384
+ var svgAttrs = stringToMap ( 'accent-height,accumulate,additive,alphabetic,arabic-form,ascent,' +
282
385
'baseProfile,bbox,begin,by,calcMode,cap-height,class,color,color-rendering,content,' +
283
386
'cx,cy,d,dx,dy,descent,display,dur,end,fill,fill-rule,font-family,font-size,font-stretch,' +
284
387
'font-style,font-variant,font-weight,from,fx,fy,g1,g2,glyph-name,gradientUnits,hanging,' +
@@ -299,14 +402,24 @@ function $SanitizeProvider() {
299
402
svgAttrs ,
300
403
htmlAttrs ) ;
301
404
302
- function toMap ( str , lowercaseKeys ) {
303
- var obj = { } , items = str . split ( ',' ) , i ;
405
+ function stringToMap ( str , lowercaseKeys ) {
406
+ return arrayToMap ( str . split ( ',' ) , lowercaseKeys ) ;
407
+ }
408
+
409
+ function arrayToMap ( items , lowercaseKeys ) {
410
+ var obj = { } , i ;
304
411
for ( i = 0 ; i < items . length ; i ++ ) {
305
412
obj [ lowercaseKeys ? lowercase ( items [ i ] ) : items [ i ] ] = true ;
306
413
}
307
414
return obj ;
308
415
}
309
416
417
+ function addElementsTo ( elementsMap , newElements ) {
418
+ if ( newElements && newElements . length ) {
419
+ extend ( elementsMap , arrayToMap ( newElements ) ) ;
420
+ }
421
+ }
422
+
310
423
/**
311
424
* Create an inert document that contains the dirty HTML that needs sanitizing
312
425
* Depending upon browser support we use one of three strategies for doing this.
0 commit comments