| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
use serde_json::json; |
| 31 |
|
| 32 |
use super::synckit_paid_sync::{auth_as, create_internal_app, harness_with_blobs}; |
| 33 |
use crate::harness::TestHarness; |
| 34 |
use makenotwork::db::{SyncAppId, UserId}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
const ADMIN_ONLY: &[(&str, &str)] = &[ |
| 39 |
("POST", "/members"), |
| 40 |
("GET", "/pubkeys"), |
| 41 |
("POST", "/rotate"), |
| 42 |
("POST", "/invitations"), |
| 43 |
("GET", "/invitations"), |
| 44 |
]; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
fn plausible_body(suffix: &str, subject: UserId) -> String { |
| 50 |
match suffix { |
| 51 |
"/members" => json!({ |
| 52 |
"member_email": "nobody@example.com", |
| 53 |
"sealed_gck": "sealed_x_v1", |
| 54 |
"member_pubkey": "pk_x", |
| 55 |
}) |
| 56 |
.to_string(), |
| 57 |
"/rotate" => json!({ |
| 58 |
"gck_version": 2, |
| 59 |
"grants": [{ "user_id": subject.to_string(), "sealed_gck": "sealed_x_v2" }], |
| 60 |
}) |
| 61 |
.to_string(), |
| 62 |
"/invitations" => json!({ "note": "join us" }).to_string(), |
| 63 |
|
| 64 |
|
| 65 |
"/pull" => { |
| 66 |
json!({ "device_id": uuid::Uuid::new_v4().to_string(), "cursor": 0 }).to_string() |
| 67 |
} |
| 68 |
_ => String::new(), |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
async fn call( |
| 73 |
h: &mut TestHarness, |
| 74 |
method: &str, |
| 75 |
path: &str, |
| 76 |
body: &str, |
| 77 |
) -> crate::harness::client::TestResponse { |
| 78 |
match method { |
| 79 |
"GET" => h.client.get(path).await, |
| 80 |
"POST" => h.client.post_json(path, body).await, |
| 81 |
"DELETE" => h.client.delete(path).await, |
| 82 |
other => panic!("unhandled method {other}"), |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
async fn create_group(h: &mut TestHarness, name: &str) -> String { |
| 87 |
let resp = h |
| 88 |
.client |
| 89 |
.post_json( |
| 90 |
"/api/sync/groups", |
| 91 |
&json!({ |
| 92 |
"id": uuid::Uuid::new_v4().to_string(), |
| 93 |
"name": name, |
| 94 |
"admin_sealed_gck": "sealed_admin_v1", |
| 95 |
"admin_pubkey": "pk_admin", |
| 96 |
}) |
| 97 |
.to_string(), |
| 98 |
) |
| 99 |
.await; |
| 100 |
assert_eq!(resp.status.as_u16(), 200, "create group: {}", resp.text); |
| 101 |
resp.json::<serde_json::Value>()["id"] |
| 102 |
.as_str() |
| 103 |
.expect("group id") |
| 104 |
.to_string() |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
async fn add_member(h: &mut TestHarness, group_id: &str, username: &str) -> UserId { |
| 109 |
let email = format!("{username}@example.com"); |
| 110 |
let user = h.signup(username, &email, "Password1!").await; |
| 111 |
sqlx::query("UPDATE users SET email_verified = true WHERE id = $1") |
| 112 |
.bind(user) |
| 113 |
.execute(&h.db) |
| 114 |
.await |
| 115 |
.expect("verify member"); |
| 116 |
|
| 117 |
let resp = h |
| 118 |
.client |
| 119 |
.post_json( |
| 120 |
&format!("/api/sync/groups/{group_id}/members"), |
| 121 |
&json!({ |
| 122 |
"member_email": email, |
| 123 |
"sealed_gck": format!("sealed_{username}_v1"), |
| 124 |
"member_pubkey": format!("pk_{username}"), |
| 125 |
}) |
| 126 |
.to_string(), |
| 127 |
) |
| 128 |
.await; |
| 129 |
assert_eq!(resp.status.as_u16(), 204, "add member: {}", resp.text); |
| 130 |
user |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
async fn group_with_admin(h: &mut TestHarness, tag: &str) -> (UserId, SyncAppId, String) { |
| 135 |
let admin = h |
| 136 |
.signup( |
| 137 |
&format!("{tag}_admin"), |
| 138 |
&format!("{tag}_admin@example.com"), |
| 139 |
"Password1!", |
| 140 |
) |
| 141 |
.await; |
| 142 |
let (app, _key) = create_internal_app(&h.db, admin).await; |
| 143 |
auth_as(h, admin, app, "admin-key"); |
| 144 |
let group_id = create_group(h, "Team").await; |
| 145 |
(admin, app, group_id) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
#[tokio::test] |
| 157 |
async fn a_stranger_in_the_same_app_is_refused_every_group_endpoint() { |
| 158 |
let (mut h, _blobs) = harness_with_blobs().await; |
| 159 |
let (_admin, app, group_id) = group_with_admin(&mut h, "stranger").await; |
| 160 |
|
| 161 |
let stranger = h |
| 162 |
.signup("stranger_eve", "stranger_eve@example.com", "Password1!") |
| 163 |
.await; |
| 164 |
auth_as(&mut h, stranger, app, "eve-key"); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
let endpoints: Vec<(&str, String, String)> = vec![ |
| 169 |
("GET", "/members".into(), String::new()), |
| 170 |
( |
| 171 |
"POST", |
| 172 |
"/members".into(), |
| 173 |
plausible_body("/members", stranger), |
| 174 |
), |
| 175 |
("GET", "/pubkeys".into(), String::new()), |
| 176 |
( |
| 177 |
"POST", |
| 178 |
"/rotate".into(), |
| 179 |
plausible_body("/rotate", stranger), |
| 180 |
), |
| 181 |
("GET", "/grant".into(), String::new()), |
| 182 |
("POST", "/pull".into(), plausible_body("/pull", stranger)), |
| 183 |
( |
| 184 |
"POST", |
| 185 |
"/invitations".into(), |
| 186 |
plausible_body("/invitations", stranger), |
| 187 |
), |
| 188 |
("GET", "/invitations".into(), String::new()), |
| 189 |
]; |
| 190 |
|
| 191 |
for (method, suffix, body) in endpoints { |
| 192 |
let path = format!("/api/sync/groups/{group_id}{suffix}"); |
| 193 |
let resp = call(&mut h, method, &path, &body).await; |
| 194 |
assert_eq!( |
| 195 |
resp.status.as_u16(), |
| 196 |
403, |
| 197 |
"{method} {suffix} must refuse a non-member, got {}: {}", |
| 198 |
resp.status, |
| 199 |
resp.text |
| 200 |
); |
| 201 |
assert!( |
| 202 |
!resp.text.contains("sealed_admin_v1"), |
| 203 |
"{method} {suffix} leaked a grant to a non-member" |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
let resp = h.client.get("/api/sync/groups").await; |
| 209 |
assert_eq!(resp.status.as_u16(), 200, "list groups: {}", resp.text); |
| 210 |
assert!( |
| 211 |
!resp.text.contains(&group_id), |
| 212 |
"a group the caller is not in must not be listed for them" |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
#[tokio::test] |
| 224 |
async fn a_member_who_is_not_the_admin_is_refused_the_admin_endpoints() { |
| 225 |
let (mut h, _blobs) = harness_with_blobs().await; |
| 226 |
let (_admin, app, group_id) = group_with_admin(&mut h, "member").await; |
| 227 |
let bob = add_member(&mut h, &group_id, "member_bob").await; |
| 228 |
|
| 229 |
auth_as(&mut h, bob, app, "bob-key"); |
| 230 |
|
| 231 |
for (method, suffix) in ADMIN_ONLY { |
| 232 |
let path = format!("/api/sync/groups/{group_id}{suffix}"); |
| 233 |
let resp = call(&mut h, method, &path, &plausible_body(suffix, bob)).await; |
| 234 |
assert_eq!( |
| 235 |
resp.status.as_u16(), |
| 236 |
403, |
| 237 |
"{method} {suffix} is admin-only, got {}: {}", |
| 238 |
resp.status, |
| 239 |
resp.text |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
for suffix in ["/members", "/grant"] { |
| 247 |
let path = format!("/api/sync/groups/{group_id}{suffix}"); |
| 248 |
let resp = h.client.get(&path).await; |
| 249 |
assert_eq!( |
| 250 |
resp.status.as_u16(), |
| 251 |
200, |
| 252 |
"GET {suffix} is open to a member: {}", |
| 253 |
resp.text |
| 254 |
); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
#[tokio::test] |
| 262 |
async fn a_member_cannot_remove_anyone_including_the_admin() { |
| 263 |
let (mut h, _blobs) = harness_with_blobs().await; |
| 264 |
let (admin, app, group_id) = group_with_admin(&mut h, "removal").await; |
| 265 |
let bob = add_member(&mut h, &group_id, "removal_bob").await; |
| 266 |
let carol = add_member(&mut h, &group_id, "removal_carol").await; |
| 267 |
|
| 268 |
auth_as(&mut h, bob, app, "bob-key"); |
| 269 |
|
| 270 |
for target in [admin, carol, bob] { |
| 271 |
let resp = h |
| 272 |
.client |
| 273 |
.delete(&format!("/api/sync/groups/{group_id}/members/{target}")) |
| 274 |
.await; |
| 275 |
assert_eq!( |
| 276 |
resp.status.as_u16(), |
| 277 |
403, |
| 278 |
"a member removing {target} must be refused, got {}: {}", |
| 279 |
resp.status, |
| 280 |
resp.text |
| 281 |
); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
auth_as(&mut h, admin, app, "admin-key"); |
| 287 |
let resp = h |
| 288 |
.client |
| 289 |
.get(&format!("/api/sync/groups/{group_id}/members")) |
| 290 |
.await; |
| 291 |
assert_eq!(resp.status.as_u16(), 200, "list members: {}", resp.text); |
| 292 |
let members = resp.json::<serde_json::Value>(); |
| 293 |
assert_eq!( |
| 294 |
members.as_array().map(Vec::len), |
| 295 |
Some(3), |
| 296 |
"all three are still in the group: {members}" |
| 297 |
); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
#[tokio::test] |
| 305 |
async fn a_group_in_another_app_is_not_found_rather_than_forbidden() { |
| 306 |
let (mut h, _blobs) = harness_with_blobs().await; |
| 307 |
let (admin, _first_app, group_id) = group_with_admin(&mut h, "scope").await; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
let second_key = "test-api-key-group-scope-second"; |
| 314 |
let second_app: SyncAppId = sqlx::query_scalar( |
| 315 |
"INSERT INTO sync_apps (creator_id, name, api_key_hash, api_key_prefix, is_internal, billing_status) \ |
| 316 |
VALUES ($1, 'SecondApp', $2, $3, TRUE, 'active') RETURNING id", |
| 317 |
) |
| 318 |
.bind(admin) |
| 319 |
.bind(crate::harness::hash_api_key(second_key)) |
| 320 |
.bind(&second_key[..8]) |
| 321 |
.fetch_one(&h.db) |
| 322 |
.await |
| 323 |
.expect("insert the second internal app"); |
| 324 |
auth_as(&mut h, admin, second_app, second_key); |
| 325 |
|
| 326 |
for (method, suffix) in [("GET", "/members"), ("GET", "/grant"), ("POST", "/pull")] { |
| 327 |
let path = format!("/api/sync/groups/{group_id}{suffix}"); |
| 328 |
let resp = call(&mut h, method, &path, &plausible_body(suffix, admin)).await; |
| 329 |
assert_eq!( |
| 330 |
resp.status.as_u16(), |
| 331 |
404, |
| 332 |
"{method} {suffix} under another app must not confirm the id exists, \ |
| 333 |
got {}: {}", |
| 334 |
resp.status, |
| 335 |
resp.text |
| 336 |
); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
#[tokio::test] |
| 344 |
async fn an_unknown_group_answers_the_same_as_one_in_another_app() { |
| 345 |
let (mut h, _blobs) = harness_with_blobs().await; |
| 346 |
let (_admin, _app, _group_id) = group_with_admin(&mut h, "ghost").await; |
| 347 |
|
| 348 |
let ghost = uuid::Uuid::new_v4(); |
| 349 |
let resp = h |
| 350 |
.client |
| 351 |
.get(&format!("/api/sync/groups/{ghost}/members")) |
| 352 |
.await; |
| 353 |
|
| 354 |
assert_eq!( |
| 355 |
resp.status.as_u16(), |
| 356 |
404, |
| 357 |
"an id that matches no group is not found: {}", |
| 358 |
resp.text |
| 359 |
); |
| 360 |
} |
| 361 |
|