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