Path: blob/trunk/third_party/closure/goog/html/safeurl.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview The SafeUrl type and its builders.16*17* TODO(xtof): Link to document stating type contract.18*/1920goog.provide('goog.html.SafeUrl');2122goog.require('goog.asserts');23goog.require('goog.fs.url');24goog.require('goog.html.TrustedResourceUrl');25goog.require('goog.i18n.bidi.Dir');26goog.require('goog.i18n.bidi.DirectionalString');27goog.require('goog.string');28goog.require('goog.string.Const');29goog.require('goog.string.TypedString');30313233/**34* A string that is safe to use in URL context in DOM APIs and HTML documents.35*36* A SafeUrl is a string-like object that carries the security type contract37* that its value as a string will not cause untrusted script execution38* when evaluated as a hyperlink URL in a browser.39*40* Values of this type are guaranteed to be safe to use in URL/hyperlink41* contexts, such as assignment to URL-valued DOM properties, in the sense that42* the use will not result in a Cross-Site-Scripting vulnerability. Similarly,43* SafeUrls can be interpolated into the URL context of an HTML template (e.g.,44* inside a href attribute). However, appropriate HTML-escaping must still be45* applied.46*47* Note that, as documented in {@code goog.html.SafeUrl.unwrap}, this type's48* contract does not guarantee that instances are safe to interpolate into HTML49* without appropriate escaping.50*51* Note also that this type's contract does not imply any guarantees regarding52* the resource the URL refers to. In particular, SafeUrls are <b>not</b>53* safe to use in a context where the referred-to resource is interpreted as54* trusted code, e.g., as the src of a script tag.55*56* Instances of this type must be created via the factory methods57* ({@code goog.html.SafeUrl.fromConstant}, {@code goog.html.SafeUrl.sanitize}),58* etc and not by invoking its constructor. The constructor intentionally59* takes no parameters and the type is immutable; hence only a default instance60* corresponding to the empty string can be obtained via constructor invocation.61*62* @see goog.html.SafeUrl#fromConstant63* @see goog.html.SafeUrl#from64* @see goog.html.SafeUrl#sanitize65* @constructor66* @final67* @struct68* @implements {goog.i18n.bidi.DirectionalString}69* @implements {goog.string.TypedString}70*/71goog.html.SafeUrl = function() {72/**73* The contained value of this SafeUrl. The field has a purposely ugly74* name to make (non-compiled) code that attempts to directly access this75* field stand out.76* @private {string}77*/78this.privateDoNotAccessOrElseSafeHtmlWrappedValue_ = '';7980/**81* A type marker used to implement additional run-time type checking.82* @see goog.html.SafeUrl#unwrap83* @const {!Object}84* @private85*/86this.SAFE_URL_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ =87goog.html.SafeUrl.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_;88};899091/**92* The innocuous string generated by goog.html.SafeUrl.sanitize when passed93* an unsafe URL.94*95* about:invalid is registered in96* http://www.w3.org/TR/css3-values/#about-invalid.97* http://tools.ietf.org/html/rfc6694#section-2.2.1 permits about URLs to98* contain a fragment, which is not to be considered when determining if an99* about URL is well-known.100*101* Using about:invalid seems preferable to using a fixed data URL, since102* browsers might choose to not report CSP violations on it, as legitimate103* CSS function calls to attr() can result in this URL being produced. It is104* also a standard URL which matches exactly the semantics we need:105* "The about:invalid URI references a non-existent document with a generic106* error condition. It can be used when a URI is necessary, but the default107* value shouldn't be resolveable as any type of document".108*109* @const {string}110*/111goog.html.SafeUrl.INNOCUOUS_STRING = 'about:invalid#zClosurez';112113114/**115* @override116* @const117*/118goog.html.SafeUrl.prototype.implementsGoogStringTypedString = true;119120121/**122* Returns this SafeUrl's value a string.123*124* IMPORTANT: In code where it is security relevant that an object's type is125* indeed {@code SafeUrl}, use {@code goog.html.SafeUrl.unwrap} instead of this126* method. If in doubt, assume that it's security relevant. In particular, note127* that goog.html functions which return a goog.html type do not guarantee that128* the returned instance is of the right type. For example:129*130* <pre>131* var fakeSafeHtml = new String('fake');132* fakeSafeHtml.__proto__ = goog.html.SafeHtml.prototype;133* var newSafeHtml = goog.html.SafeHtml.htmlEscape(fakeSafeHtml);134* // newSafeHtml is just an alias for fakeSafeHtml, it's passed through by135* // goog.html.SafeHtml.htmlEscape() as fakeSafeHtml instanceof136* // goog.html.SafeHtml.137* </pre>138*139* IMPORTANT: The guarantees of the SafeUrl type contract only extend to the140* behavior of browsers when interpreting URLs. Values of SafeUrl objects MUST141* be appropriately escaped before embedding in a HTML document. Note that the142* required escaping is context-sensitive (e.g. a different escaping is143* required for embedding a URL in a style property within a style144* attribute, as opposed to embedding in a href attribute).145*146* @see goog.html.SafeUrl#unwrap147* @override148*/149goog.html.SafeUrl.prototype.getTypedStringValue = function() {150return this.privateDoNotAccessOrElseSafeHtmlWrappedValue_;151};152153154/**155* @override156* @const157*/158goog.html.SafeUrl.prototype.implementsGoogI18nBidiDirectionalString = true;159160161/**162* Returns this URLs directionality, which is always {@code LTR}.163* @override164*/165goog.html.SafeUrl.prototype.getDirection = function() {166return goog.i18n.bidi.Dir.LTR;167};168169170if (goog.DEBUG) {171/**172* Returns a debug string-representation of this value.173*174* To obtain the actual string value wrapped in a SafeUrl, use175* {@code goog.html.SafeUrl.unwrap}.176*177* @see goog.html.SafeUrl#unwrap178* @override179*/180goog.html.SafeUrl.prototype.toString = function() {181return 'SafeUrl{' + this.privateDoNotAccessOrElseSafeHtmlWrappedValue_ +182'}';183};184}185186187/**188* Performs a runtime check that the provided object is indeed a SafeUrl189* object, and returns its value.190*191* IMPORTANT: The guarantees of the SafeUrl type contract only extend to the192* behavior of browsers when interpreting URLs. Values of SafeUrl objects MUST193* be appropriately escaped before embedding in a HTML document. Note that the194* required escaping is context-sensitive (e.g. a different escaping is195* required for embedding a URL in a style property within a style196* attribute, as opposed to embedding in a href attribute).197*198* @param {!goog.html.SafeUrl} safeUrl The object to extract from.199* @return {string} The SafeUrl object's contained string, unless the run-time200* type check fails. In that case, {@code unwrap} returns an innocuous201* string, or, if assertions are enabled, throws202* {@code goog.asserts.AssertionError}.203*/204goog.html.SafeUrl.unwrap = function(safeUrl) {205// Perform additional Run-time type-checking to ensure that safeUrl is indeed206// an instance of the expected type. This provides some additional protection207// against security bugs due to application code that disables type checks.208// Specifically, the following checks are performed:209// 1. The object is an instance of the expected type.210// 2. The object is not an instance of a subclass.211// 3. The object carries a type marker for the expected type. "Faking" an212// object requires a reference to the type marker, which has names intended213// to stand out in code reviews.214if (safeUrl instanceof goog.html.SafeUrl &&215safeUrl.constructor === goog.html.SafeUrl &&216safeUrl.SAFE_URL_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ ===217goog.html.SafeUrl.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_) {218return safeUrl.privateDoNotAccessOrElseSafeHtmlWrappedValue_;219} else {220goog.asserts.fail('expected object of type SafeUrl, got \'' +221safeUrl + '\' of type ' + goog.typeOf(safeUrl));222return 'type_error:SafeUrl';223}224};225226227/**228* Creates a SafeUrl object from a compile-time constant string.229*230* Compile-time constant strings are inherently program-controlled and hence231* trusted.232*233* @param {!goog.string.Const} url A compile-time-constant string from which to234* create a SafeUrl.235* @return {!goog.html.SafeUrl} A SafeUrl object initialized to {@code url}.236*/237goog.html.SafeUrl.fromConstant = function(url) {238return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(239goog.string.Const.unwrap(url));240};241242243/**244* A pattern that matches Blob or data types that can have SafeUrls created245* from URL.createObjectURL(blob) or via a data: URI. Only matches image and246* video types, currently.247* @const248* @private249*/250goog.html.SAFE_MIME_TYPE_PATTERN_ =251/^(?:image\/(?:bmp|gif|jpeg|jpg|png|tiff|webp)|video\/(?:mpeg|mp4|ogg|webm))$/i;252253254/**255* Creates a SafeUrl wrapping a blob URL for the given {@code blob}.256*257* The blob URL is created with {@code URL.createObjectURL}. If the MIME type258* for {@code blob} is not of a known safe image or video MIME type, then the259* SafeUrl will wrap {@link #INNOCUOUS_STRING}.260*261* @see http://www.w3.org/TR/FileAPI/#url262* @param {!Blob} blob263* @return {!goog.html.SafeUrl} The blob URL, or an innocuous string wrapped264* as a SafeUrl.265*/266goog.html.SafeUrl.fromBlob = function(blob) {267var url = goog.html.SAFE_MIME_TYPE_PATTERN_.test(blob.type) ?268goog.fs.url.createObjectUrl(blob) :269goog.html.SafeUrl.INNOCUOUS_STRING;270return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(url);271};272273274/**275* Matches a base-64 data URL, with the first match group being the MIME type.276* @const277* @private278*/279goog.html.DATA_URL_PATTERN_ = /^data:([^;,]*);base64,[a-z0-9+\/]+=*$/i;280281282/**283* Creates a SafeUrl wrapping a data: URL, after validating it matches a284* known-safe image or video MIME type.285*286* @param {string} dataUrl A valid base64 data URL with one of the whitelisted287* image or video MIME types.288* @return {!goog.html.SafeUrl} A matching safe URL, or {@link INNOCUOUS_STRING}289* wrapped as a SafeUrl if it does not pass.290*/291goog.html.SafeUrl.fromDataUrl = function(dataUrl) {292// There's a slight risk here that a browser sniffs the content type if it293// doesn't know the MIME type and executes HTML within the data: URL. For this294// to cause XSS it would also have to execute the HTML in the same origin295// of the page with the link. It seems unlikely that both of these will296// happen, particularly in not really old IEs.297var match = dataUrl.match(goog.html.DATA_URL_PATTERN_);298var valid = match && goog.html.SAFE_MIME_TYPE_PATTERN_.test(match[1]);299return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(300valid ? dataUrl : goog.html.SafeUrl.INNOCUOUS_STRING);301};302303304/**305* Creates a SafeUrl wrapping a tel: URL.306*307* @param {string} telUrl A tel URL.308* @return {!goog.html.SafeUrl} A matching safe URL, or {@link INNOCUOUS_STRING}309* wrapped as a SafeUrl if it does not pass.310*/311goog.html.SafeUrl.fromTelUrl = function(telUrl) {312// There's a risk that a tel: URL could immediately place a call once313// clicked, without requiring user confirmation. For that reason it is314// handled in this separate function.315if (!goog.string.caseInsensitiveStartsWith(telUrl, 'tel:')) {316telUrl = goog.html.SafeUrl.INNOCUOUS_STRING;317}318return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(319telUrl);320};321322323/**324* Creates a SafeUrl from TrustedResourceUrl. This is safe because325* TrustedResourceUrl is more tightly restricted than SafeUrl.326*327* @param {!goog.html.TrustedResourceUrl} trustedResourceUrl328* @return {!goog.html.SafeUrl}329*/330goog.html.SafeUrl.fromTrustedResourceUrl = function(trustedResourceUrl) {331return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(332goog.html.TrustedResourceUrl.unwrap(trustedResourceUrl));333};334335336/**337* A pattern that recognizes a commonly useful subset of URLs that satisfy338* the SafeUrl contract.339*340* This regular expression matches a subset of URLs that will not cause script341* execution if used in URL context within a HTML document. Specifically, this342* regular expression matches if (comment from here on and regex copied from343* Soy's EscapingConventions):344* (1) Either a protocol in a whitelist (http, https, mailto or ftp).345* (2) or no protocol. A protocol must be followed by a colon. The below346* allows that by allowing colons only after one of the characters [/?#].347* A colon after a hash (#) must be in the fragment.348* Otherwise, a colon after a (?) must be in a query.349* Otherwise, a colon after a single solidus (/) must be in a path.350* Otherwise, a colon after a double solidus (//) must be in the authority351* (before port).352*353* @private354* @const {!RegExp}355*/356goog.html.SAFE_URL_PATTERN_ =357/^(?:(?:https?|mailto|ftp):|[^:/?#]*(?:[/?#]|$))/i;358359360/**361* Creates a SafeUrl object from {@code url}. If {@code url} is a362* goog.html.SafeUrl then it is simply returned. Otherwise the input string is363* validated to match a pattern of commonly used safe URLs.364*365* {@code url} may be a URL with the http, https, mailto or ftp scheme,366* or a relative URL (i.e., a URL without a scheme; specifically, a367* scheme-relative, absolute-path-relative, or path-relative URL).368*369* @see http://url.spec.whatwg.org/#concept-relative-url370* @param {string|!goog.string.TypedString} url The URL to validate.371* @return {!goog.html.SafeUrl} The validated URL, wrapped as a SafeUrl.372*/373goog.html.SafeUrl.sanitize = function(url) {374if (url instanceof goog.html.SafeUrl) {375return url;376} else if (url.implementsGoogStringTypedString) {377url = url.getTypedStringValue();378} else {379url = String(url);380}381if (!goog.html.SAFE_URL_PATTERN_.test(url)) {382url = goog.html.SafeUrl.INNOCUOUS_STRING;383}384return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(url);385};386387388/**389* Type marker for the SafeUrl type, used to implement additional run-time390* type checking.391* @const {!Object}392* @private393*/394goog.html.SafeUrl.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ = {};395396397/**398* Package-internal utility method to create SafeUrl instances.399*400* @param {string} url The string to initialize the SafeUrl object with.401* @return {!goog.html.SafeUrl} The initialized SafeUrl object.402* @package403*/404goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse = function(405url) {406var safeUrl = new goog.html.SafeUrl();407safeUrl.privateDoNotAccessOrElseSafeHtmlWrappedValue_ = url;408return safeUrl;409};410411412/**413* A SafeUrl corresponding to the special about:blank url.414* @const {!goog.html.SafeUrl}415*/416goog.html.SafeUrl.ABOUT_BLANK =417goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(418'about:blank');419420421