Skip to content

Commit e9c292d

Browse files
committed
validate segment key from s3
if the key didn't exist, s3 would return the next key for a different namespace. Check that the returned key matches the requested namespace.
1 parent b57a3e9 commit e9c292d

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

libsql-wal/src/storage/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::BTreeMap;
22
use std::fmt;
33
use std::future::Future;
4-
use std::path::PathBuf;
4+
use std::path::{Path, PathBuf};
55
use std::pin::Pin;
66
use std::str::FromStr;
77
use std::sync::Arc;
@@ -85,6 +85,28 @@ impl SegmentKey {
8585
pub(crate) fn includes(&self, frame_no: u64) -> bool {
8686
(self.start_frame_no..=self.end_frame_no).contains(&frame_no)
8787
}
88+
89+
#[tracing::instrument]
90+
fn validate_from_path(mut path: &Path, ns: &NamespaceName) -> Option<Self> {
91+
// path in the form "v2/clusters/{cluster-id}/namespaces/{namespace}/indexes/{index-key}"
92+
let key: Self = path.file_name()?.to_str()?.parse().ok()?;
93+
94+
path = path.parent()?;
95+
96+
if path.file_name()? != "indexes" {
97+
tracing::debug!("invalid key, ignoring");
98+
return None;
99+
}
100+
101+
path = path.parent()?;
102+
103+
if path.file_name()? != ns.as_str() {
104+
tracing::debug!("invalid namespace for key");
105+
return None;
106+
}
107+
108+
Some(key)
109+
}
88110
}
89111

90112
impl From<&SegmentMeta> for SegmentKey {

0 commit comments

Comments
 (0)