v8_value_converter.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/v8_value_converter.h"
  5. #include <map>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/values.h"
  11. #include "shell/common/gin_helper/dictionary.h"
  12. #include "shell/common/node_bindings.h"
  13. #include "shell/common/node_includes.h"
  14. namespace electron {
  15. namespace {
  16. const int kMaxRecursionDepth = 100;
  17. } // namespace
  18. // The state of a call to FromV8Value.
  19. class V8ValueConverter::FromV8ValueState {
  20. public:
  21. // Level scope which updates the current depth of some FromV8ValueState.
  22. class Level {
  23. public:
  24. explicit Level(FromV8ValueState* state) : state_(state) {
  25. state_->max_recursion_depth_--;
  26. }
  27. ~Level() { state_->max_recursion_depth_++; }
  28. private:
  29. FromV8ValueState* state_;
  30. };
  31. FromV8ValueState() : max_recursion_depth_(kMaxRecursionDepth) {}
  32. // If |handle| is not in |unique_map_|, then add it to |unique_map_| and
  33. // return true.
  34. //
  35. // Otherwise do nothing and return false. Here "A is unique" means that no
  36. // other handle B in the map points to the same object as A. Note that A can
  37. // be unique even if there already is another handle with the same identity
  38. // hash (key) in the map, because two objects can have the same hash.
  39. bool AddToUniquenessCheck(v8::Local<v8::Object> handle) {
  40. int hash;
  41. auto iter = GetIteratorInMap(handle, &hash);
  42. if (iter != unique_map_.end())
  43. return false;
  44. unique_map_.insert(std::make_pair(hash, handle));
  45. return true;
  46. }
  47. bool RemoveFromUniquenessCheck(v8::Local<v8::Object> handle) {
  48. int unused_hash;
  49. auto iter = GetIteratorInMap(handle, &unused_hash);
  50. if (iter == unique_map_.end())
  51. return false;
  52. unique_map_.erase(iter);
  53. return true;
  54. }
  55. bool HasReachedMaxRecursionDepth() { return max_recursion_depth_ < 0; }
  56. private:
  57. using HashToHandleMap = std::multimap<int, v8::Local<v8::Object>>;
  58. using Iterator = HashToHandleMap::const_iterator;
  59. Iterator GetIteratorInMap(v8::Local<v8::Object> handle, int* hash) {
  60. *hash = handle->GetIdentityHash();
  61. // We only compare using == with handles to objects with the same identity
  62. // hash. Different hash obviously means different objects, but two objects
  63. // in a couple of thousands could have the same identity hash.
  64. std::pair<Iterator, Iterator> range = unique_map_.equal_range(*hash);
  65. for (auto it = range.first; it != range.second; ++it) {
  66. // Operator == for handles actually compares the underlying objects.
  67. if (it->second == handle)
  68. return it;
  69. }
  70. // Not found.
  71. return unique_map_.end();
  72. }
  73. HashToHandleMap unique_map_;
  74. int max_recursion_depth_;
  75. };
  76. // A class to ensure that objects/arrays that are being converted by
  77. // this V8ValueConverterImpl do not have cycles.
  78. //
  79. // An example of cycle: var v = {}; v = {key: v};
  80. // Not an example of cycle: var v = {}; a = [v, v]; or w = {a: v, b: v};
  81. class V8ValueConverter::ScopedUniquenessGuard {
  82. public:
  83. ScopedUniquenessGuard(V8ValueConverter::FromV8ValueState* state,
  84. v8::Local<v8::Object> value)
  85. : state_(state),
  86. value_(value),
  87. is_valid_(state_->AddToUniquenessCheck(value_)) {}
  88. ~ScopedUniquenessGuard() {
  89. if (is_valid_) {
  90. bool removed = state_->RemoveFromUniquenessCheck(value_);
  91. DCHECK(removed);
  92. }
  93. }
  94. bool is_valid() const { return is_valid_; }
  95. private:
  96. typedef std::multimap<int, v8::Local<v8::Object>> HashToHandleMap;
  97. V8ValueConverter::FromV8ValueState* state_;
  98. v8::Local<v8::Object> value_;
  99. bool is_valid_;
  100. DISALLOW_COPY_AND_ASSIGN(ScopedUniquenessGuard);
  101. };
  102. V8ValueConverter::V8ValueConverter() = default;
  103. void V8ValueConverter::SetRegExpAllowed(bool val) {
  104. reg_exp_allowed_ = val;
  105. }
  106. void V8ValueConverter::SetFunctionAllowed(bool val) {
  107. function_allowed_ = val;
  108. }
  109. void V8ValueConverter::SetStripNullFromObjects(bool val) {
  110. strip_null_from_objects_ = val;
  111. }
  112. v8::Local<v8::Value> V8ValueConverter::ToV8Value(
  113. const base::Value* value,
  114. v8::Local<v8::Context> context) const {
  115. v8::Context::Scope context_scope(context);
  116. v8::EscapableHandleScope handle_scope(context->GetIsolate());
  117. return handle_scope.Escape(ToV8ValueImpl(context->GetIsolate(), value));
  118. }
  119. std::unique_ptr<base::Value> V8ValueConverter::FromV8Value(
  120. v8::Local<v8::Value> val,
  121. v8::Local<v8::Context> context) const {
  122. v8::Context::Scope context_scope(context);
  123. v8::HandleScope handle_scope(context->GetIsolate());
  124. FromV8ValueState state;
  125. return FromV8ValueImpl(&state, val, context->GetIsolate());
  126. }
  127. v8::Local<v8::Value> V8ValueConverter::ToV8ValueImpl(
  128. v8::Isolate* isolate,
  129. const base::Value* value) const {
  130. switch (value->type()) {
  131. case base::Value::Type::NONE:
  132. return v8::Null(isolate);
  133. case base::Value::Type::BOOLEAN: {
  134. bool val = value->GetBool();
  135. return v8::Boolean::New(isolate, val);
  136. }
  137. case base::Value::Type::INTEGER: {
  138. int val = value->GetInt();
  139. return v8::Integer::New(isolate, val);
  140. }
  141. case base::Value::Type::DOUBLE: {
  142. double val = value->GetDouble();
  143. return v8::Number::New(isolate, val);
  144. }
  145. case base::Value::Type::STRING: {
  146. std::string val = value->GetString();
  147. return v8::String::NewFromUtf8(isolate, val.c_str(),
  148. v8::NewStringType::kNormal, val.length())
  149. .ToLocalChecked();
  150. }
  151. case base::Value::Type::LIST:
  152. return ToV8Array(isolate, static_cast<const base::ListValue*>(value));
  153. case base::Value::Type::DICTIONARY:
  154. return ToV8Object(isolate,
  155. static_cast<const base::DictionaryValue*>(value));
  156. case base::Value::Type::BINARY:
  157. return ToArrayBuffer(isolate, static_cast<const base::Value*>(value));
  158. default:
  159. LOG(ERROR) << "Unexpected value type: " << value->type();
  160. return v8::Null(isolate);
  161. }
  162. }
  163. v8::Local<v8::Value> V8ValueConverter::ToV8Array(
  164. v8::Isolate* isolate,
  165. const base::ListValue* val) const {
  166. v8::Local<v8::Array> result(v8::Array::New(isolate, val->GetSize()));
  167. auto context = isolate->GetCurrentContext();
  168. for (size_t i = 0; i < val->GetSize(); ++i) {
  169. const base::Value* child = nullptr;
  170. val->Get(i, &child);
  171. v8::Local<v8::Value> child_v8 = ToV8ValueImpl(isolate, child);
  172. v8::TryCatch try_catch(isolate);
  173. result->Set(context, static_cast<uint32_t>(i), child_v8).Check();
  174. if (try_catch.HasCaught())
  175. LOG(ERROR) << "Setter for index " << i << " threw an exception.";
  176. }
  177. return result;
  178. }
  179. v8::Local<v8::Value> V8ValueConverter::ToV8Object(
  180. v8::Isolate* isolate,
  181. const base::DictionaryValue* val) const {
  182. gin_helper::Dictionary result = gin::Dictionary::CreateEmpty(isolate);
  183. result.SetHidden("simple", true);
  184. for (base::DictionaryValue::Iterator iter(*val); !iter.IsAtEnd();
  185. iter.Advance()) {
  186. const std::string& key = iter.key();
  187. v8::Local<v8::Value> child_v8 = ToV8ValueImpl(isolate, &iter.value());
  188. v8::TryCatch try_catch(isolate);
  189. result.Set(key, child_v8);
  190. if (try_catch.HasCaught()) {
  191. LOG(ERROR) << "Setter for property " << key.c_str() << " threw an "
  192. << "exception.";
  193. }
  194. }
  195. return result.GetHandle();
  196. }
  197. v8::Local<v8::Value> V8ValueConverter::ToArrayBuffer(
  198. v8::Isolate* isolate,
  199. const base::Value* value) const {
  200. const auto* data = reinterpret_cast<const char*>(value->GetBlob().data());
  201. size_t length = value->GetBlob().size();
  202. if (NodeBindings::IsInitialized()) {
  203. return node::Buffer::Copy(isolate, data, length).ToLocalChecked();
  204. }
  205. if (length > node::Buffer::kMaxLength) {
  206. return v8::Local<v8::Object>();
  207. }
  208. auto context = isolate->GetCurrentContext();
  209. auto array_buffer = v8::ArrayBuffer::New(isolate, length);
  210. memcpy(array_buffer->GetContents().Data(), data, length);
  211. // From this point, if something goes wrong(can't find Buffer class for
  212. // example) we'll simply return a Uint8Array based on the created ArrayBuffer.
  213. // This can happen if no preload script was specified to the renderer.
  214. gin_helper::Dictionary global(isolate, context->Global());
  215. v8::Local<v8::Value> buffer_value;
  216. // Get the Buffer class stored as a hidden value in the global object. We'll
  217. // use it return a browserified Buffer.
  218. if (!global.GetHidden("Buffer", &buffer_value) ||
  219. !buffer_value->IsFunction()) {
  220. return v8::Uint8Array::New(array_buffer, 0, length);
  221. }
  222. gin::Dictionary buffer_class(
  223. isolate,
  224. buffer_value->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
  225. v8::Local<v8::Value> from_value;
  226. if (!buffer_class.Get("from", &from_value) || !from_value->IsFunction()) {
  227. return v8::Uint8Array::New(array_buffer, 0, length);
  228. }
  229. v8::Local<v8::Value> args[] = {array_buffer};
  230. auto func = v8::Local<v8::Function>::Cast(from_value);
  231. auto result = func->Call(context, v8::Null(isolate), 1, args);
  232. if (!result.IsEmpty()) {
  233. return result.ToLocalChecked();
  234. }
  235. return v8::Uint8Array::New(array_buffer, 0, length);
  236. }
  237. std::unique_ptr<base::Value> V8ValueConverter::FromV8ValueImpl(
  238. FromV8ValueState* state,
  239. v8::Local<v8::Value> val,
  240. v8::Isolate* isolate) const {
  241. FromV8ValueState::Level state_level(state);
  242. if (state->HasReachedMaxRecursionDepth())
  243. return nullptr;
  244. if (val->IsExternal())
  245. return std::make_unique<base::Value>();
  246. if (val->IsNull())
  247. return std::make_unique<base::Value>();
  248. auto context = isolate->GetCurrentContext();
  249. if (val->IsBoolean())
  250. return std::make_unique<base::Value>(val->ToBoolean(isolate)->Value());
  251. if (val->IsInt32())
  252. return std::make_unique<base::Value>(val.As<v8::Int32>()->Value());
  253. if (val->IsNumber()) {
  254. double val_as_double = val.As<v8::Number>()->Value();
  255. if (!std::isfinite(val_as_double))
  256. return nullptr;
  257. return std::make_unique<base::Value>(val_as_double);
  258. }
  259. if (val->IsString()) {
  260. v8::String::Utf8Value utf8(isolate, val);
  261. return std::make_unique<base::Value>(std::string(*utf8, utf8.length()));
  262. }
  263. if (val->IsUndefined())
  264. // JSON.stringify ignores undefined.
  265. return nullptr;
  266. if (val->IsDate()) {
  267. v8::Date* date = v8::Date::Cast(*val);
  268. v8::Local<v8::Value> toISOString =
  269. date->Get(context, v8::String::NewFromUtf8(isolate, "toISOString",
  270. v8::NewStringType::kNormal)
  271. .ToLocalChecked())
  272. .ToLocalChecked();
  273. if (toISOString->IsFunction()) {
  274. v8::MaybeLocal<v8::Value> result =
  275. toISOString.As<v8::Function>()->Call(context, val, 0, nullptr);
  276. if (!result.IsEmpty()) {
  277. v8::String::Utf8Value utf8(isolate, result.ToLocalChecked());
  278. return std::make_unique<base::Value>(std::string(*utf8, utf8.length()));
  279. }
  280. }
  281. }
  282. if (val->IsRegExp()) {
  283. if (!reg_exp_allowed_)
  284. // JSON.stringify converts to an object.
  285. return FromV8Object(val.As<v8::Object>(), state, isolate);
  286. return std::make_unique<base::Value>(*v8::String::Utf8Value(isolate, val));
  287. }
  288. // v8::Value doesn't have a ToArray() method for some reason.
  289. if (val->IsArray())
  290. return FromV8Array(val.As<v8::Array>(), state, isolate);
  291. if (val->IsFunction()) {
  292. if (!function_allowed_)
  293. // JSON.stringify refuses to convert function(){}.
  294. return nullptr;
  295. return FromV8Object(val.As<v8::Object>(), state, isolate);
  296. }
  297. if (node::Buffer::HasInstance(val)) {
  298. return FromNodeBuffer(val, state, isolate);
  299. }
  300. if (val->IsObject()) {
  301. return FromV8Object(val.As<v8::Object>(), state, isolate);
  302. }
  303. LOG(ERROR) << "Unexpected v8 value type encountered.";
  304. return nullptr;
  305. }
  306. std::unique_ptr<base::Value> V8ValueConverter::FromV8Array(
  307. v8::Local<v8::Array> val,
  308. FromV8ValueState* state,
  309. v8::Isolate* isolate) const {
  310. ScopedUniquenessGuard uniqueness_guard(state, val);
  311. if (!uniqueness_guard.is_valid())
  312. return std::make_unique<base::Value>();
  313. std::unique_ptr<v8::Context::Scope> scope;
  314. // If val was created in a different context than our current one, change to
  315. // that context, but change back after val is converted.
  316. if (!val->CreationContext().IsEmpty() &&
  317. val->CreationContext() != isolate->GetCurrentContext())
  318. scope = std::make_unique<v8::Context::Scope>(val->CreationContext());
  319. std::unique_ptr<base::ListValue> result(new base::ListValue());
  320. // Only fields with integer keys are carried over to the ListValue.
  321. for (uint32_t i = 0; i < val->Length(); ++i) {
  322. v8::TryCatch try_catch(isolate);
  323. v8::Local<v8::Value> child_v8;
  324. v8::MaybeLocal<v8::Value> maybe_child =
  325. val->Get(isolate->GetCurrentContext(), i);
  326. if (try_catch.HasCaught() || !maybe_child.ToLocal(&child_v8)) {
  327. LOG(ERROR) << "Getter for index " << i << " threw an exception.";
  328. child_v8 = v8::Null(isolate);
  329. }
  330. if (!val->HasRealIndexedProperty(isolate->GetCurrentContext(), i)
  331. .FromMaybe(false)) {
  332. result->Append(std::make_unique<base::Value>());
  333. continue;
  334. }
  335. std::unique_ptr<base::Value> child =
  336. FromV8ValueImpl(state, child_v8, isolate);
  337. if (child)
  338. result->Append(std::move(child));
  339. else
  340. // JSON.stringify puts null in places where values don't serialize, for
  341. // example undefined and functions. Emulate that behavior.
  342. result->Append(std::make_unique<base::Value>());
  343. }
  344. return std::move(result);
  345. }
  346. std::unique_ptr<base::Value> V8ValueConverter::FromNodeBuffer(
  347. v8::Local<v8::Value> value,
  348. FromV8ValueState* state,
  349. v8::Isolate* isolate) const {
  350. std::vector<char> buffer(
  351. node::Buffer::Data(value),
  352. node::Buffer::Data(value) + node::Buffer::Length(value));
  353. return std::make_unique<base::Value>(std::move(buffer));
  354. }
  355. std::unique_ptr<base::Value> V8ValueConverter::FromV8Object(
  356. v8::Local<v8::Object> val,
  357. FromV8ValueState* state,
  358. v8::Isolate* isolate) const {
  359. ScopedUniquenessGuard uniqueness_guard(state, val);
  360. if (!uniqueness_guard.is_valid())
  361. return std::make_unique<base::Value>();
  362. std::unique_ptr<v8::Context::Scope> scope;
  363. // If val was created in a different context than our current one, change to
  364. // that context, but change back after val is converted.
  365. if (!val->CreationContext().IsEmpty() &&
  366. val->CreationContext() != isolate->GetCurrentContext())
  367. scope = std::make_unique<v8::Context::Scope>(val->CreationContext());
  368. auto result = std::make_unique<base::DictionaryValue>();
  369. v8::Local<v8::Array> property_names;
  370. if (!val->GetOwnPropertyNames(isolate->GetCurrentContext())
  371. .ToLocal(&property_names)) {
  372. return std::move(result);
  373. }
  374. for (uint32_t i = 0; i < property_names->Length(); ++i) {
  375. v8::Local<v8::Value> key =
  376. property_names->Get(isolate->GetCurrentContext(), i).ToLocalChecked();
  377. // Extend this test to cover more types as necessary and if sensible.
  378. if (!key->IsString() && !key->IsNumber()) {
  379. NOTREACHED() << "Key \"" << *v8::String::Utf8Value(isolate, key)
  380. << "\" "
  381. "is neither a string nor a number";
  382. continue;
  383. }
  384. v8::String::Utf8Value name_utf8(isolate, key);
  385. v8::TryCatch try_catch(isolate);
  386. v8::Local<v8::Value> child_v8;
  387. v8::MaybeLocal<v8::Value> maybe_child =
  388. val->Get(isolate->GetCurrentContext(), key);
  389. if (try_catch.HasCaught() || !maybe_child.ToLocal(&child_v8)) {
  390. LOG(ERROR) << "Getter for property " << *name_utf8
  391. << " threw an exception.";
  392. child_v8 = v8::Null(isolate);
  393. }
  394. std::unique_ptr<base::Value> child =
  395. FromV8ValueImpl(state, child_v8, isolate);
  396. if (!child)
  397. // JSON.stringify skips properties whose values don't serialize, for
  398. // example undefined and functions. Emulate that behavior.
  399. continue;
  400. // Strip null if asked (and since undefined is turned into null, undefined
  401. // too). The use case for supporting this is JSON-schema support,
  402. // specifically for extensions, where "optional" JSON properties may be
  403. // represented as null, yet due to buggy legacy code elsewhere isn't
  404. // treated as such (potentially causing crashes). For example, the
  405. // "tabs.create" function takes an object as its first argument with an
  406. // optional "windowId" property.
  407. //
  408. // Given just
  409. //
  410. // tabs.create({})
  411. //
  412. // this will work as expected on code that only checks for the existence of
  413. // a "windowId" property (such as that legacy code). However given
  414. //
  415. // tabs.create({windowId: null})
  416. //
  417. // there *is* a "windowId" property, but since it should be an int, code
  418. // on the browser which doesn't additionally check for null will fail.
  419. // We can avoid all bugs related to this by stripping null.
  420. if (strip_null_from_objects_ && child->is_none())
  421. continue;
  422. result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()),
  423. std::move(child));
  424. }
  425. return std::move(result);
  426. }
  427. } // namespace electron