Bound the episodes the browser can add outside the account's feeds
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0aead73135
commit
f87d70c593
5 files changed
+120
-6
No files matched your search
@@ -128,10 +128,14 @@ episodes. Accounts subscribed to the same feed share one request. Each run
|
||||
checks at most 200 feeds (2400 an hour), taking accounts in turns: first the
|
||||
most overdue feed of every account, then the second, and so on. An account
|
||||
with many or slow feeds therefore does not delay the others, but can use the
|
||||
capacity nobody else needs. An account can add up to 1000 subscriptions, and
|
||||
can have two feed downloads ("add RSS", Refresh) and four proxied audio
|
||||
streams in progress at once (16 and 64 for the whole server); beyond that the
|
||||
request is refused with HTTP 429.
|
||||
capacity nobody else needs.
|
||||
|
||||
An account can add up to 1000 subscriptions and, from the browser, up to
|
||||
10 000 episodes outside its feeds (handed-off queue items; episodes synced
|
||||
from its own devices are not limited); beyond that the request is refused
|
||||
with HTTP 422. It can have two feed downloads ("add RSS", Refresh) and four
|
||||
proxied audio streams in progress at once (16 and 64 for the whole server);
|
||||
beyond that the request is refused with HTTP 429.
|
||||
|
||||
The server therefore makes outgoing HTTP(S) requests to podcast publishers.
|
||||
It only connects to public addresses on the web ports 80, 443, 8080 and 8443:
|
||||
|
||||
+20
-1
@@ -638,7 +638,9 @@ impl PodcastStore {
|
||||
/// `PodcastEpisodeRef`; the account may not have refreshed that feed yet.
|
||||
/// The reference is recorded like one learned through sync
|
||||
/// (`source = 'sync'`, never replacing feed metadata). Returns whether a
|
||||
/// row was added. No register is written.
|
||||
/// row was added. No register is written. A new row is refused with
|
||||
/// [`PodcastStoreError::ReferenceLimit`] once the account has
|
||||
/// [`super::MAX_REFERENCE_EPISODES_PER_ACCOUNT`] such episodes.
|
||||
pub(crate) async fn ensure_episode(
|
||||
&self,
|
||||
user_id: i64,
|
||||
@@ -650,6 +652,23 @@ impl PodcastStore {
|
||||
.await?
|
||||
.is_some();
|
||||
if !known {
|
||||
// Counted under the account's write lock: concurrent requests
|
||||
// cannot pass the limit together.
|
||||
let references: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM furumusic__podcast_episode
|
||||
WHERE user_id = $1 AND source = 'sync'",
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(&mut *tx)
|
||||
.await?;
|
||||
if usize::try_from(references).unwrap_or(usize::MAX)
|
||||
>= super::MAX_REFERENCE_EPISODES_PER_ACCOUNT
|
||||
{
|
||||
return Err(PodcastStoreError::ReferenceLimit(
|
||||
super::MAX_REFERENCE_EPISODES_PER_ACCOUNT,
|
||||
)
|
||||
.into());
|
||||
}
|
||||
apply::ensure_episode_from_ref(&mut tx, user_id, episode).await?;
|
||||
}
|
||||
tx.commit().await?;
|
||||
|
||||
+6
-1
@@ -73,7 +73,9 @@ impl From<anyhow::Error> for ApiError {
|
||||
PodcastStoreError::UnknownPodcast(_) => Self::not_found(error.to_string()),
|
||||
PodcastStoreError::NotSubscribed(_) => Self::new(409, error.to_string()),
|
||||
PodcastStoreError::Invalid(_) => Self::bad_request(error.to_string()),
|
||||
PodcastStoreError::SubscriptionLimit(_) => Self::new(422, error.to_string()),
|
||||
PodcastStoreError::SubscriptionLimit(_) | PodcastStoreError::ReferenceLimit(_) => {
|
||||
Self::new(422, error.to_string())
|
||||
}
|
||||
};
|
||||
}
|
||||
if let Some(SubscribeUrlError::Fetch(fetch)) = error.downcast_ref::<SubscribeUrlError>() {
|
||||
@@ -989,6 +991,9 @@ mod tests {
|
||||
anyhow::Error::from(PodcastStoreError::SubscriptionLimit(1000)).into();
|
||||
assert_eq!(limit.status, 422);
|
||||
assert_eq!(limit.message, "subscription limit reached (1000 podcasts)");
|
||||
let references: ApiError =
|
||||
anyhow::Error::from(PodcastStoreError::ReferenceLimit(10_000)).into();
|
||||
assert_eq!(references.status, 422);
|
||||
let refused: ApiError =
|
||||
anyhow::Error::from(SubscribeUrlError::Fetch(super::super::fetch::FetchError {
|
||||
kind: FetchErrorKind::Refused,
|
||||
|
||||
@@ -115,6 +115,9 @@ pub(crate) enum PodcastStoreError {
|
||||
Invalid(String),
|
||||
/// A new subscription would exceed [`MAX_SUBSCRIPTIONS_PER_ACCOUNT`].
|
||||
SubscriptionLimit(usize),
|
||||
/// A new episode reference from the browser would exceed
|
||||
/// [`MAX_REFERENCE_EPISODES_PER_ACCOUNT`].
|
||||
ReferenceLimit(usize),
|
||||
}
|
||||
|
||||
/// Active subscriptions an account may add locally (subscriptions synced
|
||||
@@ -122,6 +125,13 @@ pub(crate) enum PodcastStoreError {
|
||||
/// is a feed the server downloads about once an hour.
|
||||
pub(crate) const MAX_SUBSCRIPTIONS_PER_ACCOUNT: usize = 1000;
|
||||
|
||||
/// Episodes known only from references (`source = 'sync'`: not from one of
|
||||
/// the account's feeds) beyond which the browser cannot add another one
|
||||
/// through `POST /api/podcasts/episodes/{id}/playback`. References in
|
||||
/// operations of the account's own devices are always applied; this bounds
|
||||
/// what a signed-in user can insert directly, one request at a time.
|
||||
pub(crate) const MAX_REFERENCE_EPISODES_PER_ACCOUNT: usize = 10_000;
|
||||
|
||||
impl fmt::Display for PodcastStoreError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
@@ -131,6 +141,10 @@ impl fmt::Display for PodcastStoreError {
|
||||
Self::SubscriptionLimit(limit) => {
|
||||
write!(f, "subscription limit reached ({limit} podcasts)")
|
||||
}
|
||||
Self::ReferenceLimit(limit) => write!(
|
||||
f,
|
||||
"too many episodes from outside your feeds ({limit}); subscribe to the podcast to play it"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2470,3 +2470,75 @@ async fn postgres_podcast_playback_resumes_from_the_register() {
|
||||
|
||||
cleanup(&pool, [user]).await;
|
||||
}
|
||||
|
||||
/// The browser can add episodes outside the account's feeds (handed-off
|
||||
/// queue items) only up to a limit; the account's own devices are not
|
||||
/// limited, and known episodes always play.
|
||||
#[tokio::test]
|
||||
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
||||
async fn postgres_podcast_episode_references_from_the_browser_are_bounded() {
|
||||
use serde_json::json;
|
||||
|
||||
let pool = test_pool().await;
|
||||
let store = PodcastStore::new(pool.clone());
|
||||
let user = random_user();
|
||||
let first = PodcastEpisodeRef {
|
||||
progress_generation: None,
|
||||
..test_episode()
|
||||
};
|
||||
let reference = |guid: &str| PodcastEpisodeRef {
|
||||
episode_id: EpisodeId::from_guid(&first.podcast_id, guid).unwrap(),
|
||||
episode_title: guid.into(),
|
||||
..first.clone()
|
||||
};
|
||||
let play = |episode: PodcastEpisodeRef| {
|
||||
let store = store.clone();
|
||||
async move {
|
||||
api::playback(
|
||||
&store,
|
||||
user,
|
||||
&episode.episode_id.clone(),
|
||||
json!({ "episode": episode }),
|
||||
false,
|
||||
)
|
||||
.await
|
||||
}
|
||||
};
|
||||
play(first.clone()).await.unwrap();
|
||||
// Fill up to one below the limit with copies of that reference row.
|
||||
sqlx::query(
|
||||
"INSERT INTO furumusic__podcast_episode
|
||||
SELECT (jsonb_populate_record(e, jsonb_build_object('episode_id', 'cap-' || n))).*
|
||||
FROM furumusic__podcast_episode e, generate_series(1, $3) AS n
|
||||
WHERE e.user_id = $1 AND e.episode_id = $2",
|
||||
)
|
||||
.bind(user)
|
||||
.bind(first.episode_id.as_str())
|
||||
.bind(i32::try_from(MAX_REFERENCE_EPISODES_PER_ACCOUNT - 2).unwrap())
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
play(reference("last-allowed")).await.unwrap();
|
||||
let refused = play(reference("one-too-many")).await.unwrap_err();
|
||||
assert_eq!(refused.status, 422, "{}", refused.message);
|
||||
let unknown = reference("one-too-many").episode_id;
|
||||
assert!(store.episode_ref(user, &unknown).await.unwrap().is_none());
|
||||
// Episodes already known still play.
|
||||
play(first.clone()).await.unwrap();
|
||||
play(reference("last-allowed")).await.unwrap();
|
||||
// References in the account's own devices' operations are applied.
|
||||
let synced = reference("from-the-phone");
|
||||
store
|
||||
.apply_remote_op(user, &progress_op("phone", 1, 50, &synced, 1, 60_000))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
store
|
||||
.episode_ref(user, &synced.episode_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_some()
|
||||
);
|
||||
|
||||
cleanup(&pool, [user]).await;
|
||||
}
|
||||
Reference in new issue
Block a user