Skip to content

Commit 5feffe3

Browse files
Update process.info environ to return a Dictionary (#1549)
* Update process.info environ field to return a Dictionary Modified `eldritch-libprocess` `process.info` to return the environment variables as a Dictionary instead of a comma-separated String. This allows easier access to specific environment variables. Updated the user guide documentation to reflect this change. * Update process.info environ to return a Dictionary Modified `eldritch-libprocess` `process.info` to return environment variables as a Dictionary instead of a comma-separated String. Updated `tavern/tomes/process_info/main.eldritch` to handle the new Dictionary format. Updated `docs/_docs/user-guide/eldritch.md` to reflect the API change. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 2a86d51 commit 5feffe3

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

docs/_docs/user-guide/eldritch.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -719,21 +719,21 @@ The <b>process.info</b> method returns all information on a given process ID. De
719719
"-i"
720720
],
721721
"exe": "/home/user/realm/implants/target/debug/golem",
722-
"environ": [
723-
"USER=user",
724-
"HOME=/home/user",
725-
"PATH=/home/user/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:/home/user/.dotnet/tools",
726-
"SHELL=/bin/zsh",
727-
"TERM=xterm-256color",
728-
"SSH_TTY=/dev/pts/0",
729-
"SHLVL=1",
730-
"PWD=/home/user",
731-
"OLDPWD=/home/user",
732-
"XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop",
733-
"P9K_TTY=old",
734-
"_P9K_TTY=/dev/pts/0",
735-
"ZSH=/home/user/.oh-my-zsh",
736-
],
722+
"environ": {
723+
"USER": "user",
724+
"HOME": "/home/user",
725+
"PATH": "/home/user/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:/home/user/.dotnet/tools",
726+
"SHELL": "/bin/zsh",
727+
"TERM": "xterm-256color",
728+
"SSH_TTY": "/dev/pts/0",
729+
"SHLVL": "1",
730+
"PWD": "/home/user",
731+
"OLDPWD": "/home/user",
732+
"XDG_DATA_DIRS": "/usr/local/share:/usr/share:/var/lib/snapd/desktop",
733+
"P9K_TTY": "old",
734+
"_P9K_TTY": "/dev/pts/0",
735+
"ZSH": "/home/user/.oh-my-zsh",
736+
},
737737
"cwd": "/home/user/realm/implants",
738738
"root": "/",
739739
"memory_usage": 32317440,

implants/lib/eldritchv2/stdlib/eldritch-libprocess/src/std/info_impl.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use alloc::collections::BTreeMap;
22
use alloc::format;
33
use alloc::string::{String, ToString};
4+
use alloc::sync::Arc;
45
use eldritch_core::Value;
6+
use spin::RwLock;
57
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};
68

79
pub fn info(pid: Option<i64>) -> Result<BTreeMap<String, Value>, String> {
@@ -26,10 +28,21 @@ pub fn info(pid: Option<i64>) -> Result<BTreeMap<String, Value>, String> {
2628
"exe".to_string(),
2729
Value::String(process.exe().display().to_string()),
2830
);
31+
32+
let mut env_map = BTreeMap::new();
33+
for env_str in process.environ() {
34+
if let Some((key, val)) = env_str.split_once('=') {
35+
env_map.insert(
36+
Value::String(key.to_string()),
37+
Value::String(val.to_string()),
38+
);
39+
}
40+
}
2941
map.insert(
3042
"environ".to_string(),
31-
Value::String(process.environ().join(",")),
43+
Value::Dictionary(Arc::new(RwLock::new(env_map))),
3244
);
45+
3346
map.insert(
3447
"cwd".to_string(),
3548
Value::String(process.cwd().display().to_string()),
@@ -99,6 +112,20 @@ mod tests {
99112
assert!(info.contains_key("cmd"));
100113
assert!(info.contains_key("exe"));
101114
assert!(info.contains_key("environ"));
115+
116+
if let Some(Value::Dictionary(env_dict)) = info.get("environ") {
117+
let env_map = env_dict.read();
118+
// We can't guarantee any specific variable exists across all platforms, but we can check it's a dict.
119+
// On unix/windows PATH or HOME/USERPROFILE usually exists.
120+
// But just checking it is a Dictionary is enough per the request "assert_eq(type(proc['env']), dict)"
121+
// Using >= 0 is always true for usize, effectively we just want to ensure we could access the map.
122+
// So we'll just check that it's a valid map structure which we already did by matching Value::Dictionary.
123+
// Let's print the length just to use the variable and avoid warnings if we don't assert anything.
124+
let _ = env_map.len();
125+
} else {
126+
panic!("environ is not a dictionary");
127+
}
128+
102129
assert!(info.contains_key("cwd"));
103130
assert!(info.contains_key("root"));
104131
assert!(info.contains_key("memory_usage"));

tavern/tomes/process_info/main.eldritch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def process_info(pid):
77
print("\t- {}".format(nested_value))
88
elif key == "environ":
99
print("env_variables=")
10-
for nested_value in value.split(","):
11-
print("\t- {}".format(nested_value))
10+
for k, v in value.items():
11+
print("\t- {}={}".format(k, v))
1212
else:
1313
print("{}={}".format(key, value))
1414

0 commit comments

Comments
 (0)