Skip to content

Commit

Permalink
Allow regex removal of non-utf8 env var names. (#149)
Browse files Browse the repository at this point in the history
Fixes #148
  • Loading branch information
jsirois authored Sep 23, 2023
1 parent 2989546 commit 2f4160f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 0.13.1

Support regex removal of env vars with non-utf8 names in commands.

## 0.13.0

This release improves the help screen for BusyBox scies with more clear messages for the various
Expand Down
43 changes: 35 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [

[package]
name = "scie-jump"
version = "0.13.0"
version = "0.13.1"
description = "The self contained interpreted executable launcher."
authors = [
"John Sirois <john.sirois@gmail.com>",
Expand Down
4 changes: 3 additions & 1 deletion docs/packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ variable names will be removed. For example, `"BASH_.*": null` would remove all
variables whose name start with `BASH_` and `"=BASH_SOURCE": null` would just remove the
`BASH_SOURCE` environment variable. When processing env entries, removals are done first, then
defaults are set and finally overwrites are processed. This is regardless of the order of the env
var entries in the lift manifest JSON document.
var entries in the lift manifest JSON document. When evaluating environment variable removal regular
expressions, the regular expression syntax is that supported by the Rust [`regex` crate](
https://docs.rs/regex/latest/regex/).

You can also supply a list of commands under "scie.lift.boot.bindings". These commands are objects
with the same format as the "scie.lift.boot.commands" but they are not directly runnable by the end
Expand Down
5 changes: 3 additions & 2 deletions jump/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jump"
version = "0.13.0"
version = "0.13.1"
description = "The bulk of the scie-jump binary logic."
authors = [
"John Sirois <john.sirois@gmail.com>",
Expand All @@ -21,7 +21,8 @@ itertools = "0.10"
log = { workspace = true }
logging_timer = { workspace = true }
memmap2 = "0.7"
regex = { version = "1.7", default-features = false, features = ["std"] }
os_str_bytes = "6.5"
regex = { version = "1.9", default-features = false, features = ["std"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion jump/src/comparable_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::Deref;

use regex::Regex;
use regex::bytes::Regex;

#[derive(Clone, Debug)]
pub struct ComparableRegex(Regex);
Expand Down
38 changes: 24 additions & 14 deletions jump/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::ffi::OsString;
use std::process::{Child, Command, ExitStatus, Stdio};

use logging_timer::time;
use os_str_bytes::OsStrBytes;
use sha2::{Digest, Sha256};

use crate::comparable_regex::ComparableRegex;
Expand Down Expand Up @@ -64,19 +65,8 @@ impl EnvVars {
}
EnvVar::RemoveMatching(regex) => {
for (name, _) in env::vars_os() {
match name.into_string() {
Ok(name_utf8) => {
if regex.is_match(&name_utf8) {
removals.insert(name_utf8.into());
}
}
Err(name_os) => {
warn!(
"Cannot process env removal matching regex '{regex}' for \
non-utf8 env var name {name_os:?}",
regex = regex.as_str()
)
}
if regex.is_match(name.as_os_str().to_raw_bytes().as_ref()) {
removals.insert(name);
}
}
}
Expand Down Expand Up @@ -213,6 +203,7 @@ mod tests {
use std::ffi::OsString;
use std::sync::{Arc, OnceLock};

use os_str_bytes::OsStrBytes;
use parking_lot::ReentrantMutex;

use crate::comparable_regex::ComparableRegex;
Expand Down Expand Up @@ -334,6 +325,25 @@ mod tests {
.to_env_vars(),
"Expected removal of an env var with a non-utf8 value to succeed."
)
})
});

with_extra_env(&[(non_utf8.clone(), "baz".into())], || {
let mut re = String::new();
re.push('^');
for byte in non_utf8.as_os_str().to_raw_bytes().as_ref() {
re.push_str(format!(r"(?-u:\x{:X})", byte).as_str());
}
re.push('$');
assert_eq!(
vec![(non_utf8.clone(), None)],
EnvVars {
vars: vec![EnvVar::RemoveMatching(
ComparableRegex::try_from(re.as_str()).unwrap()
)]
}
.to_env_vars(),
"Expected removal of an env var with a non-utf8 name to succeed."
)
});
}
}

0 comments on commit 2f4160f

Please sign in to comment.