Skip to content

Commit c400713

Browse files
jmecomelifoster-blockclaude
authored
fix(media): require authenticated reads (#4610)
This change requires a valid signed Blossom authorization request and current relay membership for every media GET and HEAD request. It removes the unauthenticated compatibility path and updates desktop reads to send the required authorization. This blocks anonymous retrieval and access after relay-membership revocation. It does not yet bind a blob to its originating channel, so someone removed from a private channel can still read a known blob while remaining a relay member. That channel-ACL follow-up remains required before closing the full finding. ## Testing - `git diff --check origin/main...codex/security-media-read-auth` - Rebased onto `origin/main` at `049080e` - Full CI pending Originating Buzz thread: `buzz://message?channel=3928fe05-df61-4b5d-b9c7-d623b9b10ea1&id=3c6c02312f763fbe0d2bfc33a6c1a362f91d0354f3d18b039cf7a0558c1439d1` --------- Signed-off-by: Jordan Mecom <[email protected]> Signed-off-by: Alex Rosenzweig <[email protected]> Signed-off-by: Eli Foster <[email protected]> Co-authored-by: Eli Foster <[email protected]> Co-authored-by: Claude Opus 5 <[email protected]>
1 parent f32da9b commit c400713

18 files changed

Lines changed: 295 additions & 133 deletions

File tree

‎.env.example‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ BUZZ_S3_ADDRESSING_STYLE=path
102102
# BUZZ_MEDIA_MAX_CONCURRENT_UPLOADS=8
103103
# BUZZ_MEDIA_MAX_CONCURRENT_UPLOADS_PER_PUBKEY=2
104104
# BUZZ_MEDIA_UPLOADS_PER_MINUTE=30
105-
# Require Blossom t=get auth and relay membership for GET/HEAD /media/*.
106-
# Keep off until desktop/mobile/CLI clients that attach media read auth are deployed.
107-
# BUZZ_REQUIRE_MEDIA_GET_AUTH=false
108-
# Legacy alias accepted by the relay while rollout docs catch up:
109-
# BUZZ_REQUIRE_MEDIA_READ_AUTH=false
105+
# GET/HEAD /media/* always require Blossom t=get auth and relay membership.
106+
# BUZZ_REQUIRE_MEDIA_GET_AUTH and BUZZ_REQUIRE_MEDIA_READ_AUTH are no longer
107+
# read; setting either (including to false) changes nothing and the relay warns
108+
# about it at startup.
110109

111110
# -----------------------------------------------------------------------------
112111
# Ephemeral Channels (TTL testing)

‎.github/workflows/ci.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,19 @@ jobs:
768768
env:
769769
RELAY_URL: ws://localhost:3000
770770
GIT_CREDENTIAL_NOSTR_BIN: ${{ github.workspace }}/target/ci/git-credential-nostr
771+
- name: Media read-auth e2e
772+
# Reads require kind:24242 `t=get` auth, so these binaries are the only
773+
# coverage that a real relay rejects bare reads and honours host- and
774+
# hash-scoped tokens. They were #[ignore]d and selected by no CI job, so
775+
# the lane never ran; select it here, where MinIO and the seeded
776+
# 'localhost:3000' community already exist.
777+
# --no-fail-fast: without it cargo stops after the first failing binary,
778+
# so one broken case hides every later binary's result.
779+
run: |
780+
cargo test -p buzz-test-client --no-fail-fast --test e2e_media --test e2e_media_extended --test e2e_media_video -- --ignored --nocapture
781+
env:
782+
RELAY_URL: ws://localhost:3000
783+
RELAY_HTTP_URL: http://localhost:3000
771784
- name: Upload relay logs
772785
if: failure()
773786
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7

‎TESTING.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ out of the box with `just setup` or `just relay`. Common overrides:
277277
| `REDIS_URL` | `redis://localhost:6379` | |
278278
| `BUZZ_REQUIRE_AUTH_TOKEN` | `false` | When true, REST requires NIP-98 (no `X-Pubkey` fallback) |
279279
| `BUZZ_REQUIRE_RELAY_MEMBERSHIP` | `false` | When true, only pubkeys in `relay_members` can connect |
280-
| `BUZZ_REQUIRE_MEDIA_GET_AUTH` | `false` | When true, `GET`/`HEAD /media/*` require Blossom kind 24242 `t=get` auth plus relay membership. |
281280
| `BUZZ_DRAIN_JITTER_MS` | `0` (off) | Per-connection upper bound, in ms, for the random delay before each live WebSocket gets its `1012 Service Restart` close on graceful shutdown. `0` closes every socket at once (the previous behavior). A positive value spreads closes uniformly over `[1, value]` ms to avoid a reconnect thundering herd on rolling deploys. Values above `20000` are capped to `20000` (`MAX_DRAIN_JITTER_MS`) to leave close-frame delivery headroom under the relay's 30s hard-drain timeout. Empty or whitespace-only is treated as unset (off); a non-integer fails startup loudly. |
282281
| `BUZZ_AUDIT_ENABLED` | `true` | Tamper-evident event/media audit log. Set `false`/`0`/`off` to skip its DB pool and writes. Does not disable the separate moderation audit trail. |
283282
| `BUZZ_AUTO_MIGRATE` | `false` | Opt in with `true`/`1`/`yes`/`on` to run embedded SQLx migrations on relay startup |

‎crates/buzz-relay/src/api/media.rs‎

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,6 @@ async fn authenticate_media_read(
493493
) -> Result<MediaReadAuth, MediaError> {
494494
let tenant = bind_media_read_tenant(state, headers).await?;
495495

496-
if !state.config.require_media_get_auth {
497-
return Ok(MediaReadAuth { tenant });
498-
}
499-
500496
let auth_event = extract_blossom_auth(headers)?;
501497
let sha256 = sha256_ext.split('.').next().unwrap_or(sha256_ext);
502498
buzz_media::auth::verify_blossom_get_auth(&auth_event, sha256, Some(tenant.host()), 3600)?;
@@ -514,12 +510,8 @@ async fn authenticate_media_read(
514510
Ok(MediaReadAuth { tenant })
515511
}
516512

517-
fn blob_cache_control(require_auth: bool) -> &'static str {
518-
if require_auth {
519-
"private, max-age=31536000, immutable"
520-
} else {
521-
"public, max-age=31536000, immutable"
522-
}
513+
fn blob_cache_control() -> &'static str {
514+
"private, max-age=31536000, immutable"
523515
}
524516

525517
/// Whether a path-segment extension is a safe token.
@@ -623,7 +615,7 @@ pub(crate) async fn serve_blob_for_tenant(
623615
req_headers: &HeaderMap,
624616
) -> Result<Response, MediaError> {
625617
validate_media_path(sha256_ext)?;
626-
let cache_control = blob_cache_control(state.config.require_media_get_auth);
618+
let cache_control = blob_cache_control();
627619

628620
// Sidecar gate FIRST — reject before any blob I/O. Storage is not authoritative.
629621
let content_type = if sha256_ext.ends_with(".thumb.jpg") {
@@ -801,10 +793,9 @@ pub async fn head_blob(
801793
Path(sha256_ext): Path<String>,
802794
) -> Result<Response, MediaError> {
803795
validate_media_path(&sha256_ext)?;
804-
let require_media_get_auth = state.config.require_media_get_auth;
805796
let media_auth = authenticate_media_read(&state, &headers, &sha256_ext).await?;
806797
let tenant = media_auth.tenant;
807-
let cache_control = blob_cache_control(require_media_get_auth);
798+
let cache_control = blob_cache_control();
808799

809800
// Sidecar gate FIRST — reject before any blob I/O.
810801
let content_type = if sha256_ext.ends_with(".thumb.jpg") {
@@ -946,13 +937,8 @@ mod tests {
946937
}
947938

948939
async fn test_state() -> Arc<AppState> {
949-
test_state_with_media_get_auth(false).await
950-
}
951-
952-
async fn test_state_with_media_get_auth(require_media_get_auth: bool) -> Arc<AppState> {
953940
let mut config = crate::config::Config::from_env().expect("default config loads");
954941
config.require_relay_membership = false;
955-
config.require_media_get_auth = require_media_get_auth;
956942
config.redis_url = "redis://127.0.0.1:1".to_string();
957943
config.media_uploads_per_minute = 1;
958944
config.media_max_concurrent_uploads = 2;
@@ -994,8 +980,8 @@ mod tests {
994980
Arc::new(state)
995981
}
996982

997-
async fn media_get_auth_router(require_media_get_auth: bool) -> axum::Router {
998-
let state = test_state_with_media_get_auth(require_media_get_auth).await;
983+
async fn media_get_auth_router() -> axum::Router {
984+
let state = test_state().await;
999985
axum::Router::new()
1000986
.route(
1001987
"/media/{sha256_ext}",
@@ -1041,20 +1027,9 @@ mod tests {
10411027
}
10421028

10431029
#[tokio::test]
1044-
async fn media_get_auth_flag_off_allows_unauthenticated_read_until_sidecar_gate() {
1045-
let response = media_get_auth_router(false)
1046-
.await
1047-
.oneshot(media_request("GET", None))
1048-
.await
1049-
.expect("response");
1050-
1051-
assert_eq!(response.status(), StatusCode::NOT_FOUND);
1052-
}
1053-
1054-
#[tokio::test]
1055-
async fn media_get_auth_flag_on_rejects_unauthenticated_get_and_head_before_sidecar_gate() {
1030+
async fn media_reads_reject_unauthenticated_get_and_head_before_sidecar_gate() {
10561031
for method in ["GET", "HEAD"] {
1057-
let response = media_get_auth_router(true)
1032+
let response = media_get_auth_router()
10581033
.await
10591034
.oneshot(media_request(method, None))
10601035
.await
@@ -1065,10 +1040,10 @@ mod tests {
10651040
}
10661041

10671042
#[tokio::test]
1068-
async fn media_get_auth_flag_on_valid_server_scoped_token_reaches_sidecar_gate() {
1043+
async fn media_read_with_valid_server_scoped_token_reaches_sidecar_gate() {
10691044
let keys = Keys::generate();
10701045
let auth = media_get_auth_header(&keys, media_get_tags_for("relay.example", None));
1071-
let response = media_get_auth_router(true)
1046+
let response = media_get_auth_router()
10721047
.await
10731048
.oneshot(media_request("GET", Some(auth)))
10741049
.await
@@ -1078,7 +1053,7 @@ mod tests {
10781053
}
10791054

10801055
#[tokio::test]
1081-
async fn media_get_auth_flag_on_rejects_upload_verb_wrong_server_and_wrong_x() {
1056+
async fn media_read_rejects_upload_verb_wrong_server_and_wrong_x() {
10821057
let keys = Keys::generate();
10831058
let now = Timestamp::now().as_secs();
10841059
let expiration = (now + 300).to_string();
@@ -1102,7 +1077,7 @@ mod tests {
11021077

11031078
for tags in cases {
11041079
let auth = media_get_auth_header(&keys, tags);
1105-
let response = media_get_auth_router(true)
1080+
let response = media_get_auth_router()
11061081
.await
11071082
.oneshot(media_request("GET", Some(auth)))
11081083
.await
@@ -1119,15 +1094,15 @@ mod tests {
11191094
}
11201095

11211096
#[tokio::test]
1122-
async fn media_get_auth_flag_on_accepts_range_header_only_after_auth() {
1097+
async fn media_read_accepts_range_header_only_after_auth() {
11231098
let keys = Keys::generate();
11241099
let auth = media_get_auth_header(&keys, media_get_tags_for("relay.example", None));
11251100
let mut request = media_request("GET", Some(auth));
11261101
request
11271102
.headers_mut()
11281103
.insert(header::RANGE, "bytes=0-0".parse().expect("range header"));
11291104

1130-
let response = media_get_auth_router(true)
1105+
let response = media_get_auth_router()
11311106
.await
11321107
.oneshot(request)
11331108
.await

‎crates/buzz-relay/src/config.rs‎

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,6 @@ pub struct Config {
227227
/// Maximum media upload starts accepted from one pubkey per minute.
228228
pub media_uploads_per_minute: u32,
229229

230-
/// Require Blossom kind:24242 `t=get` auth plus relay membership before
231-
/// serving media GET/HEAD. Default off for staged client rollout.
232-
pub require_media_get_auth: bool,
233-
234230
/// Whether tamper-evident event/media audit logging is enabled. Defaults to true.
235231
/// This does not control the separate `moderation_actions` audit trail.
236232
/// Set `BUZZ_AUDIT_ENABLED=false` for deployments that do not require it.
@@ -435,6 +431,31 @@ fn ensure_git_path(
435431
Ok(git_repo_path)
436432
}
437433

434+
/// Env vars that once gated authenticated media reads.
435+
///
436+
/// `BUZZ_REQUIRE_MEDIA_GET_AUTH` was the real flag; `BUZZ_REQUIRE_MEDIA_READ_AUTH`
437+
/// was documented in `.env.example` as an accepted alias but was never read by
438+
/// the relay. Media reads are now unconditionally authenticated, so both are
439+
/// inert and an operator still setting either — especially to `false` — holds a
440+
/// belief about their deployment that is no longer true.
441+
const INERT_MEDIA_READ_AUTH_VARS: [&str; 2] = [
442+
"BUZZ_REQUIRE_MEDIA_GET_AUTH",
443+
"BUZZ_REQUIRE_MEDIA_READ_AUTH",
444+
];
445+
446+
/// Which of `names` are present, so startup can warn that they do nothing.
447+
///
448+
/// `lookup` is injected rather than calling `std::env::var` directly: process
449+
/// env is global mutable state, so a test that set real vars would race every
450+
/// other test in the binary.
451+
fn inert_env_vars<'a>(names: &[&'a str], lookup: impl Fn(&str) -> Option<String>) -> Vec<&'a str> {
452+
names
453+
.iter()
454+
.copied()
455+
.filter(|name| lookup(name).is_some())
456+
.collect()
457+
}
458+
438459
impl Config {
439460
/// Loads configuration from environment variables, falling back to development defaults.
440461
pub fn from_env() -> Result<Self, ConfigError> {
@@ -776,14 +797,13 @@ impl Config {
776797
.filter(|&v| v > 0)
777798
.unwrap_or(30);
778799

779-
let require_media_get_auth = std::env::var("BUZZ_REQUIRE_MEDIA_GET_AUTH")
780-
.map(|v| {
781-
v == "true"
782-
|| v == "1"
783-
|| v.eq_ignore_ascii_case("yes")
784-
|| v.eq_ignore_ascii_case("on")
785-
})
786-
.unwrap_or(false);
800+
for name in inert_env_vars(&INERT_MEDIA_READ_AUTH_VARS, |n| std::env::var(n).ok()) {
801+
warn!(
802+
"{name} is set but is no longer read — GET/HEAD /media/* always require \
803+
Blossom t=get auth plus relay membership. Remove it; a value of `false` \
804+
does not re-open unauthenticated media reads."
805+
);
806+
}
787807

788808
let ephemeral_ttl_override = std::env::var("BUZZ_EPHEMERAL_TTL_OVERRIDE")
789809
.ok()
@@ -1003,7 +1023,6 @@ impl Config {
10031023
media_max_concurrent_uploads,
10041024
media_max_concurrent_uploads_per_pubkey,
10051025
media_uploads_per_minute,
1006-
require_media_get_auth,
10071026
audit_enabled,
10081027
ephemeral_ttl_override,
10091028
git_repo_path,
@@ -1035,6 +1054,59 @@ mod tests {
10351054
// value set by `invalid_bind_addr_returns_error`, causing a flaky failure.
10361055
static ENV_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
10371056

1057+
/// Look up against a fixed set, standing in for process env.
1058+
fn env_of<'a>(set: &'a [(&'a str, &'a str)]) -> impl Fn(&str) -> Option<String> + use<'a> {
1059+
move |name| {
1060+
set.iter()
1061+
.find(|(key, _)| *key == name)
1062+
.map(|(_, value)| (*value).to_string())
1063+
}
1064+
}
1065+
1066+
/// The case that matters: an operator who pinned the old flag to `false`
1067+
/// must be told it is inert, not left believing media reads are still open.
1068+
#[test]
1069+
fn inert_media_read_auth_vars_are_reported_even_when_false() {
1070+
let found = inert_env_vars(
1071+
&INERT_MEDIA_READ_AUTH_VARS,
1072+
env_of(&[("BUZZ_REQUIRE_MEDIA_GET_AUTH", "false")]),
1073+
);
1074+
1075+
assert_eq!(found, vec!["BUZZ_REQUIRE_MEDIA_GET_AUTH"]);
1076+
}
1077+
1078+
/// `BUZZ_REQUIRE_MEDIA_READ_AUTH` was advertised in `.env.example` as an
1079+
/// accepted alias but the relay never read it, so operators may hold it
1080+
/// today. It warns too.
1081+
#[test]
1082+
fn inert_media_read_auth_vars_include_the_documented_alias() {
1083+
let found = inert_env_vars(
1084+
&INERT_MEDIA_READ_AUTH_VARS,
1085+
env_of(&[
1086+
("BUZZ_REQUIRE_MEDIA_GET_AUTH", "true"),
1087+
("BUZZ_REQUIRE_MEDIA_READ_AUTH", "false"),
1088+
]),
1089+
);
1090+
1091+
assert_eq!(
1092+
found,
1093+
vec![
1094+
"BUZZ_REQUIRE_MEDIA_GET_AUTH",
1095+
"BUZZ_REQUIRE_MEDIA_READ_AUTH"
1096+
]
1097+
);
1098+
}
1099+
1100+
#[test]
1101+
fn inert_media_read_auth_vars_stay_quiet_when_unset() {
1102+
let found = inert_env_vars(
1103+
&INERT_MEDIA_READ_AUTH_VARS,
1104+
env_of(&[("BUZZ_REQUIRE_RELAY_MEMBERSHIP", "true")]),
1105+
);
1106+
1107+
assert!(found.is_empty(), "unrelated vars must not warn: {found:?}");
1108+
}
1109+
10381110
#[test]
10391111
fn defaults_are_valid() {
10401112
let _guard = ENV_MUTEX.lock().unwrap();
@@ -1072,10 +1144,6 @@ mod tests {
10721144
!config.serve_git_web_gui,
10731145
"serve_git_web_gui should default to false"
10741146
);
1075-
assert!(
1076-
!config.require_media_get_auth,
1077-
"require_media_get_auth should default to false for staged client rollout"
1078-
);
10791147
assert_eq!(
10801148
config.media.s3_addressing_style,
10811149
buzz_media::config::S3AddressingStyle::Path,

‎crates/buzz-test-client/tests/conformance_multitenant.rs‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,17 +2612,27 @@ mod pubsub_presence_typing {
26122612
mod media_blossom {
26132613
use super::*;
26142614

2615-
/// Obligation: public blob `GET/HEAD /media/{sha256.ext}` stays
2616-
/// unauthenticated (N=1 compat, shared CAS bytes). The community boundary is
2617-
/// the metadata/descriptor/upload-auth/quota/audit layer: B's private upload
2618-
/// metadata/errors must not be observable from A, even when the blob bytes
2619-
/// are deduplicated and shared.
2615+
/// Obligation: blob `GET/HEAD /media/{sha256.ext}` requires Blossom read auth
2616+
/// scoped to the serving host or the blob hash, and the request is bound to the
2617+
/// tenant resolved from the request headers. A bare read is rejected before any
2618+
/// storage lookup, so the endpoint does not leak blob existence.
2619+
///
2620+
/// CAS bytes are still deduplicated across communities, so the boundary is not
2621+
/// the bytes: it is the metadata/descriptor/upload-auth/quota/audit layer plus
2622+
/// the per-tenant read binding. B's private upload metadata and errors must not
2623+
/// be observable from A even when the underlying blob is shared.
2624+
///
2625+
/// Known limitation, deferred: relay membership plus knowledge of a hash is
2626+
/// sufficient to read a blob. Read auth binds host and tenant, not the channel
2627+
/// ACL of the message the blob was attached to.
26202628
#[tokio::test]
26212629
#[ignore]
26222630
async fn media_metadata_boundary_holds_while_blob_bytes_shared() {
26232631
pending_lane(
26242632
"buzz-media",
2625-
"shared SHA bytes OK; A cannot read B's upload metadata/quota/audit; errors generic",
2633+
"reads require host/hash-scoped Blossom auth and bind to the header tenant; \
2634+
bare reads 401 before storage; shared SHA bytes OK; A cannot read B's upload \
2635+
metadata/quota/audit; errors generic",
26262636
);
26272637
}
26282638
}

0 commit comments

Comments
 (0)