forge
Explore
Log in

Don't you dare go Hollow

A private linked repo stays hidden from a viewer who cannot read it

Browse files at this commit

b20e52422e719f28e77d209936b233570b6ebd8fAhmet Kaan GümüşParent: 230dd1d
  • test
    • a private linked repo stays hidden from a viewer who cannot read it
Show full message
✅ test: a private linked repo stays hidden from a viewer who cannot read it

The team stream follows only the org's repos an owner sees, so a rename of a
private repo does not wake a non-owner whose page would not change.
crates/forge-web/src/pages/org_live.rsmodified+10 -5
@@ -12,6 +12,7 @@
12 12
13 use forge_core::User;13 use forge_core::User;
14 use forge_core::store::Store;14 use forge_core::store::Store;
15+use forge_core::store::orgs::OrgMemberRole;
15 use topcoat::Result;16 use topcoat::Result;
16 use topcoat::context::{Cx, app_context};17 use topcoat::context::{Cx, app_context};
17 use topcoat::router::request::{method, uri};18 use topcoat::router::request::{method, uri};
@@ -98,11 +99,15 @@ async fn team_fingerprint(
98 for repo in &linked {99 for repo in &linked {
99 lines.push(format!("linked {}", repo.id));100 lines.push(format!("linked {}", repo.id));
100 }101 }
101- for repo in store.list_repos_by_owner(org_id).await? {102+ // Only an owner sees the org's unlinked repos (as link choices), so only an owner's stream
102- lines.push(format!(103+ // follows them. A rename of a repo the viewer cannot read must not wake the viewer.
103- "owned {} {}/{}",104+ if store.org_member_role(org_id, &viewer.id).await? == Some(OrgMemberRole::Owner) {
104- repo.id, repo.owner_name, repo.name105+ for repo in store.list_repos_by_owner(org_id).await? {
105- ));106+ lines.push(format!(
107+ "owned {} {}/{}",
108+ repo.id, repo.owner_name, repo.name
109+ ));
110+ }
106 }111 }
107 Ok(lines.join("\n"))112 Ok(lines.join("\n"))
108 }113 }
crates/forge-web/tests/orgs_live_e2e.rsmodified+123 -2
@@ -8,8 +8,8 @@ use std::path::Path;
8 use std::process::{Command, Output};8 use std::process::{Command, Output};
9 use std::time::Duration;9 use std::time::Duration;
10 10
11-use forge_core::Store;11+use forge_core::store::orgs::{DefaultRepoRole, OrgMemberRole, OrgSettings};
12-use forge_core::store::orgs::OrgMemberRole;12+use forge_core::{Role, Store, Visibility};
13 use reqwest::header::{COOKIE, HeaderMap, LOCATION, SET_COOKIE};13 use reqwest::header::{COOKIE, HeaderMap, LOCATION, SET_COOKIE};
14 14
15 /// Each test starts its own server, so the tests run one at a time.15 /// Each test starts its own server, so the tests run one at a time.
@@ -376,3 +376,124 @@ async fn org_pages_follow_writes_live_and_refuse_outsiders() {
376 assert_eq!(res.status, 303, "owner adds dana: {}", res.body);376 assert_eq!(res.status, 303, "owner adds dana: {}", res.body);
377 next_changed(&mut bob_members, "adding dana").await;377 next_changed(&mut bob_members, "adding dana").await;
378 }378 }
379+
380+/// Creates a bare git repo for `repo_id` at its data-dir location, as the create-repo page does.
381+fn init_bare(data_dir: &Path, repo_id: &str) {
382+ let path = forge_git::repo::repo_path(data_dir, repo_id).expect("repo path");
383+ std::fs::create_dir_all(path.parent().expect("parent")).expect("create repo parent");
384+ let out = Command::new("git")
385+ .args(["init", "--bare", "--quiet"])
386+ .arg(&path)
387+ .output()
388+ .expect("run git init");
389+ assert!(out.status.success(), "git init --bare: {}", describe(&out));
390+}
391+
392+#[tokio::test]
393+async fn private_linked_repo_is_hidden_from_a_viewer_who_cannot_read_it() {
394+ let _guard = SERIAL.lock().await;
395+ if !tool_on_path("git") {
396+ eprintln!("SKIP orgs_live_e2e: `git` was not found on PATH");
397+ return;
398+ }
399+ let root = short_temp_dir();
400+ let dir = root.path().to_path_buf();
401+ let ports = pick_ports();
402+ let config = write_config(&dir, ports);
403+
404+ admin(&config, &["create-user", "tahinli", "--admin"]);
405+ admin(&config, &["create-user", "bob"]);
406+
407+ // Acme has a private repo linked to its team "devs". Bob is an org member who is not on the
408+ // team and has no default access, so he cannot read the repo. The owner can.
409+ let team_id = {
410+ let store = Store::open(&dir.join("forge.db"))
411+ .await
412+ .expect("open store");
413+ let tahinli = store
414+ .user_by_username("tahinli")
415+ .await
416+ .unwrap()
417+ .expect("tahinli");
418+ let bob = store.user_by_username("bob").await.unwrap().expect("bob");
419+ let acme = store.create_org("acme", &tahinli.id).await.expect("acme");
420+ store
421+ .add_org_member(&acme.id, &bob.id, OrgMemberRole::Member)
422+ .await
423+ .expect("bob joins acme");
424+ store
425+ .set_org_settings(
426+ &acme.id,
427+ OrgSettings {
428+ default_repo_role: DefaultRepoRole::None,
429+ members_can_create_repos: false,
430+ },
431+ )
432+ .await
433+ .expect("acme settings");
434+ let secret = store
435+ .create_repo(&acme.id, "secret", "", Visibility::Private, "main")
436+ .await
437+ .expect("secret repo");
438+ init_bare(&dir, &secret.id);
439+ let team = store
440+ .create_team(&acme.id, "devs", "", Role::Read, false)
441+ .await
442+ .expect("devs team");
443+ store
444+ .add_team_repo(&team.id, &secret.id)
445+ .await
446+ .expect("link secret to devs");
447+ team.id
448+ };
449+
450+ let (_server, ports) = launch(&dir, ports).await;
451+ let http = Http::new(ports.http);
452+ let tahinli = login(&http, "tahinli").await;
453+ let bob = login(&http, "bob").await;
454+ let csrf = csrf_for(&http, &tahinli).await;
455+ let team_page = format!("/org/acme/teams/{team_id}");
456+
457+ // (a) The owner sees the linked repo on the team page. Bob cannot read it, so he does not.
458+ let owner_view = http.get(&team_page, Some(&tahinli)).await;
459+ assert_eq!(owner_view.status, 200, "owner team page");
460+ assert!(
461+ owner_view.body.contains("acme/secret"),
462+ "the owner sees the linked repo"
463+ );
464+ let bob_view = http.get(&team_page, Some(&bob)).await;
465+ assert_eq!(bob_view.status, 200, "member team page");
466+ assert!(
467+ !bob_view.body.contains("acme/secret"),
468+ "a viewer who cannot read the repo does not see it"
469+ );
470+
471+ // (b) A rename of that repo wakes the owner's team stream, and bob's stays quiet: the page
472+ // he sees does not change, so no refresh is sent to him.
473+ let mut bob_events = http
474+ .stream(&format!("{team_page}/events"), Some(&bob))
475+ .await;
476+ assert_eq!(bob_events.status().as_u16(), 200, "bob team stream");
477+ let mut owner_events = http
478+ .stream(&format!("{team_page}/events"), Some(&tahinli))
479+ .await;
480+ assert_eq!(owner_events.status().as_u16(), 200, "owner team stream");
481+ assert_quiet(&mut bob_events, "connecting to the team stream").await;
482+
483+ let res = http
484+ .post_form(
485+ "/acme/secret/settings/rename",
486+ &[("csrf", &csrf), ("name", "vault")],
487+ Some(&tahinli),
488+ )
489+ .await;
490+ assert_eq!(res.status, 303, "rename the private repo: {}", res.body);
491+ next_changed(&mut owner_events, "renaming the linked repo").await;
492+ assert_quiet(&mut bob_events, "renaming a repo bob cannot read").await;
493+
494+ let bob_after = http.get(&team_page, Some(&bob)).await;
495+ assert!(
496+ !bob_after.body.contains("acme/vault") && !bob_after.body.contains("acme/secret"),
497+ "the repo stays hidden from bob after the rename"
498+ );
499+}