Skip to content

Commit 78d9617

Browse files
committed
allow doing a subset of a prefix when creating child perms
1 parent 522a4f3 commit 78d9617

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

runtime/permissions/lib.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ impl QueryDescriptor for EnvQueryDescriptor {
12161216
env_var_name.as_ref().starts_with(p.as_ref())
12171217
}
12181218
EnvQueryDescriptorInner::PrefixPattern(env_var_name) => {
1219-
p == env_var_name
1219+
env_var_name.as_ref().starts_with(p.as_ref())
12201220
}
12211221
},
12221222
}
@@ -4677,6 +4677,56 @@ mod tests {
46774677
assert_eq!(perms.env.revoke(Some("HomE")), PermissionState::Prompt);
46784678
}
46794679

4680+
#[test]
4681+
fn test_env_wildcards() {
4682+
set_prompter(Box::new(TestPrompter));
4683+
let _prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
4684+
let mut perms = Permissions::allow_all();
4685+
perms.env = UnaryPermission {
4686+
granted_global: false,
4687+
..Permissions::new_unary(
4688+
Some(HashSet::from([EnvDescriptor::new("HOME_*")])),
4689+
None,
4690+
false,
4691+
)
4692+
};
4693+
assert_eq!(perms.env.query(Some("HOME")), PermissionState::Prompt);
4694+
assert_eq!(perms.env.query(Some("HOME_")), PermissionState::Granted);
4695+
assert_eq!(perms.env.query(Some("HOME_TEST")), PermissionState::Granted);
4696+
4697+
// assert no privilege escalation
4698+
let parser = TestPermissionDescriptorParser;
4699+
assert!(perms
4700+
.env
4701+
.create_child_permissions(
4702+
ChildUnaryPermissionArg::GrantedList(vec!["HOME_SUB".to_string()]),
4703+
|value| parser.parse_env_descriptor(value).map(Some),
4704+
)
4705+
.is_ok());
4706+
assert!(perms
4707+
.env
4708+
.create_child_permissions(
4709+
ChildUnaryPermissionArg::GrantedList(vec!["HOME*".to_string()]),
4710+
|value| parser.parse_env_descriptor(value).map(Some),
4711+
)
4712+
.is_err());
4713+
assert!(perms
4714+
.env
4715+
.create_child_permissions(
4716+
ChildUnaryPermissionArg::GrantedList(vec!["OUTSIDE".to_string()]),
4717+
|value| parser.parse_env_descriptor(value).map(Some),
4718+
)
4719+
.is_err());
4720+
assert!(perms
4721+
.env
4722+
.create_child_permissions(
4723+
// ok because this is a subset of HOME_*
4724+
ChildUnaryPermissionArg::GrantedList(vec!["HOME_S*".to_string()]),
4725+
|value| parser.parse_env_descriptor(value).map(Some),
4726+
)
4727+
.is_ok());
4728+
}
4729+
46804730
#[test]
46814731
fn test_check_partial_denied() {
46824732
let parser = TestPermissionDescriptorParser;

0 commit comments

Comments
 (0)