@@ -153,7 +153,8 @@ function $CompileProvider($provide) {
153
153
Suffix = 'Directive' ,
154
154
COMMENT_DIRECTIVE_REGEXP = / ^ \s * d i r e c t i v e \: \s * ( [ \d \w \- _ ] + ) \s + ( .* ) $ / ,
155
155
CLASS_DIRECTIVE_REGEXP = / ( ( [ \d \w \- _ ] + ) (?: \: ( [ ^ ; ] + ) ) ? ; ? ) / ,
156
- urlSanitizationWhitelist = / ^ \s * ( h t t p s ? | f t p | m a i l t o | f i l e ) : / ;
156
+ aHrefSanitizationWhitelist = / ^ \s * ( h t t p s ? | f t p | m a i l t o | f i l e ) : / ,
157
+ imgSrcSanitizationWhitelist = / ^ \s * ( h t t p s ? | f t p | f i l e ) : | d a t a : i m a g e \/ / ;
157
158
158
159
// Ref: http://developers.whatwg.org/webappapis.html#event-handler-idl-attributes
159
160
// The assumption is that future DOM event attribute names will begin with
@@ -213,32 +214,61 @@ function $CompileProvider($provide) {
213
214
214
215
/**
215
216
* @ngdoc function
216
- * @name ng.$compileProvider#urlSanitizationWhitelist
217
+ * @name ng.$compileProvider#aHrefSanitizationWhitelist
217
218
* @methodOf ng.$compileProvider
218
219
* @function
219
220
*
220
221
* @description
221
222
* Retrieves or overrides the default regular expression that is used for whitelisting of safe
222
- * urls during a[href] and img[src] sanitization.
223
+ * urls during a[href] sanitization.
223
224
*
224
225
* The sanitization is a security measure aimed at prevent XSS attacks via html links.
225
226
*
226
- * Any url about to be assigned to a[href] or img[src] via data-binding is first normalized and
227
- * turned into an absolute url. Afterwards, the url is matched against the
228
- * `urlSanitizationWhitelist` regular expression. If a match is found, the original url is written
229
- * into the dom. Otherwise, the absolute url is prefixed with `'unsafe:'` string and only then is
230
- * it written into the DOM.
227
+ * Any url about to be assigned to a[href] via data-binding is first normalized and turned into
228
+ * an absolute url. Afterwards, the url is matched against the `aHrefSanitizationWhitelist`
229
+ * regular expression. If a match is found, the original url is written into the dom. Otherwise,
230
+ * the absolute url is prefixed with `'unsafe:'` string and only then is it written into the DOM.
231
231
*
232
232
* @param {RegExp= } regexp New regexp to whitelist urls with.
233
233
* @returns {RegExp|ng.$compileProvider } Current RegExp if called without value or self for
234
234
* chaining otherwise.
235
235
*/
236
- this . urlSanitizationWhitelist = function ( regexp ) {
236
+ this . aHrefSanitizationWhitelist = function ( regexp ) {
237
237
if ( isDefined ( regexp ) ) {
238
- urlSanitizationWhitelist = regexp ;
238
+ aHrefSanitizationWhitelist = regexp ;
239
239
return this ;
240
240
}
241
- return urlSanitizationWhitelist ;
241
+ return aHrefSanitizationWhitelist ;
242
+ } ;
243
+
244
+
245
+ /**
246
+ * @ngdoc function
247
+ * @name ng.$compileProvider#imgSrcSanitizationWhitelist
248
+ * @methodOf ng.$compileProvider
249
+ * @function
250
+ *
251
+ * @description
252
+ * Retrieves or overrides the default regular expression that is used for whitelisting of safe
253
+ * urls during img[src] sanitization.
254
+ *
255
+ * The sanitization is a security measure aimed at prevent XSS attacks via html links.
256
+ *
257
+ * Any url about to be assigned to img[src] via data-binding is first normalized and turned into an
258
+ * absolute url. Afterwards, the url is matched against the `imgSrcSanitizationWhitelist` regular
259
+ * expression. If a match is found, the original url is written into the dom. Otherwise, the
260
+ * absolute url is prefixed with `'unsafe:'` string and only then is it written into the DOM.
261
+ *
262
+ * @param {RegExp= } regexp New regexp to whitelist urls with.
263
+ * @returns {RegExp|ng.$compileProvider } Current RegExp if called without value or self for
264
+ * chaining otherwise.
265
+ */
266
+ this . imgSrcSanitizationWhitelist = function ( regexp ) {
267
+ if ( isDefined ( regexp ) ) {
268
+ imgSrcSanitizationWhitelist = regexp ;
269
+ return this ;
270
+ }
271
+ return imgSrcSanitizationWhitelist ;
242
272
} ;
243
273
244
274
@@ -298,8 +328,11 @@ function $CompileProvider($provide) {
298
328
299
329
// href property always returns normalized absolute url, so we can match against that
300
330
normalizedVal = urlSanitizationNode . href ;
301
- if ( normalizedVal !== '' && ! normalizedVal . match ( urlSanitizationWhitelist ) ) {
302
- this [ key ] = value = 'unsafe:' + normalizedVal ;
331
+ if ( normalizedVal !== '' ) {
332
+ if ( ( key === 'href' && ! normalizedVal . match ( aHrefSanitizationWhitelist ) ) ||
333
+ ( key === 'src' && ! normalizedVal . match ( imgSrcSanitizationWhitelist ) ) ) {
334
+ this [ key ] = value = 'unsafe:' + normalizedVal ;
335
+ }
303
336
}
304
337
}
305
338
0 commit comments