diff --git a/drivers/rust/driver/src/download.rs b/drivers/rust/driver/src/download.rs index dc4cd8ee..a9da28e2 100644 --- a/drivers/rust/driver/src/download.rs +++ b/drivers/rust/driver/src/download.rs @@ -18,6 +18,7 @@ use tracing::{debug, info}; use futures_util::StreamExt; +use crate::plugin_manager::validate_manifest_path; use crate::plugin_models::PactPluginManifest; use crate::utils::os_and_arch; use tar::Archive; @@ -225,6 +226,9 @@ fn gunzip_file( manifest: &PactPluginManifest, ext: &str ) -> anyhow::Result { + // entry_point comes from the (untrusted) manifest and is joined onto plugin_dir, so an + // absolute or `..`-containing value would write outside it. Reject those first. + validate_manifest_path("entry_point", &manifest.entry_point)?; let file = if ext.is_empty() { plugin_dir.join(&manifest.entry_point) } else { diff --git a/drivers/rust/driver/src/plugin_manager.rs b/drivers/rust/driver/src/plugin_manager.rs index 3f005823..5236cbee 100644 --- a/drivers/rust/driver/src/plugin_manager.rs +++ b/drivers/rust/driver/src/plugin_manager.rs @@ -1070,7 +1070,33 @@ pub async fn install_plugin_from_url( } } +/// Validate a path value taken from a (potentially untrusted) plugin manifest before it +/// is joined onto the plugin directory. Rejects any value that could escape the plugin +/// directory: `..` components, absolute paths, and Windows drive/UNC prefixes. The value +/// must be a plain relative path made only of normal components. See GH issue #120. +pub(crate) fn validate_manifest_path(label: &str, value: &str) -> anyhow::Result<()> { + use std::path::Component; + if value.trim().is_empty() { + bail!("Plugin manifest '{}' must not be empty", label); + } + for component in std::path::Path::new(value).components() { + if !matches!(component, Component::Normal(_) | Component::CurDir) { + bail!( + "Plugin manifest '{}' value '{}' contains an illegal path component; it must be a \ + plain relative path (no '..', absolute paths, or drive/UNC prefixes)", + label, value + ); + } + } + Ok(()) +} + fn create_plugin_dir(manifest: &PactPluginManifest) -> anyhow::Result { + // The name and version come from the downloaded manifest and are joined onto the + // plugins directory below, so reject anything that would escape it (e.g. `../..`). + validate_manifest_path("name", &manifest.name)?; + validate_manifest_path("version", &manifest.version)?; + let plugins_dir = pact_plugin_dir()?; if !plugins_dir.exists() { info!(plugins_dir = %plugins_dir.display(), "Creating plugins directory"); @@ -1113,7 +1139,35 @@ mod tests { CatalogueEntry, CatalogueEntryProviderType, CatalogueEntryType, register_core_entries, }; - use super::{PactPluginManifest, init_handshake, initialise_plugin, load_manifest_from_dir}; + use super::{PactPluginManifest, init_handshake, initialise_plugin, load_manifest_from_dir, validate_manifest_path}; + + // Regression test for the path-traversal fix (GH issue #120). Both install sinks + // (create_plugin_dir's "{name}-{version}" join and download::gunzip_file's + // entry_point join) route manifest-supplied values through validate_manifest_path, + // so this proves a hostile manifest can no longer escape the plugin directory. + #[test] + fn validate_manifest_path_rejects_traversal() { + // Plain relative paths — what a legitimate manifest uses — are accepted. + expect!(validate_manifest_path("name", "csv")).to(be_ok()); + expect!(validate_manifest_path("version", "0.1.20")).to(be_ok()); + expect!(validate_manifest_path("entry_point", "bin/plugin")).to(be_ok()); + expect!(validate_manifest_path("entry_point", "./pact-plugin")).to(be_ok()); + + // `..` components, absolute paths, and empty values are rejected. + expect!(validate_manifest_path("name", "../evil")).to(be_err()); + expect!(validate_manifest_path("version", "1.0.0/../..")).to(be_err()); + expect!(validate_manifest_path("entry_point", "../../../../tmp/pwned")).to(be_err()); + expect!(validate_manifest_path("entry_point", "/etc/cron.d/pwned")).to(be_err()); + expect!(validate_manifest_path("name", "")).to(be_err()); + expect!(validate_manifest_path("name", " ")).to(be_err()); + + // Windows drive-letter / UNC prefixes (only parsed as path prefixes on Windows). + #[cfg(windows)] + { + expect!(validate_manifest_path("entry_point", "C:\\Windows\\System32\\pwn.exe")).to(be_err()); + expect!(validate_manifest_path("entry_point", "\\\\attacker\\share\\pwn.exe")).to(be_err()); + } + } #[test] fn load_manifest_from_dir_test() {