Path: blob/trunk/third_party/closure/goog/html/safescript.js
2868 views
// Copyright 2014 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 SafeScript type and its builders.16*17* TODO(xtof): Link to document stating type contract.18*/1920goog.provide('goog.html.SafeScript');2122goog.require('goog.asserts');23goog.require('goog.string.Const');24goog.require('goog.string.TypedString');25262728/**29* A string-like object which represents JavaScript code and that carries the30* security type contract that its value, as a string, will not cause execution31* of unconstrained attacker controlled code (XSS) when evaluated as JavaScript32* in a browser.33*34* Instances of this type must be created via the factory method35* {@code goog.html.SafeScript.fromConstant} and not by invoking its36* constructor. The constructor intentionally takes no parameters and the type37* is immutable; hence only a default instance corresponding to the empty string38* can be obtained via constructor invocation.39*40* A SafeScript's string representation can safely be interpolated as the41* content of a script element within HTML. The SafeScript string should not be42* escaped before interpolation.43*44* Note that the SafeScript might contain text that is attacker-controlled but45* that text should have been interpolated with appropriate escaping,46* sanitization and/or validation into the right location in the script, such47* that it is highly constrained in its effect (for example, it had to match a48* set of whitelisted words).49*50* A SafeScript can be constructed via security-reviewed unchecked51* conversions. In this case producers of SafeScript must ensure themselves that52* the SafeScript does not contain unsafe script. Note in particular that53* {@code <} is dangerous, even when inside JavaScript strings, and so should54* always be forbidden or JavaScript escaped in user controlled input. For55* example, if {@code </script><script>evil</script>"} were56* interpolated inside a JavaScript string, it would break out of the context57* of the original script element and {@code evil} would execute. Also note58* that within an HTML script (raw text) element, HTML character references,59* such as "<" are not allowed. See60* http://www.w3.org/TR/html5/scripting-1.html#restrictions-for-contents-of-script-elements.61*62* @see goog.html.SafeScript#fromConstant63* @constructor64* @final65* @struct66* @implements {goog.string.TypedString}67*/68goog.html.SafeScript = function() {69/**70* The contained value of this SafeScript. The field has a purposely71* ugly name to make (non-compiled) code that attempts to directly access this72* field stand out.73* @private {string}74*/75this.privateDoNotAccessOrElseSafeScriptWrappedValue_ = '';7677/**78* A type marker used to implement additional run-time type checking.79* @see goog.html.SafeScript#unwrap80* @const {!Object}81* @private82*/83this.SAFE_SCRIPT_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ =84goog.html.SafeScript.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_;85};868788/**89* @override90* @const91*/92goog.html.SafeScript.prototype.implementsGoogStringTypedString = true;939495/**96* Type marker for the SafeScript type, used to implement additional97* run-time type checking.98* @const {!Object}99* @private100*/101goog.html.SafeScript.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ = {};102103104/**105* Creates a SafeScript object from a compile-time constant string.106*107* @param {!goog.string.Const} script A compile-time-constant string from which108* to create a SafeScript.109* @return {!goog.html.SafeScript} A SafeScript object initialized to110* {@code script}.111*/112goog.html.SafeScript.fromConstant = function(script) {113var scriptString = goog.string.Const.unwrap(script);114if (scriptString.length === 0) {115return goog.html.SafeScript.EMPTY;116}117return goog.html.SafeScript.createSafeScriptSecurityPrivateDoNotAccessOrElse(118scriptString);119};120121122/**123* Returns this SafeScript's value as a string.124*125* IMPORTANT: In code where it is security relevant that an object's type is126* indeed {@code SafeScript}, use {@code goog.html.SafeScript.unwrap} instead of127* this method. If in doubt, assume that it's security relevant. In particular,128* note that goog.html functions which return a goog.html type do not guarantee129* the returned instance is of the right type. For example:130*131* <pre>132* var fakeSafeHtml = new String('fake');133* fakeSafeHtml.__proto__ = goog.html.SafeHtml.prototype;134* var newSafeHtml = goog.html.SafeHtml.htmlEscape(fakeSafeHtml);135* // newSafeHtml is just an alias for fakeSafeHtml, it's passed through by136* // goog.html.SafeHtml.htmlEscape() as fakeSafeHtml137* // instanceof goog.html.SafeHtml.138* </pre>139*140* @see goog.html.SafeScript#unwrap141* @override142*/143goog.html.SafeScript.prototype.getTypedStringValue = function() {144return this.privateDoNotAccessOrElseSafeScriptWrappedValue_;145};146147148if (goog.DEBUG) {149/**150* Returns a debug string-representation of this value.151*152* To obtain the actual string value wrapped in a SafeScript, use153* {@code goog.html.SafeScript.unwrap}.154*155* @see goog.html.SafeScript#unwrap156* @override157*/158goog.html.SafeScript.prototype.toString = function() {159return 'SafeScript{' +160this.privateDoNotAccessOrElseSafeScriptWrappedValue_ + '}';161};162}163164165/**166* Performs a runtime check that the provided object is indeed a167* SafeScript object, and returns its value.168*169* @param {!goog.html.SafeScript} safeScript The object to extract from.170* @return {string} The safeScript object's contained string, unless171* the run-time type check fails. In that case, {@code unwrap} returns an172* innocuous string, or, if assertions are enabled, throws173* {@code goog.asserts.AssertionError}.174*/175goog.html.SafeScript.unwrap = function(safeScript) {176// Perform additional Run-time type-checking to ensure that177// safeScript is indeed an instance of the expected type. This178// provides some additional protection against security bugs due to179// application code that disables type checks.180// Specifically, the following checks are performed:181// 1. The object is an instance of the expected type.182// 2. The object is not an instance of a subclass.183// 3. The object carries a type marker for the expected type. "Faking" an184// object requires a reference to the type marker, which has names intended185// to stand out in code reviews.186if (safeScript instanceof goog.html.SafeScript &&187safeScript.constructor === goog.html.SafeScript &&188safeScript.SAFE_SCRIPT_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_ ===189goog.html.SafeScript.TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_) {190return safeScript.privateDoNotAccessOrElseSafeScriptWrappedValue_;191} else {192goog.asserts.fail('expected object of type SafeScript, got \'' +193safeScript + '\' of type ' + goog.typeOf(safeScript));194return 'type_error:SafeScript';195}196};197198199/**200* Package-internal utility method to create SafeScript instances.201*202* @param {string} script The string to initialize the SafeScript object with.203* @return {!goog.html.SafeScript} The initialized SafeScript object.204* @package205*/206goog.html.SafeScript.createSafeScriptSecurityPrivateDoNotAccessOrElse =207function(script) {208return new goog.html.SafeScript().initSecurityPrivateDoNotAccessOrElse_(209script);210};211212213/**214* Called from createSafeScriptSecurityPrivateDoNotAccessOrElse(). This215* method exists only so that the compiler can dead code eliminate static216* fields (like EMPTY) when they're not accessed.217* @param {string} script218* @return {!goog.html.SafeScript}219* @private220*/221goog.html.SafeScript.prototype.initSecurityPrivateDoNotAccessOrElse_ = function(222script) {223this.privateDoNotAccessOrElseSafeScriptWrappedValue_ = script;224return this;225};226227228/**229* A SafeScript instance corresponding to the empty string.230* @const {!goog.html.SafeScript}231*/232goog.html.SafeScript.EMPTY =233goog.html.SafeScript.createSafeScriptSecurityPrivateDoNotAccessOrElse('');234235236