Path: blob/trunk/third_party/closure/goog/html/uncheckedconversions.js
4122 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Unchecked conversions to create values of goog.html types from8* plain strings. Use of these functions could potentially result in instances9* of goog.html types that violate their type contracts, and hence result in10* security vulnerabilties.11*12* Therefore, all uses of the methods herein must be carefully security13* reviewed. Avoid use of the methods in this file whenever possible; instead14* prefer to create instances of goog.html types using inherently safe builders15* or template systems.16*17*18*/192021goog.provide('goog.html.uncheckedconversions');2223goog.require('goog.asserts');24goog.require('goog.html.SafeHtml');25goog.require('goog.html.SafeScript');26goog.require('goog.html.SafeStyle');27goog.require('goog.html.SafeStyleSheet');28goog.require('goog.html.SafeUrl');29goog.require('goog.html.TrustedResourceUrl');30goog.require('goog.string.Const');31goog.require('goog.string.internal');323334/**35* Performs an "unchecked conversion" to SafeHtml from a plain string that is36* known to satisfy the SafeHtml type contract.37*38* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure39* that the value of `html` satisfies the SafeHtml type contract in all40* possible program states.41*42*43* @param {!goog.string.Const} justification A constant string explaining why44* this use of this method is safe. May include a security review ticket45* number.46* @param {string} html A string that is claimed to adhere to the SafeHtml47* contract.48* @return {!goog.html.SafeHtml} The value of html, wrapped in a SafeHtml49* object.50*/51goog.html.uncheckedconversions.safeHtmlFromStringKnownToSatisfyTypeContract =52function(justification, html) {53'use strict';54// unwrap() called inside an assert so that justification can be optimized55// away in production code.56goog.asserts.assertString(57goog.string.Const.unwrap(justification), 'must provide justification');58goog.asserts.assert(59!goog.string.internal.isEmptyOrWhitespace(60goog.string.Const.unwrap(justification)),61'must provide non-empty justification');62return goog.html.SafeHtml.createSafeHtmlSecurityPrivateDoNotAccessOrElse(63html);64};656667/**68* Performs an "unchecked conversion" to SafeScript from a plain string that is69* known to satisfy the SafeScript type contract.70*71* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure72* that the value of `script` satisfies the SafeScript type contract in73* all possible program states.74*75*76* @param {!goog.string.Const} justification A constant string explaining why77* this use of this method is safe. May include a security review ticket78* number.79* @param {string} script The string to wrap as a SafeScript.80* @return {!goog.html.SafeScript} The value of `script`, wrapped in a81* SafeScript object.82*/83goog.html.uncheckedconversions.safeScriptFromStringKnownToSatisfyTypeContract =84function(justification, script) {85'use strict';86// unwrap() called inside an assert so that justification can be optimized87// away in production code.88goog.asserts.assertString(89goog.string.Const.unwrap(justification), 'must provide justification');90goog.asserts.assert(91!goog.string.internal.isEmptyOrWhitespace(92goog.string.Const.unwrap(justification)),93'must provide non-empty justification');94return goog.html.SafeScript.createSafeScriptSecurityPrivateDoNotAccessOrElse(95script);96};979899/**100* Performs an "unchecked conversion" to SafeStyle from a plain string that is101* known to satisfy the SafeStyle type contract.102*103* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure104* that the value of `style` satisfies the SafeStyle type contract in all105* possible program states.106*107*108* @param {!goog.string.Const} justification A constant string explaining why109* this use of this method is safe. May include a security review ticket110* number.111* @param {string} style The string to wrap as a SafeStyle.112* @return {!goog.html.SafeStyle} The value of `style`, wrapped in a113* SafeStyle object.114*/115goog.html.uncheckedconversions.safeStyleFromStringKnownToSatisfyTypeContract =116function(justification, style) {117'use strict';118// unwrap() called inside an assert so that justification can be optimized119// away in production code.120goog.asserts.assertString(121goog.string.Const.unwrap(justification), 'must provide justification');122goog.asserts.assert(123!goog.string.internal.isEmptyOrWhitespace(124goog.string.Const.unwrap(justification)),125'must provide non-empty justification');126return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(127style);128};129130131/**132* Performs an "unchecked conversion" to SafeStyleSheet from a plain string133* that is known to satisfy the SafeStyleSheet type contract.134*135* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure136* that the value of `styleSheet` satisfies the SafeStyleSheet type137* contract in all possible program states.138*139*140* @param {!goog.string.Const} justification A constant string explaining why141* this use of this method is safe. May include a security review ticket142* number.143* @param {string} styleSheet The string to wrap as a SafeStyleSheet.144* @return {!goog.html.SafeStyleSheet} The value of `styleSheet`, wrapped145* in a SafeStyleSheet object.146*/147goog.html.uncheckedconversions148.safeStyleSheetFromStringKnownToSatisfyTypeContract = function(149justification, styleSheet) {150'use strict';151// unwrap() called inside an assert so that justification can be optimized152// away in production code.153goog.asserts.assertString(154goog.string.Const.unwrap(justification), 'must provide justification');155goog.asserts.assert(156!goog.string.internal.isEmptyOrWhitespace(157goog.string.Const.unwrap(justification)),158'must provide non-empty justification');159return goog.html.SafeStyleSheet160.createSafeStyleSheetSecurityPrivateDoNotAccessOrElse(styleSheet);161};162163164/**165* Performs an "unchecked conversion" to SafeUrl from a plain string that is166* known to satisfy the SafeUrl type contract.167*168* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure169* that the value of `url` satisfies the SafeUrl type contract in all170* possible program states.171*172*173* @param {!goog.string.Const} justification A constant string explaining why174* this use of this method is safe. May include a security review ticket175* number.176* @param {string} url The string to wrap as a SafeUrl.177* @return {!goog.html.SafeUrl} The value of `url`, wrapped in a SafeUrl178* object.179*/180goog.html.uncheckedconversions.safeUrlFromStringKnownToSatisfyTypeContract =181function(justification, url) {182'use strict';183// unwrap() called inside an assert so that justification can be optimized184// away in production code.185goog.asserts.assertString(186goog.string.Const.unwrap(justification), 'must provide justification');187goog.asserts.assert(188!goog.string.internal.isEmptyOrWhitespace(189goog.string.Const.unwrap(justification)),190'must provide non-empty justification');191return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(url);192};193194195/**196* Performs an "unchecked conversion" to TrustedResourceUrl from a plain string197* that is known to satisfy the TrustedResourceUrl type contract.198*199* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure200* that the value of `url` satisfies the TrustedResourceUrl type contract201* in all possible program states.202*203*204* @param {!goog.string.Const} justification A constant string explaining why205* this use of this method is safe. May include a security review ticket206* number.207* @param {string} url The string to wrap as a TrustedResourceUrl.208* @return {!goog.html.TrustedResourceUrl} The value of `url`, wrapped in209* a TrustedResourceUrl object.210*/211goog.html.uncheckedconversions212.trustedResourceUrlFromStringKnownToSatisfyTypeContract = function(213justification, url) {214'use strict';215// unwrap() called inside an assert so that justification can be optimized216// away in production code.217goog.asserts.assertString(218goog.string.Const.unwrap(justification), 'must provide justification');219goog.asserts.assert(220!goog.string.internal.isEmptyOrWhitespace(221goog.string.Const.unwrap(justification)),222'must provide non-empty justification');223return goog.html.TrustedResourceUrl224.createTrustedResourceUrlSecurityPrivateDoNotAccessOrElse(url);225};226227228