From b01ecb612b2aa37e84b79a0340d23c1d165bd655 Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Wed, 22 Nov 2023 08:21:46 -0500 Subject: [PATCH] Base64 encode the scope value bkt permits setting a cache scope (provided via the `BKT_SCOPE` environment variable or the `--scope` command line option) to prevent collisions between unrelated command invocations. Previously, the scope value was used verbatim in a directory path, causing errors if the scope contained path-sensitive characters such as `/`. This commit modifies bkt to base64 encode the scope value. Given a scope value like `https://example.com/`, that means we now end up with a path like: /tmp/bkt-0.7-cache/keys/aHR0cHM6Ly9leGFtcGxlLmNvbS8.C3823B193F1E0C78 Instead of the invalid: /tmp/bkt-0.7-cache/keys/https://www.example.com/.C3823B193F1E0C78 Fixes #50 --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/lib.rs | 5 ++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 501bae7..6f9ca70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,12 @@ version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27" +[[package]] +name = "base64" +version = "0.21.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" + [[package]] name = "bincode" version = "1.3.3" @@ -37,6 +43,7 @@ name = "bkt" version = "0.7.1" dependencies = [ "anyhow", + "base64", "bincode", "clap", "filetime", diff --git a/Cargo.toml b/Cargo.toml index f74eadb..d929d8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ bincode = "1.3.1" humantime = "2.1.0" rand = "0.8" serde = { version = "1.0", features = ["derive"] } +base64 = "0.21.5" [dependencies.clap] version = "4.2" diff --git a/src/lib.rs b/src/lib.rs index b7fc504..a00dfa0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,9 @@ use anyhow::{anyhow, Context, Error, Result}; use serde::{Serialize, Deserialize}; use serde::de::DeserializeOwned; +use base64::{Engine as _, engine::general_purpose}; + + #[cfg(feature="debug")] macro_rules! debug_msg { ($($arg:tt)*) => { eprintln!("bkt: {}", format!($($arg)*)) } @@ -627,7 +630,7 @@ impl Cache { fn scoped(mut self, scope: String) -> Self { assert!(self.scope.is_none()); - self.scope = Some(scope); + self.scope = Some(general_purpose::STANDARD_NO_PAD.encode(scope)); self }