utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | 373d85f62fd57813ca8ed57689a622b0c04500c6e11e11d4bcaae3753df86016 |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-22 16:52 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-22 16:52 +02:00 |
| Parents | 86bd0ef83e2c |
3 files changed
modified crates/cli/src/hub/request.rs +23 −0
| @@ -2119,6 +2119,29 @@ | ||
| 2119 | 2119 | assert_eq!(prefix_on(&elsewhere, &target, &info("admin"), "fix"), None); |
| 2120 | 2120 | let main_only = me(&[("ada/demo", &["write", "land:main"])]); |
| 2121 | 2121 | assert_eq!(prefix_on(&main_only, &target, &info("write"), "fix"), None); |
| 2122 | + // The token of a CI job, which posts verdicts, lands nowhere. | |
| 2123 | + let verdicts = me(&[("ada/demo", &["read", "verify:main"])]); | |
| 2124 | + assert_eq!(prefix_on(&verdicts, &target, &info("write"), "fix"), None); | |
| 2125 | + // Beside a scope that lands it takes nothing away. Nor does a scope | |
| 2126 | + // with a right this braid cannot read, as a braid older than | |
| 2127 | + // `verify:` rights reads one: that scope is left out, and the | |
| 2128 | + // others count. | |
| 2129 | + let both = me(&[ | |
| 2130 | + ("ada/demo", &["read", "verify:main"]), | |
| 2131 | + ("ada/demo", &["write", "land:request/*"]), | |
| 2132 | + ]); | |
| 2133 | + assert_eq!( | |
| 2134 | + prefix_on(&both, &target, &info("write"), "fix").as_deref(), | |
| 2135 | + Some("request/") | |
| 2136 | + ); | |
| 2137 | + let unread = me(&[ | |
| 2138 | + ("ada/demo", &["read", "someday:main"]), | |
| 2139 | + ("ada/demo", &["write", "land:request/*"]), | |
| 2140 | + ]); | |
| 2141 | + assert_eq!( | |
| 2142 | + prefix_on(&unread, &target, &info("write"), "fix").as_deref(), | |
| 2143 | + Some("request/") | |
| 2144 | + ); | |
| 2122 | 2145 | } |
| 2123 | 2146 | |
| 2124 | 2147 | /// A request read off a connection: its `Idempotency-Key` and its body. |
modified crates/hubproto/src/api.rs +2 −1
| @@ -173,7 +173,8 @@ | ||
| 173 | 173 | pub struct Scope { |
| 174 | 174 | /// `owner/name`, `owner/*`, or `*`. |
| 175 | 175 | pub repo: String, |
| 176 | - /// `read`, `write`, `land:<glob>`, `admin`. | |
| 176 | + /// `read`, `interact`, `write`, `land:<glob>`, `verify:<glob>`, `admin` | |
| 177 | + /// (see [`crate::scope`]). | |
| 177 | 178 | pub rights: Vec<String>, |
| 178 | 179 | } |
| 179 | 180 |
modified crates/hubproto/src/scope.rs +77 −15
| @@ -3,13 +3,23 @@ | ||
| 3 | 3 | |
| 4 | 4 | //! Token scopes: which repositories a token reaches, and what it may do |
| 5 | 5 | //! there. A scope's `repo` is `owner/name`, `owner/*` or `*`; a right is |
| 6 | -//! `read`, `interact`, `write`, `land:<ref glob>` or `admin`. | |
| 6 | +//! `read`, `interact`, `write`, `land:<ref glob>`, `verify:<ref glob>` or | |
| 7 | +//! `admin`. | |
| 7 | 8 | //! |
| 8 | 9 | //! `write` includes `read`, and `admin` includes both. `interact` covers the |
| 9 | 10 | //! social writes (stars, watching, following, issues, comments, reviews and |
| 10 | 11 | //! forks) on what the account may read; `write` and `admin` include it, and |
| 11 | 12 | //! it gives no read of its own. A `land:<glob>` right lets a token land on |
| 12 | 13 | //! the refs its glob matches, and only together with `write`. |
| 14 | +//! | |
| 15 | +//! A `verify:<glob>` right lets a token post verdicts on the branches its | |
| 16 | +//! glob matches (section 25), and nothing else: it counts only where the | |
| 17 | +//! token's account may write, and only on a token that reads the repository | |
| 18 | +//! but holds no `write`, `admin`, `land:` or `interact` there, so that the | |
| 19 | +//! token of a CI job can say how its tests went and do nothing more. | |
| 20 | +//! | |
| 21 | +//! [`Right`] may gain rights: a client keeps a scope it cannot read out of | |
| 22 | +//! what it decides, as [`ParsedScope::parse`] refuses it whole. | |
| 13 | 23 | |
| 14 | 24 | use std::fmt; |
| 15 | 25 | |
| @@ -23,15 +33,18 @@ | ||
| 23 | 33 | #[error("{0:?} is not a repository pattern: expected \"owner/name\", \"owner/*\" or \"*\"")] |
| 24 | 34 | Repo(String), |
| 25 | 35 | /// A right that does not exist. |
| 26 | - #[error("{0:?} is not a right: expected read, interact, write, admin or land:<ref glob>")] | |
| 36 | + #[error( | |
| 37 | + "{0:?} is not a right: expected read, interact, write, admin, land:<ref glob> or verify:<ref glob>" | |
| 38 | + )] | |
| 27 | 39 | Right(String), |
| 28 | 40 | /// A scope without rights. |
| 29 | 41 | #[error("the scope for {0:?} grants no right")] |
| 30 | 42 | NoRights(String), |
| 31 | 43 | } |
| 32 | 44 | |
| 33 | -/// One right of a scope. | |
| 45 | +/// One right of a scope. More may come: a `match` on it ends with `_`. | |
| 34 | 46 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 47 | +#[non_exhaustive] | |
| 35 | 48 | pub enum Right { |
| 36 | 49 | /// Every `GET` and `HEAD`, `lacks`, `missing` and the ref watch. |
| 37 | 50 | Read, |
| @@ -46,27 +59,38 @@ | ||
| 46 | 59 | /// Landing on the refs the glob matches (see |
| 47 | 60 | /// [`glob_matches`](crate::names::glob_matches)), with `write`. |
| 48 | 61 | Land(String), |
| 62 | + /// Posting verdicts on the branches the glob matches (section 25), | |
| 63 | + /// where the account may write, on a token that holds nothing more | |
| 64 | + /// there than `read`. | |
| 65 | + Verify(String), | |
| 66 | +} | |
| 67 | + | |
| 68 | +/// The glob of a `land:` or `verify:` right: not empty, at most 4,096 | |
| 69 | +/// bytes, and without control characters. | |
| 70 | +fn glob(text: &str) -> Option<String> { | |
| 71 | + (!text.is_empty() && text.len() <= 4096 && !text.chars().any(char::is_control)) | |
| 72 | + .then(|| text.to_owned()) | |
| 49 | 73 | } |
| 50 | 74 | |
| 51 | 75 | impl Right { |
| 52 | - /// Reads `read`, `interact`, `write`, `admin` or `land:<glob>`. A glob | |
| 53 | - /// is not empty and holds no control character. | |
| 76 | + /// Reads `read`, `interact`, `write`, `admin`, `land:<glob>` or | |
| 77 | + /// `verify:<glob>`. A glob is not empty and holds no control character. | |
| 54 | 78 | pub fn parse(text: &str) -> Result<Right, ScopeError> { |
| 79 | + let refused = || ScopeError::Right(text.to_owned()); | |
| 55 | 80 | match text { |
| 56 | 81 | "read" => Ok(Right::Read), |
| 57 | 82 | "interact" => Ok(Right::Interact), |
| 58 | 83 | "write" => Ok(Right::Write), |
| 59 | 84 | "admin" => Ok(Right::Admin), |
| 60 | - _ => match text.strip_prefix("land:") { | |
| 61 | - Some(glob) | |
| 62 | - if !glob.is_empty() | |
| 63 | - && glob.len() <= 4096 | |
| 64 | - && !glob.chars().any(char::is_control) => | |
| 65 | - { | |
| 66 | - Ok(Right::Land(glob.to_owned())) | |
| 85 | + _ => { | |
| 86 | + if let Some(rest) = text.strip_prefix("land:") { | |
| 87 | + glob(rest).map(Right::Land).ok_or_else(refused) | |
| 88 | + } else if let Some(rest) = text.strip_prefix("verify:") { | |
| 89 | + glob(rest).map(Right::Verify).ok_or_else(refused) | |
| 90 | + } else { | |
| 91 | + Err(refused()) | |
| 67 | 92 | } |
| 68 | - _ => Err(ScopeError::Right(text.to_owned())), | |
| 69 | - }, | |
| 93 | + } | |
| 70 | 94 | } |
| 71 | 95 | } |
| 72 | 96 | } |
| @@ -79,6 +103,7 @@ | ||
| 79 | 103 | Right::Write => f.write_str("write"), |
| 80 | 104 | Right::Admin => f.write_str("admin"), |
| 81 | 105 | Right::Land(glob) => write!(f, "land:{glob}"), |
| 106 | + Right::Verify(glob) => write!(f, "verify:{glob}"), | |
| 82 | 107 | } |
| 83 | 108 | } |
| 84 | 109 | } |
| @@ -183,6 +208,8 @@ | ||
| 183 | 208 | ("admin", Right::Admin), |
| 184 | 209 | ("land:*", Right::Land("*".into())), |
| 185 | 210 | ("land:release/*", Right::Land("release/*".into())), |
| 211 | + ("verify:main", Right::Verify("main".into())), | |
| 212 | + ("verify:*", Right::Verify("*".into())), | |
| 186 | 213 | ] { |
| 187 | 214 | assert_eq!(Right::parse(text), Ok(right.clone())); |
| 188 | 215 | assert_eq!(right.to_string(), text); |
| @@ -196,9 +223,27 @@ | ||
| 196 | 223 | "land", |
| 197 | 224 | "push", |
| 198 | 225 | "land:a\nb", |
| 226 | + "verify:", | |
| 227 | + "verify", | |
| 228 | + "VERIFY:main", | |
| 229 | + "Verify:main", | |
| 230 | + "verify:a\nb", | |
| 199 | 231 | ] { |
| 200 | - assert!(Right::parse(bad).is_err(), "{bad:?}"); | |
| 232 | + assert_eq!( | |
| 233 | + Right::parse(bad), | |
| 234 | + Err(ScopeError::Right(bad.to_owned())), | |
| 235 | + "{bad:?}" | |
| 236 | + ); | |
| 201 | 237 | } |
| 238 | + let long = format!("verify:{}", "a".repeat(4096)); | |
| 239 | + assert!(Right::parse(&long).is_ok()); | |
| 240 | + assert!(Right::parse(&format!("{long}a")).is_err()); | |
| 241 | + // The error names every right there is. | |
| 242 | + let said = ScopeError::Right("push".into()).to_string(); | |
| 243 | + assert!( | |
| 244 | + said.contains("land:<ref glob>") && said.contains("verify:<ref glob>"), | |
| 245 | + "{said}" | |
| 246 | + ); | |
| 202 | 247 | } |
| 203 | 248 | |
| 204 | 249 | #[test] |
| @@ -236,5 +281,22 @@ | ||
| 236 | 281 | ParsedScope::parse(&empty), |
| 237 | 282 | Err(ScopeError::NoRights("*".into())) |
| 238 | 283 | ); |
| 284 | + // The scope of a CI job's token: it reads, and posts verdicts. | |
| 285 | + let ci = Scope { | |
| 286 | + repo: "ada/demo".into(), | |
| 287 | + rights: vec!["read".into(), "verify:main".into()], | |
| 288 | + }; | |
| 289 | + let parsed = ParsedScope::parse(&ci).unwrap(); | |
| 290 | + assert_eq!(parsed.rights, [Right::Read, Right::Verify("main".into())]); | |
| 291 | + assert_eq!(parsed.to_scope(), ci); | |
| 292 | + // A right this crate cannot read refuses its scope whole. | |
| 293 | + let later = Scope { | |
| 294 | + repo: "ada/demo".into(), | |
| 295 | + rights: vec!["read".into(), "someday:main".into()], | |
| 296 | + }; | |
| 297 | + assert_eq!( | |
| 298 | + ParsedScope::parse(&later), | |
| 299 | + Err(ScopeError::Right("someday:main".into())) | |
| 300 | + ); | |
| 239 | 301 | } |
| 240 | 302 | } |