-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpayload.rs
More file actions
71 lines (60 loc) · 2.32 KB
/
payload.rs
File metadata and controls
71 lines (60 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::os::unix::ffi::OsStringExt;
use base64::{Engine as _, prelude::BASE64_STANDARD_NO_PAD};
use bincode::{Decode, Encode, config::standard};
use bstr::BString;
#[cfg(not(target_env = "musl"))]
use fspy_shared::ipc::NativeStr;
use fspy_shared::ipc::channel::ChannelConf;
#[derive(Debug, Encode, Decode)]
pub struct Payload {
pub ipc_channel_conf: ChannelConf,
#[cfg(not(target_env = "musl"))]
pub preload_path: Box<NativeStr>,
#[cfg(target_os = "macos")]
pub artifacts: Artifacts,
#[cfg(target_os = "linux")]
#[expect(clippy::struct_field_names, reason = "descriptive field name for clarity")]
pub seccomp_payload: fspy_seccomp_unotify::payload::SeccompPayload,
}
#[cfg(target_os = "macos")]
#[derive(Debug, Encode, Decode, Clone)]
pub struct Artifacts {
pub bash_path: Box<NativeStr>,
pub coreutils_path: Box<NativeStr>,
}
pub(crate) const PAYLOAD_ENV_NAME: &str = "FSPY_PAYLOAD";
pub struct EncodedPayload {
pub payload: Payload,
pub encoded_string: BString,
}
/// Encodes the fspy payload into a base64 string for transmission via environment variable
///
/// # Panics
///
/// Panics if bincode serialization fails, which should never happen for valid `Payload` structs.
#[must_use]
pub fn encode_payload(payload: Payload) -> EncodedPayload {
let bincode_bytes = bincode::encode_to_vec(&payload, standard()).unwrap();
let encoded_string = BASE64_STANDARD_NO_PAD.encode(&bincode_bytes);
EncodedPayload { payload, encoded_string: encoded_string.into() }
}
/// Decodes the fspy payload from the environment variable
///
/// # Errors
///
/// Returns an error if:
/// - The environment variable is not found
/// - The base64 decoding fails
/// - The bincode deserialization fails
pub fn decode_payload_from_env() -> anyhow::Result<EncodedPayload> {
let Some(encoded_string) = std::env::var_os(PAYLOAD_ENV_NAME) else {
anyhow::bail!("Environment variable '{PAYLOAD_ENV_NAME}' not found");
};
decode_payload(encoded_string.into_vec().into())
}
fn decode_payload(encoded_string: BString) -> anyhow::Result<EncodedPayload> {
let bincode_bytes = BASE64_STANDARD_NO_PAD.decode(&encoded_string)?;
let (payload, n) = bincode::decode_from_slice::<Payload, _>(&bincode_bytes, standard())?;
assert_eq!(bincode_bytes.len(), n);
Ok(EncodedPayload { payload, encoded_string })
}