-
Notifications
You must be signed in to change notification settings - Fork 5
/
closure-bridge.js
192 lines (174 loc) · 5.93 KB
/
closure-bridge.js
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
/**
* @license
* Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
"use strict";
/**
* @fileoverview
* Provides a safe types bridge that recognizes goog.html.Safe*.
*/
goog.provide('security.polymer_resin.closure_bridge');
goog.require('goog.html.SafeHtml');
goog.require('goog.html.SafeScript');
goog.require('goog.html.SafeStyle');
goog.require('goog.html.SafeUrl');
goog.require('goog.html.TrustedResourceUrl');
goog.require('goog.string');
goog.require('goog.string.Const');
goog.require('goog.string.TypedString');
/**
* @define {boolean} whether bundled with its dependencies while
* exporting its public API.
*/
security.polymer_resin.closure_bridge.STANDALONE =
goog.define('security.polymer_resin.closure_bridge.STANDALONE', false);
/**
* @typedef {{typeToUnwrap: !Function, unwrap: !Function}}
*/
security.polymer_resin.closure_bridge.unwrapper;
/**
* @typedef {function (string, *): ?}
*/
security.polymer_resin.closure_bridge.filter;
/**
* Unwraps any typed string input.
* @param {?} value A value that might be a typed string.
* @return {?} the content if value is a wrapped string, or value otherwise.
* @private
*/
security.polymer_resin.closure_bridge.unwrapString_ = function (value) {
return (value && value.implementsGoogStringTypedString)
? (/** @type {!goog.string.TypedString} */(value))
.getTypedStringValue()
: value;
};
/**
* @type {!Object<string, !security.polymer_resin.closure_bridge.unwrapper>}
* @private
* @const
*/
security.polymer_resin.closure_bridge.UNWRAPPERS_ = {
// Keys are security.polymer_resin.SafeType but adding a dependency on
// polymer_resin just to get that type breaks webcomponent_binaries since
// polymer_resin is depended upon as both a js_dep and a wc_dep.
'CONSTANT': {
typeToUnwrap: goog.string.Const,
unwrap: goog.string.Const.unwrap
},
'JAVASCRIPT': {
typeToUnwrap: goog.html.SafeScript,
unwrap: goog.html.SafeScript.unwrap
},
'HTML': {
typeToUnwrap: goog.html.SafeHtml,
unwrap: goog.html.SafeHtml.unwrap
},
'RESOURCE_URL': {
typeToUnwrap: goog.html.TrustedResourceUrl,
unwrap: goog.html.TrustedResourceUrl.unwrap
},
'STRING': {
typeToUnwrap: Object,
unwrap: security.polymer_resin.closure_bridge.unwrapString_
},
'STYLE': {
typeToUnwrap: goog.html.SafeStyle,
unwrap: goog.html.SafeStyle.unwrap
},
'URL': {
typeToUnwrap: goog.html.SafeUrl,
unwrap: goog.html.SafeUrl.unwrap
}
};
/**
* @type {!security.polymer_resin.closure_bridge.filter}
* @private
* @const
*/
security.polymer_resin.closure_bridge.disallow_ = function (value, fallback) {
return fallback;
};
/**
* @type {!Object<string, !security.polymer_resin.closure_bridge.filter>}
* @private
* @const
*/
security.polymer_resin.closure_bridge.FILTERS_ = {
/* Just returns the safe replacement value because we have no
* way of knowing that a raw string is a compile-time constant.
*/
'CONSTANT': security.polymer_resin.closure_bridge.disallow_,
/* Just returns the safe replacement value because we have no
* way of knowing that a raw string is safe JavaScript so rely
* on RTTI in all cases.
*/
'JAVASCRIPT': security.polymer_resin.closure_bridge.disallow_,
/* Converts plain text to HTML that parses to a text node with
* equivalent content.
*/
'HTML': goog.string.htmlEscape,
/* Just returns the safe replacement value because we have no
* way of declaring that a raw string is a trusted resource so
* rely on RTTI in all cases.
*/
'RESOURCE_URL': security.polymer_resin.closure_bridge.disallow_,
'STRING': String,
/* Just returns the safe replacement value because we have no
* way of knowing that a raw string is safe CSS so rely on RTTI
* in all cases.
*/
'STYLE': security.polymer_resin.closure_bridge.disallow_,
'URL': (
/**
* Allows safe URLs through, but rejects unsafe ones.
* @param {string} value attribute value
* @param {*} fallback returned if value is rejected as unsafe.
* @return {?}
*/
function allowSafeUrl(value, fallback) {
// TODO: Can we do without creating a SafeUrl instance?
var safeValue = goog.html.SafeUrl.sanitize(value).getTypedStringValue();
if (safeValue === goog.html.SafeUrl.INNOCUOUS_STRING) {
return fallback;
}
return safeValue;
})
};
/**
* A security.polymer_resin.SafeTypeBridge.
*
* @param {*} value the value whose trustedness is being check.
* @param {string} type a security.polymer_resin.SafeType value
* @param {*} fallback the value to return if value is not trusted as a value of type.
* @return {?}
*/
security.polymer_resin.closure_bridge.safeTypesBridge =
function (value, type, fallback) {
/** @type {!security.polymer_resin.closure_bridge.unwrapper} */
var unwrapper = security.polymer_resin.closure_bridge.UNWRAPPERS_[type];
if (value instanceof unwrapper.typeToUnwrap) {
var uw = unwrapper.unwrap(value, fallback);
if (uw !== fallback) {
return uw;
}
}
/** @type {!security.polymer_resin.closure_bridge.filter} */
var filter = security.polymer_resin.closure_bridge.FILTERS_[type];
return filter(
'' + security.polymer_resin.closure_bridge.unwrapString_(value),
fallback);
};
if (security.polymer_resin.closure_bridge.STANDALONE) {
goog.exportSymbol(
'security.polymer_resin.closure_bridge.safeTypesBridge',
security.polymer_resin.closure_bridge.safeTypesBridge);
}