Skip to content

Commit f5e6c64

Browse files
committed
fixup! src,permission: add --allow-net permission
1 parent 26dbd30 commit f5e6c64

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

doc/api/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ req.on('error', (err) => {
289289
```
290290

291291
```console
292-
$ node --permission --allow-fs-read=./index.js index.js
292+
$ node --permission index.js
293293
Error: connect ERR_ACCESS_DENIED Access to this API has been restricted. Use --allow-net to manage permissions.
294294
code: 'ERR_ACCESS_DENIED',
295295
}

src/cares_wrap.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,11 +1943,17 @@ void GetNameInfo(const FunctionCallbackInfo<Value>& args) {
19431943
TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap.get(),
19441944
"ip", TRACE_STR_COPY(*ip), "port", port);
19451945

1946-
// TODO: check?
1947-
int err = req_wrap->Dispatch(uv_getnameinfo,
1948-
AfterGetNameInfo,
1949-
reinterpret_cast<struct sockaddr*>(&addr),
1950-
NI_NAMEREQD);
1946+
int err = 0;
1947+
if (!env->permission()->is_granted(
1948+
env, permission::PermissionScope::kNet, ip.ToStringView())) [[unlikely]] {
1949+
req_wrap->InsufficientPermissionError(*ip);
1950+
} else {
1951+
err = req_wrap->Dispatch(uv_getnameinfo,
1952+
AfterGetNameInfo,
1953+
reinterpret_cast<struct sockaddr*>(&addr),
1954+
NI_NAMEREQD);
1955+
}
1956+
19511957
if (err == 0)
19521958
// Release ownership of the pointer allowing the ownership to be transferred
19531959
USE(req_wrap.release());
@@ -1965,6 +1971,7 @@ void GetServers(const FunctionCallbackInfo<Value>& args) {
19651971

19661972
ares_addr_port_node* servers;
19671973

1974+
// TODO: check
19681975
int r = ares_get_servers_ports(channel->cares_channel(), &servers);
19691976
CHECK_EQ(r, ARES_SUCCESS);
19701977
auto cleanup = OnScopeLeave([&]() { ares_free_data(servers); });
@@ -2009,6 +2016,7 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {
20092016

20102017
uint32_t len = arr->Length();
20112018

2019+
// TODO: check
20122020
if (len == 0) {
20132021
int rv = ares_set_servers(channel->cares_channel(), nullptr);
20142022
return args.GetReturnValue().Set(rv);

src/cares_wrap.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ class GetNameInfoReqWrap final : public ReqWrap<uv_getnameinfo_t> {
215215
public:
216216
GetNameInfoReqWrap(Environment* env, v8::Local<v8::Object> req_wrap_obj);
217217

218+
void InsufficientPermissionError(std::string resource) {
219+
v8::HandleScope handle_scope(env()->isolate());
220+
v8::Context::Scope context_scope(env()->context());
221+
v8::Local<v8::Value> arg;
222+
if (!permission::CreateAccessDeniedError(env(), permission::PermissionScope::kNet, resource)
223+
.ToLocal(&arg)) {
224+
// TODO: handle error?
225+
}
226+
MakeCallback(env()->oncomplete_string(), 1, &arg);
227+
}
228+
218229
SET_NO_MEMORY_INFO()
219230
SET_MEMORY_INFO_NAME(GetNameInfoReqWrap)
220231
SET_SELF_SIZE(GetNameInfoReqWrap)

test/parallel/test-permission-net-dns.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,16 @@ const { Resolver } = dns.promises;
7777
assert.strictEqual(err.code, 'ERR_ACCESS_DENIED');
7878
}));
7979
}
80+
81+
{
82+
dns.lookupService('127.0.0.1', 80, common.mustCall((err) => {
83+
assert.strictEqual(err.code, 'ERR_ACCESS_DENIED');
84+
}));
85+
86+
dns.lookupService('8.8.8.8', 80, common.mustCall((err) => {
87+
assert.strictEqual(err.code, 'ERR_ACCESS_DENIED');
88+
}));
89+
dns.promises.lookupService('127.0.0.1', 80).catch(common.mustCall((err) => {
90+
assert.strictEqual(err.code, 'ERR_ACCESS_DENIED');
91+
}));
92+
}

0 commit comments

Comments
 (0)