AstrBot-Desktop/src-tauri/src/diagnostics/config.rs

368 行
12 KiB
Rust

use std::{fmt, io, sync::OnceLock};
use quick_xml::{Reader, events::Event};
use regex::{Captures, Regex};
use serde_json::Value as JsonValue;
use super::{
authorization::AuthorizedFile,
redaction::{REDACTED, Redactor, is_sensitive_key},
};
pub const MAX_CONFIG_BYTES: usize = 1024 * 1024;
const MAX_DEPTH: usize = 64;
const MAX_NODES: usize = 10_000;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConfigFormat {
Json,
Toml,
Yaml,
Ini,
Xml,
DotEnv,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParsedConfig {
pub display_name: String,
pub format: ConfigFormat,
pub redacted_text: String,
}
#[derive(Debug)]
pub enum ConfigError {
AuthorizationExpired,
TooLarge,
InvalidUtf8,
UnsupportedConstruct(&'static str),
InvalidSyntax(&'static str),
Io(io::Error),
}
impl fmt::Display for ConfigError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AuthorizationExpired => {
formatter.write_str("the local file authorization has expired")
}
Self::TooLarge => formatter.write_str("the selected config exceeds the read limit"),
Self::InvalidUtf8 => formatter.write_str("the selected config is not UTF-8 text"),
Self::UnsupportedConstruct(kind) => {
write!(formatter, "the selected config contains unsupported {kind}")
}
Self::InvalidSyntax(format) => write!(formatter, "the selected {format} is invalid"),
Self::Io(error) => write!(formatter, "config read failed: {}", error.kind()),
}
}
}
impl std::error::Error for ConfigError {}
pub fn parse_authorized_config(
authorized: &AuthorizedFile,
format: ConfigFormat,
redactor: &Redactor,
) -> Result<ParsedConfig, ConfigError> {
authorized
.ensure_active()
.map_err(|_| ConfigError::AuthorizationExpired)?;
let bytes = authorized.read_bounded(MAX_CONFIG_BYTES).map_err(|error| {
if error.kind() == io::ErrorKind::InvalidData {
ConfigError::TooLarge
} else {
ConfigError::Io(error)
}
})?;
let text = std::str::from_utf8(&bytes).map_err(|_| ConfigError::InvalidUtf8)?;
let normalized = match format {
ConfigFormat::Json => parse_json(text, redactor)?,
ConfigFormat::Toml => parse_toml(text, redactor)?,
ConfigFormat::Yaml => validate_yaml(text)?,
ConfigFormat::Ini => validate_ini(text)?,
ConfigFormat::Xml => validate_xml(text)?,
ConfigFormat::DotEnv => validate_dotenv(text)?,
};
let redacted_text = redact_xml_elements(&redactor.redact(&normalized));
Ok(ParsedConfig {
display_name: authorized.display_name().to_owned(),
format,
redacted_text,
})
}
fn parse_json(text: &str, redactor: &Redactor) -> Result<String, ConfigError> {
let mut value =
serde_json::from_str::<JsonValue>(text).map_err(|_| ConfigError::InvalidSyntax("JSON"))?;
let mut nodes = 0;
redact_json_value(&mut value, redactor, 0, &mut nodes)?;
serde_json::to_string_pretty(&value).map_err(|_| ConfigError::InvalidSyntax("JSON"))
}
fn redact_json_value(
value: &mut JsonValue,
redactor: &Redactor,
depth: usize,
nodes: &mut usize,
) -> Result<(), ConfigError> {
enforce_limits(depth, nodes)?;
match value {
JsonValue::Object(map) => {
for (key, child) in map {
if is_sensitive_key(key) {
*child = JsonValue::String(REDACTED.to_owned());
} else {
redact_json_value(child, redactor, depth + 1, nodes)?;
}
}
}
JsonValue::Array(values) => {
for child in values {
redact_json_value(child, redactor, depth + 1, nodes)?;
}
}
JsonValue::String(value) => *value = redactor.redact(value),
_ => {}
}
Ok(())
}
fn parse_toml(text: &str, redactor: &Redactor) -> Result<String, ConfigError> {
let mut value = text
.parse::<toml::Value>()
.map_err(|_| ConfigError::InvalidSyntax("TOML"))?;
let mut nodes = 0;
redact_toml_value(&mut value, redactor, 0, &mut nodes)?;
toml::to_string_pretty(&value).map_err(|_| ConfigError::InvalidSyntax("TOML"))
}
fn redact_toml_value(
value: &mut toml::Value,
redactor: &Redactor,
depth: usize,
nodes: &mut usize,
) -> Result<(), ConfigError> {
enforce_limits(depth, nodes)?;
match value {
toml::Value::Table(table) => {
for (key, child) in table {
if is_sensitive_key(key) {
*child = toml::Value::String(REDACTED.to_owned());
} else {
redact_toml_value(child, redactor, depth + 1, nodes)?;
}
}
}
toml::Value::Array(values) => {
for child in values {
redact_toml_value(child, redactor, depth + 1, nodes)?;
}
}
toml::Value::String(value) => *value = redactor.redact(value),
_ => {}
}
Ok(())
}
fn enforce_limits(depth: usize, nodes: &mut usize) -> Result<(), ConfigError> {
if depth > MAX_DEPTH {
return Err(ConfigError::UnsupportedConstruct("nesting depth"));
}
*nodes += 1;
if *nodes > MAX_NODES {
return Err(ConfigError::UnsupportedConstruct("node count"));
}
Ok(())
}
fn validate_yaml(text: &str) -> Result<String, ConfigError> {
let mut nodes = 0_usize;
for line in text.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') || matches!(trimmed, "---" | "...") {
continue;
}
if trimmed.contains("<<:")
|| trimmed.starts_with('!')
|| trimmed.starts_with('&')
|| trimmed.starts_with('*')
|| trimmed.contains(": &")
|| trimmed.contains(": *")
{
return Err(ConfigError::UnsupportedConstruct(
"YAML tag, anchor, or alias",
));
}
let indentation = line
.chars()
.take_while(|character| character.is_whitespace())
.count();
if indentation > MAX_DEPTH * 2 {
return Err(ConfigError::UnsupportedConstruct("YAML nesting depth"));
}
if !trimmed.starts_with('-') && !trimmed.contains(':') {
return Err(ConfigError::InvalidSyntax("YAML"));
}
nodes += 1;
if nodes > MAX_NODES {
return Err(ConfigError::UnsupportedConstruct("YAML node count"));
}
}
Ok(text.to_owned())
}
fn validate_ini(text: &str) -> Result<String, ConfigError> {
let mut nodes = 0_usize;
for line in text.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with(';') {
continue;
}
let is_section = trimmed.starts_with('[') && trimmed.ends_with(']');
if !is_section && !trimmed.contains('=') {
return Err(ConfigError::InvalidSyntax("INI"));
}
nodes += 1;
if nodes > MAX_NODES {
return Err(ConfigError::UnsupportedConstruct("INI entry count"));
}
}
Ok(text.to_owned())
}
fn validate_dotenv(text: &str) -> Result<String, ConfigError> {
let mut entries = 0_usize;
for line in text.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
let assignment = trimmed.strip_prefix("export ").unwrap_or(trimmed);
let (key, _) = assignment
.split_once('=')
.ok_or(ConfigError::InvalidSyntax(".env"))?;
if key.is_empty()
|| !key.chars().enumerate().all(|(index, value)| {
value == '_'
|| (value.is_ascii_alphanumeric() && (index > 0 || !value.is_ascii_digit()))
})
{
return Err(ConfigError::InvalidSyntax(".env"));
}
entries += 1;
if entries > MAX_NODES {
return Err(ConfigError::UnsupportedConstruct(".env entry count"));
}
}
Ok(text.to_owned())
}
fn validate_xml(text: &str) -> Result<String, ConfigError> {
let lowercase = text.to_ascii_lowercase();
if lowercase.contains("<!doctype") || lowercase.contains("<!entity") {
return Err(ConfigError::UnsupportedConstruct("XML DTD or entity"));
}
let mut reader = Reader::from_str(text);
reader.config_mut().trim_text(false);
let mut buffer = Vec::new();
let mut depth = 0_usize;
let mut nodes = 0_usize;
loop {
match reader.read_event_into(&mut buffer) {
Ok(Event::Start(_)) => {
depth += 1;
nodes += 1;
if depth > MAX_DEPTH || nodes > MAX_NODES {
return Err(ConfigError::UnsupportedConstruct("XML depth or node count"));
}
}
Ok(Event::End(_)) => depth = depth.saturating_sub(1),
Ok(Event::Empty(_)) => {
nodes += 1;
if nodes > MAX_NODES {
return Err(ConfigError::UnsupportedConstruct("XML node count"));
}
}
Ok(Event::DocType(_)) => return Err(ConfigError::UnsupportedConstruct("XML DTD")),
Ok(Event::Eof) => break,
Ok(_) => {}
Err(_) => return Err(ConfigError::InvalidSyntax("XML")),
}
buffer.clear();
}
Ok(text.to_owned())
}
fn redact_xml_elements(text: &str) -> String {
static XML_SECRET: OnceLock<Regex> = OnceLock::new();
let pattern = XML_SECRET.get_or_init(|| {
Regex::new(
r"(?is)(?P<open><(?:access[_-]?token|refresh[_-]?token|api[_-]?key|client[_-]?secret|private[_-]?key|password|passwd|secret|cookie|authorization)(?:\s[^>]*)?>).*?(?P<close></(?:access[_-]?token|refresh[_-]?token|api[_-]?key|client[_-]?secret|private[_-]?key|password|passwd|secret|cookie|authorization)\s*>)",
)
.expect("the XML redaction regex is valid")
});
pattern
.replace_all(text, |captures: &Captures<'_>| {
format!("{}{}{}", &captures["open"], REDACTED, &captures["close"])
})
.into_owned()
}
#[cfg(test)]
mod tests {
use std::{fs, time::Duration};
use super::*;
fn parse_fixture(contents: &str, format: ConfigFormat) -> Result<ParsedConfig, ConfigError> {
let path = std::env::temp_dir().join(format!("astrbot-config-{}", uuid::Uuid::new_v4()));
fs::write(&path, contents).expect("write config fixture");
let result = {
let authorized = AuthorizedFile::open_local_selection(&path, Duration::from_secs(5))
.expect("authorize config fixture");
parse_authorized_config(&authorized, format, &Redactor::default())
};
fs::remove_file(path).expect("remove config fixture");
result
}
#[test]
fn recursively_redacts_json_fields() {
let parsed = parse_fixture(
r#"{"database":{"password":"canary-value"},"safe":"visible"}"#,
ConfigFormat::Json,
)
.expect("parse JSON");
assert!(!parsed.redacted_text.contains("canary-value"));
assert!(parsed.redacted_text.contains("visible"));
}
#[test]
fn rejects_yaml_aliases_and_xml_entities() {
assert!(matches!(
parse_fixture("defaults: &defaults\n token: value", ConfigFormat::Yaml),
Err(ConfigError::UnsupportedConstruct(_))
));
assert!(matches!(
parse_fixture(
"<!DOCTYPE x [<!ENTITY y SYSTEM 'file:///x'>]><x>&y;</x>",
ConfigFormat::Xml
),
Err(ConfigError::UnsupportedConstruct(_))
));
}
#[test]
fn redacts_xml_sensitive_elements() {
let parsed = parse_fixture(
"<config><password>canary-value</password><name>visible</name></config>",
ConfigFormat::Xml,
)
.expect("parse XML");
assert!(!parsed.redacted_text.contains("canary-value"));
assert!(parsed.redacted_text.contains("visible"));
}
}