#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "extensions/browser/api/socket/socket_api.h"
#include <memory>
#include <unordered_set>
#include <utility>
#include <vector>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/types/optional_util.h"
#include "base/values.h"
#include "build/build_config.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/api/socket/socket.h"
#include "extensions/browser/api/socket/tcp_socket.h"
#include "extensions/browser/api/socket/tls_socket.h"
#include "extensions/browser/api/socket/udp_socket.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/api/sockets/sockets_manifest_data.h"
#include "extensions/common/extension.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/permissions/socket_permission.h"
#include "net/base/host_port_pair.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/network_interfaces.h"
#include "net/base/url_util.h"
#include "net/dns/public/resolve_error_info.h"
#include "net/log/net_log_with_source.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "extensions/browser/api/socket/app_firewall_hole_manager.h"
#endif
APIPermissionID;
namespace extensions {
#if BUILDFLAG(IS_CHROMEOS_ASH)
const char kCrOSTerminal[] = "chrome-untrusted://terminal";
#endif
namespace {
const char kAddressKey[] = …;
const char kPortKey[] = …;
const char kBytesWrittenKey[] = …;
const char kDataKey[] = …;
const char kResultCodeKey[] = …;
const char kSocketIdKey[] = …;
const char kSocketNotFoundError[] = …;
const char kDnsLookupFailedError[] = …;
const char kPermissionError[] = …;
const char kPortInvalidError[] = …;
const char kNetworkListError[] = …;
const char kTCPSocketBindError[] = …;
const char kMulticastSocketTypeError[] = …;
const char kSecureSocketTypeError[] = …;
const char kSocketNotConnectedError[] = …;
const char kWildcardAddress[] = …;
const uint16_t kWildcardPort = …;
#if BUILDFLAG(IS_CHROMEOS)
const char kFirewallFailure[] = "Failed to open firewall port";
#endif
bool IsPortValid(int port) { … }
}
BrowserThread;
SocketPermissionRequest;
SocketApiFunction::SocketApiFunction() = default;
SocketApiFunction::~SocketApiFunction() = default;
int SocketApiFunction::AddSocket(Socket* socket) { … }
Socket* SocketApiFunction::GetSocket(int api_resource_id) { … }
void SocketApiFunction::ReplaceSocket(int api_resource_id, Socket* socket) { … }
std::unordered_set<int>* SocketApiFunction::GetSocketIds() { … }
void SocketApiFunction::RemoveSocket(int api_resource_id) { … }
std::unique_ptr<SocketResourceManagerInterface>
SocketApiFunction::CreateSocketResourceManager() { … }
void SocketApiFunction::OpenFirewallHole(const std::string& address,
int socket_id,
Socket* socket) { … }
ExtensionFunction::ResponseAction SocketApiFunction::Run() { … }
ExtensionFunction::ResponseValue SocketApiFunction::ErrorWithCode(
int error_code,
const std::string& error) { … }
std::string SocketApiFunction::GetOriginId() const { … }
bool SocketApiFunction::CheckPermission(
const APIPermission::CheckParam& param) const { … }
bool SocketApiFunction::CheckRequest(
const content::SocketPermissionRequest& param) const { … }
SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() =
default;
SocketExtensionWithDnsLookupFunction::~SocketExtensionWithDnsLookupFunction() =
default;
void SocketExtensionWithDnsLookupFunction::StartDnsLookup(
const net::HostPortPair& host_port_pair,
net::DnsQueryType dns_query_type) { … }
void SocketExtensionWithDnsLookupFunction::OnComplete(
int result,
const net::ResolveErrorInfo& resolve_error_info,
const std::optional<net::AddressList>& resolved_addresses,
const std::optional<net::HostResolverEndpointResults>&
endpoint_results_with_metadata) { … }
SocketCreateFunction::SocketCreateFunction() = default;
SocketCreateFunction::~SocketCreateFunction() = default;
ExtensionFunction::ResponseAction SocketCreateFunction::Work() { … }
ExtensionFunction::ResponseAction SocketDestroyFunction::Work() { … }
SocketConnectFunction::SocketConnectFunction() = default;
SocketConnectFunction::~SocketConnectFunction() = default;
ExtensionFunction::ResponseAction SocketConnectFunction::Work() { … }
void SocketConnectFunction::AfterDnsLookup(int lookup_result) { … }
void SocketConnectFunction::StartConnect() { … }
void SocketConnectFunction::OnConnect(int result) { … }
ExtensionFunction::ResponseAction SocketDisconnectFunction::Work() { … }
ExtensionFunction::ResponseAction SocketBindFunction::Work() { … }
void SocketBindFunction::OnCompleted(int net_result) { … }
SocketListenFunction::SocketListenFunction() = default;
SocketListenFunction::~SocketListenFunction() = default;
ExtensionFunction::ResponseAction SocketListenFunction::Work() { … }
void SocketListenFunction::OnCompleted(int result,
const std::string& error_msg) { … }
SocketAcceptFunction::SocketAcceptFunction() = default;
SocketAcceptFunction::~SocketAcceptFunction() = default;
ExtensionFunction::ResponseAction SocketAcceptFunction::Work() { … }
void SocketAcceptFunction::OnAccept(
int result_code,
mojo::PendingRemote<network::mojom::TCPConnectedSocket> socket,
const std::optional<net::IPEndPoint>& remote_addr,
mojo::ScopedDataPipeConsumerHandle receive_pipe_handle,
mojo::ScopedDataPipeProducerHandle send_pipe_handle) { … }
SocketReadFunction::SocketReadFunction() = default;
SocketReadFunction::~SocketReadFunction() = default;
ExtensionFunction::ResponseAction SocketReadFunction::Work() { … }
void SocketReadFunction::OnCompleted(int bytes_read,
scoped_refptr<net::IOBuffer> io_buffer,
bool socket_destroying) { … }
SocketWriteFunction::SocketWriteFunction() = default;
SocketWriteFunction::~SocketWriteFunction() = default;
ExtensionFunction::ResponseAction SocketWriteFunction::Work() { … }
void SocketWriteFunction::OnCompleted(int bytes_written) { … }
SocketRecvFromFunction::SocketRecvFromFunction() = default;
SocketRecvFromFunction::~SocketRecvFromFunction() = default;
ExtensionFunction::ResponseAction SocketRecvFromFunction::Work() { … }
void SocketRecvFromFunction::OnCompleted(int bytes_read,
scoped_refptr<net::IOBuffer> io_buffer,
bool socket_destroying,
const std::string& address,
uint16_t port) { … }
SocketSendToFunction::SocketSendToFunction() = default;
SocketSendToFunction::~SocketSendToFunction() = default;
ExtensionFunction::ResponseAction SocketSendToFunction::Work() { … }
void SocketSendToFunction::AfterDnsLookup(int lookup_result) { … }
void SocketSendToFunction::StartSendTo() { … }
void SocketSendToFunction::OnCompleted(int bytes_written) { … }
SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() = default;
SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() = default;
ExtensionFunction::ResponseAction SocketSetKeepAliveFunction::Work() { … }
void SocketSetKeepAliveFunction::OnCompleted(bool success) { … }
SocketSetNoDelayFunction::SocketSetNoDelayFunction() = default;
SocketSetNoDelayFunction::~SocketSetNoDelayFunction() = default;
ExtensionFunction::ResponseAction SocketSetNoDelayFunction::Work() { … }
void SocketSetNoDelayFunction::OnCompleted(bool success) { … }
SocketGetInfoFunction::SocketGetInfoFunction() = default;
SocketGetInfoFunction::~SocketGetInfoFunction() = default;
ExtensionFunction::ResponseAction SocketGetInfoFunction::Work() { … }
ExtensionFunction::ResponseAction SocketGetNetworkListFunction::Run() { … }
void SocketGetNetworkListFunction::GotNetworkList(
const std::optional<net::NetworkInterfaceList>& interface_list) { … }
SocketJoinGroupFunction::SocketJoinGroupFunction() = default;
SocketJoinGroupFunction::~SocketJoinGroupFunction() = default;
ExtensionFunction::ResponseAction SocketJoinGroupFunction::Work() { … }
void SocketJoinGroupFunction::OnCompleted(int result) { … }
SocketLeaveGroupFunction::SocketLeaveGroupFunction() = default;
SocketLeaveGroupFunction::~SocketLeaveGroupFunction() = default;
ExtensionFunction::ResponseAction SocketLeaveGroupFunction::Work() { … }
void SocketLeaveGroupFunction::OnCompleted(int result) { … }
SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() =
default;
SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() =
default;
ExtensionFunction::ResponseAction SocketSetMulticastTimeToLiveFunction::Work() { … }
SocketSetMulticastLoopbackModeFunction::
SocketSetMulticastLoopbackModeFunction() = default;
SocketSetMulticastLoopbackModeFunction::
~SocketSetMulticastLoopbackModeFunction() = default;
ExtensionFunction::ResponseAction
SocketSetMulticastLoopbackModeFunction::Work() { … }
SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() = default;
SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() = default;
ExtensionFunction::ResponseAction SocketGetJoinedGroupsFunction::Work() { … }
SocketSecureFunction::SocketSecureFunction() = default;
SocketSecureFunction::~SocketSecureFunction() = default;
ExtensionFunction::ResponseAction SocketSecureFunction::Work() { … }
void SocketSecureFunction::TlsConnectDone(
int result,
mojo::PendingRemote<network::mojom::TLSClientSocket> tls_socket,
const net::IPEndPoint& local_addr,
const net::IPEndPoint& peer_addr,
mojo::ScopedDataPipeConsumerHandle receive_pipe_handle,
mojo::ScopedDataPipeProducerHandle send_pipe_handle) { … }
}