WS-D: harden file modes, token names; more auth tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
1 parent
b6c814a18f
commit
e25f0e83bf
3 files changed
+121
-4
No files matched your search
@@ -26,9 +26,31 @@ pub async fn open(config: &Config) -> Result<SqlitePool> {
|
||||
.await
|
||||
.with_context(|| format!("opening token database {}", path.display()))?;
|
||||
migrate(&pool).await?;
|
||||
restrict_files(path);
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
/// Make the DB and its WAL/SHM sidecars owner-only (0600).
|
||||
fn restrict_files(db: &Path) {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
for suffix in ["", "-wal", "-shm"] {
|
||||
let mut name = db.as_os_str().to_owned();
|
||||
name.push(suffix);
|
||||
let p = std::path::PathBuf::from(name);
|
||||
if p.exists() {
|
||||
if let Err(e) = std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o600))
|
||||
{
|
||||
tracing::warn!(path = %p.display(), error = %e, "could not chmod 0600");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
let _ = db;
|
||||
}
|
||||
|
||||
/// Run the embedded migrations.
|
||||
pub async fn migrate(pool: &SqlitePool) -> Result<()> {
|
||||
sqlx::migrate!("./migrations")
|
||||
@@ -39,6 +61,19 @@ pub async fn migrate(pool: &SqlitePool) -> Result<()> {
|
||||
|
||||
fn create_private_dir(dir: &Path) -> Result<()> {
|
||||
if dir.exists() {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mode = std::fs::metadata(dir)?.permissions().mode();
|
||||
if mode & 0o077 != 0 {
|
||||
if let Err(e) =
|
||||
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))
|
||||
{
|
||||
tracing::warn!(dir = %dir.display(), mode = format!("{:o}", mode & 0o777), error = %e,
|
||||
"state directory is accessible to others and could not be tightened");
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let mut b = std::fs::DirBuilder::new();
|
||||
@@ -58,8 +93,13 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn creates_dir_0700_and_migrates() {
|
||||
let tmp = std::env::temp_dir().join(format!("slinuxm-dbtest-{}", std::process::id()));
|
||||
std::fs::remove_dir_all(&tmp).ok();
|
||||
use rand::TryRng;
|
||||
let mut r = [0u8; 8];
|
||||
rand::rngs::SysRng.try_fill_bytes(&mut r).unwrap();
|
||||
let tmp = std::env::temp_dir().join(format!(
|
||||
"slinuxm-dbtest-{}",
|
||||
r.iter().map(|b| format!("{b:02x}")).collect::<String>()
|
||||
));
|
||||
let mut c = Config::default();
|
||||
c.auth.db_path = tmp.join("state/db.sqlite");
|
||||
let pool = open(&c).await.unwrap();
|
||||
@@ -73,8 +113,27 @@ mod tests {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let m = std::fs::metadata(tmp.join("state")).unwrap();
|
||||
assert_eq!(m.permissions().mode() & 0o777, 0o700);
|
||||
for f in ["db.sqlite", "db.sqlite-wal", "db.sqlite-shm"] {
|
||||
let p = tmp.join("state").join(f);
|
||||
if p.exists() {
|
||||
let m = std::fs::metadata(&p).unwrap().permissions().mode();
|
||||
assert_eq!(m & 0o777, 0o600, "{f}");
|
||||
}
|
||||
}
|
||||
assert!(tmp.join("state/db.sqlite").exists());
|
||||
}
|
||||
// reopening an existing DB works
|
||||
// a looser existing dir is tightened on reopen
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let d = tmp.join("state");
|
||||
std::fs::set_permissions(&d, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
drop(pool);
|
||||
open(&c).await.unwrap();
|
||||
let m = std::fs::metadata(&d).unwrap().permissions().mode();
|
||||
assert_eq!(m & 0o777, 0o700);
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
drop(pool);
|
||||
open(&c).await.unwrap();
|
||||
std::fs::remove_dir_all(tmp).ok();
|
||||
|
||||
Reference in new issue
Block a user