diff --git a/docs/adr/0002-part-reader-leases-protect-active-downloads.md b/docs/adr/0002-part-reader-leases-protect-active-downloads.md index b4a9309..66a4d93 100644 --- a/docs/adr/0002-part-reader-leases-protect-active-downloads.md +++ b/docs/adr/0002-part-reader-leases-protect-active-downloads.md @@ -1,3 +1,3 @@ # Storage Reader Leases protect active downloads -Concurrent downloads hold durable, expiring Storage Reader Leases, including direct downloads whose lease lasts no longer than the signed URL. A renewable, fenced Merge Lease grants a single worker authority to complete the Merge; Part Reader Leases protect concurrent readers of Parts. Both expiring lease types renew every 30 seconds while active and expire after two minutes, except direct-download leases, which expire with their signed URLs. Parts are deleted only after the merge completes and no active Part Reader Leases remain; a Storage Location is deleted only after no Storage Reader Leases remain. A reader acquires its lease in the same locked transaction that reads the Storage Location's merge state, so its scope is chosen atomically: a download arriving before the merge takes a Part Reader Lease that blocks parts deletion, and one arriving after takes a Storage Reader Lease over the merged representation. There is therefore no window in which a reader can fail to acquire a Part Reader Lease against parts that are being deleted. +Concurrent downloads hold durable, expiring Storage Reader Leases, including direct downloads, whose lease outlives the signed URL by a fixed margin. A renewable, fenced Merge Lease grants a single worker authority to complete the Merge; Part Reader Leases protect concurrent readers of Parts. Both expiring lease types renew every 30 seconds while active and expire after two minutes, except direct-download leases, which expire ten minutes after their signed URLs: storage authenticates a GET when it starts, so a transfer begun just before the URL expires keeps reading after it. Parts are deleted only after the merge completes and no active Part Reader Leases remain; a Storage Location is deleted only after no Storage Reader Leases remain. A reader acquires its lease in the same locked transaction that reads the Storage Location's merge state, so its scope is chosen atomically: a download arriving before the merge takes a Part Reader Lease that blocks parts deletion, and one arriving after takes a Storage Reader Lease over the merged representation. There is therefore no window in which a reader can fail to acquire a Part Reader Lease against parts that are being deleted. diff --git a/docs/adr/0013-direct-downloads-require-verified-tokens.md b/docs/adr/0013-direct-downloads-require-verified-tokens.md new file mode 100644 index 0000000..64c4185 --- /dev/null +++ b/docs/adr/0013-direct-downloads-require-verified-tokens.md @@ -0,0 +1,11 @@ +--- +status: accepted +--- + +# Direct downloads require verified tokens + +A direct-download URL is a bearer credential: whoever holds it reads the object from storage until it expires, without asking the server again. The runtime token's `repository_id` and `ac` claims are all that stand between a caller and that URL, and with `SKIP_TOKEN_VALIDATION=true` they are decoded but not verified, so any client that reaches the server could name any repository and walk away with a signed URL for its cache. The server therefore refuses to start with `ENABLE_DIRECT_DOWNLOADS=true` and `SKIP_TOKEN_VALIDATION=true` together, unless `UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION=true` overrides it for local testing. This extends ADR-0006: skipping verification stays a dev/test escape hatch, and the one feature that turns an unverified claim into a storage credential is out of its reach. Setups that ran both flags stop at startup after upgrading. + +A signed URL lives `DIRECT_DOWNLOAD_URL_TTL_SECONDS`, 600 by default (the fixed lifetime before the setting existed) and at most 3600. Clients request the URL right before fetching it, so the lifetime only has to cover the start of the transfer and retries; a shorter one limits replay of a URL that leaks into job logs, while clients that hold a URL and fetch it later, such as BuildKit's `type=gha` cache, need a longer one. The Storage Reader Lease outlives the URL by a fixed margin (ADR-0002). + +Verified tokens must carry `exp`; GitHub's always do, and a token without one would never expire. The optional `ACTIONS_TOKEN_AUDIENCE` lists accepted `aud` values and then requires one of them, restricting the server to tokens issued for one installation; a value that lists none (only commas) stops startup rather than silently disabling the check. Scope permissions are bit flags (read 1, write 2): lookups use only scopes with the read bit and saves only scopes with the write bit, and a scope without a name or with a malformed permission rejects the token. Rejections log the error kind and claim name, never the token or claim values. diff --git a/install/kubernetes/github-actions-cache-server/templates/_helpers.tpl b/install/kubernetes/github-actions-cache-server/templates/_helpers.tpl index 926b343..3cb2841 100644 --- a/install/kubernetes/github-actions-cache-server/templates/_helpers.tpl +++ b/install/kubernetes/github-actions-cache-server/templates/_helpers.tpl @@ -125,6 +125,8 @@ Generate environment variables from config values. value: {{ default (printf "http://%s.%s.svc.cluster.local:%v" (include "github-actions-cache-server.fullname" .) .Release.Namespace .Values.service.port) .Values.config.apiBaseUrl | quote }} - name: ENABLE_DIRECT_DOWNLOADS value: {{ .Values.config.enableDirectDownloads | quote }} +- name: DIRECT_DOWNLOAD_URL_TTL_SECONDS + value: {{ .Values.config.directDownloadUrlTtlSeconds | quote }} - name: EAGER_MERGE value: {{ .Values.config.eagerMerge | quote }} - name: MAX_CONCURRENT_MERGES diff --git a/install/kubernetes/github-actions-cache-server/values.yaml b/install/kubernetes/github-actions-cache-server/values.yaml index b354fe0..b9f216e 100644 --- a/install/kubernetes/github-actions-cache-server/values.yaml +++ b/install/kubernetes/github-actions-cache-server/values.yaml @@ -28,6 +28,10 @@ config: # The runner must be able to reach the storage provider directly. enableDirectDownloads: false + # -- Seconds a signed direct-download URL stays valid, 1 to 3600. Lower limits + # replay of a leaked URL; clients that fetch a URL late (BuildKit) need more. + directDownloadUrlTtlSeconds: 600 + # -- Merge cache parts right after upload instead of on first download, so # the first restore can be a direct download. On object storage the parts # stream through the server; on the filesystem the merge is a kernel copy. diff --git a/src/config.rs b/src/config.rs index b4b10a4..384e70e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -36,6 +36,8 @@ pub(crate) struct Config { /// OIDC issuer without trailing slash. pub(crate) actions_token_issuer: String, pub(crate) actions_token_jwks_url: Option, + /// `aud` values a token must carry one of; never empty, `None` skips the check. + pub(crate) actions_token_audience: Option>, /// How long unused entries are kept; `None` disables age-based cleanup. pub(crate) cache_retention: Option, pub(crate) cache_max_size_bytes: Option, @@ -44,6 +46,8 @@ pub(crate) struct Config { pub(crate) disable_cleanup_jobs: bool, pub(crate) debug: bool, pub(crate) enable_direct_downloads: bool, + /// Lifetime of a signed direct-download URL, 1 second to 1 hour. + pub(crate) direct_download_url_ttl: Duration, pub(crate) eager_merge: bool, /// Merges this process runs at once, eager or lazy; at least 1. pub(crate) max_concurrent_merges: usize, @@ -168,6 +172,13 @@ fn port(raw: &str) -> Result { NonZeroU16::new(integer(raw)?).ok_or_else(|| "0 is not a port".to_owned()) } +fn url_ttl(raw: &str) -> Result { + Some(integer::(raw)?) + .filter(|seconds| (1..=3600).contains(seconds)) + .map(Duration::from_secs) + .ok_or_else(|| format!("{raw} is not between 1 and 3600")) +} + fn number(raw: &str) -> Result { raw.trim() .parse::() @@ -246,8 +257,14 @@ struct Vars { /// [default: discovered from the issuer] #[arg(long, env)] actions_token_jwks_url: Option, + /// Comma-separated `aud` values a token must carry one of [default: not checked] + #[arg(long, env)] + actions_token_audience: Option, #[arg(long, env, action = ArgAction::Set, default_value_t = false)] skip_token_validation: bool, + /// Allows direct downloads with `SKIP_TOKEN_VALIDATION`; for local testing only + #[arg(long, env, action = ArgAction::Set, default_value_t = false)] + unsafe_allow_direct_downloads_without_token_validation: bool, /// Days an unused entry is kept; 0 keeps it forever #[arg(long, env, default_value = "90", value_parser = number)] cache_cleanup_older_than_days: f64, @@ -264,6 +281,9 @@ struct Vars { debug: Option, #[arg(long, env, action = ArgAction::Set, default_value_t = false)] enable_direct_downloads: bool, + /// Seconds a signed direct-download URL stays valid, 1 to 3600 + #[arg(long, env, default_value = "600", value_parser = url_ttl)] + direct_download_url_ttl_seconds: Duration, #[arg(long, env, action = ArgAction::Set, default_value_t = false)] eager_merge: bool, #[arg(long, env, default_value = "16", value_parser = integer::)] @@ -522,6 +542,39 @@ impl Config { .chain(args), )?; let mut given = Vars::from_arg_matches(&matches)?; + // A signed URL is a storage credential; unverified claims must not obtain one (ADR-0013). + if given.enable_direct_downloads + && given.skip_token_validation + && !given.unsafe_allow_direct_downloads_without_token_validation + { + return Err(ConfigError::Invalid( + "ENABLE_DIRECT_DOWNLOADS=true requires token validation, but \ + SKIP_TOKEN_VALIDATION=true would hand signed storage URLs to unverified callers. \ + Unset SKIP_TOKEN_VALIDATION, or set \ + UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION=true for local testing only" + .to_owned(), + )); + } + let actions_token_audience = given + .actions_token_audience + .take() + .map(|raw| { + let audience: Vec = raw + .split(',') + .map(str::trim) + .filter(|entry| !entry.is_empty()) + .map(str::to_owned) + .collect(); + // A typo must not silently switch the check off. + if audience.is_empty() { + return Err(ConfigError::Invalid( + "ACTIONS_TOKEN_AUDIENCE lists no audience; unset it to skip the check" + .to_owned(), + )); + } + Ok(audience) + }) + .transpose()?; let storage = given.storage()?; let db = given.db()?; let api_base_url = given.api_base_url.trim_end_matches('/').to_owned(); @@ -533,6 +586,7 @@ impl Config { api_base_url, default_actions_results_url: given.default_actions_results_url, actions_token_jwks_url: given.actions_token_jwks_url, + actions_token_audience, cache_retention: days(given.cache_cleanup_older_than_days)?, cache_max_size_bytes: given.cache_max_size_bytes, cache_filesystem_max_usage: given.cache_filesystem_max_usage_percent, @@ -542,6 +596,7 @@ impl Config { disable_cleanup_jobs: given.disable_cleanup_jobs, debug: given.debug.is_some(), enable_direct_downloads: given.enable_direct_downloads, + direct_download_url_ttl: given.direct_download_url_ttl_seconds, eager_merge: given.eager_merge, max_concurrent_merges: usize::try_from(given.max_concurrent_merges.get()) .unwrap_or(usize::MAX) diff --git a/src/config/tests.rs b/src/config/tests.rs index 2c87e2f..53b068c 100644 --- a/src/config/tests.rs +++ b/src/config/tests.rs @@ -158,3 +158,51 @@ fn ignores_variables_of_other_drivers() { "the selected driver does" ); } + +#[test] +fn direct_downloads_require_token_validation() { + let unverified = [ + ("ENABLE_DIRECT_DOWNLOADS", "true"), + ("SKIP_TOKEN_VALIDATION", "true"), + ]; + assert!(config(&unverified).is_err(), "refused without the override"); + let overridden = [ + unverified.as_slice(), + &[( + "UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION", + "true", + )], + ] + .concat(); + assert!(config(&overridden).is_ok(), "allowed with the override"); +} + +#[test] +fn bounds_the_direct_download_url_lifetime() { + assert_eq!( + config(&[]).unwrap().direct_download_url_ttl, + Duration::from_mins(10), + "ten minutes by default" + ); + for ttl in ["0", "3601", "1.5"] { + assert!( + config(&[("DIRECT_DOWNLOAD_URL_TTL_SECONDS", ttl)]).is_err(), + "{ttl} is out of range" + ); + } +} + +#[test] +fn parses_the_token_audience() { + assert_eq!( + config(&[("ACTIONS_TOKEN_AUDIENCE", " a, ,b ")]) + .unwrap() + .actions_token_audience, + Some(vec!["a".to_owned(), "b".to_owned()]), + "trimmed, blanks dropped" + ); + assert!( + config(&[("ACTIONS_TOKEN_AUDIENCE", " , ")]).is_err(), + "an audience list without audiences is refused" + ); +} diff --git a/src/scope.rs b/src/scope.rs index 34ec032..2dae046 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -5,6 +5,7 @@ use std::time::{Duration, Instant}; use axum::http::HeaderMap; use axum::http::header::AUTHORIZATION; +use jsonwebtoken::errors::ErrorKind; use jsonwebtoken::jwk::{ AlgorithmParameters, EllipticCurve, Jwk, JwkSet, KeyOperations, PublicKeyUse, }; @@ -15,8 +16,10 @@ use tokio::sync::{Mutex, OnceCell}; use crate::config::Config; use crate::error::{Failure, Result}; +/// Permission bit that allows restoring from a scope. +const READ_PERMISSION: u32 = 1; /// Permission bit that allows saving to a scope. -const WRITE_PERMISSION: i64 = 2; +const WRITE_PERMISSION: u32 = 2; /// A cache scope the token grants, with its permission bits. #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] @@ -24,7 +27,7 @@ pub(crate) struct ScopeGrant { #[serde(rename = "Scope")] pub(crate) scope: String, #[serde(rename = "Permission")] - pub(crate) permission: i64, + pub(crate) permission: u32, } /// Scopes and repository a request may access. @@ -39,12 +42,17 @@ impl CacheScope { pub(crate) fn write_scope(&self) -> Option<&ScopeGrant> { self.scopes .iter() - .find(|grant| grant.permission >= WRITE_PERMISSION) + .find(|grant| grant.permission & WRITE_PERMISSION != 0) } - /// Scope names by descending permission, the order restores search them in. + /// Readable scope names by descending permission, the order restores search them in. pub(crate) fn read_scopes(&self) -> Vec { - let mut grants = self.scopes.clone(); + let mut grants: Vec = self + .scopes + .iter() + .filter(|grant| grant.permission & READ_PERMISSION != 0) + .cloned() + .collect(); grants.sort_by_key(|grant| std::cmp::Reverse(grant.permission)); grants.into_iter().map(|grant| grant.scope).collect() } @@ -152,6 +160,7 @@ fn selects(jwk: &Jwk, header: &Header) -> bool { #[derive(Debug)] pub(crate) struct TokenVerifier { issuer: String, + audience: Option>, skip_validation: bool, http: reqwest::Client, override_jwks: Option>, @@ -173,6 +182,7 @@ impl TokenVerifier { .map(|url| RemoteJwks::new(url.to_string())), discovered_jwks: OnceCell::new(), skip_validation: config.skip_token_validation, + audience: config.actions_token_audience.clone(), issuer, http: reqwest::Client::builder().timeout(FETCH_TIMEOUT).build()?, }) @@ -230,14 +240,19 @@ impl TokenVerifier { } let header = jsonwebtoken::decode_header(token)?; let keys = self.jwks().await.keys(&self.http, &header).await?; - // Like the original server's `jose`: the issuer is required, `exp` and `nbf` are checked - // when present without leeway, and the audience is not checked. + // The issuer and `exp` are required, `nbf` is checked when present, all without leeway. + // The audience is checked only when configured, and then required. let mut validation = Validation::new(header.alg); validation.set_issuer(&[&self.issuer]); - validation.set_required_spec_claims(&["iss"]); validation.validate_nbf = true; - validation.validate_aud = false; validation.leeway = 0; + if let Some(audience) = &self.audience { + validation.set_audience(audience); + validation.set_required_spec_claims(&["iss", "exp", "aud"]); + } else { + validation.validate_aud = false; + validation.set_required_spec_claims(&["iss", "exp"]); + } // Without a `kid`, several keys can match; like `jose`, the first that verifies wins. let mut verified = Err(Failure::msg("no matching key in JWKS")); for jwk in &keys { @@ -263,7 +278,7 @@ impl TokenVerifier { .and_then(|value| value.strip_prefix("Bearer ")) .ok_or(Unauthorized("Authorization header missing or malformed"))?; let claims = self.claims(token).await.map_err(|error| { - tracing::debug!(%error, "invalid token"); + tracing::debug!(reason = rejection_reason(&error), "invalid token"); Unauthorized("Invalid token") })?; let Some(scopes) = claims.ac.filter(|ac| !ac.is_empty()) else { @@ -274,6 +289,9 @@ impl TokenVerifier { if scopes.is_empty() { return Err(Unauthorized("Token does not contain any cache scopes")); } + if scopes.iter().any(|grant| grant.scope.is_empty()) { + return Err(Unauthorized("Invalid cache scopes")); + } let Some(repo_id) = claims.repository_id.filter(|id| !id.is_empty()) else { return Err(Unauthorized("Token does not contain repository id")); }; @@ -281,5 +299,26 @@ impl TokenVerifier { } } +/// Why a token was rejected, without the token or its claims: JSON errors quote the values they +/// failed on. +fn rejection_reason(error: &Failure) -> String { + let Failure::Token(error) = error else { + return error.to_string(); + }; + if let ErrorKind::MissingRequiredClaim(claim) = error.kind() { + return format!("missing claim `{claim}`"); + } + if let ErrorKind::InvalidClaimFormat(claim) = error.kind() { + return format!("invalid claim `{claim}`"); + } + if matches!( + error.kind(), + ErrorKind::Json(_) | ErrorKind::Base64(_) | ErrorKind::Utf8(_) + ) { + return "malformed token".to_owned(); + } + error.to_string() +} + #[cfg(test)] mod tests; diff --git a/src/scope/tests.rs b/src/scope/tests.rs index e86e9c4..e2e2fcf 100644 --- a/src/scope/tests.rs +++ b/src/scope/tests.rs @@ -1,30 +1,170 @@ use axum::Router; +use axum::http::HeaderValue; use axum::routing::get; +use base64::Engine as _; +use base64::prelude::BASE64_STANDARD; use super::*; -async fn issuer(discovery: axum::routing::MethodRouter) -> String { +async fn serve(app: Router) -> String { let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let url = format!("http://{}", listener.local_addr().unwrap()); - let app = Router::new().route("/.well-known/openid-configuration", discovery); drop(tokio::spawn( async move { axum::serve(listener, app).await }, )); url } -fn verifier(issuer: String) -> TokenVerifier { - let config = Config::from_vars([ - ( - "API_BASE_URL".to_owned(), - "http://localhost:3000".to_owned(), - ), - ("ACTIONS_TOKEN_ISSUER".to_owned(), issuer), - ]) +async fn issuer(discovery: axum::routing::MethodRouter) -> String { + serve(Router::new().route("/.well-known/openid-configuration", discovery)).await +} + +fn verifier(vars: &[(&str, &str)]) -> TokenVerifier { + let config = Config::from_vars( + std::iter::once(&("API_BASE_URL", "http://localhost:3000")) + .chain(vars) + .map(|&(key, value)| (key.to_owned(), value.to_owned())), + ) .unwrap(); TokenVerifier::new(&config).unwrap() } +/// Ed25519 key pair for test tokens: the private key as base64 PKCS#8, the public key as JWK `x`. +const SIGNING_KEY: &str = "MC4CAQAwBQYDK2VwBCIEIIeuNgsi//Fdbe35Iso7yXEemjguYhGDxU1hANcjPSAY"; +const PUBLIC_KEY_X: &str = "_UCvV5rHWyhUXvtwtOVx6QFdnhdlV6adbn87zLU2wyI"; +const ISSUER: &str = "https://issuer.test"; + +/// A verifier that checks signatures against [`SIGNING_KEY`]. +async fn signature_verifier(vars: &[(&str, &str)]) -> TokenVerifier { + let jwks = serve(Router::new().route( + "/jwks", + get(|| async { + axum::Json(serde_json::json!({ + "keys": [{ "kty": "OKP", "crv": "Ed25519", "x": PUBLIC_KEY_X }] + })) + }), + )) + .await; + let jwks_url = format!("{jwks}/jwks"); + let mut vars = vars.to_vec(); + vars.extend([ + ("ACTIONS_TOKEN_ISSUER", ISSUER), + ("ACTIONS_TOKEN_JWKS_URL", &jwks_url), + ]); + verifier(&vars) +} + +/// Headers bearing a token signed with [`SIGNING_KEY`] and valid claims, with `changes` applied; +/// a `null` change removes the claim. +fn bearer(changes: &serde_json::Value) -> HeaderMap { + let mut claims = serde_json::json!({ + "iss": ISSUER, + "exp": jsonwebtoken::get_current_timestamp().saturating_add(600), + "repository_id": "1", + "ac": r#"[{"Scope":"refs/heads/main","Permission":3}]"#, + }); + for (claim, value) in changes.as_object().unwrap() { + if value.is_null() { + claims.as_object_mut().unwrap().remove(claim); + } else { + claims[claim] = value.clone(); + } + } + let token = jsonwebtoken::encode( + &Header::new(Algorithm::EdDSA), + &claims, + &jsonwebtoken::EncodingKey::from_ed_der(&BASE64_STANDARD.decode(SIGNING_KEY).unwrap()), + ) + .unwrap(); + let mut headers = HeaderMap::new(); + headers.insert( + AUTHORIZATION, + HeaderValue::from_str(&format!("Bearer {token}")).unwrap(), + ); + headers +} + +#[tokio::test] +async fn verified_tokens_need_exp_and_well_formed_scopes() { + let verifier = signature_verifier(&[]).await; + assert!( + verifier + .cache_scope(&bearer(&serde_json::json!({}))) + .await + .is_ok(), + "valid token" + ); + assert!( + verifier + .cache_scope(&bearer(&serde_json::json!({ "aud": "anything" }))) + .await + .is_ok(), + "audience is not checked unless configured" + ); + assert_eq!( + verifier + .cache_scope(&bearer(&serde_json::json!({ "exp": null }))) + .await, + Err(Unauthorized("Invalid token")), + "a token without exp never expires" + ); + for ac in [ + r#"[{"Scope":"refs/heads/main","Permission":-1}]"#, + r#"[{"Scope":"","Permission":1}]"#, + r#"[{"Scope":"refs/heads/main"}]"#, + ] { + assert!( + verifier + .cache_scope(&bearer(&serde_json::json!({ "ac": ac }))) + .await + .is_err(), + "malformed scopes are rejected: {ac}" + ); + } +} + +#[tokio::test] +async fn a_configured_audience_is_required() { + let verifier = signature_verifier(&[("ACTIONS_TOKEN_AUDIENCE", "a, b")]).await; + for (aud, accepted) in [ + (serde_json::json!("b"), true), + (serde_json::json!(["c", "a"]), true), + (serde_json::json!("c"), false), + (serde_json::Value::Null, false), + ] { + assert_eq!( + verifier + .cache_scope(&bearer(&serde_json::json!({ "aud": aud }))) + .await + .is_ok(), + accepted, + "aud {aud}" + ); + } +} + +#[test] +fn permissions_are_bit_flags() { + let grant = |scope: &str, permission| ScopeGrant { + scope: scope.to_owned(), + permission, + }; + let scope = CacheScope { + scopes: vec![grant("read", 1), grant("write", 2), grant("both", 3)], + repo_id: "1".to_owned(), + }; + assert_eq!( + scope.read_scopes(), + ["both", "read"], + "write-only scopes are not read" + ); + assert_eq!( + scope.write_scope().map(|writable| writable.scope.as_str()), + Some("write"), + "read-only scopes are not written" + ); +} + #[test] fn selects_keys_like_jose() { let jwk = |extra: serde_json::Value| { @@ -92,7 +232,10 @@ async fn discovers_the_jwks_uri() { })) .await; assert_eq!( - verifier(url).discover_jwks_url().await.unwrap(), + verifier(&[("ACTIONS_TOKEN_ISSUER", &url)]) + .discover_jwks_url() + .await + .unwrap(), "https://token.actions.githubusercontent.com/.well-known/jwks", "jwks_uri from the discovery document" ); @@ -104,7 +247,7 @@ async fn fails_discovery_without_document_or_jwks_uri() { (axum::http::StatusCode::NOT_FOUND, "Not Found") })) .await; - let not_served = verifier(missing) + let not_served = verifier(&[("ACTIONS_TOKEN_ISSUER", &missing)]) .discover_jwks_url() .await .unwrap_err() @@ -118,7 +261,7 @@ async fn fails_discovery_without_document_or_jwks_uri() { axum::Json(serde_json::json!({ "issuer": "x" })) })) .await; - let no_uri = verifier(incomplete) + let no_uri = verifier(&[("ACTIONS_TOKEN_ISSUER", &incomplete)]) .discover_jwks_url() .await .unwrap_err() diff --git a/src/storage.rs b/src/storage.rs index 3573f1c..3575b2d 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -10,7 +10,6 @@ use std::collections::HashMap; use std::fmt::Display; use std::io; use std::sync::Arc; -use std::time::Duration; use axum::body::Bytes; use base64::Engine as _; @@ -37,7 +36,7 @@ pub(crate) use self::adapter::{ UploadKind, }; use self::leases::{ - DIRECT_DOWNLOAD_LEASE_DURATION, LEASE_DURATION, LEASE_RENEWAL, acquire_merge_lease, + DIRECT_DOWNLOAD_LEASE_MARGIN, LEASE_DURATION, LEASE_RENEWAL, acquire_merge_lease, create_reader_lease, deadline, release_merge_lease, release_reader_lease, renew_merge_lease, renew_reader_lease, }; @@ -1053,10 +1052,10 @@ impl Storage { })); }; - // An entry in one object with direct downloads enabled: take a reader lease bounded by - // the signed URL lifetime, then sign. One deadline bounds both, so the URL never - // outlives the lease. - let expires_at = deadline(DIRECT_DOWNLOAD_LEASE_DURATION); + // An entry in one object with direct downloads enabled: take a reader lease that + // outlives the signed URL by a margin, then sign (ADR-0002). + let url_ttl = self.config.direct_download_url_ttl; + let expires_at = deadline(url_ttl.saturating_add(DIRECT_DOWNLOAD_LEASE_MARGIN)); let tx = self.db.pool.begin().await?; let current = storage_locations::Entity::find_by_id(location.id.as_str()) .lock_exclusive() @@ -1072,16 +1071,7 @@ impl Storage { // The merge could have been undone between validation and the lease read. let download_url = match whole { - Some((object, _)) => { - store - .create_download_url( - &object, - Duration::from_millis( - u64::try_from(expires_at.saturating_sub(now_ms())).unwrap_or(0), - ), - ) - .await? - } + Some((object, _)) => store.create_download_url(&object, url_ttl).await?, None => default_url, }; return Ok(Some(DownloadUrlMatch { diff --git a/src/storage/leases.rs b/src/storage/leases.rs index 8ad38c8..c4aee56 100644 --- a/src/storage/leases.rs +++ b/src/storage/leases.rs @@ -18,8 +18,9 @@ pub(crate) const LEASE_RENEWAL: Duration = if cfg!(test) { } else { Duration::from_secs(30) }; -/// Lifetime of a signed direct-download URL and the reader lease that protects it. -pub(crate) const DIRECT_DOWNLOAD_LEASE_DURATION: Duration = Duration::from_mins(10); +/// How long a direct-download reader lease outlives its signed URL: storage authenticates a GET +/// when it starts, so a transfer begun just before the URL expires keeps reading after it. +pub(crate) const DIRECT_DOWNLOAD_LEASE_MARGIN: Duration = Duration::from_mins(10); /// The timestamp column value `duration` from now. pub(crate) fn deadline(duration: Duration) -> i64 { diff --git a/src/tests/downloads.rs b/src/tests/downloads.rs index 6e940fb..d3af56f 100644 --- a/src/tests/downloads.rs +++ b/src/tests/downloads.rs @@ -298,12 +298,20 @@ async fn a_download_fails_once_its_reader_lease_is_lost() { } #[tokio::test] -async fn a_direct_download_lease_covers_the_signed_url_lifetime() { +async fn a_direct_download_lease_outlives_the_signed_url() { // Only S3 signs URLs against the emulators used in tests. if storage_driver() != "s3" { return; } - let server = TestServer::with(&[("ENABLE_DIRECT_DOWNLOADS", "true")]).await; + let server = TestServer::with(&[ + ("ENABLE_DIRECT_DOWNLOADS", "true"), + ( + "UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION", + "true", + ), + ("DIRECT_DOWNLOAD_URL_TTL_SECONDS", "120"), + ]) + .await; let folder = uuid::Uuid::new_v4().to_string(); put( server.adapter(), @@ -338,10 +346,10 @@ async fn a_direct_download_lease_covers_the_signed_url_lifetime() { .await .unwrap() .unwrap(); - assert!(lifetime_seconds > 0, "signed url expires"); + assert_eq!(lifetime_seconds, 120, "configured url lifetime"); assert!( - lease.expires_at - started_at >= lifetime_seconds * 1000, - "lease outlives the url" + lease.expires_at - started_at >= (120 + 10 * 60) * 1000, + "lease outlives the url by the margin" ); let accessed = find_location(server.db(), &location.id) .await