1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Soy data primitives.
*
* The goal is to encompass data types used by Soy, especially to mark content
* as known to be "safe".
*
* @author gboyer@google.com (Garrett Boyer)
*/
goog.provide('goog.soy.data.SanitizedContent');
goog.provide('goog.soy.data.SanitizedContentKind');
goog.provide('goog.soy.data.SanitizedCss');
goog.provide('goog.soy.data.SanitizedHtml');
goog.provide('goog.soy.data.SanitizedHtmlAttribute');
goog.provide('goog.soy.data.SanitizedJs');
goog.provide('goog.soy.data.SanitizedStyle');
goog.provide('goog.soy.data.SanitizedTrustedResourceUri');
goog.provide('goog.soy.data.SanitizedUri');
goog.provide('goog.soy.data.UnsanitizedText');
goog.require('goog.Uri');
goog.require('goog.asserts');
goog.require('goog.html.SafeHtml');
goog.require('goog.html.SafeScript');
goog.require('goog.html.SafeStyle');
goog.require('goog.html.SafeStyleSheet');
goog.require('goog.html.SafeUrl');
goog.require('goog.html.TrustedResourceUrl');
goog.require('goog.html.uncheckedconversions');
goog.require('goog.i18n.bidi.Dir');
goog.require('goog.string.Const');
/**
* A type of textual content.
*
* This is an enum of type Object so that these values are unforgeable.
*
* @enum {!Object}
*/
goog.soy.data.SanitizedContentKind = {
/**
* A snippet of HTML that does not start or end inside a tag, comment, entity,
* or DOCTYPE; and that does not contain any executable code
* (JS, {@code <object>}s, etc.) from a different trust domain.
*/
HTML: goog.DEBUG ? {sanitizedContentKindHtml: true} : {},
/**
* Executable Javascript code or expression, safe for insertion in a
* script-tag or event handler context, known to be free of any
* attacker-controlled scripts. This can either be side-effect-free
* Javascript (such as JSON) or Javascript that's entirely under Google's
* control.
*/
JS: goog.DEBUG ? {sanitizedContentJsChars: true} : {},
/** A properly encoded portion of a URI. */
URI: goog.DEBUG ? {sanitizedContentUri: true} : {},
/** A resource URI not under attacker control. */
TRUSTED_RESOURCE_URI:
goog.DEBUG ? {sanitizedContentTrustedResourceUri: true} : {},
/**
* Repeated attribute names and values. For example,
* {@code dir="ltr" foo="bar" onclick="trustedFunction()" checked}.
*/
ATTRIBUTES: goog.DEBUG ? {sanitizedContentHtmlAttribute: true} : {},
// TODO: Consider separating rules, declarations, and values into
// separate types, but for simplicity, we'll treat explicitly blessed
// SanitizedContent as allowed in all of these contexts.
/**
* A CSS3 declaration, property, value or group of semicolon separated
* declarations.
*/
STYLE: goog.DEBUG ? {sanitizedContentStyle: true} : {},
/** A CSS3 style sheet (list of rules). */
CSS: goog.DEBUG ? {sanitizedContentCss: true} : {},
/**
* Unsanitized plain-text content.
*
* This is effectively the "null" entry of this enum, and is sometimes used
* to explicitly mark content that should never be used unescaped. Since any
* string is safe to use as text, being of ContentKind.TEXT makes no
* guarantees about its safety in any other context such as HTML.
*/
TEXT: goog.DEBUG ? {sanitizedContentKindText: true} : {}
};
/**
* A string-like object that carries a content-type and a content direction.
*
* IMPORTANT! Do not create these directly, nor instantiate the subclasses.
* Instead, use a trusted, centrally reviewed library as endorsed by your team
* to generate these objects. Otherwise, you risk accidentally creating
* SanitizedContent that is attacker-controlled and gets evaluated unescaped in
* templates.
*
* @constructor
*/
goog.soy.data.SanitizedContent = function() {
throw new Error('Do not instantiate directly');
};
/**
* The context in which this content is safe from XSS attacks.
* @type {goog.soy.data.SanitizedContentKind}
*/
goog.soy.data.SanitizedContent.prototype.contentKind;
/**
* The content's direction; null if unknown and thus to be estimated when
* necessary.
* @type {?goog.i18n.bidi.Dir}
*/
goog.soy.data.SanitizedContent.prototype.contentDir = null;
/**
* The already-safe content.
* @protected {string}
*/
goog.soy.data.SanitizedContent.prototype.content;
/**
* Gets the already-safe content.
* @return {string}
*/
goog.soy.data.SanitizedContent.prototype.getContent = function() {
return this.content;
};
/** @override */
goog.soy.data.SanitizedContent.prototype.toString = function() {
return this.content;
};
/**
* Converts sanitized content of kind TEXT or HTML into SafeHtml. HTML content
* is converted without modification, while text content is HTML-escaped.
* @return {!goog.html.SafeHtml}
* @throws {Error} when the content kind is not TEXT or HTML.
*/
goog.soy.data.SanitizedContent.prototype.toSafeHtml = function() {
if (this.contentKind === goog.soy.data.SanitizedContentKind.TEXT) {
return goog.html.SafeHtml.htmlEscape(this.toString());
}
if (this.contentKind !== goog.soy.data.SanitizedContentKind.HTML) {
throw new Error('Sanitized content was not of kind TEXT or HTML.');
}
return goog.html.uncheckedconversions
.safeHtmlFromStringKnownToSatisfyTypeContract(
goog.string.Const.from(
'Soy SanitizedContent of kind HTML produces ' +
'SafeHtml-contract-compliant value.'),
this.toString(), this.contentDir);
};
/**
* Converts sanitized content of kind URI into SafeUrl without modification.
* @return {!goog.html.SafeUrl}
* @throws {Error} when the content kind is not URI.
*/
goog.soy.data.SanitizedContent.prototype.toSafeUrl = function() {
if (this.contentKind !== goog.soy.data.SanitizedContentKind.URI) {
throw new Error('Sanitized content was not of kind URI.');
}
return goog.html.uncheckedconversions
.safeUrlFromStringKnownToSatisfyTypeContract(
goog.string.Const.from(
'Soy SanitizedContent of kind URI produces ' +
'SafeHtml-contract-compliant value.'),
this.toString());
};
/**
* Unsanitized plain text string.
*
* While all strings are effectively safe to use as a plain text, there are no
* guarantees about safety in any other context such as HTML. This is
* sometimes used to mark that should never be used unescaped.
*
* @param {*} content Plain text with no guarantees.
* @param {?goog.i18n.bidi.Dir=} opt_contentDir The content direction; null if
* unknown and thus to be estimated when necessary. Default: null.
* @extends {goog.soy.data.SanitizedContent}
* @constructor
*/
goog.soy.data.UnsanitizedText = function(content, opt_contentDir) {
// Not calling the superclass constructor which just throws an exception.
/** @override */
this.content = String(content);
this.contentDir = opt_contentDir != null ? opt_contentDir : null;
};
goog.inherits(goog.soy.data.UnsanitizedText, goog.soy.data.SanitizedContent);
/** @override */
goog.soy.data.UnsanitizedText.prototype.contentKind =
goog.soy.data.SanitizedContentKind.TEXT;
/**
* Content of type {@link goog.soy.data.SanitizedContentKind.HTML}.
*
* The content is a string of HTML that can safely be embedded in a PCDATA
* context in your app. If you would be surprised to find that an HTML
* sanitizer produced {@code s} (e.g. it runs code or fetches bad URLs) and
* you wouldn't write a template that produces {@code s} on security or privacy
* grounds, then don't pass {@code s} here. The default content direction is
* unknown, i.e. to be estimated when necessary.
*
* @extends {goog.soy.data.SanitizedContent}
* @constructor
*/
goog.soy.data.SanitizedHtml = function() {
goog.soy.data.SanitizedHtml.base(this, 'constructor');
};
goog.inherits(goog.soy.data.SanitizedHtml, goog.soy.data.SanitizedContent);
/** @override */
goog.soy.data.SanitizedHtml.prototype.contentKind =
goog.soy.data.SanitizedContentKind.HTML;
/**
* Checks if the value could be used as the Soy type {html}.
* @param {*} value
* @return {boolean}
*/
goog.soy.data.SanitizedHtml.isCompatibleWith = function(value) {
return goog.isString(value) ||
value instanceof goog.soy.data.SanitizedHtml ||
value instanceof goog.soy.data.UnsanitizedText ||
value instanceof goog.html.SafeHtml;
};
/**
* Content of type {@link goog.soy.data.SanitizedContentKind.JS}.
*
* The content is JavaScript source that when evaluated does not execute any
* attacker-controlled scripts. The content direction is LTR.
*
* @extends {goog.soy.data.SanitizedContent}
* @constructor
*/
goog.soy.data.SanitizedJs = function() {
goog.soy.data.SanitizedJs.base(this, 'constructor');
};
goog.inherits(goog.soy.data.SanitizedJs, goog.soy.data.SanitizedContent);
/** @override */
goog.soy.data.SanitizedJs.prototype.contentKind =
goog.soy.data.SanitizedContentKind.JS;
/** @override */
goog.soy.data.SanitizedJs.prototype.contentDir = goog.i18n.bidi.Dir.LTR;
/**
* Checks if the value could be used as the Soy type {js}.
* @param {*} value
* @return {boolean}
*/
goog.soy.data.SanitizedJs.isCompatibleWith = function(value) {
return goog.isString(value) ||
value instanceof goog.soy.data.SanitizedJs ||
value instanceof goog.soy.data.UnsanitizedText ||
value instanceof goog.html.SafeScript;
};
/**
* Content of type {@link goog.soy.data.SanitizedContentKind.URI}.
*
* The content is a URI chunk that the caller knows is safe to emit in a
* template. The content direction is LTR.
*
* @extends {goog.soy.data.SanitizedContent}
* @constructor
*/
goog.soy.data.SanitizedUri = function() {
goog.soy.data.SanitizedUri.base(this, 'constructor');
};
goog.inherits(goog.soy.data.SanitizedUri, goog.soy.data.SanitizedContent);
/** @override */
goog.soy.data.SanitizedUri.prototype.contentKind =
goog.soy.data.SanitizedContentKind.URI;
/** @override */
goog.soy.data.SanitizedUri.prototype.contentDir = goog.i18n.bidi.Dir.LTR;
/**
* Checks if the value could be used as the Soy type {uri}.
* @param {*} value
* @return {boolean}
*/
goog.soy.data.SanitizedUri.isCompatibleWith = function(value) {
return goog.isString(value) ||
value instanceof goog.soy.data.SanitizedUri ||
value instanceof goog.soy.data.UnsanitizedText ||
value instanceof goog.html.SafeUrl ||
value instanceof goog.html.TrustedResourceUrl ||
value instanceof goog.Uri;
};
/**
* Content of type
* {@link goog.soy.data.SanitizedContentKind.TRUSTED_RESOURCE_URI}.
*
* The content is a TrustedResourceUri chunk that is not under attacker control.
* The content direction is LTR.
*
* @extends {goog.soy.data.SanitizedContent}
* @constructor
*/
goog.soy.data.SanitizedTrustedResourceUri = function() {
goog.soy.data.SanitizedTrustedResourceUri.base(this, 'constructor');
};
goog.inherits(
goog.soy.data.SanitizedTrustedResourceUri, goog.soy.data.SanitizedContent);
/** @override */
goog.soy.data.SanitizedTrustedResourceUri.prototype.contentKind =
goog.soy.data.SanitizedContentKind.TRUSTED_RESOURCE_URI;
/** @override */
goog.soy.data.SanitizedTrustedResourceUri.prototype.contentDir =
goog.i18n.bidi.Dir.LTR;
/**
* Converts sanitized content into TrustedResourceUrl without modification.
* @return {!goog.html.TrustedResourceUrl}
*/
goog.soy.data.SanitizedTrustedResourceUri.prototype.toTrustedResourceUrl =
function() {
return goog.html.uncheckedconversions
.trustedResourceUrlFromStringKnownToSatisfyTypeContract(
goog.string.Const.from(
'Soy SanitizedContent of kind TRUSTED_RESOURCE_URI produces ' +
'TrustedResourceUrl-contract-compliant value.'),
this.toString());
};
/**
* Checks if the value could be used as the Soy type {trusted_resource_uri}.
* @param {*} value
* @return {boolean}
*/
goog.soy.data.SanitizedTrustedResourceUri.isCompatibleWith = function(value) {
return goog.isString(value) ||
value instanceof goog.soy.data.SanitizedTrustedResourceUri ||
value instanceof goog.soy.data.UnsanitizedText ||
value instanceof goog.html.TrustedResourceUrl;
};
/**
* Content of type {@link goog.soy.data.SanitizedContentKind.ATTRIBUTES}.
*
* The content should be safely embeddable within an open tag, such as a
* key="value" pair. The content direction is LTR.
*
* @extends {goog.soy.data.SanitizedContent}
* @constructor
*/
goog.soy.data.SanitizedHtmlAttribute = function() {
goog.soy.data.SanitizedHtmlAttribute.base(this, 'constructor');
};
goog.inherits(
goog.soy.data.SanitizedHtmlAttribute, goog.soy.data.SanitizedContent);
/** @override */
goog.soy.data.SanitizedHtmlAttribute.prototype.contentKind =
goog.soy.data.SanitizedContentKind.ATTRIBUTES;
/** @override */
goog.soy.data.SanitizedHtmlAttribute.prototype.contentDir =
goog.i18n.bidi.Dir.LTR;
/**
* Checks if the value could be used as the Soy type {attribute}.
* @param {*} value
* @return {boolean}
*/
goog.soy.data.SanitizedHtmlAttribute.isCompatibleWith = function(value) {
return goog.isString(value) ||
value instanceof goog.soy.data.SanitizedHtmlAttribute ||
value instanceof goog.soy.data.UnsanitizedText;
};
/**
* Content of type {@link goog.soy.data.SanitizedContentKind.STYLE}.
*
* The content is non-attacker-exploitable CSS, such as {@code color:#c3d9ff}.
* The content direction is LTR.
*
* @extends {goog.soy.data.SanitizedContent}
* @constructor
*/
goog.soy.data.SanitizedStyle = function() {
goog.soy.data.SanitizedStyle.base(this, 'constructor');
};
goog.inherits(goog.soy.data.SanitizedStyle, goog.soy.data.SanitizedContent);
/** @override */
goog.soy.data.SanitizedStyle.prototype.contentKind =
goog.soy.data.SanitizedContentKind.STYLE;
/** @override */
goog.soy.data.SanitizedStyle.prototype.contentDir = goog.i18n.bidi.Dir.LTR;
/**
* Checks if the value could be used as the Soy type {css}.
* @param {*} value
* @return {boolean}
*/
goog.soy.data.SanitizedStyle.isCompatibleWith = function(value) {
return goog.isString(value) ||
value instanceof goog.soy.data.SanitizedStyle ||
value instanceof goog.soy.data.UnsanitizedText ||
value instanceof goog.html.SafeStyle;
};
/**
* Content of type {@link goog.soy.data.SanitizedContentKind.CSS}.
*
* The content is non-attacker-exploitable CSS, such as {@code @import url(x)}.
* The content direction is LTR.
*
* @extends {goog.soy.data.SanitizedContent}
* @constructor
*/
goog.soy.data.SanitizedCss = function() {
goog.soy.data.SanitizedCss.base(this, 'constructor');
};
goog.inherits(goog.soy.data.SanitizedCss, goog.soy.data.SanitizedContent);
/** @override */
goog.soy.data.SanitizedCss.prototype.contentKind =
goog.soy.data.SanitizedContentKind.CSS;
/** @override */
goog.soy.data.SanitizedCss.prototype.contentDir = goog.i18n.bidi.Dir.LTR;
/**
* Checks if the value could be used as the Soy type {css}.
* @param {*} value
* @return {boolean}
*/
goog.soy.data.SanitizedCss.isCompatibleWith = function(value) {
return goog.isString(value) ||
value instanceof goog.soy.data.SanitizedCss ||
value instanceof goog.soy.data.UnsanitizedText ||
value instanceof goog.html.SafeStyle || // TODO(jakubvrana): Delete.
value instanceof goog.html.SafeStyleSheet;
};
/**
* Converts SanitizedCss into SafeStyleSheet.
* Note: SanitizedCss in Soy represents both SafeStyle and SafeStyleSheet in
* Closure. It's about to be split so that SanitizedCss represents only
* SafeStyleSheet.
* @return {!goog.html.SafeStyleSheet}
*/
goog.soy.data.SanitizedCss.prototype.toSafeStyleSheet = function() {
var value = this.toString();
// TODO(jakubvrana): Remove this check when there's a separate type for style
// declaration.
goog.asserts.assert(
/[@{]|^\s*$/.test(value),
'value doesn\'t look like style sheet: ' + value);
return goog.html.uncheckedconversions
.safeStyleSheetFromStringKnownToSatisfyTypeContract(
goog.string.Const.from(
'Soy SanitizedCss produces SafeStyleSheet-contract-compliant ' +
'value.'),
value);
};
|