Skip to content

Commit 9cde2d9

Browse files
committed
grant net permission for ips when net perms given to hostname
1 parent d0a4740 commit 9cde2d9

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

ext/node/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub trait NodePermissions {
8787
path: &str,
8888
api_name: Option<&str>,
8989
) -> Result<PathBuf, PermissionCheckError>;
90+
fn grant_net(&mut self, host: &str, port: Option<u16>);
9091
}
9192

9293
impl NodePermissions for deno_permissions::PermissionsContainer {
@@ -147,6 +148,11 @@ impl NodePermissions for deno_permissions::PermissionsContainer {
147148
) -> Result<(), PermissionCheckError> {
148149
deno_permissions::PermissionsContainer::check_sys(self, kind, api_name)
149150
}
151+
152+
fn grant_net(&mut self, host: &str, port: Option<u16>) {
153+
// ignore the result when host parsing fails
154+
_ = deno_permissions::PermissionsContainer::grant_net(self, host, port);
155+
}
150156
}
151157

152158
#[allow(clippy::disallowed_types)]

ext/node/ops/dns.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ where
4444
let permissions = state_.borrow_mut::<P>();
4545
permissions.check_net((hostname.as_str(), None), "lookup")?;
4646
}
47+
4748
let mut resolver = GaiResolver::new();
4849
let name = Name::from_str(&hostname)
4950
.map_err(|_| GetAddrInfoError::Resolution(hostname.clone()))?;
@@ -52,14 +53,19 @@ where
5253
.await
5354
.map_err(|_| GetAddrInfoError::Resolution(hostname))
5455
.map(|addrs| {
56+
let mut state_ = state.borrow_mut();
57+
let permissions = state_.borrow_mut::<P>();
5558
addrs
5659
.into_iter()
57-
.map(|addr| GetAddrInfoResult {
58-
family: match addr {
59-
std::net::SocketAddr::V4(_) => 4,
60-
std::net::SocketAddr::V6(_) => 6,
61-
},
62-
address: addr.ip().to_string(),
60+
.map(|addr| {
61+
permissions.grant_net(&addr.ip().to_string(), None);
62+
GetAddrInfoResult {
63+
family: match addr {
64+
std::net::SocketAddr::V4(_) => 4,
65+
std::net::SocketAddr::V6(_) => 6,
66+
},
67+
address: addr.ip().to_string(),
68+
}
6369
})
6470
.collect::<Vec<_>>()
6571
})

runtime/permissions/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,20 @@ impl PermissionsContainer {
33443344
),
33453345
)
33463346
}
3347+
3348+
pub fn grant_net(
3349+
&self,
3350+
host: &str,
3351+
port: Option<u16>,
3352+
) -> Result<bool, NetDescriptorParseError> {
3353+
Ok(
3354+
self
3355+
.inner
3356+
.lock()
3357+
.net
3358+
.insert_granted(Some(&NetDescriptor(Host::parse(host)?, port))),
3359+
)
3360+
}
33473361
}
33483362

33493363
const fn unit_permission_from_flag_bools(

runtime/snapshot.rs

+3
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ impl deno_node::NodePermissions for Permissions {
121121
) -> Result<(), PermissionCheckError> {
122122
unreachable!("snapshotting!")
123123
}
124+
fn grant_net(&mut self, _host: &str, _port: Option<u16>) {
125+
unreachable!("snapshotting!")
126+
}
124127
}
125128

126129
impl deno_net::NetPermissions for Permissions {

0 commit comments

Comments
 (0)