[sign] Approval: canonical deploy approvals (parse, rewrite, byte-compare; run, names, commit, targets, expiry within an hour) signed as prehashed minisign approval-<run>.json
This commit is contained in:
@@ -52,6 +52,8 @@ pub enum Error {
|
||||
BadKey,
|
||||
#[error("the certificate cannot be built or read")]
|
||||
Certificate,
|
||||
#[error("the approval cannot be signed: {0}")]
|
||||
Approval(&'static str),
|
||||
}
|
||||
|
||||
/// An Ed25519 seed. Wiped on drop; never printed.
|
||||
@@ -360,3 +362,173 @@ pub fn cert_pem(der: &[u8]) -> String {
|
||||
pub fn sha256_hex(bytes: &[u8]) -> String {
|
||||
Sha256::digest(bytes).iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
// ---- deploy approvals ----
|
||||
|
||||
/// The largest approval text Custos reads (docs/SIGNING.md).
|
||||
pub const MAX_APPROVAL: usize = 8 * 1024;
|
||||
/// An approval expires at most this many seconds after it is checked.
|
||||
pub const APPROVAL_MAX_AHEAD: u64 = 3600;
|
||||
|
||||
/// A deploy approval, read from its canonical JSON (docs/SIGNING.md, "approval"). Nothing in it is secret.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Approval {
|
||||
pub run: String,
|
||||
pub repo: String,
|
||||
pub commit: String,
|
||||
pub pipeline: String,
|
||||
pub version: String,
|
||||
pub targets: Vec<String>,
|
||||
/// Unix seconds.
|
||||
pub expires: u64,
|
||||
}
|
||||
|
||||
/// The JSON as it is parsed: `v` first, the rest as [`Approval`].
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ApprovalJson {
|
||||
v: u64,
|
||||
run: String,
|
||||
repo: String,
|
||||
commit: String,
|
||||
pipeline: String,
|
||||
version: String,
|
||||
targets: Vec<String>,
|
||||
expires: u64,
|
||||
}
|
||||
|
||||
impl Approval {
|
||||
/// The canonical text (docs/SIGNING.md): one line, no spaces, keys in the fixed order, `"` and `\` escaped,
|
||||
/// nothing else. Meaningful for an approval that passed [`approval_check`].
|
||||
pub fn canonical(&self) -> String {
|
||||
fn string(out: &mut String, s: &str) {
|
||||
out.push('"');
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'"' => out.push_str("\\\""),
|
||||
'\\' => out.push_str("\\\\"),
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out.push('"');
|
||||
}
|
||||
let mut out = String::with_capacity(256);
|
||||
out.push_str("{\"v\":1,\"run\":");
|
||||
string(&mut out, &self.run);
|
||||
out.push_str(",\"repo\":");
|
||||
string(&mut out, &self.repo);
|
||||
out.push_str(",\"commit\":");
|
||||
string(&mut out, &self.commit);
|
||||
out.push_str(",\"pipeline\":");
|
||||
string(&mut out, &self.pipeline);
|
||||
out.push_str(",\"version\":");
|
||||
string(&mut out, &self.version);
|
||||
out.push_str(",\"targets\":[");
|
||||
for (i, t) in self.targets.iter().enumerate() {
|
||||
if i > 0 {
|
||||
out.push(',');
|
||||
}
|
||||
string(&mut out, t);
|
||||
}
|
||||
out.push_str("],\"expires\":");
|
||||
out.push_str(&self.expires.to_string());
|
||||
out.push('}');
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// A run id: `[A-Za-z0-9._-]{1,64}`.
|
||||
pub fn run_id_ok(run: &str) -> bool {
|
||||
(1..=64).contains(&run.len()) && run.bytes().all(|c| c.is_ascii_alphanumeric() || b"._-".contains(&c))
|
||||
}
|
||||
|
||||
/// `[A-Za-z0-9._+-]{1,64}`: an approval's repo, pipeline and version.
|
||||
fn name_ok(s: &str) -> bool {
|
||||
(1..=64).contains(&s.len()) && s.bytes().all(|c| c.is_ascii_alphanumeric() || b"._+-".contains(&c))
|
||||
}
|
||||
|
||||
/// Characters a target may not hold: control characters, and the invisible ones that change how the rest of the
|
||||
/// line is shown on the card (bidi overrides and isolates, zero-width characters, line separators, soft hyphen).
|
||||
fn unprintable(c: char) -> bool {
|
||||
c.is_control()
|
||||
|| matches!(c, '\u{00AD}' | '\u{061C}' | '\u{180E}' | '\u{200B}'..='\u{200F}' | '\u{2028}'..='\u{202E}')
|
||||
|| matches!(c, '\u{2060}'..='\u{2069}' | '\u{FEFF}')
|
||||
}
|
||||
|
||||
/// Checks that `text` is a deploy approval in canonical form that has not expired at `now_secs` and expires at most
|
||||
/// an hour later (docs/SIGNING.md). It is parsed, written again in canonical form and compared byte for byte, so
|
||||
/// any other spelling of the same values (key order, spaces, escapes, a trailing newline) is refused.
|
||||
pub fn approval_check(text: &str, now_secs: u64) -> Result<Approval, Error> {
|
||||
let err = |why: &'static str| Err(Error::Approval(why));
|
||||
if text.len() > MAX_APPROVAL {
|
||||
return err("larger than 8 KiB");
|
||||
}
|
||||
let Ok(j) = serde_json::from_str::<ApprovalJson>(text) else {
|
||||
return err("not an approval in canonical form");
|
||||
};
|
||||
if j.v != 1 {
|
||||
return err("v is not 1");
|
||||
}
|
||||
let a = Approval {
|
||||
run: j.run,
|
||||
repo: j.repo,
|
||||
commit: j.commit,
|
||||
pipeline: j.pipeline,
|
||||
version: j.version,
|
||||
targets: j.targets,
|
||||
expires: j.expires,
|
||||
};
|
||||
if !run_id_ok(&a.run) {
|
||||
return err("the run is not a run id ([A-Za-z0-9._-], 1 to 64)");
|
||||
}
|
||||
if !name_ok(&a.repo) {
|
||||
return err("the repo is not 1 to 64 of [A-Za-z0-9._+-]");
|
||||
}
|
||||
if a.commit.len() != 40 || !a.commit.bytes().all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c)) {
|
||||
return err("the commit is not 40 lower-case hex digits");
|
||||
}
|
||||
if !name_ok(&a.pipeline) {
|
||||
return err("the pipeline is not 1 to 64 of [A-Za-z0-9._+-]");
|
||||
}
|
||||
if !name_ok(&a.version) {
|
||||
return err("the version is not 1 to 64 of [A-Za-z0-9._+-]");
|
||||
}
|
||||
if !(1..=16).contains(&a.targets.len()) {
|
||||
return err("there must be 1 to 16 targets");
|
||||
}
|
||||
for t in &a.targets {
|
||||
if !(1..=80).contains(&t.chars().count()) {
|
||||
return err("a target is not 1 to 80 characters");
|
||||
}
|
||||
if t.chars().any(unprintable) {
|
||||
return err("a target has control or invisible characters");
|
||||
}
|
||||
}
|
||||
if a.canonical() != text {
|
||||
return err("not in canonical form (key order, spaces or escaping)");
|
||||
}
|
||||
if a.expires <= now_secs {
|
||||
return err("it has expired");
|
||||
}
|
||||
if a.expires > now_secs.saturating_add(APPROVAL_MAX_AHEAD) {
|
||||
return err("it expires more than an hour from now");
|
||||
}
|
||||
Ok(a)
|
||||
}
|
||||
|
||||
/// Signs a deploy approval for `run` as prehashed minisign over the text's bytes, file name `approval-<run>.json`.
|
||||
/// The text is checked with [`approval_check`] at `timestamp` (Unix seconds) and must name `run`.
|
||||
pub fn approval(
|
||||
secret: &Ed25519Secret,
|
||||
key_id: &[u8; 8],
|
||||
text: &str,
|
||||
timestamp: u64,
|
||||
run: &str,
|
||||
) -> Result<String, Error> {
|
||||
let a = approval_check(text, timestamp)?;
|
||||
if a.run != run {
|
||||
return Err(Error::Approval("the approval's run is not the request's"));
|
||||
}
|
||||
minisign(secret, key_id, &blake2b512(text.as_bytes()), &format!("approval-{run}.json"), timestamp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user