Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: extensions/renderer/object_backed_native_handler.cc

Issue 1843803002: [Extensions] Add an access check before executing native code in the renderer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/renderer/object_backed_native_handler.h" 5 #include "extensions/renderer/object_backed_native_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/linked_ptr.h" 10 #include "base/memory/linked_ptr.h"
11 #include "content/public/child/worker_thread.h"
11 #include "extensions/renderer/console.h" 12 #include "extensions/renderer/console.h"
12 #include "extensions/renderer/module_system.h" 13 #include "extensions/renderer/module_system.h"
13 #include "extensions/renderer/script_context.h" 14 #include "extensions/renderer/script_context.h"
14 #include "extensions/renderer/script_context_set.h" 15 #include "extensions/renderer/script_context_set.h"
16 #include "extensions/renderer/v8_helpers.h"
15 #include "v8/include/v8.h" 17 #include "v8/include/v8.h"
16 18
17 namespace extensions { 19 namespace extensions {
18 20
19 namespace { 21 namespace {
20 // Key for the base::Bound routed function. 22 // Key for the base::Bound routed function.
21 const char* kHandlerFunction = "handler_function"; 23 const char* kHandlerFunction = "handler_function";
24 const char* kFeatureName = "feature_name";
22 } // namespace 25 } // namespace
23 26
24 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context) 27 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context)
25 : router_data_(context->isolate()), 28 : router_data_(context->isolate()),
26 context_(context), 29 context_(context),
27 object_template_(context->isolate(), 30 object_template_(context->isolate(),
28 v8::ObjectTemplate::New(context->isolate())) { 31 v8::ObjectTemplate::New(context->isolate())) {
29 } 32 }
30 33
31 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { 34 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() {
32 } 35 }
33 36
34 v8::Local<v8::Object> ObjectBackedNativeHandler::NewInstance() { 37 v8::Local<v8::Object> ObjectBackedNativeHandler::NewInstance() {
35 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_) 38 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_)
36 ->NewInstance(); 39 ->NewInstance();
37 } 40 }
38 41
39 // static 42 // static
40 void ObjectBackedNativeHandler::Router( 43 void ObjectBackedNativeHandler::Router(
41 const v8::FunctionCallbackInfo<v8::Value>& args) { 44 const v8::FunctionCallbackInfo<v8::Value>& args) {
42 v8::Isolate* isolate = args.GetIsolate(); 45 v8::Isolate* isolate = args.GetIsolate();
43 v8::HandleScope handle_scope(isolate); 46 v8::HandleScope handle_scope(isolate);
44 v8::Local<v8::Object> data = args.Data().As<v8::Object>(); 47 v8::Local<v8::Object> data = args.Data().As<v8::Object>();
45 v8::Local<v8::Context> context = isolate->GetCurrentContext(); 48 v8::Local<v8::Context> context = isolate->GetCurrentContext();
46 49
47 v8::Local<v8::Value> handler_function_value; 50 v8::Local<v8::Value> handler_function_value;
51 v8::Local<v8::Value> feature_name_value;
48 // See comment in header file for why we do this. 52 // See comment in header file for why we do this.
49 if (!GetPrivate(context, data, kHandlerFunction, &handler_function_value) || 53 if (!GetPrivate(context, data, kHandlerFunction, &handler_function_value) ||
50 handler_function_value->IsUndefined()) { 54 handler_function_value->IsUndefined() ||
55 !GetPrivate(context, data, kFeatureName, &feature_name_value) ||
56 !feature_name_value->IsString()) {
51 ScriptContext* script_context = 57 ScriptContext* script_context =
52 ScriptContextSet::GetContextByV8Context(context); 58 ScriptContextSet::GetContextByV8Context(context);
53 console::Error(script_context ? script_context->GetRenderFrame() : nullptr, 59 console::Error(script_context ? script_context->GetRenderFrame() : nullptr,
54 "Extension view no longer exists"); 60 "Extension view no longer exists");
55 return; 61 return;
56 } 62 }
63
64 // We can't access the ScriptContextSet on a worker thread. Luckily, we also
65 // don't inject many bindings into worker threads.
66 // TODO(devlin): Figure out a way around this.
67 if (content::WorkerThread::GetCurrentId() == 0) {
68 ScriptContext* script_context =
69 ScriptContextSet::GetContextByV8Context(context);
70 v8::Local<v8::String> feature_name_string =
71 feature_name_value->ToString(context).ToLocalChecked();
72 std::string feature_name = *v8::String::Utf8Value(feature_name_string);
73 // TODO(devlin): Eventually, we should fail if either script_context is null
74 // or feature_name is empty.
75 if (script_context &&
76 !feature_name.empty() &&
77 !script_context->GetAvailability(feature_name).is_available()) {
78 return;
79 }
80 }
57 // This CHECK is *important*. Otherwise, we'll go around happily executing 81 // This CHECK is *important*. Otherwise, we'll go around happily executing
58 // something random. See crbug.com/548273. 82 // something random. See crbug.com/548273.
59 CHECK(handler_function_value->IsExternal()); 83 CHECK(handler_function_value->IsExternal());
60 static_cast<HandlerFunction*>( 84 static_cast<HandlerFunction*>(
61 handler_function_value.As<v8::External>()->Value())->Run(args); 85 handler_function_value.As<v8::External>()->Value())->Run(args);
62 } 86 }
63 87
64 void ObjectBackedNativeHandler::RouteFunction( 88 void ObjectBackedNativeHandler::RouteFunction(
65 const std::string& name, 89 const std::string& name,
66 const HandlerFunction& handler_function) { 90 const HandlerFunction& handler_function) {
91 RouteFunction(name, "", handler_function);
92 }
93
94 void ObjectBackedNativeHandler::RouteFunction(
95 const std::string& name,
96 const std::string& feature_name,
97 const HandlerFunction& handler_function) {
67 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 98 v8::Isolate* isolate = v8::Isolate::GetCurrent();
68 v8::HandleScope handle_scope(isolate); 99 v8::HandleScope handle_scope(isolate);
69 v8::Context::Scope context_scope(context_->v8_context()); 100 v8::Context::Scope context_scope(context_->v8_context());
70 101
71 v8::Local<v8::Object> data = v8::Object::New(isolate); 102 v8::Local<v8::Object> data = v8::Object::New(isolate);
72 SetPrivate(data, kHandlerFunction, 103 SetPrivate(data, kHandlerFunction,
73 v8::External::New(isolate, new HandlerFunction(handler_function))); 104 v8::External::New(isolate, new HandlerFunction(handler_function)));
105 SetPrivate(data, kFeatureName,
106 v8_helpers::ToV8StringUnsafe(isolate, feature_name));
74 v8::Local<v8::FunctionTemplate> function_template = 107 v8::Local<v8::FunctionTemplate> function_template =
75 v8::FunctionTemplate::New(isolate, Router, data); 108 v8::FunctionTemplate::New(isolate, Router, data);
76 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) 109 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_)
77 ->Set(isolate, name.c_str(), function_template); 110 ->Set(isolate, name.c_str(), function_template);
78 router_data_.Append(data); 111 router_data_.Append(data);
79 } 112 }
80 113
81 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { 114 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const {
82 return context_->isolate(); 115 return context_->isolate();
83 } 116 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 v8::Local<v8::Object> obj, 181 v8::Local<v8::Object> obj,
149 const char* key) { 182 const char* key) {
150 obj->DeletePrivate(context, 183 obj->DeletePrivate(context,
151 v8::Private::ForApi( 184 v8::Private::ForApi(
152 context->GetIsolate(), 185 context->GetIsolate(),
153 v8::String::NewFromUtf8(context->GetIsolate(), key))) 186 v8::String::NewFromUtf8(context->GetIsolate(), key)))
154 .FromJust(); 187 .FromJust();
155 } 188 }
156 189
157 } // namespace extensions 190 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/object_backed_native_handler.h ('k') | extensions/renderer/script_context.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698