Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/html/uncheckedconversions.js
4122 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Unchecked conversions to create values of goog.html types from
9
* plain strings. Use of these functions could potentially result in instances
10
* of goog.html types that violate their type contracts, and hence result in
11
* security vulnerabilties.
12
*
13
* Therefore, all uses of the methods herein must be carefully security
14
* reviewed. Avoid use of the methods in this file whenever possible; instead
15
* prefer to create instances of goog.html types using inherently safe builders
16
* or template systems.
17
*
18
*
19
*/
20
21
22
goog.provide('goog.html.uncheckedconversions');
23
24
goog.require('goog.asserts');
25
goog.require('goog.html.SafeHtml');
26
goog.require('goog.html.SafeScript');
27
goog.require('goog.html.SafeStyle');
28
goog.require('goog.html.SafeStyleSheet');
29
goog.require('goog.html.SafeUrl');
30
goog.require('goog.html.TrustedResourceUrl');
31
goog.require('goog.string.Const');
32
goog.require('goog.string.internal');
33
34
35
/**
36
* Performs an "unchecked conversion" to SafeHtml from a plain string that is
37
* known to satisfy the SafeHtml type contract.
38
*
39
* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
40
* that the value of `html` satisfies the SafeHtml type contract in all
41
* possible program states.
42
*
43
*
44
* @param {!goog.string.Const} justification A constant string explaining why
45
* this use of this method is safe. May include a security review ticket
46
* number.
47
* @param {string} html A string that is claimed to adhere to the SafeHtml
48
* contract.
49
* @return {!goog.html.SafeHtml} The value of html, wrapped in a SafeHtml
50
* object.
51
*/
52
goog.html.uncheckedconversions.safeHtmlFromStringKnownToSatisfyTypeContract =
53
function(justification, html) {
54
'use strict';
55
// unwrap() called inside an assert so that justification can be optimized
56
// away in production code.
57
goog.asserts.assertString(
58
goog.string.Const.unwrap(justification), 'must provide justification');
59
goog.asserts.assert(
60
!goog.string.internal.isEmptyOrWhitespace(
61
goog.string.Const.unwrap(justification)),
62
'must provide non-empty justification');
63
return goog.html.SafeHtml.createSafeHtmlSecurityPrivateDoNotAccessOrElse(
64
html);
65
};
66
67
68
/**
69
* Performs an "unchecked conversion" to SafeScript from a plain string that is
70
* known to satisfy the SafeScript type contract.
71
*
72
* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
73
* that the value of `script` satisfies the SafeScript type contract in
74
* all possible program states.
75
*
76
*
77
* @param {!goog.string.Const} justification A constant string explaining why
78
* this use of this method is safe. May include a security review ticket
79
* number.
80
* @param {string} script The string to wrap as a SafeScript.
81
* @return {!goog.html.SafeScript} The value of `script`, wrapped in a
82
* SafeScript object.
83
*/
84
goog.html.uncheckedconversions.safeScriptFromStringKnownToSatisfyTypeContract =
85
function(justification, script) {
86
'use strict';
87
// unwrap() called inside an assert so that justification can be optimized
88
// away in production code.
89
goog.asserts.assertString(
90
goog.string.Const.unwrap(justification), 'must provide justification');
91
goog.asserts.assert(
92
!goog.string.internal.isEmptyOrWhitespace(
93
goog.string.Const.unwrap(justification)),
94
'must provide non-empty justification');
95
return goog.html.SafeScript.createSafeScriptSecurityPrivateDoNotAccessOrElse(
96
script);
97
};
98
99
100
/**
101
* Performs an "unchecked conversion" to SafeStyle from a plain string that is
102
* known to satisfy the SafeStyle type contract.
103
*
104
* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
105
* that the value of `style` satisfies the SafeStyle type contract in all
106
* possible program states.
107
*
108
*
109
* @param {!goog.string.Const} justification A constant string explaining why
110
* this use of this method is safe. May include a security review ticket
111
* number.
112
* @param {string} style The string to wrap as a SafeStyle.
113
* @return {!goog.html.SafeStyle} The value of `style`, wrapped in a
114
* SafeStyle object.
115
*/
116
goog.html.uncheckedconversions.safeStyleFromStringKnownToSatisfyTypeContract =
117
function(justification, style) {
118
'use strict';
119
// unwrap() called inside an assert so that justification can be optimized
120
// away in production code.
121
goog.asserts.assertString(
122
goog.string.Const.unwrap(justification), 'must provide justification');
123
goog.asserts.assert(
124
!goog.string.internal.isEmptyOrWhitespace(
125
goog.string.Const.unwrap(justification)),
126
'must provide non-empty justification');
127
return goog.html.SafeStyle.createSafeStyleSecurityPrivateDoNotAccessOrElse(
128
style);
129
};
130
131
132
/**
133
* Performs an "unchecked conversion" to SafeStyleSheet from a plain string
134
* that is known to satisfy the SafeStyleSheet type contract.
135
*
136
* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
137
* that the value of `styleSheet` satisfies the SafeStyleSheet type
138
* contract in all possible program states.
139
*
140
*
141
* @param {!goog.string.Const} justification A constant string explaining why
142
* this use of this method is safe. May include a security review ticket
143
* number.
144
* @param {string} styleSheet The string to wrap as a SafeStyleSheet.
145
* @return {!goog.html.SafeStyleSheet} The value of `styleSheet`, wrapped
146
* in a SafeStyleSheet object.
147
*/
148
goog.html.uncheckedconversions
149
.safeStyleSheetFromStringKnownToSatisfyTypeContract = function(
150
justification, styleSheet) {
151
'use strict';
152
// unwrap() called inside an assert so that justification can be optimized
153
// away in production code.
154
goog.asserts.assertString(
155
goog.string.Const.unwrap(justification), 'must provide justification');
156
goog.asserts.assert(
157
!goog.string.internal.isEmptyOrWhitespace(
158
goog.string.Const.unwrap(justification)),
159
'must provide non-empty justification');
160
return goog.html.SafeStyleSheet
161
.createSafeStyleSheetSecurityPrivateDoNotAccessOrElse(styleSheet);
162
};
163
164
165
/**
166
* Performs an "unchecked conversion" to SafeUrl from a plain string that is
167
* known to satisfy the SafeUrl type contract.
168
*
169
* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
170
* that the value of `url` satisfies the SafeUrl type contract in all
171
* possible program states.
172
*
173
*
174
* @param {!goog.string.Const} justification A constant string explaining why
175
* this use of this method is safe. May include a security review ticket
176
* number.
177
* @param {string} url The string to wrap as a SafeUrl.
178
* @return {!goog.html.SafeUrl} The value of `url`, wrapped in a SafeUrl
179
* object.
180
*/
181
goog.html.uncheckedconversions.safeUrlFromStringKnownToSatisfyTypeContract =
182
function(justification, url) {
183
'use strict';
184
// unwrap() called inside an assert so that justification can be optimized
185
// away in production code.
186
goog.asserts.assertString(
187
goog.string.Const.unwrap(justification), 'must provide justification');
188
goog.asserts.assert(
189
!goog.string.internal.isEmptyOrWhitespace(
190
goog.string.Const.unwrap(justification)),
191
'must provide non-empty justification');
192
return goog.html.SafeUrl.createSafeUrlSecurityPrivateDoNotAccessOrElse(url);
193
};
194
195
196
/**
197
* Performs an "unchecked conversion" to TrustedResourceUrl from a plain string
198
* that is known to satisfy the TrustedResourceUrl type contract.
199
*
200
* IMPORTANT: Uses of this method must be carefully security-reviewed to ensure
201
* that the value of `url` satisfies the TrustedResourceUrl type contract
202
* in all possible program states.
203
*
204
*
205
* @param {!goog.string.Const} justification A constant string explaining why
206
* this use of this method is safe. May include a security review ticket
207
* number.
208
* @param {string} url The string to wrap as a TrustedResourceUrl.
209
* @return {!goog.html.TrustedResourceUrl} The value of `url`, wrapped in
210
* a TrustedResourceUrl object.
211
*/
212
goog.html.uncheckedconversions
213
.trustedResourceUrlFromStringKnownToSatisfyTypeContract = function(
214
justification, url) {
215
'use strict';
216
// unwrap() called inside an assert so that justification can be optimized
217
// away in production code.
218
goog.asserts.assertString(
219
goog.string.Const.unwrap(justification), 'must provide justification');
220
goog.asserts.assert(
221
!goog.string.internal.isEmptyOrWhitespace(
222
goog.string.Const.unwrap(justification)),
223
'must provide non-empty justification');
224
return goog.html.TrustedResourceUrl
225
.createTrustedResourceUrlSecurityPrivateDoNotAccessOrElse(url);
226
};
227
228