@@ -15,6 +15,7 @@ var $sanitizeMinErr = angular.$$minErr('$sanitize');
1515var bind ;
1616var extend ;
1717var forEach ;
18+ var isArray ;
1819var isDefined ;
1920var lowercase ;
2021var noop ;
@@ -144,9 +145,11 @@ var htmlSanitizeWriter;
144145 * Creates and configures {@link $sanitize} instance.
145146 */
146147function $SanitizeProvider ( ) {
148+ var hasBeenInstantiated = false ;
147149 var svgEnabled = false ;
148150
149151 this . $get = [ '$$sanitizeUri' , function ( $$sanitizeUri ) {
152+ hasBeenInstantiated = true ;
150153 if ( svgEnabled ) {
151154 extend ( validElements , svgElements ) ;
152155 }
@@ -187,7 +190,7 @@ function $SanitizeProvider() {
187190 * </div>
188191 *
189192 * @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
191194 * without an argument or self for chaining otherwise.
192195 */
193196 this . enableSvg = function ( enableSvg ) {
@@ -199,13 +202,113 @@ function $SanitizeProvider() {
199202 }
200203 } ;
201204
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+
202304 //////////////////////////////////////////////////////////////////////////////////////////////////
203305 // Private stuff
204306 //////////////////////////////////////////////////////////////////////////////////////////////////
205307
206308 bind = angular . bind ;
207309 extend = angular . extend ;
208310 forEach = angular . forEach ;
311+ isArray = angular . isArray ;
209312 isDefined = angular . isDefined ;
210313 lowercase = angular . $$lowercase ;
211314 noop = angular . noop ;
@@ -230,36 +333,36 @@ function $SanitizeProvider() {
230333
231334 // Safe Void Elements - HTML5
232335 // 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' ) ;
234337
235338 // Elements that you can, intentionally, leave open (and which close themselves)
236339 // 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' ) ,
239342 optionalEndTagElements = extend ( { } ,
240343 optionalEndTagInlineElements ,
241344 optionalEndTagBlockElements ) ;
242345
243346 // Safe Block Elements - HTML5
244- var blockElements = extend ( { } , optionalEndTagBlockElements , toMap ( 'address,article,' +
347+ var blockElements = extend ( { } , optionalEndTagBlockElements , stringToMap ( 'address,article,' +
245348 'aside,blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5,' +
246349 'h6,header,hgroup,hr,ins,map,menu,nav,ol,pre,section,table,ul' ) ) ;
247350
248351 // Inline Elements - HTML5
249- var inlineElements = extend ( { } , optionalEndTagInlineElements , toMap ( 'a,abbr,acronym,b,' +
352+ var inlineElements = extend ( { } , optionalEndTagInlineElements , stringToMap ( 'a,abbr,acronym,b,' +
250353 'bdi,bdo,big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s,' +
251354 'samp,small,span,strike,strong,sub,sup,time,tt,u,var' ) ) ;
252355
253356 // SVG Elements
254357 // https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Elements
255358 // Note: the elements animate,animateColor,animateMotion,animateTransform,set are intentionally omitted.
256359 // 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,' +
258361 'hkern,image,linearGradient,line,marker,metadata,missing-glyph,mpath,path,polygon,polyline,' +
259362 'radialGradient,rect,stop,svg,switch,text,title,tspan' ) ;
260363
261364 // Blocked Elements (will be stripped)
262- var blockedElements = toMap ( 'script,style' ) ;
365+ var blockedElements = stringToMap ( 'script,style' ) ;
263366
264367 var validElements = extend ( { } ,
265368 voidElements ,
@@ -268,17 +371,17 @@ function $SanitizeProvider() {
268371 optionalEndTagElements ) ;
269372
270373 //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' ) ;
272375
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,' +
274377 'color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,' +
275378 'ismap,lang,language,nohref,nowrap,rel,rev,rows,rowspan,rules,' +
276379 'scope,scrolling,shape,size,span,start,summary,tabindex,target,title,type,' +
277380 'valign,value,vspace,width' ) ;
278381
279382 // SVG attributes (without "id" and "name" attributes)
280383 // 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,' +
282385 'baseProfile,bbox,begin,by,calcMode,cap-height,class,color,color-rendering,content,' +
283386 'cx,cy,d,dx,dy,descent,display,dur,end,fill,fill-rule,font-family,font-size,font-stretch,' +
284387 'font-style,font-variant,font-weight,from,fx,fy,g1,g2,glyph-name,gradientUnits,hanging,' +
@@ -299,14 +402,24 @@ function $SanitizeProvider() {
299402 svgAttrs ,
300403 htmlAttrs ) ;
301404
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 ;
304411 for ( i = 0 ; i < items . length ; i ++ ) {
305412 obj [ lowercaseKeys ? lowercase ( items [ i ] ) : items [ i ] ] = true ;
306413 }
307414 return obj ;
308415 }
309416
417+ function addElementsTo ( elementsMap , newElements ) {
418+ if ( newElements && newElements . length ) {
419+ extend ( elementsMap , arrayToMap ( newElements ) ) ;
420+ }
421+ }
422+
310423 /**
311424 * Create an inert document that contains the dirty HTML that needs sanitizing
312425 * Depending upon browser support we use one of three strategies for doing this.
0 commit comments