/** * @license * Copyright The Closure Library Authors. * SPDX-License-Identifier: Apache-2.0 */ /** * @fileoverview The TrustedResourceUrl type and its builders. * * TODO(xtof): Link to document stating type contract. */ goog.provide('goog.html.TrustedResourceUrl'); goog.require('goog.asserts'); goog.require('goog.fs.blob'); goog.require('goog.fs.url'); goog.require('goog.html.SafeScript'); goog.require('goog.html.trustedtypes'); goog.require('goog.i18n.bidi.Dir'); goog.require('goog.i18n.bidi.DirectionalString'); goog.require('goog.string.Const'); goog.require('goog.string.TypedString'); /** * A URL which is under application control and from which script, CSS, and * other resources that represent executable code, can be fetched. * * Given that the URL can only be constructed from strings under application * control and is used to load resources, bugs resulting in a malformed URL * should not have a security impact and are likely to be easily detectable * during testing. Given the wide number of non-RFC compliant URLs in use, * stricter validation could prevent some applications from being able to use * this type. * * Instances of this type must be created via the factory method, * (`fromConstant`, `fromConstants`, `format` or `formatWithParams`), and not by * invoking its constructor. The constructor intentionally takes an extra * parameter that cannot be constructed outside of this file and the type is * immutable; hence only a default instance corresponding to the empty string * can be obtained via constructor invocation. * * Creating TrustedResourceUrl objects HAS SIDE-EFFECTS due to calling * Trusted Types Web API. * * @see goog.html.TrustedResourceUrl#fromConstant * @final * @struct * @implements {goog.i18n.bidi.DirectionalString} * @implements {goog.string.TypedString} */ goog.html.TrustedResourceUrl = class { /** * @param {!TrustedScriptURL|string} value * @param {!Object} token package-internal implementation detail. */ constructor(value, token) { /** * The contained value of this TrustedResourceUrl. The field has a * purposely ugly name to make (non-compiled) code that attempts to directly * access this field stand out. * @const * @private {!TrustedScriptURL|string} */ this.privateDoNotAccessOrElseTrustedResourceUrlWrappedValue_ = (token === goog.html.TrustedResourceUrl.CONSTRUCTOR_TOKEN_PRIVATE_) ? value : ''; } }; /** * @override * @const */ goog.html.TrustedResourceUrl.prototype.implementsGoogStringTypedString = true; /** * Returns this TrustedResourceUrl's value as a string. * * IMPORTANT: In code where it is security relevant that an object's type is * indeed `TrustedResourceUrl`, use * `goog.html.TrustedResourceUrl.unwrap` instead of this method. If in * doubt, assume that it's security relevant. In particular, note that * goog.html functions which return a goog.html type do not guarantee that * the returned instance is of the right type. For example: * *
 * var fakeSafeHtml = new String('fake');
 * fakeSafeHtml.__proto__ = goog.html.SafeHtml.prototype;
 * var newSafeHtml = goog.html.SafeHtml.htmlEscape(fakeSafeHtml);
 * // newSafeHtml is just an alias for fakeSafeHtml, it's passed through by
 * // goog.html.SafeHtml.htmlEscape() as fakeSafeHtml instanceof
 * // goog.html.SafeHtml.
 * 
* * @see goog.html.TrustedResourceUrl#unwrap * @override */ goog.html.TrustedResourceUrl.prototype.getTypedStringValue = function() { 'use strict'; return this.privateDoNotAccessOrElseTrustedResourceUrlWrappedValue_ .toString(); }; /** * @override * @const */ goog.html.TrustedResourceUrl.prototype.implementsGoogI18nBidiDirectionalString = true; /** * Returns this URLs directionality, which is always `LTR`. * @override * @return {!goog.i18n.bidi.Dir} */ goog.html.TrustedResourceUrl.prototype.getDirection = function() { 'use strict'; return goog.i18n.bidi.Dir.LTR; }; /** * Creates a new TrustedResourceUrl with params added to URL. Both search and * hash params can be specified. * * @param {string|?Object|undefined} searchParams Search parameters * to add to URL. See goog.html.TrustedResourceUrl.stringifyParams_ for * exact format definition. * @param {(string|?Object)=} opt_hashParams Hash parameters to add * to URL. See goog.html.TrustedResourceUrl.stringifyParams_ for exact * format definition. * @return {!goog.html.TrustedResourceUrl} New TrustedResourceUrl with params. */ goog.html.TrustedResourceUrl.prototype.cloneWithParams = function( searchParams, opt_hashParams) { 'use strict'; var url = goog.html.TrustedResourceUrl.unwrap(this); var parts = goog.html.TrustedResourceUrl.URL_PARAM_PARSER_.exec(url); var urlBase = parts[1]; var urlSearch = parts[2] || ''; var urlHash = parts[3] || ''; return goog.html.TrustedResourceUrl .createTrustedResourceUrlSecurityPrivateDoNotAccessOrElse( urlBase + goog.html.TrustedResourceUrl.stringifyParams_( '?', urlSearch, searchParams) + goog.html.TrustedResourceUrl.stringifyParams_( '#', urlHash, opt_hashParams)); }; /** * Returns a string-representation of this value. * * To obtain the actual string value wrapped in a TrustedResourceUrl, use * `goog.html.TrustedResourceUrl.unwrap`. * * @return {string} * @see goog.html.TrustedResourceUrl#unwrap * @override */ goog.html.TrustedResourceUrl.prototype.toString = function() { 'use strict'; return this.privateDoNotAccessOrElseTrustedResourceUrlWrappedValue_ + ''; }; /** * Performs a runtime check that the provided object is indeed a * TrustedResourceUrl object, and returns its value. * * @param {!goog.html.TrustedResourceUrl} trustedResourceUrl The object to * extract from. * @return {string} The trustedResourceUrl object's contained string, unless * the run-time type check fails. In that case, `unwrap` returns an * innocuous string, or, if assertions are enabled, throws * `goog.asserts.AssertionError`. */ goog.html.TrustedResourceUrl.unwrap = function(trustedResourceUrl) { 'use strict'; return goog.html.TrustedResourceUrl.unwrapTrustedScriptURL(trustedResourceUrl) .toString(); }; /** * Unwraps value as TrustedScriptURL if supported or as a string if not. * @param {!goog.html.TrustedResourceUrl} trustedResourceUrl * @return {!TrustedScriptURL|string} * @see goog.html.TrustedResourceUrl.unwrap */ goog.html.TrustedResourceUrl.unwrapTrustedScriptURL = function( trustedResourceUrl) { 'use strict'; // Perform additional Run-time type-checking to ensure that // trustedResourceUrl is indeed an instance of the expected type. This // provides some additional protection against security bugs due to // application code that disables type checks. // Specifically, the following checks are performed: // 1. The object is an instance of the expected type. // 2. The object is not an instance of a subclass. if (trustedResourceUrl instanceof goog.html.TrustedResourceUrl && trustedResourceUrl.constructor === goog.html.TrustedResourceUrl) { return trustedResourceUrl .privateDoNotAccessOrElseTrustedResourceUrlWrappedValue_; } else { goog.asserts.fail('expected object of type TrustedResourceUrl, got \'' + trustedResourceUrl + '\' of type ' + goog.typeOf(trustedResourceUrl)); return 'type_error:TrustedResourceUrl'; } }; /** * Creates a TrustedResourceUrl from a format string and arguments. * * The arguments for interpolation into the format string map labels to values. * Values of type `goog.string.Const` are interpolated without modifcation. * Values of other types are cast to string and encoded with * encodeURIComponent. * * `%{