Skip to content

Commit

Permalink
fix, test
Browse files Browse the repository at this point in the history
  • Loading branch information
yazan-abdalrahman committed Aug 28, 2024
1 parent 085b111 commit 1dae2e4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
58 changes: 15 additions & 43 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,61 +1329,33 @@ pub fn flags_from_vec(args: Vec<OsString>) -> clap::error::Result<Flags> {

fn process_env_permissions(allowed_env_vars: Vec<String>) -> Vec<String> {
let mut env_permissions = Vec::new();

for env_var in allowed_env_vars {
let (pattern, match_type) = if env_var.contains('*') {
if env_var.starts_with('*') {
let suffix = &env_var[1..];
(suffix.to_string(), MatchType::Suffix)
} else if env_var.ends_with('*') {
let prefix = &env_var[..env_var.len() - 1];
(prefix.to_string(), MatchType::Prefix)
} else {
let pattern = env_var.replace('*', "");
(pattern, MatchType::Wildcard)
}
} else {
(env_var, MatchType::Exact)
};

match match_type {
MatchType::Prefix => {
for (key, _value) in std::env::vars() {
if key.starts_with(&pattern) {
env_permissions.push(key);
}
if let Some(suffix) = env_var.strip_prefix('*') {
for (key, _value) in std::env::vars() {
if key.ends_with(suffix) {
env_permissions.push(key);
}
}
MatchType::Suffix => {
for (key, _value) in std::env::vars() {
if key.ends_with(&pattern) {
env_permissions.push(key);
}
} else if let Some(prefix) = env_var.strip_suffix('*') {
for (key, _value) in std::env::vars() {
if key.starts_with(prefix) {
env_permissions.push(key);
}
}
MatchType::Wildcard => {
for (key, _value) in std::env::vars() {
if key.contains(&pattern) {
env_permissions.push(key);
}
} else if env_var.contains('*') {
let pattern = env_var.replace('*', "");
for (key, _value) in std::env::vars() {
if key.contains(&pattern) {
env_permissions.push(key);
}
}
MatchType::Exact => {
env_permissions.push(pattern);
}
} else {
env_permissions.push(env_var);
}
}

env_permissions
}

enum MatchType {
Prefix,
Suffix,
Wildcard,
Exact,
}

macro_rules! heading {
($($name:ident = $title:expr),+; $total:literal) => {
$(const $name: &str = $title;)+
Expand Down
34 changes: 34 additions & 0 deletions tests/specs/permission/process_env_permissions/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"tempDir": true,
"tests": {
"deno_env_wildcard_tests": {
"envs": {
"MYAPP_HELLO": "Hello\tworld,",
"MYAPP_GOODBYE": "farewell",
"OTHER_VAR": "ignore"
},
"steps": [
{
"args": "run --allow-env=MYAPP_* main.js",
"output": "Hello\tworld,\nfarewell\n"
},
{
"args": "run --allow-env=*_HELLO,*_GOODBYE main.js",
"output": "Hello\tworld,\nfarewell\n"
},
{
"args": "run --allow-env=* main.js",
"output": "Hello\tworld,\nfarewell\n"
},
{
"args": "run --allow-env main.js",
"output": "Hello\tworld,\nfarewell\n"
},
{
"args": "run --allow-env=MYAPP_HELLO,MYAPP_GOODBYE main.js",
"output": "Hello\tworld,\nfarewell\n"
}
]
}
}
}
2 changes: 2 additions & 0 deletions tests/specs/permission/process_env_permissions/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(Deno.env.get("MYAPP_HELLO"));
console.log(Deno.env.get("MYAPP_GOODBYE"));

0 comments on commit 1dae2e4

Please sign in to comment.