Bind approved Windows PowerShell process policy and prove Linux-first contracts
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
+131
-4
@@ -1,5 +1,6 @@
|
||||
//! Version 1 codec: UTF-8 compact serde JSON of these structs, declaration field
|
||||
//! order, explicit null options, ordered vectors, UTC RFC3339 timestamps. Hash the
|
||||
//! order, explicit null legacy options (new opt-in fields omit None), ordered vectors,
|
||||
//! UTC RFC3339 timestamps. Hash the
|
||||
//! stored bytes, never a reconstructed API view. No floating point/map payloads.
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -13,6 +14,33 @@ pub fn encode<T: Serialize>(value: &T) -> Result<Vec<u8>, serde_json::Error> {
|
||||
serde_json::to_vec(value)
|
||||
}
|
||||
|
||||
/// Fixed per-job Windows PowerShell 5.1 option, never a machine/GPO policy.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)]
|
||||
pub enum WindowsExecutionProfile {
|
||||
#[serde(rename = "powershell_51_remote_signed")]
|
||||
PowerShell51RemoteSigned,
|
||||
}
|
||||
|
||||
/// A missing field uses serde's default None. A present field must be the exact
|
||||
/// string: null and serde's externally-tagged enum object form are not profiles.
|
||||
pub fn deserialize_windows_execution_profile<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<WindowsExecutionProfile>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let value = String::deserialize(deserializer)?;
|
||||
match value.as_str() {
|
||||
"powershell_51_remote_signed" => {
|
||||
Ok(Some(WindowsExecutionProfile::PowerShell51RemoteSigned))
|
||||
}
|
||||
_ => Err(serde::de::Error::unknown_variant(
|
||||
&value,
|
||||
&["powershell_51_remote_signed"],
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)]
|
||||
#[serde(tag = "action", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum Action {
|
||||
@@ -21,6 +49,12 @@ pub enum Action {
|
||||
script_utf8: String,
|
||||
arguments: Vec<String>,
|
||||
expected_stdout: Option<String>,
|
||||
#[serde(
|
||||
default,
|
||||
deserialize_with = "deserialize_windows_execution_profile",
|
||||
skip_serializing_if = "Option::is_none"
|
||||
)]
|
||||
windows_execution_profile: Option<WindowsExecutionProfile>,
|
||||
},
|
||||
RefreshInventory {
|
||||
scope: String,
|
||||
@@ -99,8 +133,10 @@ impl Action {
|
||||
script_utf8,
|
||||
arguments,
|
||||
expected_stdout,
|
||||
windows_execution_profile,
|
||||
} => {
|
||||
matches!(interpreter.as_str(), "posix_sh" | "bash" | "powershell")
|
||||
&& (windows_execution_profile.is_none() || interpreter == "powershell")
|
||||
&& (interpreter != "powershell"
|
||||
|| script_utf8.is_ascii()
|
||||
|| script_utf8.starts_with('\u{feff}'))
|
||||
@@ -219,15 +255,25 @@ impl Action {
|
||||
return false;
|
||||
}
|
||||
match self {
|
||||
Self::RunScript { interpreter, .. } if interpreter == "powershell" => {
|
||||
Self::RunScript {
|
||||
interpreter,
|
||||
windows_execution_profile,
|
||||
..
|
||||
} if interpreter == "powershell" => {
|
||||
identity == "agent_service"
|
||||
&& has("windows.base/1")
|
||||
&& !has("linux.base/1")
|
||||
&& has("rmm.sync.v1")
|
||||
&& has("script.powershell/1")
|
||||
&& (windows_execution_profile.is_none()
|
||||
|| has("script.powershell.process_remote_signed/1"))
|
||||
}
|
||||
Self::RunScript { .. } => {
|
||||
self.script_platform() == Some("linux")
|
||||
Self::RunScript {
|
||||
windows_execution_profile,
|
||||
..
|
||||
} => {
|
||||
windows_execution_profile.is_none()
|
||||
&& self.script_platform() == Some("linux")
|
||||
&& has("linux.base/1")
|
||||
&& !has("windows.base/1")
|
||||
}
|
||||
@@ -496,6 +542,7 @@ mod tests {
|
||||
script_utf8: "Write-Output 'hi'\r\n".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
};
|
||||
let mut all = caps;
|
||||
all.extend(["run_script".into(), "script.powershell/1".into()]);
|
||||
@@ -505,6 +552,7 @@ mod tests {
|
||||
script_utf8: "printf hi\n".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
};
|
||||
let reboot = Action::Reboot {
|
||||
reason: "fixed test".into(),
|
||||
@@ -526,6 +574,7 @@ mod tests {
|
||||
script_utf8: "Write-Output 'Hello'\r\n".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: Some("Hello\r\n".into()),
|
||||
windows_execution_profile: None,
|
||||
};
|
||||
let caps: Vec<String> = [
|
||||
"windows.base/1",
|
||||
@@ -601,6 +650,7 @@ mod tests {
|
||||
script_utf8: "echo hi\n".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
};
|
||||
assert_eq!(
|
||||
String::from_utf8(encode(&a).unwrap()).unwrap(),
|
||||
@@ -613,6 +663,83 @@ mod tests {
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn windows_remote_signed_profile_is_optional_exact_and_capability_bound() {
|
||||
let legacy = br#"{"action":"run_script","interpreter":"powershell","script_utf8":"Write-Output 'hi'\r\n","arguments":[],"expected_stdout":null}"#;
|
||||
let mut action: Action = serde_json::from_slice(legacy).unwrap();
|
||||
assert_eq!(encode(&action).unwrap(), legacy);
|
||||
let inherited_hash = hash(legacy);
|
||||
if let Action::RunScript {
|
||||
windows_execution_profile,
|
||||
..
|
||||
} = &mut action
|
||||
{
|
||||
*windows_execution_profile = Some(WindowsExecutionProfile::PowerShell51RemoteSigned);
|
||||
}
|
||||
let encoded = encode(&action).unwrap();
|
||||
assert_eq!(
|
||||
String::from_utf8(encoded.clone()).unwrap(),
|
||||
String::from_utf8(legacy[..legacy.len() - 1].to_vec()).unwrap()
|
||||
+ r#","windows_execution_profile":"powershell_51_remote_signed"}"#
|
||||
);
|
||||
assert_ne!(hash(&encoded), inherited_hash);
|
||||
assert_eq!(serde_json::from_slice::<Action>(&encoded).unwrap(), action);
|
||||
let caps: Vec<String> = [
|
||||
"windows.base/1",
|
||||
"rmm.sync.v1",
|
||||
"run_script",
|
||||
"script.powershell/1",
|
||||
"script.powershell.process_remote_signed/1",
|
||||
]
|
||||
.into_iter()
|
||||
.map(str::to_string)
|
||||
.collect();
|
||||
assert!(action.validate());
|
||||
assert!(action.available_for("agent_service", &caps));
|
||||
for missing in 0..caps.len() {
|
||||
let mut short = caps.clone();
|
||||
short.remove(missing);
|
||||
assert!(!action.available_for("agent_service", &short));
|
||||
}
|
||||
for identity in ["root", "system", "SYSTEM"] {
|
||||
assert!(!action.available_for(identity, &caps));
|
||||
}
|
||||
let mut mixed = caps.clone();
|
||||
mixed.push("linux.base/1".into());
|
||||
assert!(!action.available_for("agent_service", &mixed));
|
||||
for interpreter in ["posix_sh", "bash", "pwsh"] {
|
||||
let mut invalid = action.clone();
|
||||
if let Action::RunScript { interpreter: i, .. } = &mut invalid {
|
||||
*i = interpreter.into();
|
||||
}
|
||||
assert!(!invalid.validate());
|
||||
assert!(!invalid.available_for("agent_service", &caps));
|
||||
assert!(!invalid.available_for("root", &["linux.base/1".into(), "run_script".into()]));
|
||||
}
|
||||
for profile in [
|
||||
serde_json::Value::Null,
|
||||
serde_json::json!(false),
|
||||
serde_json::json!(1),
|
||||
serde_json::json!([]),
|
||||
serde_json::json!({}),
|
||||
serde_json::json!({"powershell_51_remote_signed": null}),
|
||||
serde_json::json!("bypass"),
|
||||
serde_json::json!("RemoteSigned"),
|
||||
serde_json::json!("powershell_7_remote_signed"),
|
||||
] {
|
||||
let mut bad = serde_json::to_value(&action).unwrap();
|
||||
bad["windows_execution_profile"] = profile.clone();
|
||||
assert!(serde_json::from_value::<Action>(bad).is_err(), "{profile}");
|
||||
// An opt-in profile is also never metadata on another action.
|
||||
assert!(
|
||||
serde_json::from_value::<Action>(serde_json::json!({
|
||||
"action":"refresh_inventory", "scope":"windows.base",
|
||||
"windows_execution_profile":profile
|
||||
}))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn no_terminal_replay_or_unknown_restart() {
|
||||
assert!(!JobState::Succeeded.allows(JobState::Running));
|
||||
assert!(!JobState::Unknown.allows(JobState::Running));
|
||||
|
||||
Reference in New Issue
Block a user