Proxy podcast audio from publishers on request
Browsers stream episode audio from the publisher by default. For
publishers they cannot play directly (CORS, mixed content), the
administrator can enable podcast_audio_proxy_enabled (admin Settings,
FURU_PODCAST_AUDIO_PROXY_ENABLED). The endpoints live in a new app at
/api/podcasts:
- GET /episodes/{episode_id}/audio streams an episode of the signed-in
account. The URL comes from the account's catalog, never from the
request. Requests go through SafeHttp with one sanitized byte Range and
no client headers; 200/206/416 pass through, only audio content types
are served, with nosniff and a sandbox CSP, and responses stop at 1 GiB
or 4 hours.
- GET /episodes/{episode_id}/source returns the publisher URL and, when
enabled, the proxy URL.
- POST /{podcast_id}/refresh refreshes one subscribed feed now.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bf9a6abc3f
commit
2c0c3d21aa
12 files changed
+1164
-5
No files matched your search
@@ -0,0 +1,323 @@
|
||||
//! Audio proxy: Range pass-through, servers without ranges, redirects,
|
||||
//! content types and limits, against the local test server.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use futures_util::StreamExt;
|
||||
use futures_util::stream::BoxStream;
|
||||
|
||||
use super::net::{AddressClass, GuardError};
|
||||
use super::proxy::{AudioProxy, ProxyError, sanitize_range};
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::test_server::{Route, ScriptedLookup, TestServer, noise, test_http};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Audio proxy
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn only_single_byte_ranges_are_forwarded() {
|
||||
assert_eq!(sanitize_range("bytes=0-").as_deref(), Some("bytes=0-"));
|
||||
assert_eq!(
|
||||
sanitize_range("bytes=100-199").as_deref(),
|
||||
Some("bytes=100-199")
|
||||
);
|
||||
assert_eq!(
|
||||
sanitize_range(" bytes=-500 ").as_deref(),
|
||||
Some("bytes=-500")
|
||||
);
|
||||
for invalid in [
|
||||
"bytes=1-2,5-6",
|
||||
"items=0-10",
|
||||
"bytes=5-1",
|
||||
"bytes=-",
|
||||
"bytes=-0",
|
||||
"bytes=a-b",
|
||||
"bytes=1-2\r\nCookie: x",
|
||||
"bytes=99999999999999999999999-",
|
||||
"",
|
||||
] {
|
||||
assert_eq!(sanitize_range(invalid), None, "{invalid:?}");
|
||||
}
|
||||
}
|
||||
|
||||
async fn collect(
|
||||
mut body: BoxStream<'static, Result<bytes::Bytes, ProxyError>>,
|
||||
) -> (Vec<u8>, Option<String>) {
|
||||
let mut out = Vec::new();
|
||||
while let Some(chunk) = body.next().await {
|
||||
match chunk {
|
||||
Ok(chunk) => out.extend_from_slice(&chunk),
|
||||
Err(error) => return (out, Some(error.to_string())),
|
||||
}
|
||||
}
|
||||
(out, None)
|
||||
}
|
||||
|
||||
fn proxy(lookup: Arc<ScriptedLookup>) -> AudioProxy {
|
||||
AudioProxy::with_http(test_http(lookup))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_passes_ranges_through() {
|
||||
let server = TestServer::start();
|
||||
let audio = noise(300_000, 7);
|
||||
server.set("/ep.mp3", Route::new(audio.clone(), "audio/mpeg"));
|
||||
let proxy = proxy(ScriptedLookup::with(&[]));
|
||||
let target = server.url("/ep.mp3");
|
||||
|
||||
let full = proxy.fetch(&target, None, None).await.unwrap();
|
||||
assert_eq!(full.status, 200);
|
||||
assert_eq!(full.content_type.as_deref(), Some("audio/mpeg"));
|
||||
assert_eq!(full.content_length, Some(audio.len() as u64));
|
||||
assert!(full.accept_ranges);
|
||||
assert_eq!(full.content_range, None);
|
||||
assert_eq!(collect(full.body).await, (audio.clone(), None));
|
||||
|
||||
for (range, start, end) in [
|
||||
("bytes=1000-1999", 1000, 2000),
|
||||
("bytes=299000-", 299_000, 300_000),
|
||||
("bytes=-500", 299_500, 300_000),
|
||||
("bytes=0-", 0, 300_000),
|
||||
] {
|
||||
let part = proxy.fetch(&target, None, Some(range)).await.unwrap();
|
||||
assert_eq!(part.status, 206, "{range}");
|
||||
assert_eq!(
|
||||
part.content_range,
|
||||
Some(format!("bytes {start}-{}/{}", end - 1, audio.len())),
|
||||
"{range}"
|
||||
);
|
||||
assert_eq!(part.content_length, Some((end - start) as u64));
|
||||
assert_eq!(collect(part.body).await, (audio[start..end].to_vec(), None));
|
||||
}
|
||||
|
||||
let unsatisfiable = proxy
|
||||
.fetch(&target, None, Some("bytes=400000-"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(unsatisfiable.status, 416);
|
||||
assert_eq!(
|
||||
unsatisfiable.content_range,
|
||||
Some(format!("bytes */{}", audio.len()))
|
||||
);
|
||||
assert_eq!(collect(unsatisfiable.body).await, (Vec::new(), None));
|
||||
|
||||
// Malformed or multi-range requests fetch the whole file.
|
||||
let whole = proxy
|
||||
.fetch(&target, None, Some("bytes=5-6,9-10"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(whole.status, 200);
|
||||
|
||||
let requests = server.requests("/ep.mp3");
|
||||
let ranges: Vec<Option<&str>> = requests
|
||||
.iter()
|
||||
.map(|request| request.get("range").map(String::as_str))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
ranges,
|
||||
vec![
|
||||
None,
|
||||
Some("bytes=1000-1999"),
|
||||
Some("bytes=299000-"),
|
||||
Some("bytes=-500"),
|
||||
Some("bytes=0-"),
|
||||
Some("bytes=400000-"),
|
||||
None,
|
||||
]
|
||||
);
|
||||
for request in &requests {
|
||||
assert_eq!(request["accept-encoding"], "identity");
|
||||
assert!(!request.contains_key("cookie"));
|
||||
assert!(!request.contains_key("authorization"));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_handles_servers_without_ranges_and_redirects() {
|
||||
let server = TestServer::start();
|
||||
let audio = noise(50_000, 3);
|
||||
server.set(
|
||||
"/plain.mp3",
|
||||
Route::new(audio.clone(), "audio/mpeg").no_ranges(),
|
||||
);
|
||||
server.set("/track/ep.mp3", Route::redirect(302, "/cdn/ep.mp3"));
|
||||
server.set("/cdn/ep.mp3", Route::new(audio.clone(), "audio/mpeg"));
|
||||
server.set(
|
||||
"/track/private.mp3",
|
||||
Route::redirect(302, "http://172.20.0.3/ep.mp3"),
|
||||
);
|
||||
let proxy = proxy(ScriptedLookup::with(&[]));
|
||||
|
||||
// No range support: the whole file with 200 and no Accept-Ranges, so the
|
||||
// browser knows it cannot seek by range.
|
||||
let plain = proxy
|
||||
.fetch(&server.url("/plain.mp3"), None, Some("bytes=100-"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(plain.status, 200);
|
||||
assert!(!plain.accept_ranges);
|
||||
assert_eq!(plain.content_range, None);
|
||||
assert_eq!(collect(plain.body).await.0, audio);
|
||||
|
||||
// Tracking redirects keep the range on every hop.
|
||||
let part = proxy
|
||||
.fetch(&server.url("/track/ep.mp3"), None, Some("bytes=10-19"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(part.status, 206);
|
||||
assert_eq!(collect(part.body).await.0, audio[10..20].to_vec());
|
||||
assert_eq!(server.requests("/track/ep.mp3")[0]["range"], "bytes=10-19");
|
||||
assert_eq!(server.requests("/cdn/ep.mp3")[0]["range"], "bytes=10-19");
|
||||
|
||||
match proxy
|
||||
.fetch(&server.url("/track/private.mp3"), None, None)
|
||||
.await
|
||||
{
|
||||
Err(ProxyError::Upstream(GuardError::Blocked(blocked))) => {
|
||||
assert_eq!(blocked.class, AddressClass::Private)
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_only_serves_audio() {
|
||||
let server = TestServer::start();
|
||||
let audio = noise(1000, 5);
|
||||
server.set(
|
||||
"/page.mp3",
|
||||
Route::new("<script>alert(1)</script>", "text/html"),
|
||||
);
|
||||
server.set("/video.mp4", Route::new(audio.clone(), "video/mp4"));
|
||||
server.set(
|
||||
"/binary",
|
||||
Route::new(audio.clone(), "application/octet-stream"),
|
||||
);
|
||||
server.set(
|
||||
"/binary.m4a",
|
||||
Route::new(audio.clone(), "binary/octet-stream"),
|
||||
);
|
||||
server.set(
|
||||
"/untyped.ogg",
|
||||
Route::new(audio.clone(), "x").no_content_type(),
|
||||
);
|
||||
server.set("/ogg", Route::new(audio.clone(), "application/ogg"));
|
||||
server.set(
|
||||
"/typed",
|
||||
Route::new(audio.clone(), "audio/mp4; codecs=\"mp4a.40.2\""),
|
||||
);
|
||||
server.set("/missing", Route::new("", "text/plain").status(404));
|
||||
server.set("/error", Route::new("", "audio/mpeg").status(500));
|
||||
let proxy = proxy(ScriptedLookup::with(&[]));
|
||||
let content_type = |path: &'static str, stored: Option<&'static str>| {
|
||||
let proxy = proxy.clone();
|
||||
let target = server.url(path);
|
||||
async move {
|
||||
proxy
|
||||
.fetch(&target, stored, None)
|
||||
.await
|
||||
.map(|audio| audio.content_type.unwrap())
|
||||
}
|
||||
};
|
||||
assert!(matches!(
|
||||
content_type("/page.mp3", Some("audio/mpeg")).await,
|
||||
Err(ProxyError::ContentType(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
content_type("/video.mp4", Some("audio/mpeg")).await,
|
||||
Err(ProxyError::ContentType(_))
|
||||
));
|
||||
assert_eq!(
|
||||
content_type("/binary", Some("audio/mpeg")).await.unwrap(),
|
||||
"audio/mpeg"
|
||||
);
|
||||
assert!(matches!(
|
||||
content_type("/binary", Some("text/html")).await,
|
||||
Err(ProxyError::ContentType(_))
|
||||
));
|
||||
assert_eq!(
|
||||
content_type("/binary.m4a", None).await.unwrap(),
|
||||
"audio/mp4"
|
||||
);
|
||||
assert_eq!(
|
||||
content_type("/untyped.ogg", None).await.unwrap(),
|
||||
"audio/ogg"
|
||||
);
|
||||
assert_eq!(content_type("/ogg", None).await.unwrap(), "application/ogg");
|
||||
assert_eq!(
|
||||
content_type("/typed", None).await.unwrap(),
|
||||
"audio/mp4; codecs=\"mp4a.40.2\""
|
||||
);
|
||||
let missing = content_type("/missing", None).await.unwrap_err();
|
||||
assert!(matches!(missing, ProxyError::Status(404)));
|
||||
assert_eq!(missing.http_status(), 502);
|
||||
assert!(matches!(
|
||||
content_type("/error", None).await,
|
||||
Err(ProxyError::Status(500))
|
||||
));
|
||||
assert!(matches!(
|
||||
proxy.fetch("not a url", None, None).await,
|
||||
Err(ProxyError::InvalidUrl(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_limits_size_and_reports_dropped_streams() {
|
||||
let server = TestServer::start();
|
||||
let audio = noise(100_000, 9);
|
||||
server.set("/sized.mp3", Route::new(audio.clone(), "audio/mpeg"));
|
||||
server.set(
|
||||
"/chunked.mp3",
|
||||
Route::new(audio.clone(), "audio/mpeg").no_length(),
|
||||
);
|
||||
server.set(
|
||||
"/cut.mp3",
|
||||
Route::new(audio.clone(), "audio/mpeg").cut(40_000),
|
||||
);
|
||||
let limited = proxy(ScriptedLookup::with(&[])).with_limits(60_000, Duration::from_secs(60));
|
||||
assert!(matches!(
|
||||
limited.fetch(&server.url("/sized.mp3"), None, None).await,
|
||||
Err(ProxyError::TooLarge(100_000))
|
||||
));
|
||||
// A range below the limit is fine.
|
||||
let part = limited
|
||||
.fetch(&server.url("/sized.mp3"), None, Some("bytes=0-49999"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(collect(part.body).await, (audio[..50_000].to_vec(), None));
|
||||
// Without a declared length the stream stops at the limit.
|
||||
let chunked = limited
|
||||
.fetch(&server.url("/chunked.mp3"), None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
let (received, error) = collect(chunked.body).await;
|
||||
assert!(received.len() <= 60_000);
|
||||
assert!(error.unwrap().contains("size limit"));
|
||||
// A connection dropped mid-body ends the stream with an error instead
|
||||
// of a silently truncated success.
|
||||
let cut = proxy(ScriptedLookup::with(&[]))
|
||||
.fetch(&server.url("/cut.mp3"), None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
let (received, error) = collect(cut.body).await;
|
||||
assert!(received.len() <= 40_000);
|
||||
assert!(error.is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_stream_has_a_time_limit() {
|
||||
let server = TestServer::start();
|
||||
server.set(
|
||||
"/slow.mp3",
|
||||
Route::new(noise(10, 1), "audio/mpeg").delay_body(Duration::from_secs(3)),
|
||||
);
|
||||
let limited = proxy(ScriptedLookup::with(&[])).with_limits(1 << 20, Duration::from_millis(300));
|
||||
let audio = limited
|
||||
.fetch(&server.url("/slow.mp3"), None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
let (_, error) = collect(audio.body).await;
|
||||
assert!(error.unwrap().contains("time limit"));
|
||||
}
|
||||
Reference in new issue
Block a user