fix: use write transaction in SpkiHashStore.cleanup()

query_map_vec() uses read-only connection,
so it cannot be used to delete rows.
This commit is contained in:
link2xt
2026-04-18 01:36:14 +02:00
committed by l
parent bb816ff398
commit 8cd06bb785

View File

@@ -6,7 +6,6 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use anyhow::Context as _;
use anyhow::Result; use anyhow::Result;
use base64::Engine as _; use base64::Engine as _;
use parking_lot::RwLock; use parking_lot::RwLock;
@@ -97,25 +96,28 @@ impl SpkiHashStore {
pub async fn cleanup(&self, sql: &Sql) -> Result<()> { pub async fn cleanup(&self, sql: &Sql) -> Result<()> {
let now = time(); let now = time();
let removed_hosts = sql let removed_hosts = sql
.query_map_vec( .transaction(|transaction| {
"DELETE FROM tls_spki WHERE ? > timestamp + ? RETURNING host", let mut stmt = transaction
(now, 30 * 24 * 60 * 60), .prepare("DELETE FROM tls_spki WHERE ? > timestamp + ? RETURNING host")?;
|row| { let mut res = Vec::new();
for row in stmt.query_map((now, 30 * 24 * 60 * 60), |row| {
let host: String = row.get(0)?; let host: String = row.get(0)?;
Ok(host) Ok(host)
}, })? {
) res.push(row?);
.await }
.context("DELETE FROM tls_spki")?;
// Fix timestamps that happen to be in the future // Fix timestamps that happen to be in the future
// if we had clock set incorrectly when the timestamp was stored. // if we had clock set incorrectly when the timestamp was stored.
// Otherwise entry may take more than 30 days to expire. // Otherwise entry may take more than 30 days to expire.
sql.execute( transaction.execute(
"UPDATE tls_spki SET timestamp = ?1 WHERE timestamp > ?1", "UPDATE tls_spki SET timestamp = ?1 WHERE timestamp > ?1",
(now,), (now,),
) )?;
.await?;
Ok(res)
})
.await?;
let mut lock = self.hash_store.write(); let mut lock = self.hash_store.write();
for host in removed_hosts { for host in removed_hosts {