Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/api/permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ Each message is an object with the following properties:

* `permission` {string} The name of the denied permission scope.
* `resource` {string} The resource that access was denied to (e.g. a file path
or host).
or host). For network operations on an IP address, this is the address in
its canonical form and the port, such as `127.0.0.1:8080` or `[::1]:8080`.
The port is the requested one: `0` when the operating system chooses it. For
network operations that have no address, such as adopting a file descriptor,
this is an empty string.
* `drop` {boolean|undefined} `true` when the message was published by
[`permission.drop()`][] rather than by a failed permission check.

Expand Down
11 changes: 8 additions & 3 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "node.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_sockaddr-inl.h"
#include "req_wrap-inl.h"
#include "util-inl.h"
#include "uv.h"
Expand Down Expand Up @@ -2092,10 +2093,14 @@ void GetNameInfo(const FunctionCallbackInfo<Value>& args) {
"ip", TRACE_STR_COPY(*ip), "port", port);

int err = 0;
std::string resource;
if (env->permission()->enabled()) [[unlikely]] {
resource =
SocketAddress(reinterpret_cast<const sockaddr*>(&addr)).ToString();
}
if (!env->permission()->is_granted(
env, permission::PermissionScope::kNet, ip.ToStringView()))
[[unlikely]] {
req_wrap->InsufficientPermissionError(*ip);
env, permission::PermissionScope::kNet, resource)) [[unlikely]] {
req_wrap->InsufficientPermissionError(resource);
} else {
err = req_wrap->Dispatch(uv_getnameinfo,
AfterGetNameInfo,
Expand Down
10 changes: 7 additions & 3 deletions src/dtls/dtls_endpoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -983,11 +983,14 @@ void DTLSEndpoint::DoBind(const FunctionCallbackInfo<Value>& args) {
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid address");
}

std::string resource = addr.ToString();
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kNet, addr.ToString());
env, permission::PermissionScope::kNet, resource);

int err = endpoint->Bind(addr);
if (err != 0) {
if (err == 0) {
endpoint->bound_net_resource_ = std::move(resource);
} else {
// A libuv failure reported as ERR_INVALID_STATE loses the errno and the
// syscall, so a caller cannot tell EADDRINUSE from anything else. Every
// other binding surfaces these as the error code libuv gave, and code
Expand All @@ -1001,7 +1004,8 @@ void DTLSEndpoint::DoListen(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&endpoint, args.This());
Environment* env = endpoint->env();

THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kNet, "");
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kNet, endpoint->bound_net_resource_);

CHECK(DTLSContext::HasInstance(env, args[0]));
DTLSContext* context;
Expand Down
4 changes: 4 additions & 0 deletions src/dtls/dtls_endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <openssl/ssl.h>

#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -195,6 +196,9 @@ class DTLSEndpoint final : public HandleWrap {
bool MakeRoomForPendingHandshake();

uv_udp_t handle_;
// The address and port passed to Bind(), as network permission checks
// report them. Empty when the endpoint is not bound.
std::string bound_net_resource_;

// Reusable receive buffer for uv_udp_recv (see OnAlloc). libuv delivers one
// datagram at a time and OnRecv consumes each before the next OnAlloc, so a
Expand Down
5 changes: 4 additions & 1 deletion src/quic/endpoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2093,7 +2093,10 @@ JS_METHOD_IMPL(Endpoint::DoListen) {
ASSIGN_OR_RETURN_UNWRAP(&endpoint, args.This());
auto env = Environment::GetCurrent(args);

THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kNet, "");
THROW_IF_INSUFFICIENT_PERMISSIONS(
env,
permission::PermissionScope::kNet,
endpoint->options_.local_address->ToString());

Session::Options options;
if (Session::Options::From(env, args[0]).To(&options)) {
Expand Down
96 changes: 59 additions & 37 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "node_buffer.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "node_sockaddr-inl.h"
#include "permission/permission.h"
#include "stream_base-inl.h"
#include "stream_wrap.h"
Expand Down Expand Up @@ -443,7 +444,8 @@ std::unique_ptr<worker::TransferData> TCPWrap::TransferForMessaging() {
// it, or until TransferData is destroyed if the message is not delivered.
Close();

return std::make_unique<TransferData>(duplicate, type);
return std::make_unique<TransferData>(
duplicate, type, std::move(bound_net_resource_));
}

TCPWrap::TransferData::~TransferData() {
Expand Down Expand Up @@ -480,6 +482,7 @@ BaseObjectPtr<BaseObject> TCPWrap::TransferData::Deserialize(
if (wrap == nullptr) return {};

if (uv_tcp_open(&wrap->handle_, socket_) != 0) return {};
wrap->bound_net_resource_ = std::move(bound_net_resource_);

#ifdef _WIN32
socket_ = static_cast<uv_os_sock_t>(-1);
Expand All @@ -501,12 +504,23 @@ void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args,
&wrap, args.This(), args.GetReturnValue().Set(UV_EBADF));
Environment* env = wrap->env();

THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kNet, "");

node::Utf8Value ip_address(env->isolate(), args[0]);
int port;
unsigned int flags = 0;
if (!args[1]->Int32Value(env->context()).To(&port)) return;

T addr;
int err = uv_ip_addr(*ip_address, port, &addr);
if (err != 0) return args.GetReturnValue().Set(err);

std::string resource;
if (env->permission()->enabled()) [[unlikely]] {
resource =
SocketAddress(reinterpret_cast<const sockaddr*>(&addr)).ToString();
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kNet, resource);
}

if (args.Length() >= 3 && args[2]->IsUint32()) {
if (!args[2]->Uint32Value(env->context()).To(&flags)) return;
// Can not set IPV6 flags on IPV4 socket
Expand All @@ -515,13 +529,9 @@ void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args,
}
}

T addr;
int err = uv_ip_addr(*ip_address, port, &addr);

if (err == 0) {
err = uv_tcp_bind(
&wrap->handle_, reinterpret_cast<const sockaddr*>(&addr), flags);
}
err = uv_tcp_bind(
&wrap->handle_, reinterpret_cast<const sockaddr*>(&addr), flags);
if (err == 0) wrap->bound_net_resource_ = std::move(resource);
args.GetReturnValue().Set(err);
}

Expand All @@ -541,7 +551,15 @@ void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
int backlog;
if (!args[0]->Int32Value(env->context()).To(&backlog)) return;

THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kNet, "");
if (env->permission()->enabled()) [[unlikely]] {
// A handle opened from a file descriptor was not bound through Bind().
THROW_IF_INSUFFICIENT_PERMISSIONS(
env,
permission::PermissionScope::kNet,
wrap->bound_net_resource_.empty()
? SocketAddress::FromSockName(wrap->handle_).ToString()
: wrap->bound_net_resource_);
}

int err = uv_listen(
reinterpret_cast<uv_stream_t*>(&wrap->handle_), backlog, OnConnection);
Expand Down Expand Up @@ -576,36 +594,40 @@ void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
Local<Object> req_wrap_obj = args[0].As<Object>();
node::Utf8Value ip_address(env->isolate(), args[1]);

ERR_ACCESS_DENIED_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kNet, ip_address.ToStringView(), args);

T addr;
int err = uv_ip_addr(*ip_address, port, &addr);
if (err != 0) return args.GetReturnValue().Set(err);

if (env->permission()->enabled()) [[unlikely]] {
ERR_ACCESS_DENIED_IF_INSUFFICIENT_PERMISSIONS(
env,
permission::PermissionScope::kNet,
SocketAddress(reinterpret_cast<const sockaddr*>(&addr)).ToString(),
args);
}

if (err == 0) {
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
ConnectWrap* req_wrap =
new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP);
err = req_wrap->Dispatch(uv_tcp_connect,
&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
AfterConnect);
if (err) {
delete req_wrap;
} else {
CHECK(args[2]->Uint32Value(env->context()).IsJust());
uint32_t port;
if (!args[2]->Uint32Value(env->context()).To(&port)) {
return;
}
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native),
"connect",
req_wrap,
"ip",
TRACE_STR_COPY(*ip_address),
"port",
port);
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
ConnectWrap* req_wrap =
new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP);
err = req_wrap->Dispatch(uv_tcp_connect,
&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
AfterConnect);
if (err) {
delete req_wrap;
} else {
CHECK(args[2]->Uint32Value(env->context()).IsJust());
uint32_t port;
if (!args[2]->Uint32Value(env->context()).To(&port)) {
return;
}
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native),
"connect",
req_wrap,
"ip",
TRACE_STR_COPY(*ip_address),
"port",
port);
}

args.GetReturnValue().Set(err);
Expand Down
14 changes: 12 additions & 2 deletions src/tcp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "connection_wrap.h"
#include "node_messaging.h"

#include <string>

namespace node {

class ExternalReferenceRegistry;
Expand Down Expand Up @@ -74,8 +76,12 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {

class TransferData : public worker::TransferData {
public:
explicit TransferData(uv_os_sock_t socket, SocketType type)
: socket_(socket), type_(type) {}
explicit TransferData(uv_os_sock_t socket,
SocketType type,
std::string bound_net_resource)
: socket_(socket),
type_(type),
bound_net_resource_(std::move(bound_net_resource)) {}
~TransferData() override;

BaseObjectPtr<BaseObject> Deserialize(
Expand All @@ -90,6 +96,7 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
private:
uv_os_sock_t socket_;
SocketType type_;
std::string bound_net_resource_;
};

template <typename T,
Expand All @@ -99,6 +106,9 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
TCPWrap(Environment* env, v8::Local<v8::Object> object,
ProviderType provider);

// The resource Bind() was checked with. Empty if Bind() was not called.
std::string bound_net_resource_;

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetNoDelay(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetKeepAlive(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
Loading
Loading