Keep Jam guests from removing or timing the host's private items
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d0b352258
commit
a9736e5914
1 file changed
+135
-2
+135
-2
@@ -1741,6 +1741,100 @@ mod queue_persistence_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod jam_command_tests {
|
||||
use super::*;
|
||||
use music_dht::device_sync::{PRIVATE_ITEM_TITLE, PlaybackTrack};
|
||||
|
||||
fn library(id: i64) -> serde_json::Value {
|
||||
serde_json::json!({"id": id, "title": format!("Song {id}"), "duration_seconds": 100.0})
|
||||
}
|
||||
|
||||
fn host_tracks() -> Vec<serde_json::Value> {
|
||||
let episode = PlaybackTrack::podcast_episode(queue_items::test_episode());
|
||||
vec![
|
||||
library(1),
|
||||
queue_items::external_track_json(&episode, 1, None),
|
||||
library(2),
|
||||
]
|
||||
}
|
||||
|
||||
fn check(command: &str, payload: serde_json::Value) -> Result<(), &'static str> {
|
||||
let mut payload = payload;
|
||||
restore_jam_command_payload(command, &mut payload, &host_tracks())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn participants_cannot_remove_or_clear_private_items() {
|
||||
assert!(check("queue_remove", serde_json::json!({"index": 1})).is_err());
|
||||
assert!(check("queue_clear", serde_json::json!({})).is_err());
|
||||
// Music can still be removed, and private items moved.
|
||||
assert!(check("queue_remove", serde_json::json!({"index": 0})).is_ok());
|
||||
assert!(check("queue_remove", serde_json::json!({"index": 2})).is_ok());
|
||||
assert!(
|
||||
check(
|
||||
"queue_move",
|
||||
serde_json::json!({"from_index": 1, "to_index": 0})
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
// The host runs `Number(index)`: other spellings of an index are refused.
|
||||
for index in [
|
||||
serde_json::json!("1"),
|
||||
serde_json::json!(1.0),
|
||||
serde_json::json!(-1),
|
||||
] {
|
||||
assert!(check("queue_remove", serde_json::json!({"index": index})).is_err());
|
||||
}
|
||||
assert!(check("queue_remove", serde_json::json!({})).is_err());
|
||||
// Without private items the host queue can be cleared as before.
|
||||
let mut payload = serde_json::json!({});
|
||||
assert!(restore_jam_command_payload("queue_clear", &mut payload, &[library(1)]).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redacted_current_episode_has_no_duration() {
|
||||
let tracks = host_tracks();
|
||||
let playback = PlayerDevicePlaybackStateDto {
|
||||
track: Some(tracks[1].clone()),
|
||||
tracks: tracks.clone(),
|
||||
index: 1,
|
||||
position_seconds: 1200.0,
|
||||
duration_seconds: 1800.0,
|
||||
paused: false,
|
||||
shuffle: false,
|
||||
repeat_mode: "off".into(),
|
||||
volume: 0.5,
|
||||
updated_at_ms: 0,
|
||||
};
|
||||
let redacted = redact_playback_state_for_jam(playback.clone());
|
||||
assert_eq!(redacted.duration_seconds, 0.0);
|
||||
assert_eq!(
|
||||
redacted.track.as_ref().unwrap()["title"],
|
||||
PRIVATE_ITEM_TITLE
|
||||
);
|
||||
assert_eq!(redacted.position_seconds, 1200.0);
|
||||
let leaked = serde_json::to_string(&redacted).unwrap();
|
||||
assert!(!leaked.contains("1800") && !leaked.contains("Episode"));
|
||||
|
||||
// A music track keeps its duration.
|
||||
let music = redact_playback_state_for_jam(PlayerDevicePlaybackStateDto {
|
||||
track: Some(tracks[0].clone()),
|
||||
index: 0,
|
||||
duration_seconds: 100.0,
|
||||
..playback.clone()
|
||||
});
|
||||
assert_eq!(music.duration_seconds, 100.0);
|
||||
|
||||
// Without `track` the current item is taken from `tracks[index]`.
|
||||
let untracked = redact_playback_state_for_jam(PlayerDevicePlaybackStateDto {
|
||||
track: None,
|
||||
..playback
|
||||
});
|
||||
assert_eq!(untracked.duration_seconds, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod page_template_tests {
|
||||
use super::*;
|
||||
@@ -6102,13 +6196,23 @@ async fn fed_devices_sync_handler(
|
||||
fn redact_playback_state_for_jam(
|
||||
mut playback: PlayerDevicePlaybackStateDto,
|
||||
) -> PlayerDevicePlaybackStateDto {
|
||||
let index = usize::try_from(playback.index).unwrap_or(0);
|
||||
let current_is_private = playback
|
||||
.track
|
||||
.as_ref()
|
||||
.or_else(|| playback.tracks.get(index))
|
||||
.is_some_and(queue_items::browser_track_requires_podcast_support);
|
||||
playback.tracks = queue_items::redact_tracks_for_jam(&playback.tracks);
|
||||
if current_is_private {
|
||||
// Placeholders carry no duration; the host's position is kept like
|
||||
// `redact_for_jam` keeps `position_secs`.
|
||||
playback.duration_seconds = 0.0;
|
||||
}
|
||||
if playback
|
||||
.track
|
||||
.as_ref()
|
||||
.is_some_and(queue_items::browser_track_requires_podcast_support)
|
||||
{
|
||||
let index = usize::try_from(playback.index).unwrap_or(0);
|
||||
playback.track = playback.tracks.get(index).cloned().or_else(|| {
|
||||
Some(
|
||||
queue_items::redact_tracks_for_jam(std::slice::from_ref(playback.track.as_ref()?))
|
||||
@@ -6125,7 +6229,8 @@ fn redact_playback_state_for_jam(
|
||||
/// Participants cannot add podcast episodes. A full queue they send (play or
|
||||
/// transfer) gets the host's private items back in place of the
|
||||
/// placeholders; queues that drop, duplicate or inject private items are
|
||||
/// rejected.
|
||||
/// rejected. Participants cannot remove a private item, nor clear a queue
|
||||
/// that holds one; they may still move it (`queue_move`).
|
||||
fn restore_jam_command_payload(
|
||||
command: &str,
|
||||
payload: &mut serde_json::Value,
|
||||
@@ -6183,6 +6288,34 @@ fn restore_jam_command_payload(
|
||||
object.insert("tracks".to_string(), serde_json::Value::Array(restored));
|
||||
}
|
||||
}
|
||||
"queue_remove" => {
|
||||
// The participant view has the host's length and order, so the
|
||||
// index names the same item on the host.
|
||||
let index = payload
|
||||
.get("index")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
.and_then(|index| usize::try_from(index).ok());
|
||||
let removes_private = match index {
|
||||
Some(index) => host_tracks
|
||||
.get(index)
|
||||
.is_some_and(queue_items::browser_track_requires_podcast_support),
|
||||
// The host browser runs `Number(index)`, so "1" or 1.0 would
|
||||
// still name an item: only plain indexes pass a private queue.
|
||||
None => host_tracks
|
||||
.iter()
|
||||
.any(queue_items::browser_track_requires_podcast_support),
|
||||
};
|
||||
if removes_private {
|
||||
return Err(REJECTED);
|
||||
}
|
||||
}
|
||||
"queue_clear"
|
||||
if host_tracks
|
||||
.iter()
|
||||
.any(queue_items::browser_track_requires_podcast_support) =>
|
||||
{
|
||||
return Err(REJECTED);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
Reference in new issue
Block a user