Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/html/safestyle.js
4143 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview The SafeStyle type and its builders.
9
*
10
* TODO(xtof): Link to document stating type contract.
11
*/
12
13
goog.module('goog.html.SafeStyle');
14
goog.module.declareLegacyNamespace();
15
16
const Const = goog.require('goog.string.Const');
17
const SafeUrl = goog.require('goog.html.SafeUrl');
18
const TypedString = goog.require('goog.string.TypedString');
19
const {AssertionError, assert, fail} = goog.require('goog.asserts');
20
const {contains, endsWith} = goog.require('goog.string.internal');
21
const utils = goog.require('goog.utils');
22
23
/**
24
* Token used to ensure that object is created only from this file. No code
25
* outside of this file can access this token.
26
* @type {!Object}
27
* @const
28
*/
29
const CONSTRUCTOR_TOKEN_PRIVATE = {};
30
31
/**
32
* A string-like object which represents a sequence of CSS declarations
33
* (`propertyName1: propertyvalue1; propertyName2: propertyValue2; ...`)
34
* and that carries the security type contract that its value, as a string,
35
* will not cause untrusted script execution (XSS) when evaluated as CSS in a
36
* browser.
37
*
38
* Instances of this type must be created via the factory methods
39
* (`SafeStyle.create` or `SafeStyle.fromConstant`)
40
* and not by invoking its constructor. The constructor intentionally takes an
41
* extra parameter that cannot be constructed outside of this file and the type
42
* is immutable; hence only a default instance corresponding to the empty string
43
* can be obtained via constructor invocation.
44
*
45
* SafeStyle's string representation can safely be:
46
* <ul>
47
* <li>Interpolated as the content of a *quoted* HTML style attribute.
48
* However, the SafeStyle string *must be HTML-attribute-escaped* before
49
* interpolation.
50
* <li>Interpolated as the content of a {}-wrapped block within a stylesheet.
51
* '<' characters in the SafeStyle string *must be CSS-escaped* before
52
* interpolation. The SafeStyle string is also guaranteed not to be able
53
* to introduce new properties or elide existing ones.
54
* <li>Interpolated as the content of a {}-wrapped block within an HTML
55
* &lt;style&gt; element. '<' characters in the SafeStyle string
56
* *must be CSS-escaped* before interpolation.
57
* <li>Assigned to the style property of a DOM node. The SafeStyle string
58
* should not be escaped before being assigned to the property.
59
* </ul>
60
*
61
* A SafeStyle may never contain literal angle brackets. Otherwise, it could
62
* be unsafe to place a SafeStyle into a &lt;style&gt; tag (where it can't
63
* be HTML escaped). For example, if the SafeStyle containing
64
* `font: 'foo &lt;style/&gt;&lt;script&gt;evil&lt;/script&gt;'` were
65
* interpolated within a &lt;style&gt; tag, this would then break out of the
66
* style context into HTML.
67
*
68
* A SafeStyle may contain literal single or double quotes, and as such the
69
* entire style string must be escaped when used in a style attribute (if
70
* this were not the case, the string could contain a matching quote that
71
* would escape from the style attribute).
72
*
73
* Values of this type must be composable, i.e. for any two values
74
* `style1` and `style2` of this type,
75
* `SafeStyle.unwrap(style1) +
76
* SafeStyle.unwrap(style2)` must itself be a value that satisfies
77
* the SafeStyle type constraint. This requirement implies that for any value
78
* `style` of this type, `SafeStyle.unwrap(style)` must
79
* not end in a "property value" or "property name" context. For example,
80
* a value of `background:url("` or `font-` would not satisfy the
81
* SafeStyle contract. This is because concatenating such strings with a
82
* second value that itself does not contain unsafe CSS can result in an
83
* overall string that does. For example, if `javascript:evil())"` is
84
* appended to `background:url("}, the resulting string may result in
85
* the execution of a malicious script.
86
*
87
* TODO(mlourenco): Consider whether we should implement UTF-8 interchange
88
* validity checks and blacklisting of newlines (including Unicode ones) and
89
* other whitespace characters (\t, \f). Document here if so and also update
90
* SafeStyle.fromConstant().
91
*
92
* The following example values comply with this type's contract:
93
* <ul>
94
* <li><pre>width: 1em;</pre>
95
* <li><pre>height:1em;</pre>
96
* <li><pre>width: 1em;height: 1em;</pre>
97
* <li><pre>background:url('http://url');</pre>
98
* </ul>
99
* In addition, the empty string is safe for use in a CSS attribute.
100
*
101
* The following example values do NOT comply with this type's contract:
102
* <ul>
103
* <li><pre>background: red</pre> (missing a trailing semi-colon)
104
* <li><pre>background:</pre> (missing a value and a trailing semi-colon)
105
* <li><pre>1em</pre> (missing an attribute name, which provides context for
106
* the value)
107
* </ul>
108
*
109
* @see SafeStyle#create
110
* @see SafeStyle#fromConstant
111
* @see http://www.w3.org/TR/css3-syntax/
112
* @final
113
* @struct
114
* @implements {TypedString}
115
*/
116
class SafeStyle {
117
/**
118
* @param {string} value
119
* @param {!Object} token package-internal implementation detail.
120
*/
121
constructor(value, token) {
122
if (goog.DEBUG && token !== CONSTRUCTOR_TOKEN_PRIVATE) {
123
throw Error('SafeStyle is not meant to be built directly');
124
}
125
126
/**
127
* The contained value of this SafeStyle. The field has a purposely
128
* ugly name to make (non-compiled) code that attempts to directly access
129
* this field stand out.
130
* @const
131
* @private {string}
132
*/
133
this.privateDoNotAccessOrElseSafeStyleWrappedValue_ = value;
134
135
/**
136
* @override
137
* @const {boolean}
138
*/
139
this.implementsGoogStringTypedString = true;
140
}
141
142
143
/**
144
* Creates a SafeStyle object from a compile-time constant string.
145
*
146
* `style` should be in the format
147
* `name: value; [name: value; ...]` and must not have any < or >
148
* characters in it. This is so that SafeStyle's contract is preserved,
149
* allowing the SafeStyle to correctly be interpreted as a sequence of CSS
150
* declarations and without affecting the syntactic structure of any
151
* surrounding CSS and HTML.
152
*
153
* This method performs basic sanity checks on the format of `style`
154
* but does not constrain the format of `name` and `value`, except
155
* for disallowing tag characters.
156
*
157
* @param {!Const} style A compile-time-constant string from which
158
* to create a SafeStyle.
159
* @return {!SafeStyle} A SafeStyle object initialized to
160
* `style`.
161
*/
162
static fromConstant(style) {
163
const styleString = Const.unwrap(style);
164
if (styleString.length === 0) {
165
return SafeStyle.EMPTY;
166
}
167
assert(
168
endsWith(styleString, ';'),
169
`Last character of style string is not ';': ${styleString}`);
170
assert(
171
contains(styleString, ':'),
172
'Style string must contain at least one \':\', to ' +
173
'specify a "name: value" pair: ' + styleString);
174
return SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
175
styleString);
176
};
177
178
179
/**
180
* Returns this SafeStyle's value as a string.
181
*
182
* IMPORTANT: In code where it is security relevant that an object's type is
183
* indeed `SafeStyle`, use `SafeStyle.unwrap` instead of
184
* this method. If in doubt, assume that it's security relevant. In
185
* particular, note that goog.html functions which return a goog.html type do
186
* not guarantee the returned instance is of the right type. For example:
187
*
188
* <pre>
189
* var fakeSafeHtml = new String('fake');
190
* fakeSafeHtml.__proto__ = goog.html.SafeHtml.prototype;
191
* var newSafeHtml = goog.html.SafeHtml.htmlEscape(fakeSafeHtml);
192
* // newSafeHtml is just an alias for fakeSafeHtml, it's passed through by
193
* // goog.html.SafeHtml.htmlEscape() as fakeSafeHtml
194
* // instanceof goog.html.SafeHtml.
195
* </pre>
196
*
197
* @return {string}
198
* @see SafeStyle#unwrap
199
* @override
200
*/
201
getTypedStringValue() {
202
return this.privateDoNotAccessOrElseSafeStyleWrappedValue_;
203
}
204
205
206
/**
207
* Returns a string-representation of this value.
208
*
209
* To obtain the actual string value wrapped in a SafeStyle, use
210
* `SafeStyle.unwrap`.
211
*
212
* @return {string}
213
* @see SafeStyle#unwrap
214
* @override
215
*/
216
toString() {
217
return this.privateDoNotAccessOrElseSafeStyleWrappedValue_.toString();
218
}
219
220
221
/**
222
* Performs a runtime check that the provided object is indeed a
223
* SafeStyle object, and returns its value.
224
*
225
* @param {!SafeStyle} safeStyle The object to extract from.
226
* @return {string} The safeStyle object's contained string, unless
227
* the run-time type check fails. In that case, `unwrap` returns an
228
* innocuous string, or, if assertions are enabled, throws
229
* `AssertionError`.
230
*/
231
static unwrap(safeStyle) {
232
// Perform additional Run-time type-checking to ensure that
233
// safeStyle is indeed an instance of the expected type. This
234
// provides some additional protection against security bugs due to
235
// application code that disables type checks.
236
// Specifically, the following checks are performed:
237
// 1. The object is an instance of the expected type.
238
// 2. The object is not an instance of a subclass.
239
if (safeStyle instanceof SafeStyle && safeStyle.constructor === SafeStyle) {
240
return safeStyle.privateDoNotAccessOrElseSafeStyleWrappedValue_;
241
} else {
242
fail(
243
`expected object of type SafeStyle, got '${safeStyle}` +
244
'\' of type ' + utils.typeOf(safeStyle));
245
return 'type_error:SafeStyle';
246
}
247
}
248
249
250
/**
251
* Package-internal utility method to create SafeStyle instances.
252
*
253
* @param {string} style The string to initialize the SafeStyle object with.
254
* @return {!SafeStyle} The initialized SafeStyle object.
255
* @package
256
*/
257
static createSafeStyleSecurityPrivateDoNotAccessOrElse(style) {
258
return new SafeStyle(style, CONSTRUCTOR_TOKEN_PRIVATE);
259
}
260
261
/**
262
* Creates a new SafeStyle object from the properties specified in the map.
263
* @param {!SafeStyle.PropertyMap} map Mapping of property names to
264
* their values, for example {'margin': '1px'}. Names must consist of
265
* [-_a-zA-Z0-9]. Values might be strings consisting of
266
* [-,.'"%_!# a-zA-Z0-9[\]], where ", ', and [] must be properly balanced.
267
* We also allow simple functions like rgb() and url() which sanitizes its
268
* contents. Other values must be wrapped in Const. URLs might
269
* be passed as SafeUrl which will be wrapped into url(""). We
270
* also support array whose elements are joined with ' '. Null value
271
* causes skipping the property.
272
* @return {!SafeStyle}
273
* @throws {!Error} If invalid name is provided.
274
* @throws {!AssertionError} If invalid value is provided. With
275
* disabled assertions, invalid value is replaced by
276
* SafeStyle.INNOCUOUS_STRING.
277
*/
278
static create(map) {
279
let style = '';
280
for (let name in map) {
281
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty#Using_hasOwnProperty_as_a_property_name
282
if (Object.prototype.hasOwnProperty.call(map, name)) {
283
if (!/^[-_a-zA-Z0-9]+$/.test(name)) {
284
throw new Error(`Name allows only [-_a-zA-Z0-9], got: ${name}`);
285
}
286
let value = map[name];
287
if (value == null) {
288
continue;
289
}
290
if (Array.isArray(value)) {
291
value = value.map(sanitizePropertyValue).join(' ');
292
} else {
293
value = sanitizePropertyValue(value);
294
}
295
style += `${name}:${value};`;
296
}
297
}
298
if (!style) {
299
return SafeStyle.EMPTY;
300
}
301
return SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(style);
302
};
303
304
/**
305
* Creates a new SafeStyle object by concatenating the values.
306
* @param {...(!SafeStyle|!Array<!SafeStyle>)} var_args
307
* SafeStyles to concatenate.
308
* @return {!SafeStyle}
309
*/
310
static concat(var_args) {
311
let style = '';
312
313
/**
314
* @param {!SafeStyle|!Array<!SafeStyle>} argument
315
*/
316
const addArgument = argument => {
317
if (Array.isArray(argument)) {
318
argument.forEach(addArgument);
319
} else {
320
style += SafeStyle.unwrap(argument);
321
}
322
};
323
324
Array.prototype.forEach.call(arguments, addArgument);
325
if (!style) {
326
return SafeStyle.EMPTY;
327
}
328
return SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(style);
329
};
330
}
331
332
/**
333
* A SafeStyle instance corresponding to the empty string.
334
* @const {!SafeStyle}
335
*/
336
SafeStyle.EMPTY = SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse('');
337
338
339
/**
340
* The innocuous string generated by SafeStyle.create when passed
341
* an unsafe value.
342
* @const {string}
343
*/
344
SafeStyle.INNOCUOUS_STRING = 'zClosurez';
345
346
347
/**
348
* A single property value.
349
* @typedef {string|!Const|!SafeUrl}
350
*/
351
SafeStyle.PropertyValue;
352
353
354
/**
355
* Mapping of property names to their values.
356
* We don't support numbers even though some values might be numbers (e.g.
357
* line-height or 0 for any length). The reason is that most numeric values need
358
* units (e.g. '1px') and allowing numbers could cause users forgetting about
359
* them.
360
* @typedef {!Object<string, ?SafeStyle.PropertyValue|
361
* ?Array<!SafeStyle.PropertyValue>>}
362
*/
363
SafeStyle.PropertyMap;
364
365
366
367
/**
368
* Checks and converts value to string.
369
* @param {!SafeStyle.PropertyValue} value
370
* @return {string}
371
*/
372
function sanitizePropertyValue(value) {
373
if (value instanceof SafeUrl) {
374
const url = SafeUrl.unwrap(value);
375
return 'url("' + url.replace(/</g, '%3c').replace(/[\\"]/g, '\\$&') + '")';
376
}
377
const result = value instanceof Const ?
378
Const.unwrap(value) :
379
sanitizePropertyValueString(String(value));
380
// These characters can be used to change context and we don't want that even
381
// with const values.
382
if (/[{;}]/.test(result)) {
383
throw new AssertionError('Value does not allow [{;}], got: %s.', [result]);
384
}
385
return result;
386
}
387
388
389
/**
390
* Checks string value.
391
* @param {string} value
392
* @return {string}
393
*/
394
function sanitizePropertyValueString(value) {
395
// Some CSS property values permit nested functions. We allow one level of
396
// nesting, and all nested functions must also be in the FUNCTIONS_RE_ list.
397
const valueWithoutFunctions = value.replace(FUNCTIONS_RE, '$1')
398
.replace(FUNCTIONS_RE, '$1')
399
.replace(URL_RE, 'url');
400
if (!VALUE_RE.test(valueWithoutFunctions)) {
401
fail(
402
`String value allows only ${VALUE_ALLOWED_CHARS}` +
403
' and simple functions, got: ' + value);
404
return SafeStyle.INNOCUOUS_STRING;
405
} else if (COMMENT_RE.test(value)) {
406
fail(`String value disallows comments, got: ${value}`);
407
return SafeStyle.INNOCUOUS_STRING;
408
} else if (!hasBalancedQuotes(value)) {
409
fail(`String value requires balanced quotes, got: ${value}`);
410
return SafeStyle.INNOCUOUS_STRING;
411
} else if (!hasBalancedSquareBrackets(value)) {
412
fail(
413
'String value requires balanced square brackets and one' +
414
' identifier per pair of brackets, got: ' + value);
415
return SafeStyle.INNOCUOUS_STRING;
416
}
417
return sanitizeUrl(value);
418
}
419
420
421
/**
422
* Checks that quotes (" and ') are properly balanced inside a string. Assumes
423
* that neither escape (\) nor any other character that could result in
424
* breaking out of a string parsing context are allowed;
425
* see http://www.w3.org/TR/css3-syntax/#string-token-diagram.
426
* @param {string} value Untrusted CSS property value.
427
* @return {boolean} True if property value is safe with respect to quote
428
* balancedness.
429
*/
430
function hasBalancedQuotes(value) {
431
let outsideSingle = true;
432
let outsideDouble = true;
433
for (let i = 0; i < value.length; i++) {
434
const c = value.charAt(i);
435
if (c == '\'' && outsideDouble) {
436
outsideSingle = !outsideSingle;
437
} else if (c == '"' && outsideSingle) {
438
outsideDouble = !outsideDouble;
439
}
440
}
441
return outsideSingle && outsideDouble;
442
}
443
444
445
/**
446
* Checks that square brackets ([ and ]) are properly balanced inside a string,
447
* and that the content in the square brackets is one ident-token;
448
* see https://www.w3.org/TR/css-syntax-3/#ident-token-diagram.
449
* For practicality, and in line with other restrictions posed on SafeStyle
450
* strings, we restrict the character set allowable in the ident-token to
451
* [-_a-zA-Z0-9].
452
* @param {string} value Untrusted CSS property value.
453
* @return {boolean} True if property value is safe with respect to square
454
* bracket balancedness.
455
*/
456
function hasBalancedSquareBrackets(value) {
457
let outside = true;
458
const tokenRe = /^[-_a-zA-Z0-9]$/;
459
for (let i = 0; i < value.length; i++) {
460
const c = value.charAt(i);
461
if (c == ']') {
462
if (outside) return false; // Unbalanced ].
463
outside = true;
464
} else if (c == '[') {
465
if (!outside) return false; // No nesting.
466
outside = false;
467
} else if (!outside && !tokenRe.test(c)) {
468
return false;
469
}
470
}
471
return outside;
472
}
473
474
475
/**
476
* Characters allowed in VALUE_RE.
477
* @type {string}
478
*/
479
const VALUE_ALLOWED_CHARS = '[-+,."\'%_!#/ a-zA-Z0-9\\[\\]]';
480
481
482
/**
483
* Regular expression for safe values.
484
* Quotes (" and ') are allowed, but a check must be done elsewhere to ensure
485
* they're balanced.
486
* Square brackets ([ and ]) are allowed, but a check must be done elsewhere
487
* to ensure they're balanced. The content inside a pair of square brackets must
488
* be one alphanumeric identifier.
489
* ',' allows multiple values to be assigned to the same property
490
* (e.g. background-attachment or font-family) and hence could allow
491
* multiple values to get injected, but that should pose no risk of XSS.
492
* The expression checks only for XSS safety, not for CSS validity.
493
* @const {!RegExp}
494
*/
495
const VALUE_RE = new RegExp(`^${VALUE_ALLOWED_CHARS}+\$`);
496
497
498
/**
499
* Regular expression for url(). We support URLs allowed by
500
* https://www.w3.org/TR/css-syntax-3/#url-token-diagram without using escape
501
* sequences. Use percent-encoding if you need to use special characters like
502
* backslash.
503
* @const {!RegExp}
504
*/
505
const URL_RE = new RegExp(
506
'\\b(url\\([ \t\n]*)(' +
507
'\'[ -&(-\\[\\]-~]*\'' + // Printable characters except ' and \.
508
'|"[ !#-\\[\\]-~]*"' + // Printable characters except " and \.
509
'|[!#-&*-\\[\\]-~]*' + // Printable characters except [ "'()\\].
510
')([ \t\n]*\\))',
511
'g');
512
513
/**
514
* Names of functions allowed in FUNCTIONS_RE.
515
* @const {!Array<string>}
516
*/
517
const ALLOWED_FUNCTIONS = [
518
'calc',
519
'cubic-bezier',
520
'fit-content',
521
'hsl',
522
'hsla',
523
'linear-gradient',
524
'matrix',
525
'minmax',
526
'radial-gradient',
527
'repeat',
528
'rgb',
529
'rgba',
530
'(rotate|scale|translate)(X|Y|Z|3d)?',
531
'steps',
532
'var',
533
];
534
535
536
/**
537
* Regular expression for simple functions.
538
* @const {!RegExp}
539
*/
540
const FUNCTIONS_RE = new RegExp(
541
'\\b(' + ALLOWED_FUNCTIONS.join('|') + ')' +
542
'\\([-+*/0-9a-zA-Z.%#\\[\\], ]+\\)',
543
'g');
544
545
546
/**
547
* Regular expression for comments. These are disallowed in CSS property values.
548
* @const {!RegExp}
549
*/
550
const COMMENT_RE = /\/\*/;
551
552
553
/**
554
* Sanitize URLs inside url().
555
* NOTE: We could also consider using CSS.escape once that's available in the
556
* browsers. However, loosely matching URL e.g. with url\(.*\) and then escaping
557
* the contents would result in a slightly different language than CSS leading
558
* to confusion of users. E.g. url(")") is valid in CSS but it would be invalid
559
* as seen by our parser. On the other hand, url(\) is invalid in CSS but our
560
* parser would be fine with it.
561
* @param {string} value Untrusted CSS property value.
562
* @return {string}
563
*/
564
function sanitizeUrl(value) {
565
return value.replace(URL_RE, (match, before, url, after) => {
566
let quote = '';
567
url = url.replace(/^(['"])(.*)\1$/, (match, start, inside) => {
568
quote = start;
569
return inside;
570
});
571
const sanitized = SafeUrl.sanitize(url).getTypedStringValue();
572
return before + quote + sanitized + quote + after;
573
});
574
}
575
576
577
exports = SafeStyle;
578
579