diff options
| author | Tobias Bosch | 2013-11-25 15:40:18 -0800 | 
|---|---|---|
| committer | Tobias Bosch | 2013-11-26 14:29:38 -0800 | 
| commit | 333523483f3ce6dd3177b697a5e5a7177ca364c8 (patch) | |
| tree | 34cd700540680a7d9276cd1fe231b6f72fd232ae /src/ng/sanitizeUri.js | |
| parent | 68ceb17272bdd2ebc838565070973d93704f4427 (diff) | |
| download | angular.js-333523483f3ce6dd3177b697a5e5a7177ca364c8.tar.bz2 | |
fix($sanitize): Use same whitelist mechanism as $compile does.
`$sanitize` now uses the same mechanism as `$compile` to validate uris.
By this, the validation in `$sanitize` is more general and can be
configured in the same way as the one in `$compile`.
Changes
- Creates the new private service `$$sanitizeUri`.
- Moves related specs from `compileSpec.js` into `sanitizeUriSpec.js`.
- Refactors the `linky` filter to be less dependent on `$sanitize`
  internal functions.
Fixes #3748.
Diffstat (limited to 'src/ng/sanitizeUri.js')
| -rw-r--r-- | src/ng/sanitizeUri.js | 74 | 
1 files changed, 74 insertions, 0 deletions
| diff --git a/src/ng/sanitizeUri.js b/src/ng/sanitizeUri.js new file mode 100644 index 00000000..97325094 --- /dev/null +++ b/src/ng/sanitizeUri.js @@ -0,0 +1,74 @@ +'use strict'; + +/** + * @description + * Private service to sanitize uris for links and images. Used by $compile and $sanitize. + */ +function $$SanitizeUriProvider() { +  var aHrefSanitizationWhitelist = /^\s*(https?|ftp|mailto|tel|file):/, +    imgSrcSanitizationWhitelist = /^\s*(https?|ftp|file):|data:image\//; + +  /** +   * @description +   * Retrieves or overrides the default regular expression that is used for whitelisting of safe +   * 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] 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.aHrefSanitizationWhitelist = function(regexp) { +    if (isDefined(regexp)) { +      aHrefSanitizationWhitelist = regexp; +      return this; +    } +    return aHrefSanitizationWhitelist; +  }; + + +  /** +   * @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; +  }; + +  this.$get = function() { +    return function sanitizeUri(uri, isImage) { +      var regex = isImage ? imgSrcSanitizationWhitelist : aHrefSanitizationWhitelist; +      var normalizedVal; +      // NOTE: urlResolve() doesn't support IE < 8 so we don't sanitize for that case. +      if (!msie || msie >= 8 ) { +        normalizedVal = urlResolve(uri).href; +        if (normalizedVal !== '' && !normalizedVal.match(regex)) { +          return 'unsafe:'+normalizedVal; +        } +      } +      return uri; +    }; +  }; +} | 
