-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathparse_env_file.rs
More file actions
116 lines (97 loc) · 3.29 KB
/
Copy pathparse_env_file.rs
File metadata and controls
116 lines (97 loc) · 3.29 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// SPDX-FileCopyrightText: © 2024-2025 Phala Network <dstack@phala.network>
//
// SPDX-License-Identifier: Apache-2.0
use anyhow::{bail, Context, Result};
use serde::Deserialize;
use std::collections::{BTreeMap, BTreeSet};
use tracing::warn;
fn escape_value(v: &str) -> String {
let mut needs_quotes = false;
let mut escaped = String::with_capacity(v.len());
// Check if we need quotes (spaces or special chars)
if v.chars().any(|c| " \t|&;<>()$`\\\"'\n".contains(c)) {
needs_quotes = true;
}
// Escape special characters
for c in v.chars() {
match c {
'\n' => escaped.push_str("\\n"),
'"' => escaped.push_str("\\\""),
'$' => escaped.push_str("\\$"),
'`' => escaped.push_str("\\`"),
_ => escaped.push(c),
}
}
// Wrap in quotes if needed
if needs_quotes {
format!("\"{}\"", escaped)
} else {
escaped
}
}
#[derive(Debug, Clone, Deserialize)]
struct Pair {
key: String,
value: String,
}
#[derive(Debug, Clone, Deserialize)]
struct Data {
env: Vec<Pair>,
}
pub fn parse_env(env_json: &[u8], allowed: &BTreeSet<String>) -> Result<BTreeMap<String, String>> {
const MAX_ITEMS: usize = 1024;
const MAX_TOTAL_SIZE: usize = 1024 * 1024;
let data: Data = serde_json::from_slice(env_json).context("Failed to parse env")?;
if data.env.len() > MAX_ITEMS {
bail!("Too many environment variables: {}", data.env.len());
}
const KEY_REGEX: &str = r"^[a-zA-Z_][a-zA-Z0-9_]*$";
let key_regex = regex::Regex::new(KEY_REGEX)
.context("Failed to compile environment key validation regex")?;
let mut env = BTreeMap::new();
let mut total_size = 0;
for Pair { key, value } in data.env {
if !allowed.contains(&key) {
warn!("Skipping unauthorized environment variable: {key}");
continue;
}
// Check key length (common Linux limit is 255)
if key.len() > 255 {
bail!("Environment variable name too long: {}", key);
}
// Check value length (common Linux limit is around 128KB)
if value.len() > 128 * 1024 {
bail!("Environment variable value too long for key: {}", key);
}
// validate key
if !key_regex.is_match(&key) {
bail!("Invalid env key: {}", key);
}
total_size += key.len() + value.len();
if total_size > MAX_TOTAL_SIZE {
bail!("Environment variables total size too large");
}
env.insert(key, value);
}
Ok(env)
}
pub fn convert_env_to_str(parsed_env: &BTreeMap<String, String>) -> String {
#[allow(clippy::format_collect)]
parsed_env
.iter()
.map(|(key, value)| format!("{}={}\n", key, escape_value(value)))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_escape_value() {
assert_eq!(escape_value("simple"), "simple");
assert_eq!(escape_value("hello world"), "\"hello world\"");
assert_eq!(escape_value("say \"hello\""), "\"say \\\"hello\\\"\"");
assert_eq!(escape_value("line1\nline2"), "\"line1\\nline2\"");
assert_eq!(escape_value("price=$100"), "\"price=\\$100\"");
assert_eq!(escape_value("command=`date`"), "\"command=\\`date\\`\"");
}
}