Refuse a sandbox's turn/checkpoint request outside its grant (blocking: opens the journal or the repository, and drops it, before the handler opens its own).
(
handle: &RepoHandle,
grants: &dyn SandboxGrants,
caller: &SandboxCaller,
scope: TurnScope<'_>,
)
| 349 | /// opens the journal or the repository, and drops it, before the handler |
| 350 | /// opens its own). |
| 351 | fn check_turn_scope( |
| 352 | handle: &RepoHandle, |
| 353 | grants: &dyn SandboxGrants, |
| 354 | caller: &SandboxCaller, |
| 355 | scope: TurnScope<'_>, |
| 356 | ) -> Result<(), Status> { |
| 357 | let grant = &caller.grant; |
| 358 | let forbidden = |what: &str| { |
| 359 | domain_status( |
| 360 | ErrorCode::Forbidden, |
| 361 | format!("{what} is not this sandbox's (view '{}')", grant.view), |
| 362 | ) |
| 363 | }; |
| 364 | // Whether nothing has ever been recorded for the session: one indexed |
| 365 | // lookup for its highest turn, never a walk to a wire-supplied number. |
| 366 | let unused = |session_id: &str| -> Result<bool, Status> { |
| 367 | let store = handle.change_store()?; |
| 368 | let last = store |
| 369 | .last_provenance_turn_for(session_id) |
| 370 | .map_err(|e| domain_status(ErrorCode::ProvenanceStore, e.to_string()))?; |
| 371 | Ok(last.is_none()) |
| 372 | }; |
| 373 | let owns_turn = |id: u64| grants.owns_provenance(grant, id).map_err(|e| e.status()); |
| 374 | match scope { |
| 375 | TurnScope::Claim(session_id) => { |
| 376 | let unused = unused(session_id)?; |
| 377 | if !grants |
| 378 | .claim_session(grant, session_id, unused) |
| 379 | .map_err(|e| e.status())? |
| 380 | { |
| 381 | return Err(forbidden("that session")); |
| 382 | } |
| 383 | } |
| 384 | TurnScope::Provenance(id) => { |
| 385 | if !owns_turn(id)? { |
| 386 | return Err(forbidden("that provenance turn")); |
| 387 | } |
| 388 | } |
| 389 | TurnScope::Prepare(id, explained) => { |
| 390 | if !owns_turn(id)? { |
| 391 | return Err(forbidden("that provenance turn")); |
| 392 | } |
| 393 | let repo = handle.repository_readonly()?; |
| 394 | if let Some(foreign) = repo |
| 395 | .first_foreign_change(&grant.view, explained) |
| 396 | .map_err(|e| domain_status(ErrorCode::Repository, e.to_string()))? |
| 397 | { |
| 398 | return Err(domain_status( |
| 399 | ErrorCode::Forbidden, |
| 400 | format!( |
| 401 | "change {} is not on view '{}'", |
| 402 | atomic_core::types::Base32::to_base32(&foreign), |
| 403 | grant.view |
| 404 | ), |
| 405 | )); |
| 406 | } |
| 407 | } |
| 408 | TurnScope::Session(session_id) => { |
no test coverage detected