Skip to content

Commit e9e2715

Browse files
Merge #99
99: fix: root NUC validation r=mfontanini a=mfontanini The recent change on NUC validation when there were root keys in the validator but there were no proofs in the token caused tokens minted by nilauth to be considered invalid. This changes that so that if there are no proofs and there are root keys, we consider the token valid if the token itself is signed by a root keypair. Co-authored-by: Matias Fontanini <[email protected]>
2 parents 8d8d0d5 + 7f08e2f commit e9e2715

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

libs/nucs/src/validator.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl NucValidator {
9696

9797
// Create a sequence [root, ..., token]
9898
let token_chain = iter::once(token).chain(proofs.iter().copied()).rev();
99-
Self::validate_proofs(&proofs, &self.root_keys)?;
99+
Self::validate_proofs(token, &proofs, &self.root_keys)?;
100100
Self::validate_token_chain(token_chain, &parameters)?;
101101
Self::validate_token(token, &proofs, &parameters.token_requirements)?;
102102

@@ -147,14 +147,14 @@ impl NucValidator {
147147
}
148148

149149
// Validations applied to proofs
150-
fn validate_proofs(proofs: &[&NucToken], root_keys: &HashSet<Box<[u8]>>) -> ValidationResult {
151-
let contains_root_signature = match proofs.last() {
152-
Some(proof) => root_keys.contains(proof.issuer.public_key.as_slice()),
153-
// if there's no proof but also no root keys we don't need one.
154-
None => root_keys.is_empty(),
155-
};
156-
if !contains_root_signature {
157-
return Err(ValidationError::Validation(ValidationKind::RootKeySignatureMissing));
150+
fn validate_proofs(token: &NucToken, proofs: &[&NucToken], root_keys: &HashSet<Box<[u8]>>) -> ValidationResult {
151+
if !root_keys.is_empty() {
152+
// The root issuer of this token is either the last proof issuer or the issuer of the
153+
// token itself.
154+
let root_issuer = &proofs.last().unwrap_or(&token).issuer;
155+
if !root_keys.contains(root_issuer.public_key.as_slice()) {
156+
return Err(ValidationError::Validation(ValidationKind::RootKeySignatureMissing));
157+
}
158158
}
159159

160160
for proof in proofs {
@@ -991,4 +991,13 @@ mod tests {
991991
asserter.root_keys = Vec::new();
992992
asserter.assert_success(envelope);
993993
}
994+
995+
#[test]
996+
fn root_token_validation() {
997+
let key = secret_key();
998+
let root = delegation(&key).command(["nil"]).issued_by_root();
999+
1000+
let envelope = Chainer::default().chain([root]);
1001+
Asserter::default().assert_success(envelope);
1002+
}
9941003
}

0 commit comments

Comments
 (0)