2015-10-31 13:39:07 +00:00
|
|
|
// 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"
|
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2018-09-19 11:10:26 +00:00
|
|
|
#include <string>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
2015-12-05 01:48:19 +00:00
|
|
|
#include <vector>
|
2015-11-05 14:06:36 +00:00
|
|
|
|
2015-12-01 04:52:22 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
2019-03-12 00:13:43 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2016-07-19 03:06:17 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2016-10-13 15:14:23 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "base/values.h"
|
2015-10-31 13:39:07 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-12-05 01:48:19 +00:00
|
|
|
#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"
|
2015-11-05 14:06:36 +00:00
|
|
|
#include "net/cert/x509_certificate.h"
|
2018-04-12 19:55:43 +00:00
|
|
|
#include "net/cert/x509_util.h"
|
2015-12-01 04:52:22 +00:00
|
|
|
#include "net/http/http_response_headers.h"
|
2015-10-31 13:39:07 +00:00
|
|
|
#include "net/url_request/url_request.h"
|
2016-08-23 00:38:10 +00:00
|
|
|
#include "storage/browser/blob/upload_blob_element_reader.h"
|
2015-10-31 13:39:07 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2017-04-04 13:19:23 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool CertFromData(const std::string& data,
|
2018-04-18 01:55:30 +00:00
|
|
|
scoped_refptr<net::X509Certificate>* out) {
|
2017-04-04 13:19:23 +00:00
|
|
|
auto cert_list = net::X509Certificate::CreateCertificateListFromBytes(
|
2018-04-18 01:55:30 +00:00
|
|
|
data.c_str(), data.length(),
|
|
|
|
net::X509Certificate::FORMAT_SINGLE_CERTIFICATE);
|
2017-04-04 13:19:23 +00:00
|
|
|
if (cert_list.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto leaf_cert = cert_list.front();
|
|
|
|
if (!leaf_cert)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*out = leaf_cert;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-04 13:23:30 +00:00
|
|
|
} // namespace
|
2017-04-04 13:19:23 +00:00
|
|
|
|
2015-10-31 13:39:07 +00:00
|
|
|
// static
|
|
|
|
v8::Local<v8::Value> Converter<const net::AuthChallengeInfo*>::ToV8(
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
const net::AuthChallengeInfo* val) {
|
2015-10-31 13:39:07 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-11-05 14:06:36 +00:00
|
|
|
// static
|
|
|
|
v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
const scoped_refptr<net::X509Certificate>& val) {
|
2015-11-05 14:06:36 +00:00
|
|
|
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
|
|
|
std::string encoded_data;
|
2018-04-12 19:55:43 +00:00
|
|
|
net::X509Certificate::GetPEMEncoded(val->cert_buffer(), &encoded_data);
|
2016-11-06 13:37:07 +00:00
|
|
|
|
2016-08-10 21:47:05 +00:00
|
|
|
dict.Set("data", encoded_data);
|
2016-11-12 12:18:38 +00:00
|
|
|
dict.Set("issuer", val->issuer());
|
2015-11-05 14:06:36 +00:00
|
|
|
dict.Set("issuerName", val->issuer().GetDisplayName());
|
2016-11-12 12:18:38 +00:00
|
|
|
dict.Set("subject", val->subject());
|
2016-07-12 17:05:28 +00:00
|
|
|
dict.Set("subjectName", val->subject().GetDisplayName());
|
2016-07-19 03:06:17 +00:00
|
|
|
dict.Set("serialNumber", base::HexEncode(val->serial_number().data(),
|
|
|
|
val->serial_number().size()));
|
2016-07-12 17:05:28 +00:00
|
|
|
dict.Set("validStart", val->valid_start().ToDoubleT());
|
|
|
|
dict.Set("validExpiry", val->valid_expiry().ToDoubleT());
|
2016-07-14 11:09:11 +00:00
|
|
|
dict.Set("fingerprint",
|
2018-04-12 19:55:43 +00:00
|
|
|
net::HashValue(val->CalculateFingerprint256(val->cert_buffer()))
|
2018-04-18 01:55:30 +00:00
|
|
|
.ToString());
|
2016-07-12 17:05:28 +00:00
|
|
|
|
2018-04-12 19:55:43 +00:00
|
|
|
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(
|
2018-10-02 18:11:45 +00:00
|
|
|
bssl::UpRef(intermediate_buffers[i].get()));
|
2018-04-12 19:55:43 +00:00
|
|
|
}
|
2016-11-09 17:19:35 +00:00
|
|
|
const scoped_refptr<net::X509Certificate>& issuer_cert =
|
2018-04-12 19:55:43 +00:00
|
|
|
net::X509Certificate::CreateFromBuffer(
|
2018-10-02 18:11:45 +00:00
|
|
|
bssl::UpRef(intermediate_buffers[0].get()),
|
2018-04-12 19:55:43 +00:00
|
|
|
std::move(issuer_intermediates));
|
2016-11-12 12:18:38 +00:00
|
|
|
dict.Set("issuerCert", issuer_cert);
|
2016-11-09 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
|
2017-03-31 18:16:26 +00:00
|
|
|
bool Converter<scoped_refptr<net::X509Certificate>>::FromV8(
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
2017-03-31 18:16:26 +00:00
|
|
|
scoped_refptr<net::X509Certificate>* out) {
|
|
|
|
mate::Dictionary dict;
|
|
|
|
if (!ConvertFromV8(isolate, val, &dict))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
dict.Get("data", &data);
|
2017-04-01 01:27:49 +00:00
|
|
|
scoped_refptr<net::X509Certificate> leaf_cert;
|
|
|
|
if (!CertFromData(data, &leaf_cert))
|
2017-03-31 19:44:47 +00:00
|
|
|
return false;
|
|
|
|
|
2018-04-12 19:55:43 +00:00
|
|
|
scoped_refptr<net::X509Certificate> issuer_cert;
|
|
|
|
if (dict.Get("issuerCert", &issuer_cert)) {
|
|
|
|
std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates;
|
2018-10-02 18:11:45 +00:00
|
|
|
intermediates.push_back(bssl::UpRef(issuer_cert->cert_buffer()));
|
2018-04-12 19:55:43 +00:00
|
|
|
auto cert = net::X509Certificate::CreateFromBuffer(
|
2018-10-02 18:11:45 +00:00
|
|
|
bssl::UpRef(leaf_cert->cert_buffer()), std::move(intermediates));
|
2017-04-03 20:27:53 +00:00
|
|
|
if (!cert)
|
2017-04-01 01:27:49 +00:00
|
|
|
return false;
|
|
|
|
|
2017-04-03 20:27:53 +00:00
|
|
|
*out = cert;
|
|
|
|
} else {
|
|
|
|
*out = leaf_cert;
|
2017-04-01 01:27:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 17:53:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:19:35 +00:00
|
|
|
// static
|
|
|
|
v8::Local<v8::Value> Converter<net::CertPrincipal>::ToV8(
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
const net::CertPrincipal& val) {
|
2016-11-09 17:19:35 +00:00
|
|
|
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
|
|
|
|
|
|
|
dict.Set("commonName", val.common_name);
|
2016-11-12 12:18:38 +00:00
|
|
|
dict.Set("organizations", val.organization_names);
|
|
|
|
dict.Set("organizationUnits", val.organization_unit_names);
|
2016-11-09 17:19:35 +00:00
|
|
|
dict.Set("locality", val.locality_name);
|
|
|
|
dict.Set("state", val.state_or_province_name);
|
|
|
|
dict.Set("country", val.country_name);
|
|
|
|
|
2015-11-05 14:06:36 +00:00
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
// static
|
2016-10-25 10:41:01 +00:00
|
|
|
v8::Local<v8::Value> Converter<net::HttpResponseHeaders*>::ToV8(
|
2016-10-13 15:14:23 +00:00
|
|
|
v8::Isolate* isolate,
|
2016-10-25 10:41:01 +00:00
|
|
|
net::HttpResponseHeaders* headers) {
|
2016-10-13 15:14:23 +00:00
|
|
|
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);
|
2018-06-20 03:07:10 +00:00
|
|
|
if (response_headers.FindKey(key)) {
|
2016-10-13 15:14:23 +00:00
|
|
|
base::ListValue* values = nullptr;
|
|
|
|
if (response_headers.GetList(key, &values))
|
|
|
|
values->AppendString(value);
|
|
|
|
} else {
|
2018-06-18 07:32:55 +00:00
|
|
|
auto values = std::make_unique<base::ListValue>();
|
2016-10-13 15:14:23 +00:00
|
|
|
values->AppendString(value);
|
|
|
|
response_headers.Set(key, std::move(values));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ConvertToV8(isolate, response_headers);
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:46:49 +00:00
|
|
|
bool Converter<net::HttpResponseHeaders*>::FromV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
net::HttpResponseHeaders* out) {
|
|
|
|
if (!val->IsObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-25 00:34:20 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-11-03 17:46:49 +00:00
|
|
|
auto context = isolate->GetCurrentContext();
|
|
|
|
auto headers = v8::Local<v8::Object>::Cast(val);
|
2019-01-21 16:27:11 +00:00
|
|
|
auto keys = headers->GetOwnPropertyNames(context).ToLocalChecked();
|
2017-11-03 17:46:49 +00:00
|
|
|
for (uint32_t i = 0; i < keys->Length(); i++) {
|
2019-01-21 16:27:11 +00:00
|
|
|
v8::Local<v8::Value> keyVal;
|
|
|
|
if (!keys->Get(context, i).ToLocal(&keyVal)) {
|
2017-11-03 17:46:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-25 00:34:20 +00:00
|
|
|
std::string key;
|
|
|
|
mate::ConvertFromV8(isolate, keyVal, &key);
|
|
|
|
|
2019-01-21 16:27:11 +00:00
|
|
|
auto localVal = headers->Get(context, keyVal).ToLocalChecked();
|
2018-10-25 00:34:20 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-11-03 17:46:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-31 13:39:07 +00:00
|
|
|
} // namespace mate
|
2016-01-26 08:28:21 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2016-06-08 13:52:21 +00:00
|
|
|
void FillRequestDetails(base::DictionaryValue* details,
|
|
|
|
const net::URLRequest* request) {
|
|
|
|
details->SetString("method", request->method());
|
|
|
|
std::string url;
|
2018-04-18 01:55:30 +00:00
|
|
|
if (!request->url_chain().empty())
|
|
|
|
url = request->url().spec();
|
2017-11-27 07:58:49 +00:00
|
|
|
details->SetKey("url", base::Value(url));
|
2016-06-08 13:52:21 +00:00
|
|
|
details->SetString("referrer", request->referrer());
|
2018-06-18 07:32:55 +00:00
|
|
|
auto list = std::make_unique<base::ListValue>();
|
2016-06-08 13:52:21 +00:00
|
|
|
GetUploadData(list.get(), request);
|
|
|
|
if (!list->empty())
|
|
|
|
details->Set("uploadData", std::move(list));
|
2018-06-18 07:32:55 +00:00
|
|
|
auto headers_value = std::make_unique<base::DictionaryValue>();
|
2017-11-03 17:45:46 +00:00
|
|
|
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));
|
2016-06-08 13:52:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 08:28:21 +00:00
|
|
|
void GetUploadData(base::ListValue* upload_data_list,
|
|
|
|
const net::URLRequest* request) {
|
|
|
|
const net::UploadDataStream* upload_data = request->get_upload();
|
|
|
|
if (!upload_data)
|
|
|
|
return;
|
2016-05-23 01:59:39 +00:00
|
|
|
const std::vector<std::unique_ptr<net::UploadElementReader>>* readers =
|
2016-01-26 08:28:21 +00:00
|
|
|
upload_data->GetElementReaders();
|
|
|
|
for (const auto& reader : *readers) {
|
2018-06-18 07:32:55 +00:00
|
|
|
auto upload_data_dict = std::make_unique<base::DictionaryValue>();
|
2016-01-26 08:28:21 +00:00
|
|
|
if (reader->AsBytesReader()) {
|
|
|
|
const net::UploadBytesElementReader* bytes_reader =
|
|
|
|
reader->AsBytesReader();
|
2018-06-27 19:57:10 +00:00
|
|
|
auto bytes = std::make_unique<base::Value>(
|
2018-06-26 22:08:27 +00:00
|
|
|
std::vector<char>(bytes_reader->bytes(),
|
2018-06-27 19:57:10 +00:00
|
|
|
bytes_reader->bytes() + bytes_reader->length()));
|
2016-03-08 14:28:53 +00:00
|
|
|
upload_data_dict->Set("bytes", std::move(bytes));
|
2016-01-26 08:28:21 +00:00
|
|
|
} else if (reader->AsFileReader()) {
|
2018-04-18 01:55:30 +00:00
|
|
|
const net::UploadFileElementReader* file_reader = reader->AsFileReader();
|
2016-01-26 08:28:21 +00:00
|
|
|
auto file_path = file_reader->path().AsUTF8Unsafe();
|
2017-11-27 07:58:49 +00:00
|
|
|
upload_data_dict->SetKey("file", base::Value(file_path));
|
2016-08-23 00:38:10 +00:00
|
|
|
} else {
|
|
|
|
const storage::UploadBlobElementReader* blob_reader =
|
|
|
|
static_cast<storage::UploadBlobElementReader*>(reader.get());
|
|
|
|
upload_data_dict->SetString("blobUUID", blob_reader->uuid());
|
2016-01-26 08:28:21 +00:00
|
|
|
}
|
2016-03-08 14:28:53 +00:00
|
|
|
upload_data_list->Append(std::move(upload_data_dict));
|
2016-01-26 08:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|