[edge] Share fixes: revocation epoch floor kept in edge's own state across restarts, every share=ish1. parameter stripped (also encoded)
This commit is contained in:
+12
-11
@@ -178,19 +178,16 @@ fn split_query(query: &str) -> (Option<String>, String) {
|
||||
let mut token = None;
|
||||
let mut rest = Vec::new();
|
||||
for pair in query.split('&') {
|
||||
if token.is_none()
|
||||
&& let Some(v) = pair
|
||||
.strip_prefix(QUERY_PARAM)
|
||||
.and_then(|r| r.strip_prefix('='))
|
||||
// Every `share=ish1.…`, also with the name or value encoded, comes out; the first
|
||||
// one is the token.
|
||||
if let Some((k, v)) = form_urlencoded::parse(pair.as_bytes()).next()
|
||||
&& k == QUERY_PARAM
|
||||
&& v.starts_with(LINK_PREFIX)
|
||||
{
|
||||
let decoded: String = form_urlencoded::parse(format!("x={v}").as_bytes())
|
||||
.next()
|
||||
.map(|(_, v)| v.into_owned())
|
||||
.unwrap_or_default();
|
||||
if decoded.starts_with(LINK_PREFIX) {
|
||||
token = Some(decoded);
|
||||
continue;
|
||||
if token.is_none() {
|
||||
token = Some(v.into_owned());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !pair.is_empty() {
|
||||
rest.push(pair);
|
||||
@@ -458,6 +455,10 @@ mod tests {
|
||||
(Some("ish1.a-b".into()), "x=%20".into())
|
||||
);
|
||||
assert_eq!(split_query(""), (None, String::new()));
|
||||
assert_eq!(
|
||||
split_query("share=ish1.a&b=1&share=ish1.b&sh%61re=ish1.c"),
|
||||
(Some("ish1.a".into()), "b=1".into())
|
||||
);
|
||||
assert_eq!(split_query("sharex=ish1.a"), (None, "sharex=ish1.a".into()));
|
||||
}
|
||||
|
||||
|
||||
@@ -122,6 +122,7 @@ fn same_listeners(a: &Runtime, b: &Runtime) -> Result<(), EdgeError> {
|
||||
|
||||
impl Shared {
|
||||
pub fn new(rt: Runtime, paths: Paths, credentials_dir: Option<PathBuf>) -> Arc<Shared> {
|
||||
let floor = read_epoch_floor(&rt);
|
||||
let certs = Arc::new(CertStore::default());
|
||||
certs.replace_files(rt.file_certs.clone());
|
||||
let (changed, _) = tokio::sync::watch::channel(1);
|
||||
@@ -136,7 +137,7 @@ impl Shared {
|
||||
paths,
|
||||
credentials_dir,
|
||||
revocations: ArcSwap::from_pointee(RevocationState::default()),
|
||||
epoch_seen: std::sync::atomic::AtomicU32::new(0),
|
||||
epoch_seen: std::sync::atomic::AtomicU32::new(floor),
|
||||
throttle: crate::share::Throttle::default(),
|
||||
cert_states: Mutex::new(HashMap::new()),
|
||||
acme_wake: tokio::sync::Notify::new(),
|
||||
@@ -367,7 +368,13 @@ impl Shared {
|
||||
file.epoch
|
||||
)));
|
||||
}
|
||||
self.epoch_seen.store(file.epoch, Ordering::SeqCst);
|
||||
if file.epoch > seen {
|
||||
self.epoch_seen.store(file.epoch, Ordering::SeqCst);
|
||||
if let Err(e) = write_epoch_floor(&self.current(), file.epoch) {
|
||||
// Still refused within this process; only a restart could forget it.
|
||||
log::error!(target: "ianitor::share", "cannot save the revocation epoch floor: {e}");
|
||||
}
|
||||
}
|
||||
let stamp = Some(runtime::sha256_hex(text.as_bytes()));
|
||||
self.revocations.store(Arc::new(RevocationState {
|
||||
revocations: Revocations {
|
||||
@@ -414,6 +421,34 @@ impl Shared {
|
||||
}
|
||||
}
|
||||
|
||||
/// Edge's own record of the highest revocation epoch it has accepted, in its state
|
||||
/// directory: a restart (even after control's files were rolled back) still refuses lower.
|
||||
fn epoch_floor_path(rt: &Runtime) -> PathBuf {
|
||||
Path::new(&rt.cfg.state_dir).join("share-epoch-floor")
|
||||
}
|
||||
|
||||
fn read_epoch_floor(rt: &Runtime) -> u32 {
|
||||
std::fs::read_to_string(epoch_floor_path(rt))
|
||||
.ok()
|
||||
.and_then(|s| s.trim().parse().ok())
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn write_epoch_floor(rt: &Runtime, epoch: u32) -> std::io::Result<()> {
|
||||
let path = epoch_floor_path(rt);
|
||||
if let Some(dir) = path.parent() {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
}
|
||||
let tmp = path.with_extension("tmp");
|
||||
{
|
||||
use std::io::Write;
|
||||
let mut f = std::fs::File::create(&tmp)?;
|
||||
f.write_all(format!("{epoch}\n").as_bytes())?;
|
||||
f.sync_all()?;
|
||||
}
|
||||
std::fs::rename(&tmp, &path)
|
||||
}
|
||||
|
||||
fn poisoned() -> AdminError {
|
||||
AdminError::Conflict("internal state is unavailable".into())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user