authoravatarclover caruso<git@paperclover.net>2026-10-20 23:27:52-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-21 02:52:29-07:00
log48149be04c0e2d6f0d219eb70e07ca7f45d824c9
tree9c32ea3b9828f99826ae9df46399df7f4e63d1c1
parentb980b208fd9571a5a1302788c2cb363c8483a828
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Restrict The Knife native source behind an account allowlist

Gate web, raw, archive, search, object, and Git HTTPS surfaces before proxying. Retain pinned native history after merges, resolve references from one snapshot, and rebuild sensitive diffs from public files. Grant only snow initially. Support SSO and guest accounts through knife-native membership, validate Shale personal tokens against their enabled account, and apply revocation on the next request. Preserve other repositories and CGI Git status handling. Verified Rust and Python checks, Caddy integration, disposable merge redaction, SSO and guest grants, nonmember rejection, revocation, Git discovery/POST, and browser login, keyboard, themes, narrow layout, and text scaling. Assisted-by: gpt-6.1-sol

17 files changed, 521 insertions(+), 34 deletions(-)

config/shale.json+1-1
...@@ -1 +1 @@...@@ -1 +1 @@
1{"actorOwners":{"ai":"8c909fab-98b0-4472-9171-9606ccebe9d5"},"statusDelegates":{"ai":["chat","snowbound"]}}1{"actorOwners":{"ai":"8c909fab-98b0-4472-9171-9606ccebe9d5"},"statusDelegates":{"ai":["chat","snowbound"]},"nativeRestriction":{"commits":["8726b8fe5947ae38ad585b0e253feaf92e4f02d5"]}}
dashboard/src/auth.rs+5-2
...@@ -12,7 +12,7 @@ const COOKIE: &str = "__Host-snow-session";...@@ -12,7 +12,7 @@ const COOKIE: &str = "__Host-snow-session";
12const FLOW_COOKIE: &str = "__Host-snow-flow";12const FLOW_COOKIE: &str = "__Host-snow-flow";
13const CSRF_COOKIE: &str = "__Host-snow-csrf";13const CSRF_COOKIE: &str = "__Host-snow-csrf";
14const SESSION_TTL: i64 = 30 * 86400;14const SESSION_TTL: i64 = 30 * 86400;
15const GROUPS: &[&str] = &["infra-admin", "media", "media-manage", "metrics", "vm", "ai"];15const GROUPS: &[&str] = &["infra-admin", "media", "media-manage", "metrics", "vm", "ai", "knife-native"];
1616
17pub struct Store {17pub struct Store {
18 pub db: Mutex<Connection>,18 pub db: Mutex<Connection>,
...@@ -201,10 +201,13 @@ impl Store {...@@ -201,10 +201,13 @@ impl Store {
201 oidc::initialise(&db)?;201 oidc::initialise(&db)?;
202 guest::initialise(&db)?;202 guest::initialise(&db)?;
203 for name in GROUPS {203 for name in GROUPS {
204 db.execute(204 let added = db.execute(
205 "INSERT OR IGNORE INTO roles(id,name) VALUES (?,?)",205 "INSERT OR IGNORE INTO roles(id,name) VALUES (?,?)",
206 sql![uuid::Uuid::new_v4().to_string(), name],206 sql![uuid::Uuid::new_v4().to_string(), name],
207 )?;207 )?;
208 if *name == "knife-native" && added != 0 {
209 db.execute("INSERT OR IGNORE INTO memberships SELECT users.id,roles.id FROM users,roles WHERE users.username='snow' AND roles.name='knife-native'", [])?;
210 }
208 }211 }
209 Ok(Self {212 Ok(Self {
210 db: Mutex::new(db),213 db: Mutex::new(db),
dashboard/src/guest.rs+7-2
...@@ -22,7 +22,9 @@ pub fn is_guest(user: &Value) -> bool {...@@ -22,7 +22,9 @@ pub fn is_guest(user: &Value) -> bool {
22pub fn initialise(db: &Connection) -> Result<()> {22pub fn initialise(db: &Connection) -> Result<()> {
23 db.execute_batch("CREATE TABLE IF NOT EXISTS guest_providers (id TEXT PRIMARY KEY, client_id TEXT NOT NULL, secret TEXT NOT NULL);23 db.execute_batch("CREATE TABLE IF NOT EXISTS guest_providers (id TEXT PRIMARY KEY, client_id TEXT NOT NULL, secret TEXT NOT NULL);
24 CREATE TABLE IF NOT EXISTS external_identities (provider TEXT NOT NULL, subject TEXT NOT NULL, user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, PRIMARY KEY(provider,subject), UNIQUE(user_id));24 CREATE TABLE IF NOT EXISTS external_identities (provider TEXT NOT NULL, subject TEXT NOT NULL, user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, PRIMARY KEY(provider,subject), UNIQUE(user_id));
25 CREATE TRIGGER IF NOT EXISTS guest_no_groups BEFORE INSERT ON memberships WHEN (SELECT json_extract(profile,'$.kind') FROM users WHERE id=NEW.user_id)='guest' BEGIN SELECT RAISE(ABORT,'guest accounts cannot join groups'); END;25 DROP TRIGGER IF EXISTS guest_no_groups;
26 CREATE TRIGGER guest_no_groups BEFORE INSERT ON memberships WHEN (SELECT json_extract(profile,'$.kind') FROM users WHERE id=NEW.user_id)='guest' AND (SELECT name FROM roles WHERE id=NEW.role_id)!='knife-native' BEGIN SELECT RAISE(ABORT,'guest accounts can only join knife-native'); END;
27 CREATE TRIGGER IF NOT EXISTS guest_no_group_updates BEFORE UPDATE ON memberships WHEN (SELECT json_extract(profile,'$.kind') FROM users WHERE id=NEW.user_id)='guest' AND (SELECT name FROM roles WHERE id=NEW.role_id)!='knife-native' BEGIN SELECT RAISE(ABORT,'guest accounts can only join knife-native'); END;
26 CREATE TRIGGER IF NOT EXISTS guest_no_credentials BEFORE INSERT ON credentials WHEN (SELECT json_extract(profile,'$.kind') FROM users WHERE id=NEW.user_id)='guest' BEGIN SELECT RAISE(ABORT,'guest accounts use external sign-in'); END;28 CREATE TRIGGER IF NOT EXISTS guest_no_credentials BEFORE INSERT ON credentials WHEN (SELECT json_extract(profile,'$.kind') FROM users WHERE id=NEW.user_id)='guest' BEGIN SELECT RAISE(ABORT,'guest accounts use external sign-in'); END;
27 CREATE TRIGGER IF NOT EXISTS guest_kind_fixed BEFORE UPDATE OF profile ON users WHEN json_extract(OLD.profile,'$.kind')='guest' AND coalesce(json_extract(NEW.profile,'$.kind'),'')!='guest' BEGIN SELECT RAISE(ABORT,'guest account kind is fixed'); END;")?;29 CREATE TRIGGER IF NOT EXISTS guest_kind_fixed BEFORE UPDATE OF profile ON users WHEN json_extract(OLD.profile,'$.kind')='guest' AND coalesce(json_extract(NEW.profile,'$.kind'),'')!='guest' BEGIN SELECT RAISE(ABORT,'guest account kind is fixed'); END;")?;
28 Ok(())30 Ok(())
...@@ -934,7 +936,7 @@ mod tests {...@@ -934,7 +936,7 @@ mod tests {
934 }936 }
935937
936 #[test]938 #[test]
937 fn identities_never_link_by_name_or_email_and_cannot_gain_credentials_or_groups() {939 fn identities_stay_separate_and_guests_can_only_join_native_access() {
938 let path = std::env::temp_dir().join(format!("guest-test-{}", uuid::Uuid::new_v4()));940 let path = std::env::temp_dir().join(format!("guest-test-{}", uuid::Uuid::new_v4()));
939 let auth = auth::Store::new(941 let auth = auth::Store::new(
940 &path,942 &path,
...@@ -983,6 +985,9 @@ mod tests {...@@ -983,6 +985,9 @@ mod tests {
983 db.execute("INSERT INTO memberships VALUES (?,'admin')", [&first])985 db.execute("INSERT INTO memberships VALUES (?,'admin')", [&first])
984 .is_err()986 .is_err()
985 );987 );
988 db.execute("INSERT INTO memberships SELECT ?,id FROM roles WHERE name='knife-native'", [&first]).unwrap();
989 assert_eq!(auth::user(&db, &first).unwrap()["groups"][0]["name"], "knife-native");
990 assert!(db.execute("UPDATE memberships SET role_id='admin' WHERE user_id=?", [&first]).is_err());
986 assert!(auth::set_password(&db, &first, "a-password-hash").is_err());991 assert!(auth::set_password(&db, &first, "a-password-hash").is_err());
987 assert!(992 assert!(
988 db.execute(993 db.execute(
dashboard/src/knife-restricted.html created+28
...@@ -0,0 +1,28 @@
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<meta name="viewport" content="width=device-width, initial-scale=1">
5<title>The Knife's Native UI is restricted</title>
6<style>
7:root { color-scheme: light dark; font: 16px/1.5 system-ui, sans-serif; background: #faf8fc; color: #29232e; }
8body { margin: 0; min-height: 100svh; display: grid; place-items: center; }
9main { box-sizing: border-box; width: min(100%, 42rem); padding: 2rem; text-align: center; }
10h1 { margin: 0 0 1rem; font-size: clamp(1.5rem, 4vw, 2rem); line-height: 1.25; }
11p { margin: 0 auto 1.75rem; max-width: 34rem; color: #62596b; }
12a { display: inline-block; padding: .625rem 1.375rem; border: 1px solid transparent; border-radius: .375rem; background: #6b3e9e; color: white; text-decoration: none; font-weight: 600; }
13a:hover { background: #552a84; }
14a:focus-visible { outline: 3px solid #8857be; outline-offset: 4px; }
15@media (prefers-color-scheme: dark) {
16:root { background: #1c1821; color: #f4eff8; }
17p { color: #c2b6cd; }
18a { background: #b495db; color: #21162e; }
19a:hover { background: #c8ade9; }
20a:focus-visible { outline-color: #d7b8ff; }
21}
22</style>
23<main>
24<h1>The Knife's Native UI is restricted</h1>
25<p>This part of the code is sensitive. Please contact clover to request private access.</p>
26<a href="/-/login">Login</a>
27</main>
28</html>
dashboard/src/shale.rs+1-1
...@@ -487,7 +487,7 @@ fn fields(query: &str) -> Result<HashMap<String, String>> {...@@ -487,7 +487,7 @@ fn fields(query: &str) -> Result<HashMap<String, String>> {
487 }487 }
488 Ok(fields)488 Ok(fields)
489}489}
490fn username(page: &str) -> Result<String> {490pub(crate) fn username(page: &str) -> Result<String> {
491 let document = Html::parse_document(page);491 let document = Html::parse_document(page);
492 if document492 if document
493 .select(&Selector::parse("body#page-user-settings").unwrap())493 .select(&Selector::parse("body#page-user-settings").unwrap())
dashboard/src/shale_page.rs+129-5
...@@ -347,6 +347,61 @@ fn strip_hop_headers(headers: &mut HeaderMap) {...@@ -347,6 +347,61 @@ fn strip_hop_headers(headers: &mut HeaderMap) {
347 }347 }
348}348}
349349
350fn knife_uri(path: &str) -> bool {
351 let mut decoded = path.to_owned();
352 for _ in 0..5 {
353 if decoded.split(['/', '\\']).any(|part| part.eq_ignore_ascii_case("the-knife") || part.eq_ignore_ascii_case("the-knife.git")) {
354 return true;
355 }
356 let next = percent_encoding::percent_decode_str(&decoded).decode_utf8_lossy().into_owned();
357 if next == decoded { break; }
358 decoded = next;
359 }
360 false
361}
362
363fn restricted(authenticate: bool) -> Response {
364 (if authenticate { StatusCode::UNAUTHORIZED } else { StatusCode::FORBIDDEN }, [
365 ("content-type", "text/html; charset=utf-8"),
366 ("cache-control", "private, no-store"),
367 ("vary", "Cookie, Authorization"),
368 ("referrer-policy", "same-origin"),
369 ("x-content-type-options", "nosniff"),
370 ("content-security-policy", "default-src 'none'; style-src 'unsafe-inline'; frame-ancestors 'none'; base-uri 'none'"),
371 ("www-authenticate", "Basic realm=\"The Knife\""),
372 ], include_str!("knife-restricted.html")).into_response()
373}
374
375fn redact_knife(html: &str, policy: &Value) -> Result<String> {
376 let commits: HashSet<&str> = array(&policy["commits"]).iter().filter_map(Value::as_str).collect();
377 let document = scraper::Html::parse_document(html);
378 let diff = policy["safeDiff"].as_str();
379 if diff.is_some() && document.select(&scraper::Selector::parse("#m-code").unwrap()).count() != 1 {
380 return Err(Error::new(403, "The Knife's Native UI is restricted"));
381 }
382 let links = scraper::Selector::parse("a[href]").unwrap();
383 let rows: Vec<bool> = document.select(&scraper::Selector::parse("tr").unwrap()).map(|row| {
384 row.select(&links).any(|link| link.attr("href").is_some_and(|href| {
385 href.split(['/', '?', '#', '.']).any(|part| commits.contains(part))
386 }))
387 }).collect();
388 let mut row_index = 0;
389 Ok(lol_html::rewrite_str(html, RewriteStrSettings::new()
390 .append_element_content_handler(element!("#m-code", |element| {
391 if let Some(diff) = diff {
392 let diff = diff.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;");
393 element.set_inner_content(&format!("<pre><code>{diff}</code></pre>"), ContentType::Html);
394 }
395 Ok(())
396 }))
397 .append_element_content_handler(element!("tr", |element| {
398 if *rows.get(row_index).unwrap_or(&true) { element.remove(); }
399 row_index += 1;
400 Ok(())
401 }))
402 )?)
403}
404
350pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {405pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
351 let (mut parts, body) = request.into_parts();406 let (mut parts, body) = request.into_parts();
352 let host = parts407 let host = parts
...@@ -379,6 +434,46 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {...@@ -379,6 +434,46 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
379 return Err(Error::new(403, "Open Shale to continue."));434 return Err(Error::new(403, "Open Shale to continue."));
380 }435 }
381 let origin = url::Url::parse(&format!("https://{host}{uri}"))?;436 let origin = url::Url::parse(&format!("https://{host}{uri}"))?;
437 let knife = knife_uri(uri.path());
438 let mut target = match &app.internal {
439 Some((base, _)) => format!("{base}services/{service}{uri}"),
440 None => format!("http://{upstream}{uri}"),
441 };
442 let mut knife_policy = Value::Null;
443 if knife {
444 let mut account = String::new();
445 if let Some(cookie) = parts.headers.get("cookie") {
446 let settings = match &app.internal {
447 Some((base, _)) => format!("{base}services/{service}/-/settings"),
448 None => format!("http://{upstream}/-/settings"),
449 };
450 let mut identity = app.shale_http.get(settings).header("cookie", cookie).header("accept", "text/html").timeout(Duration::from_secs(3));
451 if app.internal.is_none() { identity = identity.header("host", host); }
452 for name in ["x-forwarded-for", "x-real-ip", "user-agent"] {
453 if let Some(value) = parts.headers.get(name) { identity = identity.header(name, value); }
454 }
455 if let Ok(response) = identity.send().await
456 && response.status() == StatusCode::OK
457 && let Ok(body) = response.text().await
458 {
459 account = shale::username(&body).unwrap_or_default();
460 }
461 }
462 knife_policy = match host::call(json!({"operation":"shale.access","service":service,"uri":uri.to_string(),"account":account,"authorization":parts.headers.get("authorization").and_then(|value| value.to_str().ok()).unwrap_or_default()})).await {
463 Ok(policy) => policy,
464 Err(_) => return Ok(restricted(false)),
465 };
466 if knife_policy["deny"] != false { return Ok(restricted(knife_policy["authenticate"] == true)); }
467 if let Some(uri) = knife_policy["uri"].as_str() {
468 target = match &app.internal {
469 Some((base, _)) => format!("{base}services/{service}{uri}"),
470 None => format!("http://{upstream}{uri}"),
471 };
472 }
473 if knife_policy["rawDiff"] == true && let Some(diff) = knife_policy["safeDiff"].as_str() {
474 return Ok(([("content-type", "text/plain; charset=utf-8"), ("cache-control", "private, no-store"), ("vary", "Cookie, Authorization")], diff.to_owned()).into_response());
475 }
476 }
382 let profiles = profiles(&app.auth)?;477 let profiles = profiles(&app.auth)?;
383 if matches!(parts.method, Method::GET | Method::HEAD)478 if matches!(parts.method, Method::GET | Method::HEAD)
384 && let Some(url) = account_path(uri.path()).and_then(|name| profiles.get(name)).and_then(|profile| profile.url.as_deref())479 && let Some(url) = account_path(uri.path()).and_then(|name| profiles.get(name)).and_then(|profile| profile.url.as_deref())
...@@ -393,10 +488,6 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {...@@ -393,10 +488,6 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
393 )488 )
394 .into_response());489 .into_response());
395 }490 }
396 let target = match &app.internal {
397 Some((base, _)) => format!("{base}services/{service}{uri}"),
398 None => format!("http://{upstream}{uri}"),
399 };
400 if app.internal.is_some() {491 if app.internal.is_some() {
401 parts.headers.remove("host");492 parts.headers.remove("host");
402 }493 }
...@@ -424,9 +515,17 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {...@@ -424,9 +515,17 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
424 .body(reqwest::Body::wrap_stream(body.into_data_stream()))515 .body(reqwest::Body::wrap_stream(body.into_data_stream()))
425 .send()516 .send()
426 .await?;517 .await?;
427 let status = upstream_response.status();518 let status = upstream_response.headers().get("status")
519 .and_then(|value| value.to_str().ok()).and_then(|value| value.split_whitespace().next())
520 .and_then(|value| value.parse::<u16>().ok()).and_then(|value| StatusCode::from_u16(value).ok())
521 .unwrap_or(upstream_response.status());
428 let mut headers = upstream_response.headers().clone();522 let mut headers = upstream_response.headers().clone();
429 strip_hop_headers(&mut headers);523 strip_hop_headers(&mut headers);
524 headers.remove("status");
525 if knife {
526 headers.insert("cache-control", "private, no-store".parse().unwrap());
527 headers.insert("vary", "Cookie, Authorization".parse().unwrap());
528 }
430 let html = parts.method != Method::HEAD529 let html = parts.method != Method::HEAD
431 && headers530 && headers
432 .get("content-type")531 .get("content-type")
...@@ -482,6 +581,14 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {...@@ -482,6 +581,14 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
482 profiles.insert(name.clone(), profile);581 profiles.insert(name.clone(), profile);
483 }582 }
484 }583 }
584 let redacted;
585 let text = if knife && knife_policy["allowed"] != true {
586 redacted = match redact_knife(text, &knife_policy) {
587 Ok(html) => html,
588 Err(_) => return Ok(restricted(false)),
589 };
590 redacted.as_str()
591 } else { text };
485 let rewritten = rewrite(text, &origin, &profiles, &avatars, metadata["openIssues"].as_u64())?;592 let rewritten = rewrite(text, &origin, &profiles, &avatars, metadata["openIssues"].as_u64())?;
486 for name in [593 for name in [
487 "content-length",594 "content-length",
...@@ -498,6 +605,9 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {...@@ -498,6 +605,9 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
498 return Ok((status, headers, rewritten).into_response());605 return Ok((status, headers, rewritten).into_response());
499 }606 }
500 }607 }
608 if knife && knife_policy["allowed"] != true && (html || parts.method != Method::HEAD && !status.is_redirection() && knife_policy["htmlOnly"] == true) {
609 return Ok(restricted(false));
610 }
501 let stream = futures::stream::once(async move { Ok::<_, reqwest::Error>(Bytes::from(prefix)) })611 let stream = futures::stream::once(async move { Ok::<_, reqwest::Error>(Bytes::from(prefix)) })
502 .chain(upstream_response.bytes_stream());612 .chain(upstream_response.bytes_stream());
503 Ok((status, headers, Body::from_stream(stream)).into_response())613 Ok((status, headers, Body::from_stream(stream)).into_response())
...@@ -509,6 +619,20 @@ mod tests {...@@ -509,6 +619,20 @@ mod tests {
509 use rusqlite::params as sql;619 use rusqlite::params as sql;
510 use scraper::{Html, Selector};620 use scraper::{Html, Selector};
511621
622 #[test]
623 fn native_content_is_removed_from_both_diff_styles_and_history_rows() {
624 let policy = json!({"commits":["deadbeef"],"safeDiff":"PUBLIC SOURCE\nThe Knife's Native UI is restricted\n"});
625 let html = "<html><body id=page-commit><table><tr><td><a href='/the-knife/commit/deadbeef'>PRIVATE MESSAGE</a></td></tr><tr><td>PUBLIC ROW</td></tr></table><div id=m-code><div id=m-changedfiles>PRIVATE PATHS</div><details class=m-usa-accordion><summary><code>knife/private.rs</code></summary><table><tr><td>PRIVATE SOURCE</td></tr></table></details><details class=m-usa-accordion><summary><code>src/public.rs</code></summary><table><tr><td>PUBLIC SOURCE</td></tr></table></details><details class=m-usa-accordion><summary><code>nested/Cargo.lock</code></summary><table><tr><td>PRIVATE LOCK</td></tr></table></details></div></body></html>";
626 let output = redact_knife(html, &policy).unwrap();
627 assert!(!output.contains("PRIVATE"), "{output}");
628 assert!(output.contains("PUBLIC ROW"), "{output}");
629 assert!(output.contains("PUBLIC SOURCE"), "{output}");
630 assert_eq!(output.matches("The Knife's Native UI is restricted").count(), 1);
631 assert!(redact_knife("<div>unknown diff markup</div>", &policy).is_err());
632 for path in ["/the-knife/tree/main/knife", "/THE-KNIFE.git/info/refs", "/%74he-knife/plain/main/x", "/%2574he-knife/tree/main/x", "//the-knife/tree/main/x"] { assert!(knife_uri(path), "{path}"); }
633 assert!(!knife_uri("/snowbound/commit/native"));
634 }
635
512 #[test]636 #[test]
513 fn persisted_atproto_profiles_enrich_shale_authors_and_account_links() {637 fn persisted_atproto_profiles_enrich_shale_authors_and_account_links() {
514 let path = std::env::temp_dir().join(format!("shale-atproto-test-{}", uuid::Uuid::new_v4()));638 let path = std::env::temp_dir().join(format!("shale-atproto-test-{}", uuid::Uuid::new_v4()));
dashboard/src/users.rs+9-3
...@@ -158,10 +158,13 @@ pub async fn route(...@@ -158,10 +158,13 @@ pub async fn route(
158 for id in &users {158 for id in &users {
159 uuid(id)?;159 uuid(id)?;
160 let user = auth::user(&transaction, id)?;160 let user = auth::user(&transaction, id)?;
161 if guest::is_guest(&user) || user["kind"] == "ai" {161 let guest_groups = if guest::is_guest(&user) {
162 add.iter().chain(&remove).any(|group| transaction.query_row("SELECT name != 'knife-native' FROM roles WHERE id=?", [group], |r| r.get::<_,bool>(0)).unwrap_or(true))
163 } else { false };
164 if guest_groups || user["kind"] == "ai" {
162 return Err(Error::new(165 return Err(Error::new(
163 400,166 400,
164 "Guests can use Shale only. Select regular accounts to edit groups.",167 "Guests can only join knife-native.",
165 ));168 ));
166 }169 }
167 for group in &add {170 for group in &add {
...@@ -292,7 +295,7 @@ pub async fn route(...@@ -292,7 +295,7 @@ pub async fn route(
292 let transaction = db.transaction()?;295 let transaction = db.transaction()?;
293 let mut user = auth::user(&transaction, id)?;296 let mut user = auth::user(&transaction, id)?;
294 if guest::is_guest(&user)297 if guest::is_guest(&user)
295 && matches!(parts, [_, "password"] | [_, "groups", _])298 && matches!(parts, [_, "password"])
296 && method != Method::GET299 && method != Method::GET
297 {300 {
298 return Err(Error::new(301 return Err(Error::new(
...@@ -342,6 +345,9 @@ pub async fn route(...@@ -342,6 +345,9 @@ pub async fn route(
342 .optional()?;345 .optional()?;
343 let name = name346 let name = name
344 .ok_or_else(|| Error::new(404, "This group no longer exists. Reload the page."))?;347 .ok_or_else(|| Error::new(404, "This group no longer exists. Reload the page."))?;
348 if guest::is_guest(&user) && name != "knife-native" {
349 return Err(Error::new(400, "Guests can only join knife-native."));
350 }
345 if own && method == Method::DELETE && name == "infra-admin" {351 if own && method == Method::DELETE && name == "infra-admin" {
346 return Err(Error::new(352 return Err(Error::new(
347 400,353 400,
dashboard/web/pages/Users.tsx+2-1
...@@ -68,6 +68,7 @@ function adds(group: string, services: ServiceSummary[]) {...@@ -68,6 +68,7 @@ function adds(group: string, services: ServiceSummary[]) {
6868
69/** "30 members · opens Jellyfin, Navidrome" */69/** "30 members · opens Jellyfin, Navidrome" */
70function groupTip(group: string, d: Data) {70function groupTip(group: string, d: Data) {
71 if (group === "knife-native") return `${plural(d.users.filter((user) => names(user).includes(group)).length, "member")} · opens The Knife’s Native UI`;
71 const more = adds(group, d.services);72 const more = adds(group, d.services);
72 const members = d.users.filter((user) => names(user).includes(group)).length;73 const members = d.users.filter((user) => names(user).includes(group)).length;
73 const metrics = sectionsOf([group]).includes("metrics") ? ["machine metrics"] : [];74 const metrics = sectionsOf([group]).includes("metrics") ? ["machine metrics"] : [];
...@@ -575,7 +576,7 @@ function Profile(props: { user: User; data: Data; refetch: () => unknown }) {...@@ -575,7 +576,7 @@ function Profile(props: { user: User; data: Data; refetch: () => unknown }) {
575 <div class="toggles" role="group" aria-label="Groups">576 <div class="toggles" role="group" aria-label="Groups">
576 <For each={props.data.groups}>577 <For each={props.data.groups}>
577 {(group) => (578 {(group) => (
578 <button class="chip toggle" aria-pressed={member(group)} disabled={props.user.kind === "guest" || busy() === group.id}579 <button class="chip toggle" aria-pressed={member(group)} disabled={props.user.kind === "guest" && group.name !== "knife-native" || busy() === group.id}
579 data-tip={groupTip(group.name, props.data)} onClick={() => toggleGroup(group)}>580 data-tip={groupTip(group.name, props.data)} onClick={() => toggleGroup(group)}>
580 {group.name}581 {group.name}
581 </button>582 </button>
flake.nix+1
...@@ -5,6 +5,7 @@...@@ -5,6 +5,7 @@
55
6 outputs = { self, nixpkgs, ... }: {6 outputs = { self, nixpkgs, ... }: {
7 packages.x86_64-linux.dashboard = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/dashboard.nix { };7 packages.x86_64-linux.dashboard = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/dashboard.nix { };
8 packages.x86_64-linux.host-python = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/host-python.nix { };
8 packages.x86_64-linux.dashboard-image = self.packages.x86_64-linux.dashboard.image;9 packages.x86_64-linux.dashboard-image = self.packages.x86_64-linux.dashboard.image;
9 packages.x86_64-linux.installer = self.nixosConfigurations.installer.config.system.build.isoImage;10 packages.x86_64-linux.installer = self.nixosConfigurations.installer.config.system.build.isoImage;
1011
nixos/configuration.nix+3-2
...@@ -6,9 +6,10 @@ let...@@ -6,9 +6,10 @@ let
6 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";6 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";
7 hostTools = lib.fileset.toSource {7 hostTools = lib.fileset.toSource {
8 root = ../.;8 root = ../.;
9 fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../config/shale.json ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../tools/namespace-transition.py ../dashboard/src/namespace-transition.json ../guest/protocol.json ];9 fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../tools/namespace-transition.py ../dashboard/src/namespace-transition.json ../guest/protocol.json ];
10 };10 };
11 nativePkl = pkgs.callPackage ./pkl.nix { };11 nativePkl = pkgs.callPackage ./pkl.nix { };
12 hostPython = pkgs.callPackage ./host-python.nix { };
12 screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);13 screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);
13 screenPlugins = (with pkgs.gst_all_1; [ (lib.getLib gstreamer) gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly gst-plugins-rs ]) ++ [ (lib.getLib pkgs.libnice) ];14 screenPlugins = (with pkgs.gst_all_1; [ (lib.getLib gstreamer) gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly gst-plugins-rs ]) ++ [ (lib.getLib pkgs.libnice) ];
14 secureOvmf = pkgs.OVMF.override {15 secureOvmf = pkgs.OVMF.override {
...@@ -195,7 +196,7 @@ in...@@ -195,7 +196,7 @@ in
195 SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables;196 SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables;
196 };197 };
197 serviceConfig = {198 serviceConfig = {
198 ExecStart = "${pkgs.python3}/bin/python3 ${hostTools}/tools/dashboard-host.py";199 ExecStart = "${hostPython}/bin/python3 ${hostTools}/tools/dashboard-host.py";
199 Type = "notify";200 Type = "notify";
200 Group = "snowglobe-dashboard";201 Group = "snowglobe-dashboard";
201 StateDirectory = "snowglobe/host";202 StateDirectory = "snowglobe/host";
nixos/host-python.nix created+2
...@@ -0,0 +1,2 @@
1{ python3 }:
2python3.withPackages (packages: [ packages.pygit2 packages.argon2-cffi ])
tools/dashboard-host.py+3
...@@ -92,6 +92,7 @@ class Host:...@@ -92,6 +92,7 @@ class Host:
92 "index.diff": {"dataset", "from", "to"},92 "index.diff": {"dataset", "from", "to"},
93 "index.discard": {"dataset", "snapshot"},93 "index.discard": {"dataset", "snapshot"},
94 "shale.metadata": {"repository", "accounts"},94 "shale.metadata": {"repository", "accounts"},
95 "shale.access": {"service", "uri", "account", "authorization"},
95 "shale.write": {"grant", "repository", "action", "arguments"},96 "shale.write": {"grant", "repository", "action", "arguments"},
96 "host.sample": set(),97 "host.sample": set(),
97 "host.usage": {"refresh"},98 "host.usage": {"refresh"},
...@@ -105,6 +106,8 @@ class Host:...@@ -105,6 +106,8 @@ class Host:
105 raise Rejected("Use only the fields required by this host operation.")106 raise Rejected("Use only the fields required by this host operation.")
106 if operation == "copyparty.manage":107 if operation == "copyparty.manage":
107 return copyparty.handle(request)108 return copyparty.handle(request)
109 if operation == "shale.access":
110 return importlib.import_module("shale-access").access(request)
108 if operation == "shale.metadata":111 if operation == "shale.metadata":
109 return shale_labels.metadata(request)112 return shale_labels.metadata(request)
110 if operation == "shale.write":113 if operation == "shale.write":
tools/dashboard-stage.py+12-2
...@@ -18,6 +18,7 @@ import snowglobe...@@ -18,6 +18,7 @@ import snowglobe
1818
1919
20OPERATIONS = frozenset({20OPERATIONS = frozenset({
21 "shale.access",
21 "host.sample", "host.usage", "storage.mounts", "storage.datasets", "storage.pool",22 "host.sample", "host.usage", "storage.mounts", "storage.datasets", "storage.pool",
22 "storage.snapshots", "vm.node", "vm.domains", "vm.library", "deploy.current", "deploy.main", "deploy.history",23 "storage.snapshots", "vm.node", "vm.domains", "vm.library", "deploy.current", "deploy.main", "deploy.history",
23 "deploy.stages", "deploy.managed", "deploy.release", "deploy.last",24 "deploy.stages", "deploy.managed", "deploy.release", "deploy.last",
...@@ -34,6 +35,14 @@ class PreviewHost(boundary.Host):...@@ -34,6 +35,14 @@ class PreviewHost(boundary.Host):
3435
35 def handle(self, request):36 def handle(self, request):
36 operation = request.get("operation") if isinstance(request, dict) else None37 operation = request.get("operation") if isinstance(request, dict) else None
38 if operation == "shale.access":
39 service = request.get("service", "")
40 if not re.fullmatch(r"shale-preview-[0-9a-f]{8}", service):
41 raise boundary.dashboard_runs.Error(403, "Use a Shale preview with this dashboard.")
42 linked = json.loads((snowglobe.STATE / "stages" / (service + ".json")).read_text())
43 if linked.get("sourceId") != "shale" or linked.get("gatewayStage") != Path(self.stage["mount"]).name:
44 raise boundary.dashboard_runs.Error(403, "Use the Shale preview linked to this dashboard.")
45 return import_module("shale-access").access(request, accounts=Path(self.stage["mount"]) / "data")
37 if operation in {"files.snapshot", "files.snapshots", "files.discard"}:46 if operation in {"files.snapshot", "files.snapshots", "files.discard"}:
38 datasets = request.get("datasets") if operation != "files.discard" else [request.get("dataset")]47 datasets = request.get("datasets") if operation != "files.discard" else [request.get("dataset")]
39 if not isinstance(datasets, list) or datasets != [self.stage["clone"] + "/files"]:48 if not isinstance(datasets, list) or datasets != [self.stage["clone"] + "/files"]:
...@@ -159,11 +168,12 @@ def stage():...@@ -159,11 +168,12 @@ def stage():
159 runtime = "/run/snowglobe-preview/" + identity168 runtime = "/run/snowglobe-preview/" + identity
160 host_environment = snowglobe.transition.host_environment()169 host_environment = snowglobe.transition.host_environment()
161 host_path = next(value for value in shlex.split(host_environment) if value.startswith("PATH="))170 host_path = next(value for value in shlex.split(host_environment) if value.startswith("PATH="))
171 host_python = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip()
162 snowglobe.transition.stop_preview(identity)172 snowglobe.transition.stop_preview(identity)
163 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity,173 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity,
164 "--property=Restart=on-failure", "--property=MemoryMax=256M", "--property=TasksMax=32",174 "--property=Restart=on-failure", "--property=MemoryMax=256M", "--property=TasksMax=32",
165 "--setenv=" + host_path,175 "--setenv=" + host_path, "--setenv=SNOWGLOBE_DOMAIN=" + site["domain"],
166 sys.executable, str(Path(__file__).resolve()), "--serve", identity)176 host_python + "/bin/python3", str(Path(__file__).resolve()), "--serve", identity)
167 for _ in range(100):177 for _ in range(100):
168 if Path(runtime, "host.sock").exists():178 if Path(runtime, "host.sock").exists():
169 break179 break
tools/release.py+1-1
...@@ -63,7 +63,7 @@ def host_digest(root):...@@ -63,7 +63,7 @@ def host_digest(root):
63 digest = hashlib.sha256()63 digest = hashlib.sha256()
64 paths = sorted(path for path, relative in files(root)64 paths = sorted(path for path, relative in files(root)
65 if relative.parts[0] in {"nixos", "dashboard", "config", "service"}65 if relative.parts[0] in {"nixos", "dashboard", "config", "service"}
66 or str(relative) in {"flake.nix", "flake.lock", "tools/router.py", "tools/dashboard-host.py", "tools/dashboard-run.py", "tools/release.py", "tools/namespace-transition.py", "tools/vms.py", "tools/mac-vms.py", "tools/vm-screen.py", "guest/protocol.json"})66 or str(relative) in {"flake.nix", "flake.lock", "tools/router.py", "tools/dashboard-host.py", "tools/shale-access.py", "tools/shale-labels.py", "tools/dashboard-run.py", "tools/release.py", "tools/namespace-transition.py", "tools/vms.py", "tools/mac-vms.py", "tools/vm-screen.py", "guest/protocol.json"})
67 for path in paths:67 for path in paths:
68 digest.update(str(path.relative_to(root)).encode())68 digest.update(str(path.relative_to(root)).encode())
69 _hash_contents(digest, path)69 _hash_contents(digest, path)
tools/router.py+1-14
...@@ -89,18 +89,6 @@ def shale_write_routes(host):...@@ -89,18 +89,6 @@ def shale_write_routes(host):
89 " }", ' respond @shale_unsafe_origin "Forbidden" 403']89 " }", ' respond @shale_unsafe_origin "Forbidden" 403']
9090
9191
92def shale_git_routes(upstreams):
93 lines = [" @shale_git path */info/refs */git-upload-pack */git-receive-pack",
94 " handle @shale_git {", f" reverse_proxy {upstreams} {{"]
95 # Shale forwards CGI Status as a header instead of the HTTP status.
96 for status in (400, 403, 404, 405, 415, 500):
97 lines += [f' @cgi_{status} header Status "{status} *"',
98 f" handle_response @cgi_{status} {{",
99 " header {", " -Status", " defer", " }",
100 f" copy_response {status}", " }"]
101 return [*lines, " }", " }"]
102
103
104def shale_login_routes(host):92def shale_login_routes(host):
105 # Shale parses the login Referer as an absolute URL after OAuth.93 # Shale parses the login Referer as an absolute URL after OAuth.
106 return_url = rf"^https://{re.escape(host)}/(?:[A-Za-z0-9._~!$&'()*+,;=:@/?#\[\]-]|%[0-9A-Fa-f]{{2}})*$"94 return_url = rf"^https://{re.escape(host)}/(?:[A-Za-z0-9._~!$&'()*+,;=:@/?#\[\]-]|%[0-9A-Fa-f]{{2}})*$"
...@@ -304,8 +292,7 @@ def render(token):...@@ -304,8 +292,7 @@ def render(token):
304 # Keep the Origin guard before every static/proxy handle;292 # Keep the Origin guard before every static/proxy handle;
305 # otherwise Caddy sorts those handles ahead of respond.293 # otherwise Caddy sorts those handles ahead of respond.
306 lines += [" route {", *shale_write_routes(host),294 lines += [" route {", *shale_write_routes(host),
307 *shale_login_routes(host),295 *shale_login_routes(host)]
308 *shale_git_routes(" ".join(sorted(route["upstreams"])))]
309 if re.fullmatch(r"shale-preview-[0-9a-f]{8}", service):296 if re.fullmatch(r"shale-preview-[0-9a-f]{8}", service):
310 lines += shale_attachment_routes(" ".join(sorted(route["upstreams"])),297 lines += shale_attachment_routes(" ".join(sorted(route["upstreams"])),
311 "/srv/staging/" + service)298 "/srv/staging/" + service)
tools/shale-access-test.py created+140
...@@ -0,0 +1,140 @@
1#!/usr/bin/env python3
2import base64
3import importlib
4import json
5import os
6from pathlib import Path
7import sqlite3
8import tempfile
9import unittest
10from unittest.mock import patch
11
12import pygit2
13from argon2 import PasswordHasher
14
15shale = importlib.import_module('shale-access')
16
17
18class Access(unittest.TestCase):
19 def setUp(self):
20 self.temp = tempfile.TemporaryDirectory()
21 self.addCleanup(self.temp.cleanup)
22 self.root = Path(self.temp.name)
23 self.env = patch.dict(os.environ, {'SNOWGLOBE_SHALE_ROOT': str(self.root), 'SNOWGLOBE_DASHBOARD_DATA': str(self.root), 'SNOWGLOBE_SHALE_POLICY': str(self.root / 'policy.json'), 'SNOWGLOBE_DOMAIN': 'example.test'})
24 self.env.start()
25 self.addCleanup(self.env.stop)
26 (self.root / 'data').mkdir()
27 self.uuid = '0' * 26
28 self.repo = pygit2.init_repository(str(self.root / 'repositories_owned' / self.uuid), bare=True)
29 self.base = self.commit({'README.md': 'PUBLIC', 'Cargo.lock': 'SECRET LOCK'})
30 self.native = self.commit({'knife/private.rs': 'SECRET NATIVE'}, parents=[])
31 self.repo.create_reference('refs/heads/native', self.native)
32 self.public = self.commit({'README.md': 'PUBLIC NEXT', 'knife/private.rs': 'SECRET MERGED', 'nested/Cargo.lock': 'SECRET LOCK', 'src/public.rs': 'PUBLIC CODE'}, parents=[self.base, self.native])
33 self.repo.create_reference('refs/heads/main', self.public)
34 self.repo.set_head('refs/heads/main')
35 (self.root / 'policy.json').write_text(json.dumps({'nativeRestriction': {'commits': [str(self.native)]}}))
36 with sqlite3.connect(self.root / 'accounts.sqlite') as db:
37 db.executescript('CREATE TABLE users(id TEXT,profile TEXT); CREATE TABLE roles(id TEXT,name TEXT); CREATE TABLE memberships(user_id TEXT,role_id TEXT); INSERT INTO roles VALUES(\'access\',\'knife-native\');')
38 for name in ('snow', 'guest', 'stranger', 'disabled', 'ai'):
39 db.execute('INSERT INTO users VALUES(?,?)', (name, json.dumps({'enabled': name != 'disabled', 'kind': 'ai' if name == 'ai' else 'guest' if name == 'guest' else 'user', 'requiredActions': []})))
40 if name != 'stranger': db.execute("INSERT INTO memberships VALUES(?,'access')", (name,))
41 with sqlite3.connect(self.root / 'data/astheno.shale.db') as db:
42 db.executescript('CREATE TABLE repositories(uuid TEXT,name TEXT); CREATE TABLE users(id INTEGER,name TEXT,provider TEXT,snowflake TEXT); CREATE TABLE personal_tokens(uuid TEXT,agent INTEGER,secret TEXT);')
43 db.execute("INSERT INTO repositories VALUES(?,'the-knife')", (self.uuid,))
44 for i, name in enumerate(('snow', 'guest', 'stranger', 'disabled', 'ai')):
45 db.execute('INSERT INTO users VALUES(?,?,?,?)', (i, name, 'snowglobe.example.test', name))
46 db.execute('INSERT INTO personal_tokens VALUES(?,?,?)', ('fixture-token', 0, PasswordHasher().hash('private-fixture-secret')))
47
48 def commit(self, files, parents=None):
49 index = pygit2.Index()
50 for path, content in files.items():
51 index.add(pygit2.IndexEntry(path, self.repo.create_blob(content.encode()), pygit2.GIT_FILEMODE_BLOB))
52 tree = index.write_tree(self.repo)
53 author = pygit2.Signature('Fixture', 'fixture@example.invalid')
54 return self.repo.create_commit(None, author, author, 'Fixture', tree, parents or [])
55
56 def access(self, path, account='', authorization=''):
57 return shale.access({'service': 'shale', 'uri': path, 'account': account, 'authorization': authorization})
58
59 def test_public_tree_and_original_native_history_stays_restricted_after_merge(self):
60 public = self.access('/the-knife/tree/main/')
61 self.assertFalse(public['deny'])
62 self.assertIn(str(self.public), public['uri'])
63 for rev in ('native', str(self.native), str(self.native)[:12]):
64 for route in ('tree', 'plain', 'commit'):
65 self.assertTrue(self.access(f'/the-knife/{route}/{rev}/')['deny'], (route, rev))
66 self.assertTrue(self.access('/the-knife/log/?from=' + rev)['deny'])
67 self.repo.references.delete('refs/heads/native')
68 self.assertTrue(self.access('/the-knife/commit/' + str(self.native))['deny'])
69
70 def test_every_revision_blocks_files_even_when_absent(self):
71 for rev in ('main', str(self.base)):
72 for route in ('tree', 'plain'):
73 for path in ('knife', 'knife/private.rs', 'Cargo.lock', 'nested/Cargo.lock', '%6bnife/private.rs', '%256bnife/private.rs'):
74 self.assertTrue(self.access(f'/the-knife/{route}/{rev}/{path}')['deny'], (route, rev, path))
75 self.assertFalse(self.access('/the-knife/plain/main/src/public.rs')['deny'])
76
77 def test_diff_redaction_covers_both_merge_parents_and_raw_formats_fail_closed(self):
78 result = self.access('/the-knife/commit/' + str(self.public))
79 self.assertFalse(result['deny'])
80 self.assertNotIn('SECRET', result['safeDiff'])
81 self.assertIn('PUBLIC', result['safeDiff'])
82 for suffix in ('diff', 'patch'):
83 result = self.access('/the-knife/commit/' + str(self.public) + '.' + suffix)
84 self.assertFalse(result['deny'])
85 self.assertNotIn('SECRET', result['safeDiff'])
86 self.assertIn('PUBLIC', result['safeDiff'])
87 for suffix in ('stat', 'diffraw'):
88 self.assertTrue(self.access('/the-knife/commit/' + str(self.public) + '.' + suffix)['deny'])
89
90 def test_only_enabled_explicit_members_can_read_and_revocation_is_immediate(self):
91 path = '/the-knife/plain/native/knife/private.rs'
92 for name in ('snow', 'guest'):
93 self.assertTrue(self.access(path, name)['allowed'])
94 for name in ('stranger', 'disabled', 'ai', ''):
95 self.assertTrue(self.access(path, name)['deny'])
96 with sqlite3.connect(self.root / 'accounts.sqlite') as db:
97 db.execute("DELETE FROM memberships WHERE user_id='snow'")
98 self.assertTrue(self.access(path, 'snow')['deny'])
99
100 def test_git_requires_a_valid_live_token_even_with_an_allowed_browser(self):
101 path = '/the-knife/info/refs?service=git-upload-pack'
102 self.assertTrue(self.access(path, 'snow')['deny'])
103 token = 'Basic ' + base64.b64encode(b'snow:private-fixture-secret').decode()
104 self.assertTrue(self.access(path, authorization=token)['allowed'])
105 self.assertTrue(self.access(path, 'snow', 'Basic ' + base64.b64encode(b'snow:incorrect').decode())['deny'])
106 with sqlite3.connect(self.root / 'data/astheno.shale.db') as db:
107 db.execute('DELETE FROM personal_tokens')
108 self.assertTrue(self.access(path, authorization=token)['deny'])
109
110 def test_native_push_during_classification_cannot_change_the_checked_revision(self):
111 newer = self.commit({'README.md': 'RESTRICTED NEW COMMIT'}, parents=[self.native])
112 repo = self.repo
113 class ConcurrentPush:
114 def __getattr__(self, name): return getattr(repo, name)
115 def __getitem__(self, key): return repo[key]
116 def walk(self, *args):
117 repo.lookup_reference('refs/heads/native').set_target(newer)
118 repo.create_reference('refs/heads/deadbeef', newer, force=True)
119 return repo.walk(*args)
120 for revision in ('native', 'deadbeef'):
121 repo.lookup_reference('refs/heads/native').set_target(self.native)
122 if 'refs/heads/deadbeef' in repo.references:
123 repo.references.delete('refs/heads/deadbeef')
124 with patch.object(shale.pygit2, 'Repository', return_value=ConcurrentPush()):
125 self.assertTrue(self.access('/the-knife/tree/' + revision + '/README.md')['deny'])
126
127 def test_duplicate_names_cannot_impersonate_an_allowlisted_identity(self):
128 with sqlite3.connect(self.root / 'data/astheno.shale.db') as db:
129 db.execute("INSERT INTO users VALUES(42,'snow','other-provider','stranger')")
130 self.assertTrue(self.access('/the-knife/commit/native', 'snow')['deny'])
131 token = 'Basic ' + base64.b64encode(b'snow:private-fixture-secret').decode()
132 self.assertTrue(self.access('/the-knife.git/info/refs', authorization=token)['deny'])
133
134 def test_unknown_formats_search_archives_object_endpoints_and_duplicate_queries_close(self):
135 for path in ('/the-knife/search/main/?q=SECRET', '/the-knife/filesearch/main/', '/the-knife/archive/main.zip', '/the-knife/objects/x', '/the-knife.git/info/refs', '/THE-KNIFE/tree/main/', '/the-knife/commit/main?diff=unified&diff=split', '/the-knife/tree/main/?raw=1'):
136 self.assertTrue(self.access(path)['deny'], path)
137
138
139if __name__ == '__main__':
140 unittest.main()
tools/shale-access.py created+176
...@@ -0,0 +1,176 @@
1import base64
2from contextlib import closing
3import importlib
4import json
5import os
6from pathlib import Path
7import posixpath
8import re
9import sqlite3
10import subprocess
11from urllib.parse import parse_qsl, quote, unquote, urlencode, urlsplit
12
13import pygit2
14from argon2 import PasswordHasher
15from argon2.exceptions import VerificationError, InvalidHashError
16
17Error = importlib.import_module('dashboard-run').Error
18# The root host helper reads repositories owned by the Shale service UID.
19pygit2.option(pygit2.GIT_OPT_SET_OWNER_VALIDATION, 0)
20
21
22def restricted_path(path):
23 parts = path.split('/')
24 return parts[0] == 'knife' or 'Cargo.lock' in parts
25
26
27def allowed_account(db, user_id):
28 row = db.execute("SELECT a.profile FROM users u JOIN auth.users a ON a.id=u.snowflake "
29 "WHERE u.id=? AND u.provider=? AND EXISTS(SELECT 1 FROM auth.memberships m "
30 "JOIN auth.roles r ON r.id=m.role_id WHERE m.user_id=a.id AND r.name='knife-native')",
31 (user_id, 'snowglobe.' + os.environ['SNOWGLOBE_DOMAIN'])).fetchone()
32 if not row:
33 return False
34 profile = json.loads(row[0])
35 return (profile.get('enabled') is True and not profile.get('requiredActions')
36 and profile.get('kind') != 'ai')
37
38
39def access(request, accounts=None):
40 service = request['service']
41 if service != 'shale' and not re.fullmatch(r'shale-preview-[0-9a-f]{8}', service):
42 raise Error(400, 'Choose a Shale instance.')
43 if fixture_root := os.environ.get('SNOWGLOBE_SHALE_ROOT'):
44 root = Path(fixture_root)
45 else:
46 site = json.loads(subprocess.check_output(['pkl', 'eval', '-f', 'json', str(Path(__file__).resolve().parent.parent / 'config/site.pkl')], text=True, timeout=5))
47 root = Path(site['root']) / ('prod/shale' if service == 'shale' else 'staging/' + service)
48 uri = urlsplit(request['uri'])
49 path = uri.path
50 for _ in range(4):
51 decoded = unquote(path, errors='strict')
52 if decoded == path:
53 break
54 path = decoded
55 path = posixpath.normpath('/' + path.lstrip('/'))
56 parts = path.strip('/').split('/')
57 if parts[0].lower() not in ('the-knife', 'the-knife.git'):
58 return {'allowed': False, 'deny': True}
59 git = parts[0].lower().endswith('.git') or len(parts) > 1 and parts[1] in ('info', 'objects', 'git-upload-pack', 'git-receive-pack', 'HEAD')
60 with closing(sqlite3.connect((root / 'data/astheno.shale.db').as_uri() + '?mode=ro', uri=True, timeout=2)) as db:
61 accounts = Path(accounts or os.environ.get('SNOWGLOBE_DASHBOARD_DATA', '/var/lib/snowglobe/dashboard'))
62 db.execute('ATTACH DATABASE ? AS auth', ((accounts / 'accounts.sqlite').as_uri() + '?mode=ro',))
63 authorization = request['authorization']
64 allowed = False
65 if authorization:
66 try:
67 scheme, value = authorization.split(' ', 1)
68 if scheme.lower() == 'basic':
69 name, secret = base64.b64decode(value, validate=True).decode().split(':', 1)
70 users = db.execute('SELECT id FROM users WHERE name=?', (name,)).fetchall()
71 if len(users) == 1 and allowed_account(db, users[0][0]):
72 for (hashed,) in db.execute('SELECT secret FROM personal_tokens WHERE agent=?', users[0]):
73 try:
74 if PasswordHasher().verify(hashed, secret):
75 allowed = True
76 break
77 except (VerificationError, InvalidHashError):
78 pass
79 except (ValueError, UnicodeError):
80 pass
81 elif request['account'] and not git:
82 users = db.execute('SELECT id FROM users WHERE name=?', (request['account'],)).fetchall()
83 allowed = len(users) == 1 and allowed_account(db, users[0][0])
84 if allowed:
85 return {'allowed': True, 'deny': False}
86 row = db.execute("SELECT uuid FROM repositories WHERE name='the-knife'").fetchone()
87 if not row or not re.fullmatch(r'[A-Z0-9]{26}', row[0]):
88 return {'allowed': False, 'deny': True}
89 repo = pygit2.Repository(str(root / 'repositories_owned' / row[0]))
90 policy = json.loads(Path(os.environ.get('SNOWGLOBE_SHALE_POLICY', str(Path(__file__).resolve().parent.parent / 'config/shale.json'))).read_text())['nativeRestriction']
91 head = repo.lookup_reference('HEAD').target
92 references = {name: repo.lookup_reference(name).resolve().target for name in repo.references}
93 protected = set()
94 tips = [repo[tip].peel(pygit2.Commit).id for tip in policy['commits']]
95 if 'refs/heads/native' in references:
96 tips.append(references['refs/heads/native'])
97 for tip in tips:
98 protected.update(str(commit.id) for commit in repo.walk(tip))
99 result = {'allowed': False, 'deny': True, 'commits': sorted(protected), 'htmlOnly': True, 'authenticate': git and not authorization}
100 if parts[0] != 'the-knife' or '%' in path or '\\' in path:
101 return result
102 route = parts[1] if len(parts) > 1 else ''
103 query = dict(parse_qsl(uri.query, keep_blank_values=True))
104 if len(query) != len(parse_qsl(uri.query, keep_blank_values=True)):
105 return result
106 if route in ('', 'issues'):
107 if route == '' and query:
108 return result
109 result['htmlOnly'] = route != 'issues'
110 result['deny'] = False
111 return result
112 if route in ('heads', 'tags') and len(parts) == 2 and not query:
113 result['deny'] = False
114 return result
115 if route in ('log', 'commit') and len(parts) != (2 if route == 'log' else 3):
116 return result
117 if route not in ('log', 'tree', 'plain', 'commit'):
118 return result
119 revision = query.get('from', '-') if route == 'log' else parts[2] if len(parts) > 2 else '-'
120 suffix = ''
121 if route == 'commit':
122 revision, dot, suffix = revision.partition('.')
123 if dot and suffix not in ('diff', 'patch', 'stat', 'diffraw'):
124 return result
125 if not revision or revision == '-':
126 revision = 'HEAD'
127 # Only named refs and object IDs have the same meaning in Shale and libgit2.
128 if not re.fullmatch(r'[A-Za-z0-9_/-]+', revision):
129 return result
130 try:
131 if revision == 'HEAD':
132 oid = references.get(head) if isinstance(head, str) else head
133 else:
134 candidates = {references[name] for name in (revision, 'refs/heads/' + revision, 'refs/tags/' + revision) if name in references}
135 if len(candidates) > 1:
136 return result
137 if candidates:
138 oid = candidates.pop()
139 elif re.fullmatch(r'[0-9a-fA-F]{4,40}', revision):
140 oid = repo[revision].id
141 else:
142 return result
143 if oid is None:
144 return result
145 commit = repo[oid].peel(pygit2.Commit)
146 except (KeyError, ValueError, pygit2.GitError):
147 return result
148 if str(commit.id) in protected:
149 return result
150 if route in ('tree', 'plain'):
151 result['htmlOnly'] = route != 'plain'
152 file = '/'.join(parts[3:])
153 if restricted_path(file):
154 return result
155 if query.keys() - ({'from', 'q'} if route == 'log' else {'diff'} if route == 'commit' else set()):
156 return result
157 if route == 'commit':
158 diffs = [repo.diff(parent.tree, commit.tree) for parent in commit.parents] if commit.parents else [commit.tree.diff_to_tree(swap=True)]
159 sensitive = any(restricted_path(delta.old_file.path) or restricted_path(delta.new_file.path) for diff in diffs for delta in diff.deltas)
160 if sensitive:
161 if suffix in ('stat', 'diffraw'):
162 return result
163 result['safeDiff'] = ''.join(patch.text for patch in diffs[0] if not restricted_path(patch.delta.old_file.path) and not restricted_path(patch.delta.new_file.path)) + "\nThe Knife's Native UI is restricted\n"
164 if suffix:
165 result['rawDiff'] = True
166 result['deny'] = False
167 return result
168 if route == 'log':
169 result['uri'] = '/the-knife/log/?' + urlencode({**query, 'from': str(commit.id)})
170 else:
171 parts[2:3] = [str(commit.id) + ('.' + suffix if suffix else '')]
172 result['uri'] = '/' + '/'.join(quote(part, safe='') for part in parts) + ('/' if uri.path.endswith('/') else '')
173 if query:
174 result['uri'] += '?' + urlencode(query)
175 result['deny'] = False
176 return result