summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/trustedtypes/trusted_types_util.cc
blob: ce9921c712a2e5caf83792eb218845b9ccc2d1de (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/trustedtypes/trusted_types_util.h"

#include "third_party/blink/renderer/bindings/core/v8/string_or_trusted_html.h"
#include "third_party/blink/renderer/bindings/core/v8/string_or_trusted_html_or_trusted_script_or_trusted_script_url_or_trusted_url.h"
#include "third_party/blink/renderer/bindings/core/v8/string_or_trusted_script.h"
#include "third_party/blink/renderer/bindings/core/v8/string_or_trusted_script_url.h"
#include "third_party/blink/renderer/bindings/core/v8/usv_string_or_trusted_url.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/origin_trials/origin_trials.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_html.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_script.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_script_url.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy_factory.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_url.h"

namespace blink {

String GetStringFromTrustedType(
    const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURLOrTrustedURL&
        string_or_trusted_type,
    const Document* doc,
    ExceptionState& exception_state) {
  DCHECK(string_or_trusted_type.IsString() ||
         origin_trials::TrustedDOMTypesEnabled(doc));
  DCHECK(!string_or_trusted_type.IsNull());

  if (string_or_trusted_type.IsString() && doc && doc->RequireTrustedTypes()) {
    exception_state.ThrowTypeError(
        "This document requires a Trusted Type assignment.");

    // Test case docs (Document::CreateForTest) might not have a window.
    if (doc->ExecutingWindow())
      doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();

    return g_empty_string;
  }

  if (string_or_trusted_type.IsTrustedHTML())
    return string_or_trusted_type.GetAsTrustedHTML()->toString();
  if (string_or_trusted_type.IsTrustedScript())
    return string_or_trusted_type.GetAsTrustedScript()->toString();
  if (string_or_trusted_type.IsTrustedScriptURL())
    return string_or_trusted_type.GetAsTrustedScriptURL()->toString();
  if (string_or_trusted_type.IsTrustedURL())
    return string_or_trusted_type.GetAsTrustedURL()->toString();

  return string_or_trusted_type.GetAsString();
}

String GetStringFromTrustedTypeWithoutCheck(
    const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURLOrTrustedURL&
        string_or_trusted_type) {
  if (string_or_trusted_type.IsTrustedHTML())
    return string_or_trusted_type.GetAsTrustedHTML()->toString();
  if (string_or_trusted_type.IsTrustedScript())
    return string_or_trusted_type.GetAsTrustedScript()->toString();
  if (string_or_trusted_type.IsTrustedScriptURL())
    return string_or_trusted_type.GetAsTrustedScriptURL()->toString();
  if (string_or_trusted_type.IsTrustedURL())
    return string_or_trusted_type.GetAsTrustedURL()->toString();
  if (string_or_trusted_type.IsString())
    return string_or_trusted_type.GetAsString();

  return g_empty_string;
}

String GetStringFromSpecificTrustedType(
    const StringOrTrustedHTMLOrTrustedScriptOrTrustedScriptURLOrTrustedURL&
        string_or_trusted_type,
    SpecificTrustedType specific_trusted_type,
    const Document* doc,
    ExceptionState& exception_state) {
  switch (specific_trusted_type) {
    case SpecificTrustedType::kTrustedHTML: {
      StringOrTrustedHTML string_or_trusted_html =
          string_or_trusted_type.IsTrustedHTML()
              ? StringOrTrustedHTML::FromTrustedHTML(
                    string_or_trusted_type.GetAsTrustedHTML())
              : StringOrTrustedHTML::FromString(
                    GetStringFromTrustedTypeWithoutCheck(
                        string_or_trusted_type));
      return GetStringFromTrustedHTML(string_or_trusted_html, doc,
                                      exception_state);
    }
    case SpecificTrustedType::kTrustedScript: {
      StringOrTrustedScript string_or_trusted_script =
          string_or_trusted_type.IsTrustedScript()
              ? StringOrTrustedScript::FromTrustedScript(
                    string_or_trusted_type.GetAsTrustedScript())
              : StringOrTrustedScript::FromString(
                    GetStringFromTrustedTypeWithoutCheck(
                        string_or_trusted_type));
      return GetStringFromTrustedScript(string_or_trusted_script, doc,
                                        exception_state);
    }
    case SpecificTrustedType::kTrustedScriptURL: {
      StringOrTrustedScriptURL string_or_trusted_script_url =
          string_or_trusted_type.IsTrustedScriptURL()
              ? StringOrTrustedScriptURL::FromTrustedScriptURL(
                    string_or_trusted_type.GetAsTrustedScriptURL())
              : StringOrTrustedScriptURL::FromString(
                    GetStringFromTrustedTypeWithoutCheck(
                        string_or_trusted_type));
      return GetStringFromTrustedScriptURL(string_or_trusted_script_url, doc,
                                           exception_state);
    }
    case SpecificTrustedType::kTrustedURL: {
      USVStringOrTrustedURL string_or_trusted_url =
          string_or_trusted_type.IsTrustedURL()
              ? USVStringOrTrustedURL::FromTrustedURL(
                    string_or_trusted_type.GetAsTrustedURL())
              : USVStringOrTrustedURL::FromUSVString(
                    GetStringFromTrustedTypeWithoutCheck(
                        string_or_trusted_type));
      return GetStringFromTrustedURL(string_or_trusted_url, doc,
                                     exception_state);
    }
  }
}

String GetStringFromTrustedHTML(StringOrTrustedHTML string_or_trusted_html,
                                const Document* doc,
                                ExceptionState& exception_state) {
  DCHECK(string_or_trusted_html.IsString() ||
         origin_trials::TrustedDOMTypesEnabled(doc));
  DCHECK(!string_or_trusted_html.IsNull());

  bool require_trusted_type = doc && doc->RequireTrustedTypes();
  if (!require_trusted_type && string_or_trusted_html.IsString()) {
    return string_or_trusted_html.GetAsString();
  }

  if (string_or_trusted_html.IsTrustedHTML()) {
    return string_or_trusted_html.GetAsTrustedHTML()->toString();
  }

  TrustedTypePolicy* default_policy =
      doc->ExecutingWindow()->trustedTypes()->getExposedPolicy("default");
  if (!default_policy) {
    exception_state.ThrowTypeError(
        "This document requires `TrustedHTML` assignment.");
    doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();
    return g_empty_string;
  }

  TrustedHTML* result = default_policy->CreateHTML(
      doc->GetIsolate(), string_or_trusted_html.GetAsString(), exception_state);
  if (exception_state.HadException()) {
    exception_state.ClearException();
    exception_state.ThrowTypeError(
        "This document requires `TrustedHTML` assignment and 'default' policy "
        "failed to execute.");
    doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();
    return g_empty_string;
  }

  return result->toString();
}

String GetStringFromTrustedScript(
    StringOrTrustedScript string_or_trusted_script,
    const Document* doc,
    ExceptionState& exception_state) {
  DCHECK(string_or_trusted_script.IsString() ||
         origin_trials::TrustedDOMTypesEnabled(doc));

  // To remain compatible with legacy behaviour, HTMLElement uses extended IDL
  // attributes to allow for nullable union of (DOMString or TrustedScript).
  // Thus, this method is required to handle the case where
  // string_or_trusted_script.IsNull(), unlike the various similar methods in
  // this file.

  bool require_trusted_type = doc && doc->RequireTrustedTypes();
  if (!require_trusted_type) {
    if (string_or_trusted_script.IsString()) {
      return string_or_trusted_script.GetAsString();
    }
    if (string_or_trusted_script.IsNull()) {
      return g_empty_string;
    }
  }

  if (string_or_trusted_script.IsTrustedScript()) {
    return string_or_trusted_script.GetAsTrustedScript()->toString();
  }

  DCHECK(require_trusted_type);
  DCHECK(string_or_trusted_script.IsNull() ||
         string_or_trusted_script.IsString());

  TrustedTypePolicy* default_policy =
      doc->ExecutingWindow()->trustedTypes()->getExposedPolicy("default");
  if (!default_policy) {
    exception_state.ThrowTypeError(
        "This document requires `TrustedScript` assignment.");
    doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();
    return g_empty_string;
  }

  const String& string_value_or_empty =
      string_or_trusted_script.IsNull()
          ? g_empty_string
          : string_or_trusted_script.GetAsString();
  TrustedScript* result = default_policy->CreateScript(
      doc->GetIsolate(), string_value_or_empty, exception_state);
  DCHECK_EQ(!result, exception_state.HadException());
  if (exception_state.HadException()) {
    exception_state.ClearException();
    exception_state.ThrowTypeError(
        "This document requires `TrustedScript` assignment and 'default' "
        "policy "
        "failed to execute.");
    doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();
    return g_empty_string;
  }

  return result->toString();
}

String GetStringFromTrustedScriptURL(
    StringOrTrustedScriptURL string_or_trusted_script_url,
    const Document* doc,
    ExceptionState& exception_state) {
  DCHECK(string_or_trusted_script_url.IsString() ||
         origin_trials::TrustedDOMTypesEnabled(doc));
  DCHECK(!string_or_trusted_script_url.IsNull());

  bool require_trusted_type = doc &&
                              origin_trials::TrustedDOMTypesEnabled(doc) &&
                              doc->RequireTrustedTypes();
  if (!require_trusted_type && string_or_trusted_script_url.IsString()) {
    return string_or_trusted_script_url.GetAsString();
  }

  if (string_or_trusted_script_url.IsTrustedScriptURL()) {
    return string_or_trusted_script_url.GetAsTrustedScriptURL()->toString();
  }

  TrustedTypePolicy* default_policy =
      doc->ExecutingWindow()->trustedTypes()->getExposedPolicy("default");
  if (!default_policy) {
    exception_state.ThrowTypeError(
        "This document requires `TrustedScriptURL` assignment.");
    doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();
    return g_empty_string;
  }

  TrustedScriptURL* result = default_policy->CreateScriptURL(
      doc->GetIsolate(), string_or_trusted_script_url.GetAsString(),
      exception_state);

  if (exception_state.HadException()) {
    exception_state.ClearException();
    exception_state.ThrowTypeError(
        "This document requires `TrustedScriptURL` assignment and 'default' "
        "policy "
        "failed to execute.");
    doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();
    return g_empty_string;
  }

  return result->toString();
}

String GetStringFromTrustedURL(USVStringOrTrustedURL string_or_trusted_url,
                               const Document* doc,
                               ExceptionState& exception_state) {
  DCHECK(string_or_trusted_url.IsUSVString() ||
         origin_trials::TrustedDOMTypesEnabled(doc));
  DCHECK(!string_or_trusted_url.IsNull());

  bool require_trusted_type = doc && doc->RequireTrustedTypes();
  if (!require_trusted_type && string_or_trusted_url.IsUSVString()) {
    return string_or_trusted_url.GetAsUSVString();
  }

  if (string_or_trusted_url.IsTrustedURL()) {
    return string_or_trusted_url.GetAsTrustedURL()->toString();
  }

  TrustedTypePolicy* default_policy =
      doc->ExecutingWindow()->trustedTypes()->getExposedPolicy("default");
  if (!default_policy) {
    exception_state.ThrowTypeError(
        "This document requires `TrustedURL` assignment.");
    doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();
    return g_empty_string;
  }

  TrustedURL* result = default_policy->CreateURL(
      doc->GetIsolate(), string_or_trusted_url.GetAsUSVString(),
      exception_state);
  if (exception_state.HadException()) {
    exception_state.ClearException();
    exception_state.ThrowTypeError(
        "This document requires `TrustedURL` assignment and 'default' policy "
        "failed to execute.");
    doc->ExecutingWindow()->trustedTypes()->CountTrustedTypeAssignmentError();
    return g_empty_string;
  }

  return result->toString();
}
}  // namespace blink