Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "extensions/renderer/messaging_util.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Hans Wennborg | 0997959 | 2020-04-27 12:34:30 | [diff] [blame] | 9 | #include "base/check.h" |
Devlin Cronin | fe7aae6 | 2017-11-16 03:49:55 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
Hans Wennborg | 0997959 | 2020-04-27 12:34:30 | [diff] [blame] | 11 | #include "base/notreached.h" |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
Devlin Cronin | b15f7f0 | 2018-01-31 19:37:32 | [diff] [blame] | 13 | #include "components/crx_file/id_util.h" |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 14 | #include "extensions/common/api/messaging/message.h" |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 15 | #include "extensions/common/extension.h" |
Devlin Cronin | b134472 | 2017-11-29 02:04:17 | [diff] [blame] | 16 | #include "extensions/common/manifest.h" |
| 17 | #include "extensions/common/manifest_handlers/background_info.h" |
Mustaq Ahmed | 4cd69a2 | 2018-11-15 16:34:53 | [diff] [blame] | 18 | #include "extensions/renderer/get_script_context.h" |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 19 | #include "extensions/renderer/script_context.h" |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 20 | #include "gin/converter.h" |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 21 | #include "gin/dictionary.h" |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame] | 22 | #include "third_party/blink/public/web/web_local_frame.h" |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 23 | |
| 24 | namespace extensions { |
| 25 | namespace messaging_util { |
| 26 | |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | constexpr char kExtensionIdRequiredErrorTemplate[] = |
| 30 | "chrome.%s() called from a webpage must specify an " |
| 31 | "Extension ID (string) for its first argument."; |
| 32 | |
Devlin Cronin | fe7aae6 | 2017-11-16 03:49:55 | [diff] [blame] | 33 | constexpr char kErrorCouldNotSerialize[] = "Could not serialize message."; |
| 34 | |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 35 | } // namespace |
| 36 | |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 37 | const char kSendMessageChannel[] = "chrome.runtime.sendMessage"; |
| 38 | const char kSendRequestChannel[] = "chrome.extension.sendRequest"; |
| 39 | |
Devlin Cronin | 182b089 | 2017-11-10 22:22:16 | [diff] [blame] | 40 | const char kOnMessageEvent[] = "runtime.onMessage"; |
| 41 | const char kOnMessageExternalEvent[] = "runtime.onMessageExternal"; |
| 42 | const char kOnRequestEvent[] = "extension.onRequest"; |
| 43 | const char kOnRequestExternalEvent[] = "extension.onRequestExternal"; |
| 44 | const char kOnConnectEvent[] = "runtime.onConnect"; |
| 45 | const char kOnConnectExternalEvent[] = "runtime.onConnectExternal"; |
Maksim Ivanov | 1dd8391e | 2019-02-26 01:08:19 | [diff] [blame] | 46 | const char kOnConnectNativeEvent[] = "runtime.onConnectNative"; |
Devlin Cronin | 182b089 | 2017-11-10 22:22:16 | [diff] [blame] | 47 | |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 48 | const int kNoFrameId = -1; |
| 49 | |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 50 | std::unique_ptr<Message> MessageFromV8(v8::Local<v8::Context> context, |
Devlin Cronin | fe7aae6 | 2017-11-16 03:49:55 | [diff] [blame] | 51 | v8::Local<v8::Value> value, |
| 52 | std::string* error_out) { |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 53 | DCHECK(!value.IsEmpty()); |
| 54 | v8::Isolate* isolate = context->GetIsolate(); |
| 55 | v8::Context::Scope context_scope(context); |
| 56 | |
| 57 | // TODO(devlin): For some reason, we don't use the signature for |
| 58 | // Port.postMessage when evaluating the parameters. We probably should, but |
| 59 | // we don't know how many extensions that may break. It would be good to |
| 60 | // investigate, and, ideally, use the signature. |
| 61 | |
| 62 | if (value->IsUndefined()) { |
| 63 | // JSON.stringify won't serialized undefined (it returns undefined), but it |
| 64 | // will serialized null. We've always converted undefined to null in JS |
| 65 | // bindings, so preserve this behavior for now. |
| 66 | value = v8::Null(isolate); |
| 67 | } |
| 68 | |
| 69 | bool success = false; |
| 70 | v8::Local<v8::String> stringified; |
| 71 | { |
| 72 | v8::TryCatch try_catch(isolate); |
| 73 | success = v8::JSON::Stringify(context, value).ToLocal(&stringified); |
| 74 | } |
| 75 | |
Devlin Cronin | fe7aae6 | 2017-11-16 03:49:55 | [diff] [blame] | 76 | if (!success) { |
| 77 | *error_out = kErrorCouldNotSerialize; |
| 78 | return nullptr; |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 79 | } |
| 80 | |
Mustaq Ahmed | 4cd69a2 | 2018-11-15 16:34:53 | [diff] [blame] | 81 | ScriptContext* script_context = GetScriptContextFromV8Context(context); |
| 82 | blink::WebLocalFrame* web_frame = |
| 83 | script_context ? script_context->web_frame() : nullptr; |
Mustaq Ahmed | b4ad5a5 | 2020-10-19 16:06:02 | [diff] [blame^] | 84 | bool privileged_context = |
| 85 | script_context && script_context->context_type() == |
| 86 | extensions::Feature::BLESSED_EXTENSION_CONTEXT; |
| 87 | return MessageFromJSONString(isolate, stringified, error_out, web_frame, |
| 88 | privileged_context); |
Devlin Cronin | fe7aae6 | 2017-11-16 03:49:55 | [diff] [blame] | 89 | } |
| 90 | |
Mustaq Ahmed | b4ad5a5 | 2020-10-19 16:06:02 | [diff] [blame^] | 91 | std::unique_ptr<Message> MessageFromJSONString(v8::Isolate* isolate, |
| 92 | v8::Local<v8::String> json, |
| 93 | std::string* error_out, |
| 94 | blink::WebLocalFrame* web_frame, |
| 95 | bool privileged_context) { |
Devlin Cronin | fe7aae6 | 2017-11-16 03:49:55 | [diff] [blame] | 96 | std::string message; |
Dan Elphick | 38a50805 | 2018-07-23 22:19:53 | [diff] [blame] | 97 | message = gin::V8ToString(isolate, json); |
Devlin Cronin | fe7aae6 | 2017-11-16 03:49:55 | [diff] [blame] | 98 | // JSON.stringify can fail to produce a string value in one of two ways: it |
| 99 | // can throw an exception (as with unserializable objects), or it can return |
| 100 | // `undefined` (as with e.g. passing a function). If JSON.stringify returns |
| 101 | // `undefined`, the v8 API then coerces it to the string value "undefined". |
| 102 | // Check for this, and consider it a failure (since we didn't properly |
| 103 | // serialize a value). |
| 104 | if (message == "undefined") { |
| 105 | *error_out = kErrorCouldNotSerialize; |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 106 | return nullptr; |
Devlin Cronin | fe7aae6 | 2017-11-16 03:49:55 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | size_t message_length = message.length(); |
| 110 | |
| 111 | // Max bucket at 512 MB - anything over that, and we don't care. |
| 112 | static constexpr int kMaxUmaLength = 1024 * 1024 * 512; |
| 113 | static constexpr int kMinUmaLength = 1; |
| 114 | static constexpr int kBucketCount = 50; |
| 115 | UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.Messaging.MessageSize", |
| 116 | message_length, kMinUmaLength, kMaxUmaLength, |
| 117 | kBucketCount); |
| 118 | |
| 119 | // IPC messages will fail at > 128 MB. Restrict extension messages to 64 MB. |
| 120 | // A 64 MB JSON-ifiable object is scary enough as is. |
| 121 | static constexpr size_t kMaxMessageLength = 1024 * 1024 * 64; |
| 122 | if (message_length > kMaxMessageLength) { |
| 123 | *error_out = "Message length exceeded maximum allowed length."; |
| 124 | return nullptr; |
| 125 | } |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 126 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame] | 127 | bool has_transient_user_activation = |
| 128 | web_frame ? web_frame->HasTransientUserActivation() : false; |
Mustaq Ahmed | b4ad5a5 | 2020-10-19 16:06:02 | [diff] [blame^] | 129 | return std::make_unique<Message>(message, has_transient_user_activation, |
| 130 | privileged_context); |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | v8::Local<v8::Value> MessageToV8(v8::Local<v8::Context> context, |
| 134 | const Message& message) { |
| 135 | v8::Isolate* isolate = context->GetIsolate(); |
| 136 | v8::Context::Scope context_scope(context); |
| 137 | |
| 138 | v8::Local<v8::String> v8_message_string = |
| 139 | gin::StringToV8(isolate, message.data); |
| 140 | v8::Local<v8::Value> parsed_message; |
| 141 | v8::TryCatch try_catch(isolate); |
| 142 | if (!v8::JSON::Parse(context, v8_message_string).ToLocal(&parsed_message)) { |
| 143 | NOTREACHED(); |
| 144 | return v8::Local<v8::Value>(); |
| 145 | } |
| 146 | return parsed_message; |
| 147 | } |
| 148 | |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 149 | int ExtractIntegerId(v8::Local<v8::Value> value) { |
Dan Elphick | d010a85a | 2018-08-03 11:32:26 | [diff] [blame] | 150 | if (value->IsInt32()) |
| 151 | return value.As<v8::Int32>()->Value(); |
| 152 | |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 153 | // Account for -0, which is a valid integer, but is stored as a number in v8. |
Dan Elphick | d010a85a | 2018-08-03 11:32:26 | [diff] [blame] | 154 | DCHECK(value->IsNumber() && value.As<v8::Number>()->Value() == 0.0); |
| 155 | return 0; |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 156 | } |
| 157 | |
Devlin Cronin | 85efd62 | 2017-12-05 19:31:57 | [diff] [blame] | 158 | MessageOptions ParseMessageOptions(v8::Local<v8::Context> context, |
| 159 | v8::Local<v8::Object> v8_options, |
| 160 | int flags) { |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 161 | DCHECK(!v8_options.IsEmpty()); |
| 162 | DCHECK(!v8_options->IsNull()); |
| 163 | |
| 164 | v8::Isolate* isolate = context->GetIsolate(); |
| 165 | |
| 166 | MessageOptions options; |
| 167 | |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 168 | gin::Dictionary options_dict(isolate, v8_options); |
| 169 | if ((flags & PARSE_CHANNEL_NAME) != 0) { |
| 170 | v8::Local<v8::Value> v8_channel_name; |
Devlin Cronin | 85efd62 | 2017-12-05 19:31:57 | [diff] [blame] | 171 | bool success = options_dict.Get("name", &v8_channel_name); |
| 172 | DCHECK(success); |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 173 | |
| 174 | if (!v8_channel_name->IsUndefined()) { |
Devlin Cronin | 85efd62 | 2017-12-05 19:31:57 | [diff] [blame] | 175 | DCHECK(v8_channel_name->IsString()); |
Dan Elphick | 38a50805 | 2018-07-23 22:19:53 | [diff] [blame] | 176 | options.channel_name = gin::V8ToString(isolate, v8_channel_name); |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 180 | if ((flags & PARSE_FRAME_ID) != 0) { |
| 181 | v8::Local<v8::Value> v8_frame_id; |
Devlin Cronin | 85efd62 | 2017-12-05 19:31:57 | [diff] [blame] | 182 | bool success = options_dict.Get("frameId", &v8_frame_id); |
| 183 | DCHECK(success); |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 184 | |
| 185 | if (!v8_frame_id->IsUndefined()) { |
Devlin Cronin | 85efd62 | 2017-12-05 19:31:57 | [diff] [blame] | 186 | DCHECK(v8_frame_id->IsInt32()); |
Devlin Cronin | f054d0e | 2018-04-21 00:25:33 | [diff] [blame] | 187 | int frame_id = v8_frame_id.As<v8::Int32>()->Value(); |
| 188 | // NOTE(devlin): JS bindings coerce any negative value to -1. For |
| 189 | // backwards compatibility, we do the same here. |
| 190 | options.frame_id = frame_id < 0 ? -1 : frame_id; |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Nick Harper | 41374d5 | 2020-01-30 22:36:47 | [diff] [blame] | 194 | // Note: the options object may also include an includeTlsChannelId property. |
| 195 | // That property has been a no-op since M72. See crbug.com/1045232. |
Devlin Cronin | 85efd62 | 2017-12-05 19:31:57 | [diff] [blame] | 196 | return options; |
Devlin Cronin | 3719893 | 2017-11-07 20:15:23 | [diff] [blame] | 197 | } |
| 198 | |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 199 | bool GetTargetExtensionId(ScriptContext* script_context, |
| 200 | v8::Local<v8::Value> v8_target_id, |
| 201 | const char* method_name, |
| 202 | std::string* target_out, |
| 203 | std::string* error_out) { |
| 204 | DCHECK(!v8_target_id.IsEmpty()); |
Devlin Cronin | e4eb1d91 | 2018-03-23 14:55:30 | [diff] [blame] | 205 | // Argument parsing should guarantee this is null or a string before we reach |
| 206 | // this point. |
| 207 | DCHECK(v8_target_id->IsNull() || v8_target_id->IsString()); |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 208 | |
| 209 | std::string target_id; |
Devlin Cronin | e4eb1d91 | 2018-03-23 14:55:30 | [diff] [blame] | 210 | // If omitted, we use the extension associated with the context. |
| 211 | // Note: we deliberately treat the empty string as omitting the id, even |
| 212 | // though it's not strictly correct. See https://crbug.com/823577. |
| 213 | if (v8_target_id->IsNull() || |
| 214 | (v8_target_id->IsString() && |
| 215 | v8_target_id.As<v8::String>()->Length() == 0)) { |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 216 | if (!script_context->extension()) { |
| 217 | *error_out = |
| 218 | base::StringPrintf(kExtensionIdRequiredErrorTemplate, method_name); |
| 219 | return false; |
| 220 | } |
| 221 | |
Devlin Cronin | b15f7f0 | 2018-01-31 19:37:32 | [diff] [blame] | 222 | target_id = script_context->extension()->id(); |
| 223 | // An extension should never have an invalid id. |
| 224 | DCHECK(crx_file::id_util::IdIsValid(target_id)); |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 225 | } else { |
| 226 | DCHECK(v8_target_id->IsString()); |
Dan Elphick | 38a50805 | 2018-07-23 22:19:53 | [diff] [blame] | 227 | target_id = gin::V8ToString(script_context->isolate(), v8_target_id); |
Devlin Cronin | b15f7f0 | 2018-01-31 19:37:32 | [diff] [blame] | 228 | // NOTE(devlin): JS bindings only validate that the extension id is present, |
| 229 | // rather than validating its content. This seems better. Let's see how this |
| 230 | // goes. |
| 231 | if (!crx_file::id_util::IdIsValid(target_id)) { |
| 232 | *error_out = |
| 233 | base::StringPrintf("Invalid extension id: '%s'", target_id.c_str()); |
| 234 | return false; |
| 235 | } |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 236 | } |
| 237 | |
Devlin Cronin | b15f7f0 | 2018-01-31 19:37:32 | [diff] [blame] | 238 | *target_out = std::move(target_id); |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 239 | return true; |
| 240 | } |
| 241 | |
| 242 | void MassageSendMessageArguments( |
| 243 | v8::Isolate* isolate, |
| 244 | bool allow_options_argument, |
| 245 | std::vector<v8::Local<v8::Value>>* arguments_out) { |
| 246 | base::span<const v8::Local<v8::Value>> arguments = *arguments_out; |
| 247 | size_t max_size = allow_options_argument ? 4u : 3u; |
| 248 | if (arguments.empty() || arguments.size() > max_size) |
| 249 | return; |
| 250 | |
| 251 | v8::Local<v8::Value> target_id = v8::Null(isolate); |
| 252 | v8::Local<v8::Value> message = v8::Null(isolate); |
| 253 | v8::Local<v8::Value> options; |
| 254 | if (allow_options_argument) |
| 255 | options = v8::Null(isolate); |
| 256 | v8::Local<v8::Value> response_callback = v8::Null(isolate); |
| 257 | |
| 258 | // If the last argument is a function, it is the response callback. |
| 259 | // Ignore it for the purposes of further argument parsing. |
| 260 | if ((*arguments.rbegin())->IsFunction()) { |
| 261 | response_callback = *arguments.rbegin(); |
| 262 | arguments = arguments.first(arguments.size() - 1); |
| 263 | } |
| 264 | |
| 265 | // Re-check for too many arguments after looking for the callback. If there |
| 266 | // are, early-out and rely on normal signature parsing to report the error. |
| 267 | if (arguments.size() >= max_size) |
| 268 | return; |
| 269 | |
| 270 | switch (arguments.size()) { |
| 271 | case 0: |
| 272 | // Required argument (message) is missing. |
| 273 | // Early-out and rely on normal signature parsing to report this error. |
| 274 | return; |
| 275 | case 1: |
| 276 | // Argument must be the message. |
| 277 | message = arguments[0]; |
| 278 | break; |
Devlin Cronin | 7c723eb6 | 2018-04-13 19:38:26 | [diff] [blame] | 279 | case 2: { |
| 280 | // Assume the first argument is the ID if we don't expect options, or if |
| 281 | // the argument could match the ID parameter. |
| 282 | // ID could be either a string, or null/undefined (since it's optional). |
| 283 | bool could_match_id = |
| 284 | arguments[0]->IsString() || arguments[0]->IsNullOrUndefined(); |
| 285 | if (!allow_options_argument || could_match_id) { |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 286 | target_id = arguments[0]; |
| 287 | message = arguments[1]; |
Devlin Cronin | 7c723eb6 | 2018-04-13 19:38:26 | [diff] [blame] | 288 | } else { // Otherwise, the meaning is (message, options). |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 289 | message = arguments[0]; |
| 290 | options = arguments[1]; |
| 291 | } |
| 292 | break; |
Devlin Cronin | 7c723eb6 | 2018-04-13 19:38:26 | [diff] [blame] | 293 | } |
Devlin Cronin | c4b07fb | 2017-11-14 20:26:34 | [diff] [blame] | 294 | case 3: |
| 295 | DCHECK(allow_options_argument); |
| 296 | // The meaning in this case is unambiguous. |
| 297 | target_id = arguments[0]; |
| 298 | message = arguments[1]; |
| 299 | options = arguments[2]; |
| 300 | break; |
| 301 | default: |
| 302 | NOTREACHED(); |
| 303 | } |
| 304 | |
| 305 | if (allow_options_argument) |
| 306 | *arguments_out = {target_id, message, options, response_callback}; |
| 307 | else |
| 308 | *arguments_out = {target_id, message, response_callback}; |
| 309 | } |
| 310 | |
Devlin Cronin | b134472 | 2017-11-29 02:04:17 | [diff] [blame] | 311 | bool IsSendRequestDisabled(ScriptContext* script_context) { |
| 312 | const Extension* extension = script_context->extension(); |
| 313 | return extension && Manifest::IsUnpackedLocation(extension->location()) && |
| 314 | BackgroundInfo::HasLazyBackgroundPage(extension); |
| 315 | } |
| 316 | |
Devlin Cronin | 0b87567 | 2017-10-06 00:49:21 | [diff] [blame] | 317 | } // namespace messaging_util |
| 318 | } // namespace extensions |