diff options
| author | Chirayu Krishnappa | 2013-07-16 12:48:29 -0700 | 
|---|---|---|
| committer | Chirayu Krishnappa | 2013-07-18 11:29:50 -0700 | 
| commit | 3e39ac7e1b10d4812a44dad2f959a93361cd823b (patch) | |
| tree | 96185a21871d78862c63e8c1adf16cf18d0f66c7 /src | |
| parent | e449c6df06d92136f9fab95caa29ac2e74b5e58b (diff) | |
| download | angular.js-3e39ac7e1b10d4812a44dad2f959a93361cd823b.tar.bz2 | |
fix($compile): allow data: image URIs in img[src]
Ref: 1adf29af13890d61286840177607edd552a9df97
BREAKING CHANGE: img[src] URLs are now sanitized via a separate
    whitelist regex instead of sharing the whitelist regex with a[href].
    With this change, img[src] URLs may also be data: URI's matching
    mime types image/*.  mailto: URLs are disallowed (and do not make
    sense for img[src] but were allowed under the a[href] whitelist used
    before.)
Diffstat (limited to 'src')
| -rw-r--r-- | src/ng/compile.js | 59 | 
1 files changed, 46 insertions, 13 deletions
diff --git a/src/ng/compile.js b/src/ng/compile.js index b33e830a..7d2b6dc7 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -153,7 +153,8 @@ function $CompileProvider($provide) {        Suffix = 'Directive',        COMMENT_DIRECTIVE_REGEXP = /^\s*directive\:\s*([\d\w\-_]+)\s+(.*)$/,        CLASS_DIRECTIVE_REGEXP = /(([\d\w\-_]+)(?:\:([^;]+))?;?)/, -      urlSanitizationWhitelist = /^\s*(https?|ftp|mailto|file):/; +      aHrefSanitizationWhitelist = /^\s*(https?|ftp|mailto|file):/, +      imgSrcSanitizationWhitelist = /^\s*(https?|ftp|file):|data:image\//;    // Ref: http://developers.whatwg.org/webappapis.html#event-handler-idl-attributes    // The assumption is that future DOM event attribute names will begin with @@ -213,32 +214,61 @@ function $CompileProvider($provide) {    /**     * @ngdoc function -   * @name ng.$compileProvider#urlSanitizationWhitelist +   * @name ng.$compileProvider#aHrefSanitizationWhitelist     * @methodOf ng.$compileProvider     * @function     *     * @description     * Retrieves or overrides the default regular expression that is used for whitelisting of safe -   * urls during a[href] and img[src] sanitization. +   * urls during a[href] sanitization.     *     * The sanitization is a security measure aimed at prevent XSS attacks via html links.     * -   * Any url about to be assigned to a[href] or img[src] via data-binding is first normalized and -   * turned into an absolute url. Afterwards, the url is matched against the -   * `urlSanitizationWhitelist` regular expression. If a match is found, the original url is written -   * into the dom. Otherwise, the absolute url is prefixed with `'unsafe:'` string and only then is -   * it written into the DOM. +   * Any url about to be assigned to a[href] via data-binding is first normalized and turned into +   * an absolute url. Afterwards, the url is matched against the `aHrefSanitizationWhitelist` +   * regular expression. If a match is found, the original url is written into the dom. Otherwise, +   * the absolute url is prefixed with `'unsafe:'` string and only then is it written into the DOM.     *     * @param {RegExp=} regexp New regexp to whitelist urls with.     * @returns {RegExp|ng.$compileProvider} Current RegExp if called without value or self for     *    chaining otherwise.     */ -  this.urlSanitizationWhitelist = function(regexp) { +  this.aHrefSanitizationWhitelist = function(regexp) {      if (isDefined(regexp)) { -      urlSanitizationWhitelist = regexp; +      aHrefSanitizationWhitelist = regexp;        return this;      } -    return urlSanitizationWhitelist; +    return aHrefSanitizationWhitelist; +  }; + + +  /** +   * @ngdoc function +   * @name ng.$compileProvider#imgSrcSanitizationWhitelist +   * @methodOf ng.$compileProvider +   * @function +   * +   * @description +   * Retrieves or overrides the default regular expression that is used for whitelisting of safe +   * urls during img[src] sanitization. +   * +   * The sanitization is a security measure aimed at prevent XSS attacks via html links. +   * +   * Any url about to be assigned to img[src] via data-binding is first normalized and turned into an +   * absolute url. Afterwards, the url is matched against the `imgSrcSanitizationWhitelist` regular +   * expression. If a match is found, the original url is written into the dom. Otherwise, the +   * absolute url is prefixed with `'unsafe:'` string and only then is it written into the DOM. +   * +   * @param {RegExp=} regexp New regexp to whitelist urls with. +   * @returns {RegExp|ng.$compileProvider} Current RegExp if called without value or self for +   *    chaining otherwise. +   */ +  this.imgSrcSanitizationWhitelist = function(regexp) { +    if (isDefined(regexp)) { +      imgSrcSanitizationWhitelist = regexp; +      return this; +    } +    return imgSrcSanitizationWhitelist;    }; @@ -298,8 +328,11 @@ function $CompileProvider($provide) {            // href property always returns normalized absolute url, so we can match against that            normalizedVal = urlSanitizationNode.href; -          if (normalizedVal !== '' && !normalizedVal.match(urlSanitizationWhitelist)) { -            this[key] = value = 'unsafe:' + normalizedVal; +          if (normalizedVal !== '') { +            if ((key === 'href' && !normalizedVal.match(aHrefSanitizationWhitelist)) || +                (key === 'src' && !normalizedVal.match(imgSrcSanitizationWhitelist))) { +              this[key] = value = 'unsafe:' + normalizedVal; +            }            }          }  | 
