File: script_context_set.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (361 lines) | stat: -rw-r--r-- 14,408 bytes parent folder | download | duplicates (6)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/script_context_set.h"

#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "content/public/common/url_constants.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/worker_thread.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/mojom/host_id.mojom.h"
#include "extensions/common/utils/extension_utils.h"
#include "extensions/renderer/extension_frame_helper.h"
#include "extensions/renderer/extensions_renderer_client.h"
#include "extensions/renderer/isolated_world_manager.h"
#include "extensions/renderer/renderer_frame_context_data.h"
#include "extensions/renderer/script_context.h"
#include "pdf/buildflags.h"
#include "third_party/blink/public/platform/scheduler/web_agent_group_scheduler.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "url/origin.h"
#include "v8/include/v8-isolate.h"
#include "v8/include/v8-object.h"

namespace extensions {

namespace {
// There is only ever one instance of the ScriptContextSet.
ScriptContextSet* g_context_set = nullptr;
}

ScriptContextSet::ScriptContextSet(ExtensionIdSet* active_extension_ids)
    : active_extension_ids_(active_extension_ids) {
  DCHECK(!g_context_set);
  g_context_set = this;
}

ScriptContextSet::~ScriptContextSet() {
  g_context_set = nullptr;
}

ScriptContext* ScriptContextSet::Register(
    blink::WebLocalFrame* frame,
    const v8::Local<v8::Context>& v8_context,
    int32_t world_id,
    bool is_webview) {
  const Extension* extension =
      GetExtensionFromFrameAndWorld(frame, world_id, false);
  const Extension* effective_extension =
      GetExtensionFromFrameAndWorld(frame, world_id, true);

  mojom::ViewType view_type = mojom::ViewType::kInvalid;
  content::RenderFrame* render_frame =
      content::RenderFrame::FromWebFrame(frame);
  // In production, we should always have a corresponding render frame.
  // Unfortunately, this isn't the case in unit tests, so we can't DCHECK here.
  if (render_frame) {
    ExtensionFrameHelper* frame_helper =
        ExtensionFrameHelper::Get(render_frame);
    DCHECK(frame_helper);
    view_type = frame_helper->view_type();
  }
  GURL frame_url = ScriptContext::GetDocumentLoaderURLForFrame(frame);
  mojom::ContextType context_type = ClassifyJavaScriptContext(
      extension, world_id, frame_url, frame->GetDocument().GetSecurityOrigin(),
      view_type, is_webview);
  mojom::ContextType effective_context_type = ClassifyJavaScriptContext(
      effective_extension, world_id,
      ScriptContext::GetEffectiveDocumentURLForContext(frame, frame_url, true),
      frame->GetDocument().GetSecurityOrigin(), view_type, is_webview);

  mojom::HostID host_id;
  RendererFrameContextData context_data = RendererFrameContextData(frame);
  // By default, the context will use a HostID kExtensions type. Specific
  // cases override that value below.
  host_id.type = mojom::HostID::HostType::kExtensions;
  if (extension) {
    host_id.id = extension->id();
  } else if (effective_context_type == mojom::ContextType::kWebUi) {
    host_id.type = mojom::HostID::HostType::kWebUi;
  } else if (effective_context_type == mojom::ContextType::kWebPage &&
             !is_webview && context_data.HasControlledFrameCapability()) {
    host_id.type = mojom::HostID::HostType::kControlledFrameEmbedder;
    host_id.id = url::Origin::Create(frame_url).Serialize();
  }

  std::optional<int> blink_isolated_world_id;
  if (IsolatedWorldManager::IsExtensionIsolatedWorld(world_id)) {
    blink_isolated_world_id = world_id;
  }

  ScriptContext* context = new ScriptContext(
      v8_context, frame, host_id, extension, std::move(blink_isolated_world_id),
      context_type, effective_extension, effective_context_type);
  contexts_.insert(context);  // takes ownership
  return context;
}

void ScriptContextSet::Remove(ScriptContext* context) {
  if (contexts_.erase(context)) {
    context->Invalidate();
    base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE,
                                                                  context);
  }
}

ScriptContext* ScriptContextSet::GetCurrent() const {
  v8::Isolate* isolate = v8::Isolate::TryGetCurrent();
  if (!isolate) [[unlikely]] {
    return nullptr;
  }
  return isolate->InContext() ? GetByV8Context(isolate->GetCurrentContext())
                              : nullptr;
}

ScriptContext* ScriptContextSet::GetByV8Context(
    const v8::Local<v8::Context>& v8_context) const {
  for (ScriptContext* script_context : contexts_) {
    if (script_context->v8_context() == v8_context)
      return script_context;
  }
  return nullptr;
}

ScriptContext* ScriptContextSet::GetContextByObject(
    const v8::Local<v8::Object>& object) {
  v8::Local<v8::Context> context;
  if (!object->GetCreationContext().ToLocal(&context))
    return nullptr;
  return GetContextByV8Context(context);
}

ScriptContext* ScriptContextSet::GetContextByV8Context(
    const v8::Local<v8::Context>& v8_context) {
  // g_context_set can be null in unittests.
  return g_context_set ? g_context_set->GetByV8Context(v8_context) : nullptr;
}

ScriptContext* ScriptContextSet::GetMainWorldContextForFrame(
    content::RenderFrame* render_frame) {
  blink::WebLocalFrame* web_frame = render_frame->GetWebFrame();
  v8::HandleScope handle_scope(web_frame->GetAgentGroupScheduler()->Isolate());
  return GetContextByV8Context(web_frame->MainWorldScriptContext());
}

void ScriptContextSet::ForEach(
    const mojom::HostID& host_id,
    content::RenderFrame* render_frame,
    const base::RepeatingCallback<void(ScriptContext*)>& callback) {
  // We copy the context list, because calling into javascript may modify it
  // out from under us.
  std::set<raw_ptr<ScriptContext, SetExperimental>> contexts_copy = contexts_;

  for (ScriptContext* context : contexts_copy) {
    // For the same reason as above, contexts may become invalid while we run.
    if (!context->is_valid()) {
      continue;
    }

    switch (host_id.type) {
      case mojom::HostID::HostType::kExtensions:
        // Note: If the type is kExtensions and host_id.id is empty, then the
        // call should affect all extensions. See comment in dispatcher.cc
        // UpdateAllBindings().
        if (host_id.id.empty() || context->GetExtensionID() == host_id.id) {
          ExecuteCallbackWithContext(context, render_frame, callback);
        }
        break;

      case mojom::HostID::HostType::kWebUi:
        DCHECK(host_id.id.empty());
        ExecuteCallbackWithContext(context, render_frame, callback);
        break;

      case mojom::HostID::HostType::kControlledFrameEmbedder:
        DCHECK(!host_id.id.empty());
        // Verify that host_id matches context->host_id.
        if (context->host_id().type == host_id.type &&
            context->host_id().id == host_id.id) {
          ExecuteCallbackWithContext(context, render_frame, callback);
        }
    }
  }
}

void ScriptContextSet::ExecuteCallbackWithContext(
    ScriptContext* context,
    content::RenderFrame* render_frame,
    const base::RepeatingCallback<void(ScriptContext*)>& callback) {
  CHECK(context);
  content::RenderFrame* context_render_frame = context->GetRenderFrame();
  if (!render_frame || render_frame == context_render_frame) {
    callback.Run(context);
  }
}

void ScriptContextSet::OnExtensionUnloaded(const ExtensionId& extension_id) {
  ScriptContextSetIterable::ForEach(
      GenerateHostIdFromExtensionId(extension_id),
      base::BindRepeating(&ScriptContextSet::Remove, base::Unretained(this)));
}

void ScriptContextSet::AddForTesting(std::unique_ptr<ScriptContext> context) {
  contexts_.insert(context.release());  // Takes ownership
}

const Extension* ScriptContextSet::GetExtensionFromFrameAndWorld(
    blink::WebLocalFrame* frame,
    int32_t world_id,
    bool use_effective_url) {
  ExtensionId extension_id;
  if (world_id != 0) {
    // Isolated worlds (content script).
    extension_id =
        IsolatedWorldManager::GetInstance().GetHostIdForIsolatedWorld(world_id);
  } else {
    // For looking up the extension associated with this frame, we either want
    // to use the current url or possibly the data source url (which this frame
    // may be navigating to shortly), depending on the security origin of the
    // frame. We don't always want to use the data source url because some
    // frames (eg iframes and windows created via window.open) briefly contain
    // an about:blank script context that is scriptable by their parent/opener
    // before they finish navigating.
    GURL frame_url = ScriptContext::GetAccessCheckedFrameURL(frame);
    frame_url = ScriptContext::GetEffectiveDocumentURLForContext(
        frame, frame_url, use_effective_url);
    extension_id =
        RendererExtensionRegistry::Get()->GetExtensionOrAppIDByURL(frame_url);
  }

  // There are conditions where despite a context being associated with an
  // extension, no extension actually gets found. Ignore "invalid" because CSP
  // blocks extension page loading by switching the extension ID to "invalid".
  const Extension* extension =
      RendererExtensionRegistry::Get()->GetByID(extension_id);
  if (!extension && !extension_id.empty() && extension_id != "invalid") {
    // TODO(kalman): Do something here?
  }
  return extension;
}

mojom::ContextType ScriptContextSet::ClassifyJavaScriptContext(
    const Extension* extension,
    int32_t world_id,
    const GURL& url,
    const blink::WebSecurityOrigin& origin,
    mojom::ViewType view_type,
    bool is_webview) {
  // WARNING: This logic must match `ProcessMap::GetMostLikelyContextType()`
  // as much as possible.

  // Worlds not within this range are not for content scripts, so ignore them.
  // TODO(devlin): Isolated worlds with a non-zero id could belong to
  // chrome-internal pieces, like dom distiller and translate. Do we need any
  // bindings (even those for basic web pages) for those?
  if (world_id >= ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId()) {
    // A non-main-world should (currently) only ever happen on the main thread.
    // We don't support injection of content scripts or user scripts into worker
    // contexts.
    CHECK_EQ(kMainThreadId, content::WorkerThread::GetCurrentId());
    std::optional<mojom::ExecutionWorld> execution_world =
        IsolatedWorldManager::GetInstance().GetExecutionWorldForIsolatedWorld(
            world_id);
    if (execution_world == mojom::ExecutionWorld::kUserScript) {
      CHECK(extension);
      return mojom::ContextType::kUserScript;
    }

    return extension ?  // TODO(kalman): when does this happen?
               mojom::ContextType::kContentScript
                     : mojom::ContextType::kUnspecified;
  }

  // We have an explicit check for sandboxed pages before checking whether the
  // extension is active in this process because:
  // 1. Sandboxed extension pages which are not listed in the extension's
  //    manifest sandbox section run in the same process as regular extension
  //    pages, so the extension is considered active. (In contrast,
  //    manifest-sandboxed pages run in a different process because they do not
  //    have API access.)
  // 2. ScriptContext creation (which triggers bindings injection) happens
  //    before the SecurityContext is updated with the sandbox flags (after
  //    reading the CSP header), so the caller can't check if the context's
  //    security origin is unique yet.
  if (ScriptContext::IsSandboxedPage(url)) {
    // TODO(https://crbug.com/347031402): it's weird returning kWebPage if
    // `extension` is non-null (which it is if `IsSandboxedPage` returns true).
    // It would be better to return kUnprivileged in that case.
    return mojom::ContextType::kWebPage;
  }

  if (extension && active_extension_ids_->count(extension->id()) > 0) {
    // |extension| is active in this process, but it could be either a true
    // extension process or within the extent of a hosted app. In the latter
    // case this would usually be considered a (privileged) web page context,
    // unless the extension in question is a component extension, in which case
    // we cheat and call it privileged.
    if (extension->is_hosted_app() &&
        extension->location() != mojom::ManifestLocation::kComponent) {
      return mojom::ContextType::kPrivilegedWebPage;
    }

    if (is_webview) {
#if BUILDFLAG(ENABLE_PDF)
      // The PDF Viewer extension in a webview needs to be a privileged
      // extension in order to load.
      if (extension->id() == extension_misc::kPdfExtensionId) {
        return mojom::ContextType::kPrivilegedExtension;
      }
#endif  // BUILDFLAG(ENABLE_PDF)

      return mojom::ContextType::kUnprivilegedExtension;
    }

    if (view_type == mojom::ViewType::kOffscreenDocument) {
      return mojom::ContextType::kOffscreenExtension;
    }

    return mojom::ContextType::kPrivilegedExtension;
  }

  // None of the following feature types should ever be present in an
  // offscreen document.
  DCHECK_NE(mojom::ViewType::kOffscreenDocument, view_type);

  // TODO(kalman): This IsOpaque() check is wrong, it should be performed as
  // part of ScriptContext::IsSandboxedPage().
  if (!origin.IsOpaque() &&
      RendererExtensionRegistry::Get()->ExtensionBindingsAllowed(url)) {
    if (!extension)  // TODO(kalman): when does this happen?
      return mojom::ContextType::kUnspecified;
    return extension->is_hosted_app()
               ? mojom::ContextType::kPrivilegedWebPage
               : mojom::ContextType::kUnprivilegedExtension;
  }

  if (!url.is_valid()) {
    return mojom::ContextType::kUnspecified;
  }

  if (url.SchemeIs(content::kChromeUIScheme)) {
    return mojom::ContextType::kWebUi;
  }

  if (url.SchemeIs(content::kChromeUIUntrustedScheme)) {
    return mojom::ContextType::kUntrustedWebUi;
  }

  return mojom::ContextType::kWebPage;
}

}  // namespace extensions