You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We propose to provide a `TrustedTypePolicyFactory` implementation under `window.trustedTypes`. The most important function available in a `TrustedTypePolicyFactory` is `createPolicy`.
@@ -145,21 +142,18 @@ The policy rules for creating individual types are configured via the properties
145
142
```webidl
146
143
interface TrustedTypeInnerPolicy {
147
144
string createHTML(string);
148
-
string createURL(string);
149
145
string createScriptURL(string);
150
146
string createScript(string);
151
147
}
152
148
```
153
-
154
149
Policy (with a unique name) can be created like this:
createURL: (s) => { /* parse and validate the url. throw if non-conformant */ },
154
+
createScriptURL: (s) => { /* parse and validate the url. throw if non-conformant */ },
160
155
})
161
156
```
162
-
163
157
The policy object is returned, and can be used as a capability to create typed objects i.e. code parts without a reference to the policy object cannot use it.
164
158
165
159
The policy object can be used directly to create typed values that conform to its rules:
@@ -175,22 +169,49 @@ the polyfill code.
175
169
176
170
#### Limiting policies
177
171
178
-
We propose to allow for whitelisting policy names in a CSP, e.g. in a following fashion:
172
+
Applications may want to further limit how policies are created; We propose to allow for
173
+
allow-listing policy names in a CSP, e.g. in a following fashion:
179
174
```http
180
175
Content-Security-Policy: trusted-types foo bar
181
176
```
182
177
183
178
That will assure that no additional policies are created at runtime. Creating a policy with a name
184
179
that was already created, or was not specified in the CSP throws, so introduction of non-reviewed
185
-
policies breaks the application functionally.
180
+
policies breaks the application functionally. There's also an espace hatch - `'allow-duplicate'`
181
+
CSP keyword that allows the applications to create a given policy multiple times (that's useful
182
+
if a dependency is used twice in an application).
186
183
187
184
#### Default policy
188
185
189
-
There is an experimental support for a default policy that allows applications
190
-
to use strings with the injection sinks. These strings would be passed to a single
191
-
user-defined policy that sanitizes the value or rejects it. The intention is to
192
-
allow for a gradual migration of the code from strings towards Trusted Types.
193
-
Please check the [specification draft](https://w3c.github.io/trusted-types/dist/spec/#default-policy-hdr) for details.
186
+
One of the policies the application may create is special, in that it allows to use strings with the injection sinks.
187
+
These strings would be passed to a single user-defined policy that sanitizes the value or rejects it.
188
+
The intention is to allow for a gradual migration of the code from strings towards Trusted Types.
189
+
Please check the [specification draft](https://w3c.github.io/webappsec-trusted-types/dist/spec/#default-policy-hdr) for details.
190
+
191
+
192
+
#### javascript: URLs
193
+
194
+
Using `javascript:` URLs as a payload for DOM XSS exploitation is quite common. At the same time,
195
+
there are many sinks in the platform that accept URLs, and it would be prohibitive for the authors to have to r
196
+
rewrite all of their `HTMLAnchorElement.href` assignments only because a `javascript:` URL could be used.
197
+
198
+
Instead of that we propose a simple workaround - with Trusted Types enforcement (`require-trusted-types-for 'script'`)
199
+
navigation to `javascript:` URLs will be guarded via a default policy mechanism. Commonly, they will simply stop working.
200
+
To reenable them, the application needs to create a default policy that allows it to control the code before it
201
+
executes like so:
202
+
203
+
```javascript
204
+
trustedTypes.createPolicy('default', {
205
+
createScript:payload=> {
206
+
if (payload ==='void(0)') { // javascript:void(0) navigation or, e.g. eval('void(0)')
207
+
return'void(0)';
208
+
} // returning undefined rejects a value and stops navigation.
209
+
}
210
+
});
211
+
```
212
+
213
+
This mechanism compliments CSP's `'unsafe-inline'`, allowing the authors to enable strong security
214
+
controls in their application even if it occasionally uses `javascript:` URLs for legitimate purposes.
194
215
195
216
### DOM Sinks
196
217
@@ -229,63 +250,7 @@ Please check the [specification draft](https://w3c.github.io/trusted-types/dist/
229
250
DOMString srcdoc;
230
251
};
231
252
```
232
-
233
-
* **URL Contexts**: Given something like `typedef (USVString or TrustedURL) URLString`, we'd poke
234
-
at a number of methods and attribute setters to accept the new type:
235
-
236
-
```webidl
237
-
partial interface Location {
238
-
stringifier attribute URLString href;
239
-
void assign(URLString url);
240
-
void replace(URLString url);
241
-
242
-
// (These aren't `URLString`, but they should be something)
243
-
DOMString pathname;
244
-
DOMString search;
245
-
};
246
-
```
247
-
248
-
```webidl
249
-
// A few element types go here. `HTMLBaseElement`, `HTMLLinkElement`
250
-
// `HTMLHyperlinkElementUtils` from a quick skim through HTML.
251
-
partial interface HTMLXXXElement : HTMLElement {
252
-
attribute URLString href;
253
-
};
254
-
```
255
-
256
-
```webidl
257
-
// A few element types go here. `HTMLSourceElement`, `HTMLImageElement`,
0 commit comments