summaryrefslogtreecommitdiff
path: root/src/group.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/group.rs')
-rwxr-xr-xsrc/group.rs29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/group.rs b/src/group.rs
index 3f1844b..8ce4a88 100755
--- a/src/group.rs
+++ b/src/group.rs
@@ -5,6 +5,21 @@ use colored::Colorize;
use crate::config;
use crate::install;
+const GROUPS_DIR: &str = "/var/ror/packages/groups";
+
+fn sanitize_group_name(name: &str) -> Result<(), String> {
+ if name.is_empty() {
+ return Err("Group name is empty".to_string());
+ }
+ if name.contains('/') || name.contains('\\') || name.contains("..") || name.contains('\0') {
+ return Err(format!("Invalid group name: '{}' contains path traversal characters", name));
+ }
+ if name.starts_with('-') {
+ return Err(format!("Invalid group name: '{}' starts with dash", name));
+ }
+ Ok(())
+}
+
#[derive(Debug, Deserialize)]
pub struct Group {
#[allow(dead_code)]
@@ -34,7 +49,11 @@ pub struct Group {
#[allow(dead_code)]
pub fn install_group(group_name: &str, cfg: &config::Config) {
- let group_file = format!("/var/ror/packages/groups/{}.yaml", group_name);
+ if let Err(e) = sanitize_group_name(group_name) {
+ eprintln!("{} {}", "[ror]".red().bold(), e);
+ return;
+ }
+ let group_file = format!("{}/{}.yaml", GROUPS_DIR, group_name);
if !Path::new(&group_file).exists() {
eprintln!("{} Group '{}' not found!", "[ror]".red().bold(), group_name);
return;
@@ -76,7 +95,8 @@ pub fn install_group(group_name: &str, cfg: &config::Config) {
}
pub fn load_group(group_name: &str) -> Result<Group, String> {
- let group_file = format!("/var/ror/packages/groups/{}.yaml", group_name);
+ sanitize_group_name(group_name)?;
+ let group_file = format!("{}/{}.yaml", GROUPS_DIR, group_name);
let content = fs::read_to_string(&group_file)
.map_err(|e| format!("Failed to read group file: {}", e))?;
let group: Group = serde_yaml::from_str(&content)
@@ -85,14 +105,13 @@ pub fn load_group(group_name: &str) -> Result<Group, String> {
}
pub fn list_groups() {
- let groups_dir = "/var/ror/packages/groups";
- if !Path::new(groups_dir).exists() {
+ if !Path::new(GROUPS_DIR).exists() {
println!("{} Groups directory not found.", "[ror]".yellow());
return;
}
println!("{} Available package groups:", "[ror]".blue().bold());
- if let Ok(entries) = std::fs::read_dir(groups_dir) {
+ if let Ok(entries) = std::fs::read_dir(GROUPS_DIR) {
let mut groups: Vec<String> = entries.flatten()
.filter_map(|e| e.file_name().to_str().map(|s| s.to_string()))
.filter(|n| n.ends_with(".yaml"))