0.1.5: uploading checks everything first; no silent joining
- Before anything is made on the team, upload checks the vault the way the hub will (the hub's per-entry check now lives in core): a vault on one device only holds every line that looks like a secret as a red card to decide; lines marked "not a secret" are told to the team's hub at upload; history with a flagged entry offers a fresh start. A refused upload no longer leaves an empty vault on the team. - A vault on one device joins a same-named team vault only when they share history; otherwise the team's vault arrives beside it in its own folder. (A failed upload's empty vault had silently swallowed Brain.) - The upload sheet follows the vault live and waits for those decisions. - Full checks of such a vault are cached by content. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::db;
|
||||
use anyhow::{Context, Result};
|
||||
use lune_core::git2::{self, Oid, Repository};
|
||||
use lune_core::git2::{Oid, Repository};
|
||||
use lune_core::secrets;
|
||||
use std::io::BufRead;
|
||||
use std::path::PathBuf;
|
||||
@@ -45,7 +45,7 @@ pub fn pre_receive() -> Result<bool> {
|
||||
}
|
||||
}
|
||||
for id in walk {
|
||||
found.extend(check_commit(&repo, &repo.find_commit(id?)?, &allowed)?);
|
||||
found.extend(secrets::check_commit(&repo, &repo.find_commit(id?)?, &allowed)?);
|
||||
}
|
||||
}
|
||||
if found.is_empty() {
|
||||
@@ -59,34 +59,3 @@ pub fn pre_receive() -> Result<bool> {
|
||||
db::log(&conn, &actor, "change refused", &format!("{vault}: possible secret — {}", found.join("; ")))?;
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Lines this history entry adds (relative to all its parents) that look like secrets.
|
||||
fn check_commit(repo: &Repository, commit: &git2::Commit, allowed: &std::collections::HashSet<String>) -> Result<Vec<String>> {
|
||||
let parents: Vec<git2::Tree> = commit.parents().map(|p| p.tree()).collect::<Result<_, _>>()?;
|
||||
let tree = commit.tree()?;
|
||||
let first = parents.first();
|
||||
let diff = repo.diff_tree_to_tree(first, Some(&tree), None)?;
|
||||
let mut out = vec![];
|
||||
for delta in diff.deltas() {
|
||||
if !matches!(delta.status(), git2::Delta::Added | git2::Delta::Modified) {
|
||||
continue;
|
||||
}
|
||||
let Some(path) = delta.new_file().path() else { continue };
|
||||
let new = repo.find_blob(delta.new_file().id())?;
|
||||
// A line already present in any parent isn't new here.
|
||||
let mut old = Vec::new();
|
||||
for p in &parents {
|
||||
if let Ok(e) = p.get_path(path)
|
||||
&& let Ok(b) = repo.find_blob(e.id())
|
||||
{
|
||||
old.extend_from_slice(b.content());
|
||||
old.push(b'\n');
|
||||
}
|
||||
}
|
||||
let old = if old.is_empty() { None } else { Some(old.as_slice()) };
|
||||
for hit in secrets::scan_bytes(new.content(), old, allowed) {
|
||||
out.push(format!("{} line {}: possible {}", path.display(), hit.line, hit.rule));
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
@@ -664,6 +664,8 @@ fn a_vault_moves_onto_the_hub_only_once_it_is_detached() {
|
||||
sen.create_vault("Brain").unwrap();
|
||||
check_in(&mut mac);
|
||||
assert!(mac.find("Brain").unwrap().hub_vault.is_none(), "a vault on its own remote stays put");
|
||||
let brains = |c: &Config| c.vaults.iter().filter(|v| v.name == "Brain").count();
|
||||
assert_eq!(brains(&mac), 1, "and the team's vault waits until it can arrive beside it");
|
||||
|
||||
// Detaching shares what was waiting there one last time, then it's only on this device.
|
||||
lune_core::upload::detach(&mut mac, "Brain").unwrap();
|
||||
@@ -677,10 +679,22 @@ fn a_vault_moves_onto_the_hub_only_once_it_is_detached() {
|
||||
assert_eq!(lune_core::status::of(mac.find("Brain").unwrap(), "Mac").headline, "Only on this device · not shared");
|
||||
drop(v);
|
||||
|
||||
// Uploading joins the same-named team vault, history included; another device gets every note.
|
||||
// The team's unrelated "Brain" doesn't swallow it: it arrives in its own folder, and the name is taken.
|
||||
check_in(&mut mac);
|
||||
assert!(mac.find("Brain").unwrap().hub_vault.is_none());
|
||||
assert_eq!(brains(&mac), 2, "{:?}", mac.vaults.iter().map(|v| &v.path).collect::<Vec<_>>());
|
||||
let err = lune_core::upload::upload(&mut mac, &sen, "Brain", false).unwrap_err().to_string();
|
||||
assert!(err.contains("already has a vault named Brain"), "{err}");
|
||||
mac.use_hub_credentials();
|
||||
sen.delete_vault("Brain").unwrap();
|
||||
check_in(&mut mac);
|
||||
let mac_brain = mac.find("Brain").unwrap().clone();
|
||||
assert!(mac_brain.hub_vault.is_none() && brains(&mac) == 1, "the deleted team vault left");
|
||||
|
||||
// Uploading, history included; another device gets every note.
|
||||
let said = lune_core::upload::upload(&mut mac, &sen, "Brain", false).unwrap();
|
||||
assert!(said.contains("history included"), "{said}");
|
||||
assert!(mac.find("Brain").unwrap().hub_vault.is_some());
|
||||
assert!(mac.find("Brain").unwrap().hub_vault.is_some() && mac.find("Brain").unwrap().path == mac_brain.path);
|
||||
let code = sen.pair().unwrap();
|
||||
let other_w = hub::login(&url, &code, "Air").unwrap();
|
||||
let mut other = device(&url, &other_w, "Air", &tmp.path().join("air"));
|
||||
@@ -690,6 +704,66 @@ fn a_vault_moves_onto_the_hub_only_once_it_is_detached() {
|
||||
assert_eq!(std::fs::read(o.root.join("later.md")).unwrap(), b"kept here\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn possible_secrets_are_decided_before_anything_goes_to_the_team() {
|
||||
let _turn = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let tmp = tempfile::TempDir::new().unwrap();
|
||||
let (_hub, url, owner_link) = start_hub(&tmp.path().join("data"));
|
||||
let (_, sen_w) = hub::join(&owner_link, "Mac").unwrap();
|
||||
let mut mac = device(&url, &sen_w, "Mac", &tmp.path().join("mac"));
|
||||
let sen = as_device(&url, &sen_w);
|
||||
let elsewhere = tmp.path().join("gitlab.git");
|
||||
lune_core::git2::Repository::init_bare(&elsewhere).unwrap();
|
||||
let remote_url = format!("file://{}", elsewhere.display());
|
||||
|
||||
// Two vaults whose history already has a key line (it was shared elsewhere before Lune checked anything).
|
||||
for name in ["Kept", "Fresh"] {
|
||||
let dir = tmp.path().join(format!("src-{name}"));
|
||||
let repo = lune_core::git2::Repository::init(&dir).unwrap();
|
||||
std::fs::write(dir.join("aws.md"), "aws_access_key_id = AKIAIOSFODNN7EXAMPLE\n").unwrap();
|
||||
std::fs::write(dir.join("plan.md"), "plan\n").unwrap();
|
||||
let mut index = repo.index().unwrap();
|
||||
index.add_all(["*"], Default::default(), None).unwrap();
|
||||
let tree = repo.find_tree(index.write_tree().unwrap()).unwrap();
|
||||
let sig = lune_core::git2::Signature::now("x", "x@example.com").unwrap();
|
||||
repo.commit(Some("HEAD"), &sig, &sig, "old", &tree, &[]).unwrap();
|
||||
let (mut cfg, _, _) = vault::add(name, &dir, &remote_url, "Mac").unwrap();
|
||||
cfg.remote = remote_url.clone();
|
||||
mac.vaults.push(cfg);
|
||||
lune_core::upload::detach(&mut mac, name).unwrap();
|
||||
// Only on this device now: the key line is held, like any note that hasn't gone anywhere yet.
|
||||
let v = open(&mac, name);
|
||||
assert_eq!(v.secret_holds().unwrap().len(), 1);
|
||||
let err = lune_core::upload::upload(&mut mac, &sen, name, false).unwrap_err().to_string();
|
||||
assert!(err.contains("Decide about each first"), "{err}");
|
||||
}
|
||||
assert!(sen.me().unwrap().vaults.is_empty(), "nothing was made on the team");
|
||||
|
||||
// "Not a secret" here travels with the upload, so the history goes up whole.
|
||||
let v = open(&mac, "Kept");
|
||||
let hit = v.secret_holds().unwrap()[0].hits[0].clone();
|
||||
secrets::allow(&v.git_dir, &hit, "aws.md").unwrap();
|
||||
assert!(v.secret_holds().unwrap().is_empty());
|
||||
drop(v);
|
||||
let said = lune_core::upload::upload(&mut mac, &sen, "Kept", false).unwrap();
|
||||
assert!(said.contains("history included"), "{said}");
|
||||
|
||||
// Replaced with a pointer: the old entry still has it, so only a fresh start goes up.
|
||||
let v = open(&mac, "Fresh");
|
||||
let note = v.secret_holds().unwrap().remove(0);
|
||||
let text = std::fs::read_to_string(v.root.join("aws.md")).unwrap();
|
||||
std::fs::write(v.root.join("aws.md"), secrets::replace_with_pointer(&text, ¬e.hits, None, "in 1Password: AWS")).unwrap();
|
||||
v.save_changes(None, &HashSet::new()).unwrap();
|
||||
drop(v);
|
||||
let err = lune_core::upload::upload(&mut mac, &sen, "Fresh", false).unwrap_err().to_string();
|
||||
assert!(err.contains("A fresh start"), "{err}");
|
||||
assert_eq!(sen.me().unwrap().vaults.len(), 1, "still nothing made for it");
|
||||
let said = lune_core::upload::upload(&mut mac, &sen, "Fresh", true).unwrap();
|
||||
assert!(said.contains("starting fresh"), "{said}");
|
||||
let v = open(&mac, "Fresh");
|
||||
assert_eq!(std::fs::read_to_string(v.root.join("aws.md")).unwrap(), "aws_access_key_id = in 1Password: AWS\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaving_finishes_even_when_the_folder_points_elsewhere_and_history_has_a_secret() {
|
||||
let _turn = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
|
||||
|
||||
Reference in New Issue
Block a user