electron/atom/common/native_mate_converters/net_converter.cc
Electron Bot 716cb28430 chore: bump chromium to 1e9f9a24aa12 (master) (#17880)
* chore: bump chromium in DEPS to 1e9f9a24aa12bea9cf194a82a7e249bd1242ec4f

* chore: update patches

* Make WebContents' theme color a base::Optional<SkColor>

https://chromium-review.googlesource.com/c/chromium/src/+/1540022

* update autofill patch for incorrect header includes

* Move Shell messages to web_test and rename to BlinkTest.

https://chromium-review.googlesource.com/c/chromium/src/+/1525181

* Make PlatformNotificationServiceImpl a KeyedService.

https://chromium-review.googlesource.com/c/chromium/src/+/1336150

* Move MediaPlayerId to its own file.

https://chromium-review.googlesource.com/c/chromium/src/+/1547057

* Remove net/base/completion_callback.h, which is no longer used

https://chromium-review.googlesource.com/c/chromium/src/+/1552821

* AW NS: support file scheme cookies

https://chromium-review.googlesource.com/c/chromium/src/+/1533486

* Remove SecurityInfo and adapt remaining consumers

https://chromium-review.googlesource.com/c/chromium/src/+/1509455

* Remove deprecated type-specific number to string conversion functions

https://chromium-review.googlesource.com/c/chromium/src/+/1545881

* DevTools: Adding new performance histograms for launch of top 4 tools

https://chromium-review.googlesource.com/c/chromium/src/+/1506388

* Update include paths for //base/hash/hash.h

https://chromium-review.googlesource.com/c/chromium/src/+/1544630

* build: Disable ensure_gn_version gclient hook for mac CI checkout

* update patches

* use maybe version of v8::String::NewFromTwoByte

* bump appveyor image version

* fix mac ci hopefully

* Convert enum to enum class for MenuAnchorPosition

https://chromium-review.googlesource.com/c/chromium/src/+/1530508

* use maybe version of ToObject

* RenderViewHost::GetProcess is no longer const

* Unrefcount AuthChallengeInfo

https://chromium-review.googlesource.com/c/chromium/src/+/1550631

* MenuButtonController takes Button rather than MenuButton

https://chromium-review.googlesource.com/c/chromium/src/+/1500935

* add //ui/views_bridge_mac to deps to fix link error

* forward declare views::Button in atom::MenuDelegate

* more v8 patches

* base/{=> hash}/md5.h

https://chromium-review.googlesource.com/c/chromium/src/+/1535124

* gfx::{PlatformFontWin => win}::*

https://chromium-review.googlesource.com/c/chromium/src/+/1534178

* fix v8 patches

* [base] Rename TaskScheduler to ThreadPool

https://chromium-review.googlesource.com/c/chromium/src/+/1561552

* use internal_config_base for bytecode_builtins_list_generator

avoids windows link errors

* FIXME: temporarily disable v8/breakpad integration

* FIXME: temporarily disable prevent-will-redirect test

* FIXME: disable neon on aarch64 pending crbug.com/953815

* update to account for WebCursor refactor

https://chromium-review.googlesource.com/c/chromium/src/+/1562755

* enable stack dumping on appveyor

* Revert "FIXME: disable neon on aarch64 pending crbug.com/953815"

This reverts commit 57f082026be3d83069f2a2814684abf4dc9e7b53.

* fix: remove const qualifiers to match upstream

* fix: remove const qualifiers to match upstream in cc files as well

* don't throw an error when testing if an object is an object

* use non-deprecated Buffer constructor

* Remove net::CookieSameSite::DEFAULT_MODE enum value

https://chromium-review.googlesource.com/c/chromium/src/+/1567955

* depend on modded dbus-native to work around buffer deprecation

https://github.com/sidorares/dbus-native/pull/262

* revert clang roll to fix arm build on linux

* fixup! depend on modded dbus-native to work around buffer deprecation

need more coffee

* update coffee-script

* robustify verify-mksnapshot w.r.t. command-line parameters

* Revert "robustify verify-mksnapshot w.r.t. command-line parameters"

This reverts commit a49af01411f684f6025528d604895c3696e0bc57.

* fix mksnapshot by matching args

* update patches

* TMP: enable rdp on appveyor

* Changed ContentBrowserClient::CreateQuotaPermissionContext() to return scoped_refptr.

https://chromium-review.googlesource.com/c/chromium/src/+/1569376

* Make content::ResourceType an enum class.

https://chromium-review.googlesource.com/c/chromium/src/+/1569345

* fixup! Make content::ResourceType an enum class.

* turn off rdp

* use net::CompletionRepeatingCallback instead of base::Callback<void(int)>

* remove disable_ensure_gn_version_gclient_hook.patch

* copy repeating callback instead of std::move

* fix lint

* add completion_repeating_callback.h include
2019-04-20 10:20:37 -07:00

279 lines
9.2 KiB
C++

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/common/native_mate_converters/net_converter.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_includes.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "native_mate/dictionary.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/base/upload_element_reader.h"
#include "net/base/upload_file_element_reader.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "storage/browser/blob/upload_blob_element_reader.h"
namespace mate {
namespace {
bool CertFromData(const std::string& data,
scoped_refptr<net::X509Certificate>* out) {
auto cert_list = net::X509Certificate::CreateCertificateListFromBytes(
data.c_str(), data.length(),
net::X509Certificate::FORMAT_SINGLE_CERTIFICATE);
if (cert_list.empty())
return false;
auto leaf_cert = cert_list.front();
if (!leaf_cert)
return false;
*out = leaf_cert;
return true;
}
} // namespace
// static
v8::Local<v8::Value> Converter<net::AuthChallengeInfo>::ToV8(
v8::Isolate* isolate,
const net::AuthChallengeInfo& val) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("isProxy", val.is_proxy);
dict.Set("scheme", val.scheme);
dict.Set("host", val.challenger.host());
dict.Set("port", static_cast<uint32_t>(val.challenger.port()));
dict.Set("realm", val.realm);
return mate::ConvertToV8(isolate, dict);
}
// static
v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
v8::Isolate* isolate,
const scoped_refptr<net::X509Certificate>& val) {
mate::Dictionary dict(isolate, v8::Object::New(isolate));
std::string encoded_data;
net::X509Certificate::GetPEMEncoded(val->cert_buffer(), &encoded_data);
dict.Set("data", encoded_data);
dict.Set("issuer", val->issuer());
dict.Set("issuerName", val->issuer().GetDisplayName());
dict.Set("subject", val->subject());
dict.Set("subjectName", val->subject().GetDisplayName());
dict.Set("serialNumber", base::HexEncode(val->serial_number().data(),
val->serial_number().size()));
dict.Set("validStart", val->valid_start().ToDoubleT());
dict.Set("validExpiry", val->valid_expiry().ToDoubleT());
dict.Set("fingerprint",
net::HashValue(val->CalculateFingerprint256(val->cert_buffer()))
.ToString());
const auto& intermediate_buffers = val->intermediate_buffers();
if (!intermediate_buffers.empty()) {
std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> issuer_intermediates;
issuer_intermediates.reserve(intermediate_buffers.size() - 1);
for (size_t i = 1; i < intermediate_buffers.size(); ++i) {
issuer_intermediates.push_back(
bssl::UpRef(intermediate_buffers[i].get()));
}
const scoped_refptr<net::X509Certificate>& issuer_cert =
net::X509Certificate::CreateFromBuffer(
bssl::UpRef(intermediate_buffers[0].get()),
std::move(issuer_intermediates));
dict.Set("issuerCert", issuer_cert);
}
return dict.GetHandle();
}
bool Converter<scoped_refptr<net::X509Certificate>>::FromV8(
v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<net::X509Certificate>* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
std::string data;
dict.Get("data", &data);
scoped_refptr<net::X509Certificate> leaf_cert;
if (!CertFromData(data, &leaf_cert))
return false;
scoped_refptr<net::X509Certificate> issuer_cert;
if (dict.Get("issuerCert", &issuer_cert)) {
std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates;
intermediates.push_back(bssl::UpRef(issuer_cert->cert_buffer()));
auto cert = net::X509Certificate::CreateFromBuffer(
bssl::UpRef(leaf_cert->cert_buffer()), std::move(intermediates));
if (!cert)
return false;
*out = cert;
} else {
*out = leaf_cert;
}
return true;
}
// static
v8::Local<v8::Value> Converter<net::CertPrincipal>::ToV8(
v8::Isolate* isolate,
const net::CertPrincipal& val) {
mate::Dictionary dict(isolate, v8::Object::New(isolate));
dict.Set("commonName", val.common_name);
dict.Set("organizations", val.organization_names);
dict.Set("organizationUnits", val.organization_unit_names);
dict.Set("locality", val.locality_name);
dict.Set("state", val.state_or_province_name);
dict.Set("country", val.country_name);
return dict.GetHandle();
}
// static
v8::Local<v8::Value> Converter<net::HttpResponseHeaders*>::ToV8(
v8::Isolate* isolate,
net::HttpResponseHeaders* headers) {
base::DictionaryValue response_headers;
if (headers) {
size_t iter = 0;
std::string key;
std::string value;
while (headers->EnumerateHeaderLines(&iter, &key, &value)) {
key = base::ToLowerASCII(key);
if (response_headers.FindKey(key)) {
base::ListValue* values = nullptr;
if (response_headers.GetList(key, &values))
values->AppendString(value);
} else {
auto values = std::make_unique<base::ListValue>();
values->AppendString(value);
response_headers.Set(key, std::move(values));
}
}
}
return ConvertToV8(isolate, response_headers);
}
bool Converter<net::HttpResponseHeaders*>::FromV8(
v8::Isolate* isolate,
v8::Local<v8::Value> val,
net::HttpResponseHeaders* out) {
if (!val->IsObject()) {
return false;
}
auto addHeaderFromValue = [&isolate, &out](
const std::string& key,
const v8::Local<v8::Value>& localVal) {
auto context = isolate->GetCurrentContext();
v8::Local<v8::String> localStrVal;
if (!localVal->ToString(context).ToLocal(&localStrVal)) {
return false;
}
std::string value;
mate::ConvertFromV8(isolate, localStrVal, &value);
out->AddHeader(key + ": " + value);
return true;
};
auto context = isolate->GetCurrentContext();
auto headers = v8::Local<v8::Object>::Cast(val);
auto keys = headers->GetOwnPropertyNames(context).ToLocalChecked();
for (uint32_t i = 0; i < keys->Length(); i++) {
v8::Local<v8::Value> keyVal;
if (!keys->Get(context, i).ToLocal(&keyVal)) {
return false;
}
std::string key;
mate::ConvertFromV8(isolate, keyVal, &key);
auto localVal = headers->Get(context, keyVal).ToLocalChecked();
if (localVal->IsArray()) {
auto values = v8::Local<v8::Array>::Cast(localVal);
for (uint32_t j = 0; j < values->Length(); j++) {
if (!addHeaderFromValue(key, values->Get(j))) {
return false;
}
}
} else {
if (!addHeaderFromValue(key, localVal)) {
return false;
}
}
}
return true;
}
} // namespace mate
namespace atom {
void FillRequestDetails(base::DictionaryValue* details,
const net::URLRequest* request) {
details->SetString("method", request->method());
std::string url;
if (!request->url_chain().empty())
url = request->url().spec();
details->SetKey("url", base::Value(url));
details->SetString("referrer", request->referrer());
auto list = std::make_unique<base::ListValue>();
GetUploadData(list.get(), request);
if (!list->empty())
details->Set("uploadData", std::move(list));
auto headers_value = std::make_unique<base::DictionaryValue>();
for (net::HttpRequestHeaders::Iterator it(request->extra_request_headers());
it.GetNext();) {
headers_value->SetString(it.name(), it.value());
}
details->Set("headers", std::move(headers_value));
}
void GetUploadData(base::ListValue* upload_data_list,
const net::URLRequest* request) {
const net::UploadDataStream* upload_data = request->get_upload();
if (!upload_data)
return;
const std::vector<std::unique_ptr<net::UploadElementReader>>* readers =
upload_data->GetElementReaders();
for (const auto& reader : *readers) {
auto upload_data_dict = std::make_unique<base::DictionaryValue>();
if (reader->AsBytesReader()) {
const net::UploadBytesElementReader* bytes_reader =
reader->AsBytesReader();
auto bytes = std::make_unique<base::Value>(
std::vector<char>(bytes_reader->bytes(),
bytes_reader->bytes() + bytes_reader->length()));
upload_data_dict->Set("bytes", std::move(bytes));
} else if (reader->AsFileReader()) {
const net::UploadFileElementReader* file_reader = reader->AsFileReader();
auto file_path = file_reader->path().AsUTF8Unsafe();
upload_data_dict->SetKey("file", base::Value(file_path));
} else {
const storage::UploadBlobElementReader* blob_reader =
static_cast<storage::UploadBlobElementReader*>(reader.get());
upload_data_dict->SetString("blobUUID", blob_reader->uuid());
}
upload_data_list->Append(std::move(upload_data_dict));
}
}
} // namespace atom