Path: blob/trunk/third_party/closure/goog/html/legacyconversions.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 Transitional utilities to unsafely trust random strings as16* goog.html types. Intended for temporary use when upgrading a library that17* used to accept plain strings to use safe types, but where it's not18* practical to transitively update callers.19*20* IMPORTANT: No new code should use the conversion functions in this file,21* they are intended for refactoring old code to use goog.html types. New code22* should construct goog.html types via their APIs, template systems or23* sanitizers. If that’s not possible it should use24* goog.html.uncheckedconversions and undergo security review.2526*27* The semantics of the conversions in goog.html.legacyconversions are very28* different from the ones provided by goog.html.uncheckedconversions. The29* latter are for use in code where it has been established through manual30* security review that the value produced by a piece of code will always31* satisfy the SafeHtml contract (e.g., the output of a secure HTML sanitizer).32* In uses of goog.html.legacyconversions, this guarantee is not given -- the33* value in question originates in unreviewed legacy code and there is no34* guarantee that it satisfies the SafeHtml contract.35*36* There are only three valid uses of legacyconversions:37*38* 1. Introducing a goog.html version of a function which currently consumes39* string and passes that string to a DOM API which can execute script - and40* hence cause XSS - like innerHTML. For example, Dialog might expose a41* setContent method which takes a string and sets the innerHTML property of42* an element with it. In this case a setSafeHtmlContent function could be43* added, consuming goog.html.SafeHtml instead of string, and using44* goog.dom.safe.setInnerHtml instead of directly setting innerHTML.45* setContent could then internally use legacyconversions to create a SafeHtml46* from string and pass the SafeHtml to setSafeHtmlContent. In this scenario47* remember to document the use of legacyconversions in the modified setContent48* and consider deprecating it as well.49*50* 2. Automated refactoring of application code which handles HTML as string51* but needs to call a function which only takes goog.html types. For example,52* in the Dialog scenario from (1) an alternative option would be to refactor53* setContent to accept goog.html.SafeHtml instead of string and then refactor54* all current callers to use legacyconversions to pass SafeHtml. This is55* generally preferable to (1) because it keeps the library clean of56* legacyconversions, and makes code sites in application code that are57* potentially vulnerable to XSS more apparent.58*59* 3. Old code which needs to call APIs which consume goog.html types and for60* which it is prohibitively expensive to refactor to use goog.html types.61* Generally, this is code where safety from XSS is either hopeless or62* unimportant.63*64* @visibility {//closure/goog/html:approved_for_legacy_conversion}65* @visibility {//closure/goog/bin/sizetests:__pkg__}66*/676869goog.provide('goog.html.legacyconversions');7071goog.require('goog.html.SafeHtml');72goog.require('goog.html.SafeStyle');73goog.require('goog.html.SafeStyleSheet');74goog.require('goog.html.SafeUrl');75goog.require('goog.html.TrustedResourceUrl');767778/**79* Performs an "unchecked conversion" from string to SafeHtml for legacy API80* purposes.81*82* Please read fileoverview documentation before using.83*84* @param {string} html A string to be converted to SafeHtml.85* @return {!goog.html.SafeHtml} The value of html, wrapped in a SafeHtml86* object.87*/88goog.html.legacyconversions.safeHtmlFromString = function(html) {89goog.html.legacyconversions.reportCallback_();90return goog.html.SafeHtml.createSafeHtmlSecurityPrivateDoNotAccessOrElse(91html, null /* dir */);92};939495/**96* Performs an "unchecked conversion" from string to SafeStyle for legacy API97* purposes.98*99* Please read fileoverview documentation before using.100*101* @param {string} style A string to be converted to SafeStyle.102* @return {!goog.html.SafeStyle} The value of style, wrapped in a SafeStyle103* object.104*/105goog.html.legacyconversions.safeStyleFromString = function(style) {106goog.html.legacyconversions.reportCallback_();107return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(108style);109};110111112/**113* Performs an "unchecked conversion" from string to SafeStyleSheet for legacy114* API purposes.115*116* Please read fileoverview documentation before using.117*118* @param {string} styleSheet A string to be converted to SafeStyleSheet.119* @return {!goog.html.SafeStyleSheet} The value of style sheet, wrapped in120* a SafeStyleSheet object.121*/122goog.html.legacyconversions.safeStyleSheetFromString = function(styleSheet) {123goog.html.legacyconversions.reportCallback_();124return goog.html.SafeStyleSheet125.createSafeStyleSheetSecurityPrivateDoNotAccessOrElse(styleSheet);126};127128129/**130* Performs an "unchecked conversion" from string to SafeUrl for legacy API131* purposes.132*133* Please read fileoverview documentation before using.134*135* @param {string} url A string to be converted to SafeUrl.136* @return {!goog.html.SafeUrl} The value of url, wrapped in a SafeUrl137* object.138*/139goog.html.legacyconversions.safeUrlFromString = function(url) {140goog.html.legacyconversions.reportCallback_();141return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(url);142};143144145/**146* Performs an "unchecked conversion" from string to TrustedResourceUrl for147* legacy API purposes.148*149* Please read fileoverview documentation before using.150*151* @param {string} url A string to be converted to TrustedResourceUrl.152* @return {!goog.html.TrustedResourceUrl} The value of url, wrapped in a153* TrustedResourceUrl object.154*/155goog.html.legacyconversions.trustedResourceUrlFromString = function(url) {156goog.html.legacyconversions.reportCallback_();157return goog.html.TrustedResourceUrl158.createTrustedResourceUrlSecurityPrivateDoNotAccessOrElse(url);159};160161/**162* @private {function(): undefined}163*/164goog.html.legacyconversions.reportCallback_ = goog.nullFunction;165166167/**168* Sets a function that will be called every time a legacy conversion is169* performed. The function is called with no parameters but it can use170* goog.debug.getStacktrace to get a stacktrace.171*172* @param {function(): undefined} callback Error callback as defined above.173*/174goog.html.legacyconversions.setReportCallback = function(callback) {175goog.html.legacyconversions.reportCallback_ = callback;176};177178179