feat(hub): email one-time-code sign-in

POST /v1/auth/email/start and /verify with HMAC-protected 8-digit codes
(10 min, 5 tries, atomic single use), browser-bound via a __Host- cookie,
same-origin checks, per-address/source/global rate limits and an injectable
sender. Enabled in /v1/info only when SMTP is configured. Email identities
use external_identities(provider='email') and never merge with GitHub.
This commit is contained in:
Loki 2026-10-24 17:42:49 +02:00
parent f1883e5001
commit cca084b0ae
Signed by: boba
GPG key ID: 253067914055423B
20 changed files with 1756 additions and 59 deletions

View file

@ -72,6 +72,37 @@ impl Issuer {
}
}
/// Loads a 32-byte server secret stored as hex in a 0600 file, creating it from the OS
/// CSPRNG on first use. The value is never logged and has no Debug/Serialize wrapper.
pub fn load_or_create_secret(directory: &Path, name: &str) -> Result<[u8; 32]> {
check_private_directory(directory)?;
let path = directory.join(name);
for _ in 0..2 {
match OpenOptions::new().read(true).open(&path) {
Ok(file) => {
check_private_permissions(&file)?;
if file.metadata()?.len() > 256 {
bail!("Hub secret file exceeds the expected size");
}
let text = std::io::read_to_string(file).context("reading Hub secret file")?;
return hex::decode(text.trim())
.ok()
.and_then(|bytes| <[u8; 32]>::try_from(bytes).ok())
.context("Hub secret file has invalid encoding or length");
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
use rand::Rng;
let mut bytes = [0u8; 32];
rand::rng().fill_bytes(&mut bytes);
// A concurrent creator winning the race is fine: the loop re-reads its file.
let _ = persist_new_file(&path, hex::encode(bytes).as_bytes());
}
Err(error) => return Err(error).context("opening Hub secret file"),
}
}
bail!("could not create Hub secret file")
}
fn check_private_permissions(file: &std::fs::File) -> Result<()> {
#[cfg(unix)]
{
@ -97,6 +128,10 @@ fn check_private_directory(directory: &Path) -> Result<()> {
// Publish a fully synced key file without replacing any concurrent issuer.
// A failed first write must never leave a truncated final identity behind.
fn persist_new_identity(path: &Path, identity: &StoredIdentity) -> Result<()> {
persist_new_file(path, &serde_json::to_vec_pretty(identity)?)
}
fn persist_new_file(path: &Path, contents: &[u8]) -> Result<()> {
let temporary = path.with_extension(format!("{}.tmp", Uuid::new_v4()));
let result = (|| -> Result<()> {
let mut options = OpenOptions::new();
@ -110,7 +145,7 @@ fn persist_new_identity(path: &Path, identity: &StoredIdentity) -> Result<()> {
let mut file = options
.open(&temporary)
.context("preparing Hub issuer identity")?;
file.write_all(&serde_json::to_vec_pretty(identity)?)
file.write_all(contents)
.context("writing Hub issuer identity")?;
file.sync_all().context("syncing Hub issuer identity")?;
}
@ -129,6 +164,43 @@ fn persist_new_identity(path: &Path, identity: &StoredIdentity) -> Result<()> {
mod tests {
use super::*;
#[test]
fn generated_secret_is_stable_private_and_validated() {
let directory = tempfile::tempdir().unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(directory.path(), std::fs::Permissions::from_mode(0o700))
.unwrap();
}
let first = load_or_create_secret(directory.path(), "s.key").unwrap();
assert_eq!(
first,
load_or_create_secret(directory.path(), "s.key").unwrap()
);
assert_ne!(first, [0; 32]);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::metadata(directory.path().join("s.key"))
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o077, 0);
}
std::fs::write(directory.path().join("bad.key"), "zz").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(
directory.path().join("bad.key"),
std::fs::Permissions::from_mode(0o600),
)
.unwrap();
}
assert!(load_or_create_secret(directory.path(), "bad.key").is_err());
}
#[test]
fn publishing_never_overwrites_an_existing_identity_or_leaves_temp_files() {
let directory = tempfile::tempdir().unwrap();