|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Shared validation helpers for driver-config mounts. |
| 5 | +
|
| 6 | +use std::path::Path; |
| 7 | + |
| 8 | +const RESERVED_MOUNT_TARGETS: &[&str] = &[ |
| 9 | + "/opt/openshell", |
| 10 | + "/etc/openshell", |
| 11 | + "/etc/openshell-tls", |
| 12 | + "/run/netns", |
| 13 | +]; |
| 14 | + |
| 15 | +/// Validate a non-empty driver mount source. |
| 16 | +pub fn validate_mount_source(source: &str, field: &str) -> Result<String, String> { |
| 17 | + let source = source.trim(); |
| 18 | + if source.is_empty() { |
| 19 | + return Err(format!("{field} must not be empty")); |
| 20 | + } |
| 21 | + if source.as_bytes().contains(&0) { |
| 22 | + return Err(format!("{field} must not contain NUL bytes")); |
| 23 | + } |
| 24 | + Ok(source.to_string()) |
| 25 | +} |
| 26 | + |
| 27 | +/// Validate a bind mount source as an absolute host path. |
| 28 | +pub fn validate_absolute_mount_source(source: &str, field: &str) -> Result<String, String> { |
| 29 | + let source = validate_mount_source(source, field)?; |
| 30 | + if !Path::new(&source).is_absolute() { |
| 31 | + return Err(format!("{field} must be an absolute host path")); |
| 32 | + } |
| 33 | + Ok(source) |
| 34 | +} |
| 35 | + |
| 36 | +/// Validate a relative subpath inside a runtime-managed mount source. |
| 37 | +pub fn validate_mount_subpath(subpath: &str) -> Result<String, String> { |
| 38 | + let subpath = subpath.trim(); |
| 39 | + if subpath.is_empty() { |
| 40 | + return Err("mount subpath must not be empty".to_string()); |
| 41 | + } |
| 42 | + if subpath.as_bytes().contains(&0) { |
| 43 | + return Err("mount subpath must not contain NUL bytes".to_string()); |
| 44 | + } |
| 45 | + let path = Path::new(subpath); |
| 46 | + if path.is_absolute() |
| 47 | + || path |
| 48 | + .components() |
| 49 | + .any(|component| matches!(component, std::path::Component::ParentDir)) |
| 50 | + { |
| 51 | + return Err("mount subpath must be relative and must not contain '..'".to_string()); |
| 52 | + } |
| 53 | + Ok(subpath.to_string()) |
| 54 | +} |
| 55 | + |
| 56 | +/// Validate a container-side mount target for user-supplied driver mounts. |
| 57 | +pub fn validate_container_mount_target(target: &str) -> Result<String, String> { |
| 58 | + let target = normalize_container_mount_target(target); |
| 59 | + if target.is_empty() { |
| 60 | + return Err("mount target must not be empty".to_string()); |
| 61 | + } |
| 62 | + if target.as_bytes().contains(&0) { |
| 63 | + return Err("mount target must not contain NUL bytes".to_string()); |
| 64 | + } |
| 65 | + if !target.starts_with('/') { |
| 66 | + return Err("mount target must be an absolute container path".to_string()); |
| 67 | + } |
| 68 | + if target == "/" { |
| 69 | + return Err("mount target must not be the container root".to_string()); |
| 70 | + } |
| 71 | + let path = Path::new(&target); |
| 72 | + if path |
| 73 | + .components() |
| 74 | + .any(|component| matches!(component, std::path::Component::ParentDir)) |
| 75 | + { |
| 76 | + return Err("mount target must not contain '..'".to_string()); |
| 77 | + } |
| 78 | + if target == "/sandbox" { |
| 79 | + return Err("mount target '/sandbox' is reserved for the OpenShell workspace".to_string()); |
| 80 | + } |
| 81 | + for reserved in RESERVED_MOUNT_TARGETS { |
| 82 | + if path_is_or_under(&target, reserved) { |
| 83 | + return Err(format!( |
| 84 | + "mount target '{target}' conflicts with reserved OpenShell path '{reserved}'" |
| 85 | + )); |
| 86 | + } |
| 87 | + } |
| 88 | + Ok(target) |
| 89 | +} |
| 90 | + |
| 91 | +fn normalize_container_mount_target(target: &str) -> String { |
| 92 | + let target = target.trim(); |
| 93 | + if target == "/" { |
| 94 | + return target.to_string(); |
| 95 | + } |
| 96 | + target.trim_end_matches('/').to_string() |
| 97 | +} |
| 98 | + |
| 99 | +fn path_is_or_under(path: &str, parent: &str) -> bool { |
| 100 | + path == parent |
| 101 | + || path |
| 102 | + .strip_prefix(parent) |
| 103 | + .is_some_and(|rest| rest.starts_with('/')) |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use super::*; |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn container_target_allows_paths_under_workspace() { |
| 112 | + assert_eq!( |
| 113 | + validate_container_mount_target("/sandbox/work/").unwrap(), |
| 114 | + "/sandbox/work" |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn container_target_rejects_workspace_root_only() { |
| 120 | + let err = validate_container_mount_target("/sandbox/").unwrap_err(); |
| 121 | + |
| 122 | + assert!(err.contains("reserved for the OpenShell workspace")); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn container_target_rejects_reserved_openshell_tls_legacy_path() { |
| 127 | + let err = validate_container_mount_target("/etc/openshell-tls/client").unwrap_err(); |
| 128 | + |
| 129 | + assert!(err.contains("/etc/openshell-tls")); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn container_target_rejects_reserved_openshell_tree() { |
| 134 | + let err = validate_container_mount_target("/etc/openshell/tls/client").unwrap_err(); |
| 135 | + |
| 136 | + assert!(err.contains("/etc/openshell")); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn container_target_does_not_prefix_match_unrelated_paths() { |
| 141 | + assert_eq!( |
| 142 | + validate_container_mount_target("/etc/openshell-tools").unwrap(), |
| 143 | + "/etc/openshell-tools" |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn mount_subpath_must_be_relative_without_parent_dirs() { |
| 149 | + assert_eq!(validate_mount_subpath(" project/a ").unwrap(), "project/a"); |
| 150 | + assert!(validate_mount_subpath("/project").is_err()); |
| 151 | + assert!(validate_mount_subpath("../project").is_err()); |
| 152 | + } |
| 153 | +} |
0 commit comments