Skip to content

Commit 24df1db

Browse files
committed
src: pass address and port to net permission checks
Network permission checks reported the denied resource inconsistently: some passed only the IP address, some the address and port, and several passed nothing at all. Report `ip:port` (`[ip]:port` for IPv6) from every check that operates on an IP address, so that the `resource` of `ERR_ACCESS_DENIED` errors and of `node:permission-model:net` diagnostics channel messages identifies the endpoint. The address is formatted from the parsed socket address, so it is reported in its canonical form. An address that cannot be parsed now fails with `EINVAL` before the permission check, instead of with `ERR_ACCESS_DENIED`. Assisted-by: Claude Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
1 parent fdd741c commit 24df1db

14 files changed

Lines changed: 405 additions & 91 deletions

‎doc/api/permissions.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ Each message is an object with the following properties:
189189

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

‎src/cares_wrap.cc‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "node.h"
3030
#include "node_errors.h"
3131
#include "node_external_reference.h"
32+
#include "node_sockaddr-inl.h"
3233
#include "req_wrap-inl.h"
3334
#include "util-inl.h"
3435
#include "uv.h"
@@ -2092,10 +2093,14 @@ void GetNameInfo(const FunctionCallbackInfo<Value>& args) {
20922093
"ip", TRACE_STR_COPY(*ip), "port", port);
20932094

20942095
int err = 0;
2096+
std::string resource;
2097+
if (env->permission()->enabled()) [[unlikely]] {
2098+
resource =
2099+
SocketAddress(reinterpret_cast<const sockaddr*>(&addr)).ToString();
2100+
}
20952101
if (!env->permission()->is_granted(
2096-
env, permission::PermissionScope::kNet, ip.ToStringView()))
2097-
[[unlikely]] {
2098-
req_wrap->InsufficientPermissionError(*ip);
2102+
env, permission::PermissionScope::kNet, resource)) [[unlikely]] {
2103+
req_wrap->InsufficientPermissionError(resource);
20992104
} else {
21002105
err = req_wrap->Dispatch(uv_getnameinfo,
21012106
AfterGetNameInfo,

‎src/dtls/dtls_endpoint.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,11 +983,14 @@ void DTLSEndpoint::DoBind(const FunctionCallbackInfo<Value>& args) {
983983
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid address");
984984
}
985985

986+
std::string resource = addr.ToString();
986987
THROW_IF_INSUFFICIENT_PERMISSIONS(
987-
env, permission::PermissionScope::kNet, addr.ToString());
988+
env, permission::PermissionScope::kNet, resource);
988989

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

1004-
THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kNet, "");
1007+
THROW_IF_INSUFFICIENT_PERMISSIONS(
1008+
env, permission::PermissionScope::kNet, endpoint->bound_net_resource_);
10051009

10061010
CHECK(DTLSContext::HasInstance(env, args[0]));
10071011
DTLSContext* context;

‎src/dtls/dtls_endpoint.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <openssl/ssl.h>
1515

1616
#include <memory>
17+
#include <string>
1718
#include <unordered_map>
1819
#include <vector>
1920

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

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

199203
// Reusable receive buffer for uv_udp_recv (see OnAlloc). libuv delivers one
200204
// datagram at a time and OnRecv consumes each before the next OnAlloc, so a

‎src/quic/endpoint.cc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,10 @@ JS_METHOD_IMPL(Endpoint::DoListen) {
20932093
ASSIGN_OR_RETURN_UNWRAP(&endpoint, args.This());
20942094
auto env = Environment::GetCurrent(args);
20952095

2096-
THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kNet, "");
2096+
THROW_IF_INSUFFICIENT_PERMISSIONS(
2097+
env,
2098+
permission::PermissionScope::kNet,
2099+
endpoint->options_.local_address->ToString());
20972100

20982101
Session::Options options;
20992102
if (Session::Options::From(env, args[0]).To(&options)) {

‎src/tcp_wrap.cc‎

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "node_buffer.h"
3434
#include "node_external_reference.h"
3535
#include "node_internals.h"
36+
#include "node_sockaddr-inl.h"
3637
#include "permission/permission.h"
3738
#include "stream_base-inl.h"
3839
#include "stream_wrap.h"
@@ -443,7 +444,8 @@ std::unique_ptr<worker::TransferData> TCPWrap::TransferForMessaging() {
443444
// it, or until TransferData is destroyed if the message is not delivered.
444445
Close();
445446

446-
return std::make_unique<TransferData>(duplicate, type);
447+
return std::make_unique<TransferData>(
448+
duplicate, type, std::move(bound_net_resource_));
447449
}
448450

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

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

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

504-
THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kNet, "");
505-
506507
node::Utf8Value ip_address(env->isolate(), args[0]);
507508
int port;
508509
unsigned int flags = 0;
509510
if (!args[1]->Int32Value(env->context()).To(&port)) return;
511+
512+
T addr;
513+
int err = uv_ip_addr(*ip_address, port, &addr);
514+
if (err != 0) return args.GetReturnValue().Set(err);
515+
516+
std::string resource;
517+
if (env->permission()->enabled()) [[unlikely]] {
518+
resource =
519+
SocketAddress(reinterpret_cast<const sockaddr*>(&addr)).ToString();
520+
THROW_IF_INSUFFICIENT_PERMISSIONS(
521+
env, permission::PermissionScope::kNet, resource);
522+
}
523+
510524
if (args.Length() >= 3 && args[2]->IsUint32()) {
511525
if (!args[2]->Uint32Value(env->context()).To(&flags)) return;
512526
// Can not set IPV6 flags on IPV4 socket
@@ -515,13 +529,9 @@ void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args,
515529
}
516530
}
517531

518-
T addr;
519-
int err = uv_ip_addr(*ip_address, port, &addr);
520-
521-
if (err == 0) {
522-
err = uv_tcp_bind(
523-
&wrap->handle_, reinterpret_cast<const sockaddr*>(&addr), flags);
524-
}
532+
err = uv_tcp_bind(
533+
&wrap->handle_, reinterpret_cast<const sockaddr*>(&addr), flags);
534+
if (err == 0) wrap->bound_net_resource_ = std::move(resource);
525535
args.GetReturnValue().Set(err);
526536
}
527537

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

544-
THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kNet, "");
554+
if (env->permission()->enabled()) [[unlikely]] {
555+
// A handle opened from a file descriptor was not bound through Bind().
556+
THROW_IF_INSUFFICIENT_PERMISSIONS(
557+
env,
558+
permission::PermissionScope::kNet,
559+
wrap->bound_net_resource_.empty()
560+
? SocketAddress::FromSockName(wrap->handle_).ToString()
561+
: wrap->bound_net_resource_);
562+
}
545563

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

579-
ERR_ACCESS_DENIED_IF_INSUFFICIENT_PERMISSIONS(
580-
env, permission::PermissionScope::kNet, ip_address.ToStringView(), args);
581-
582597
T addr;
583598
int err = uv_ip_addr(*ip_address, port, &addr);
599+
if (err != 0) return args.GetReturnValue().Set(err);
600+
601+
if (env->permission()->enabled()) [[unlikely]] {
602+
ERR_ACCESS_DENIED_IF_INSUFFICIENT_PERMISSIONS(
603+
env,
604+
permission::PermissionScope::kNet,
605+
SocketAddress(reinterpret_cast<const sockaddr*>(&addr)).ToString(),
606+
args);
607+
}
584608

585-
if (err == 0) {
586-
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
587-
ConnectWrap* req_wrap =
588-
new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP);
589-
err = req_wrap->Dispatch(uv_tcp_connect,
590-
&wrap->handle_,
591-
reinterpret_cast<const sockaddr*>(&addr),
592-
AfterConnect);
593-
if (err) {
594-
delete req_wrap;
595-
} else {
596-
CHECK(args[2]->Uint32Value(env->context()).IsJust());
597-
uint32_t port;
598-
if (!args[2]->Uint32Value(env->context()).To(&port)) {
599-
return;
600-
}
601-
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native),
602-
"connect",
603-
req_wrap,
604-
"ip",
605-
TRACE_STR_COPY(*ip_address),
606-
"port",
607-
port);
609+
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
610+
ConnectWrap* req_wrap =
611+
new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP);
612+
err = req_wrap->Dispatch(uv_tcp_connect,
613+
&wrap->handle_,
614+
reinterpret_cast<const sockaddr*>(&addr),
615+
AfterConnect);
616+
if (err) {
617+
delete req_wrap;
618+
} else {
619+
CHECK(args[2]->Uint32Value(env->context()).IsJust());
620+
uint32_t port;
621+
if (!args[2]->Uint32Value(env->context()).To(&port)) {
622+
return;
608623
}
624+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native),
625+
"connect",
626+
req_wrap,
627+
"ip",
628+
TRACE_STR_COPY(*ip_address),
629+
"port",
630+
port);
609631
}
610632

611633
args.GetReturnValue().Set(err);

‎src/tcp_wrap.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "connection_wrap.h"
2929
#include "node_messaging.h"
3030

31+
#include <string>
32+
3133
namespace node {
3234

3335
class ExternalReferenceRegistry;
@@ -74,8 +76,12 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
7476

7577
class TransferData : public worker::TransferData {
7678
public:
77-
explicit TransferData(uv_os_sock_t socket, SocketType type)
78-
: socket_(socket), type_(type) {}
79+
explicit TransferData(uv_os_sock_t socket,
80+
SocketType type,
81+
std::string bound_net_resource)
82+
: socket_(socket),
83+
type_(type),
84+
bound_net_resource_(std::move(bound_net_resource)) {}
7985
~TransferData() override;
8086

8187
BaseObjectPtr<BaseObject> Deserialize(
@@ -90,6 +96,7 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
9096
private:
9197
uv_os_sock_t socket_;
9298
SocketType type_;
99+
std::string bound_net_resource_;
93100
};
94101

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

109+
// The resource Bind() was checked with. Empty if Bind() was not called.
110+
std::string bound_net_resource_;
111+
102112
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
103113
static void SetNoDelay(const v8::FunctionCallbackInfo<v8::Value>& args);
104114
static void SetKeepAlive(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)