Skip to content

Commit bc43984

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

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

ext/node/lib.rs

Lines changed: 6 additions & 0 deletions
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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ pub async fn op_getaddrinfo<P>(
3939
where
4040
P: crate::NodePermissions + 'static,
4141
{
42-
{
43-
let mut state_ = state.borrow_mut();
44-
let permissions = state_.borrow_mut::<P>();
45-
permissions.check_net((hostname.as_str(), None), "lookup")?;
46-
}
42+
let mut state_ = state.borrow_mut();
43+
let permissions = state_.borrow_mut::<P>();
44+
permissions.check_net((hostname.as_str(), None), "lookup")?;
45+
4746
let mut resolver = GaiResolver::new();
4847
let name = Name::from_str(&hostname)
4948
.map_err(|_| GetAddrInfoError::Resolution(hostname.clone()))?;
@@ -54,12 +53,15 @@ where
5453
.map(|addrs| {
5554
addrs
5655
.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(),
56+
.map(|addr| {
57+
permissions.grant_net(&addr.ip().to_string(), None);
58+
GetAddrInfoResult {
59+
family: match addr {
60+
std::net::SocketAddr::V4(_) => 4,
61+
std::net::SocketAddr::V6(_) => 6,
62+
},
63+
address: addr.ip().to_string(),
64+
}
6365
})
6466
.collect::<Vec<_>>()
6567
})

runtime/permissions/lib.rs

Lines changed: 14 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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)