Skip to content

Commit bfa4116

Browse files
committed
Simplify the surrogate key for Fastly
In the original implementation, we simply used the path for invalidations as the surrogate key. This worked fine when using the user interface to invalidate the cache, but broke when trying to use Fastly's API to do the same. While it is possible that we simply needed to URL-encode the key, a safer implementation is to avoid the problem in the first place by restricting the key to alphanumeric characters.
1 parent 5f7edbc commit bfa4116

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/fastly.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ impl Fastly {
1717
}
1818

1919
pub fn purge(&mut self, path: &str) -> Result<(), Error> {
20-
let sanitized_path = path.trim_start_matches('/');
20+
let surrogate_key = path_to_surrogate_key(path);
2121
let url = format!(
2222
"https://api.fastly.com/service/{}/purge/{}",
23-
self.service_id, sanitized_path
23+
self.service_id, surrogate_key
2424
);
2525

2626
self.start_new_request()?;
@@ -43,3 +43,21 @@ impl Fastly {
4343
Ok(())
4444
}
4545
}
46+
47+
fn path_to_surrogate_key(path: &str) -> String {
48+
path.chars().filter(|c| c.is_alphanumeric()).collect()
49+
}
50+
51+
#[cfg(test)]
52+
mod tests {
53+
use super::*;
54+
55+
#[test]
56+
fn path_to_surrogate_key_dist() {
57+
let path = "/dist/*";
58+
59+
let surrogate_key = path_to_surrogate_key(path);
60+
61+
assert_eq!("dist", surrogate_key);
62+
}
63+
}

0 commit comments

Comments
 (0)