Skip to content

Commit

Permalink
Base64 encode the scope value (#51)
Browse files Browse the repository at this point in the history
* Add test for scope with path separators

This commit adds a test that exercises the issue described in #50.

* 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

* Tweak test name

---------

Co-authored-by: Michael Diamond <[email protected]>
  • Loading branch information
larsks and dimo414 authored Jan 12, 2024
1 parent 84c483a commit 29ae490
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 22 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*)) }
Expand Down Expand Up @@ -661,7 +664,7 @@ impl Cache {

fn key_path(&self, key: &str) -> PathBuf {
let file = match &self.scope {
Some(scope) => format!("{}.{}", scope, key),
Some(scope) => format!("{}.{}", general_purpose::STANDARD_NO_PAD.encode(scope), key),
None => key.into(),
};
self.key_dir().join(file)
Expand Down Expand Up @@ -931,6 +934,24 @@ mod cache_tests {
assert_eq!(present_scoped.unwrap().0, val_b);
}

#[test]
fn scopes_support_special_chars() {
let dir = TestDir::temp();
let key = "foo".to_string();
let val_a = "A".to_string();
let val_b = "B".to_string();
let cache = Cache::new(dir.root());
let cache_scoped = Cache::new(dir.root()).scoped("/scope/with/path/separators".into());

cache.store(&key, &val_a, Duration::from_secs(100)).unwrap();
cache_scoped.store(&key, &val_b, Duration::from_secs(100)).unwrap();

let present = cache.lookup::<_, String>(&key, Duration::from_secs(20)).unwrap();
assert_eq!(present.unwrap().0, val_a);
let present_scoped = cache_scoped.lookup::<_, String>(&key, Duration::from_secs(20)).unwrap();
assert_eq!(present_scoped.unwrap().0, val_b);
}

#[test]
fn cleanup() {
let dir = TestDir::temp();
Expand Down

0 comments on commit 29ae490

Please sign in to comment.