fix(downloads): trust the audio bytes over a provider's FLAC label

A provider could label a download as FLAC while returning other bytes,
and the backend then wrote FLAC tags into them and failed with "fLaC
head incorrect" (Mornye-Desktop issue #6), ending the whole download.
The download probe now reads the container from the file itself: FLAC,
including behind an ID3v2 tag, MP4 or MP3. A FLAC label on MP4 or MP3
bytes yields to the bytes, so the file is published with its real
suffix and FLAC in MP4 is still remuxed by the app. Unrecognized bytes
behind a FLAC label, such as an error page, fail that provider with a
clear message so the next provider can try.
This commit is contained in:
zarzet committed 2026-10-24 10:47:45 +07:00
1 parent 6eaffa5942
commit 22a0bee300
5 files changed
+182 -59

No files matched your search

+52
View File
@@ -68,6 +68,37 @@ fn read_container_format<'a>(
Ok(container)
}
/// The container a file's bytes begin with, for names that cannot be trusted:
/// "flac" (also behind an ID3v2 tag, as the FLAC tag writer accepts), "m4a",
/// "mp3", or "" for anything else.
pub fn sniff_container(reader: &mut (impl Read + Seek)) -> Result<&'static str, String> {
let mut header = [0; 10];
let length = read_prefix(reader, 0, &mut header)?;
let mut start = [0; 4];
let found = match id3::v2_tag_length(&header[..length]) {
Some(tag) => read_prefix(reader, tag, &mut start)?,
None => {
let found = length.min(start.len());
start[..found].copy_from_slice(&header[..found]);
found
}
};
let container = if found == 4 && &start == b"fLaC" {
"flac"
} else if length >= 8 && &header[4..8] == b"ftyp" {
"m4a"
} else if found >= 2 && start[0] == 0xff && start[1] & 0xe6 == 0xe2 {
// MPEG audio Layer III. ADTS shares the sync word with layer 0.
"mp3"
} else {
""
};
reader
.seek(SeekFrom::Start(0))
.map_err(|error| error.to_string())?;
Ok(container)
}
/// Fills as much of `data` as the file has from `offset`, without treating a
/// short file as an error.
fn read_prefix(
@@ -312,6 +343,27 @@ mod tests {
use std::collections::BTreeMap;
use std::io::Cursor;
#[test]
fn sniff_container_reads_the_bytes_instead_of_the_name() {
let tag = b"ID3\x04\0\0\0\0\0\x02\0\0";
for (bytes, expected) in [
(b"fLaC\x80\0\0\x22".to_vec(), "flac"),
([tag.as_slice(), b"fLaC"].concat(), "flac"),
(b"\0\0\0\x18ftypM4A ".to_vec(), "m4a"),
(b"\xff\xfb\x90\x00".to_vec(), "mp3"),
([tag.as_slice(), b"\xff\xf3\x90\x00"].concat(), "mp3"),
// ADTS AAC and text bodies are neither FLAC nor MP3.
(b"\xff\xf1\x50\x80".to_vec(), ""),
(b"<!DOCTYPE html>".to_vec(), ""),
(b"fL".to_vec(), ""),
(Vec::new(), ""),
] {
let mut reader = Cursor::new(&bytes);
assert_eq!(sniff_container(&mut reader).unwrap(), expected, "{bytes:?}");
assert_eq!(reader.position(), 0);
}
}
#[test]
fn library_scan_preserves_replaygain_values_written_to_flac() {
let mut source = b"fLaC\x80\0\0\x22".to_vec();
@@ -838,7 +838,7 @@ impl Backend {
self.enrich_download_extended(&mut enriched, &check)?;
}
let request = &enriched;
probe_result(
let container = probe_result(
&files,
&mut result,
request.album_name.trim().is_empty()
@@ -849,6 +849,20 @@ impl Backend {
return success(request, &result, &returned, true, manifest, &check)
.map(DownloadOutcome::Final);
}
let mut response = success(request, &result, &path, false, manifest, &check)?;
if text(&response, "actual_extension") == ".flac" && container != "flac" {
// Unrecognized bytes behind a FLAC label, such as an error page,
// cannot take FLAC tags. Another provider may return real audio.
return Ok(DownloadOutcome::Retry(failure(
&manifest.name,
&format!(
"provider {} returned a file that is not FLAC audio",
manifest.name
),
"file_error",
0,
)));
}
if !request.item_id.is_empty() {
let _ = environment
.download_state()
@@ -856,7 +870,6 @@ impl Backend {
.finalizing(&request.item_id);
}
let mut response = success(request, &result, &path, false, manifest, &check)?;
let mut resolved = request.clone();
resolved.output_ext = text(&response, "actual_extension").to_owned();
if !request.output_path.is_empty() {
@@ -37,6 +37,15 @@ fn provider_format_fields_and_returned_path_override_the_planned_extension() {
json!({"file_path":"staged/Track.m4a","actual_extension":".mp3","output_extension":".flac"}),
".mp3",
),
// A FLAC label yields to the container its bytes were probed as.
(
json!({"file_path":"staged/Track.flac","actual_extension":".flac","actual_container":"m4a"}),
".m4a",
),
(
json!({"file_path":"staged/Track.flac","output_extension":"flac","actual_container":"mp3"}),
".mp3",
),
(json!({}), ".flac"),
] {
let response = success(
@@ -64,54 +73,26 @@ fn downloads_publish_the_actual_container_for_regular_and_explicit_working_paths
("opus", b"OggSexample audio".as_slice(), ".opus"),
// A legacy provider may leave the requested suffix on MP4 bytes.
("flac", b"\0\0\0\x18ftypM4A fixture".as_slice(), ".m4a"),
// Or on MP3 bytes, which FLAC tags could not be written into.
(
"flac",
b"ID3\x04\0\0\0\0\0\0\xff\xfb\x90\0fixture".as_slice(),
".mp3",
),
] {
for explicit_path in [false, true] {
let directory = tempfile::tempdir().unwrap();
let source = directory.path().join("sources/example.audio");
std::fs::create_dir_all(&source).unwrap();
std::fs::write(
source.join("manifest.json"),
json!({"name":"example.audio","version":"1",
"description":"Generic native-container fixture","type":["download_provider"],
"permissions":{"file":true},"skipMetadataEnrichment":true})
.to_string(),
)
.unwrap();
let encoded = base64::engine::general_purpose::STANDARD.encode(payload);
std::fs::write(
source.join("index.js"),
format!(
r#"registerExtension({{checkAvailability(){{return {{available:true,track_id:'track'}};}},
download(trackId,quality,outputPath){{
const path=outputPath.replace(/\.[^.]+$/,'.{returned_extension}');
let (backend, output) = container_fixture(
directory.path(),
&format!(
"const path=outputPath.replace(/\\.[^.]+$/,'.{returned_extension}');
file.writeBytes(path,{encoded});
return {{success:true,file_path:path}};}}}});"#,
return {{success:true,file_path:path}};",
encoded = json!(encoded),
),
)
.unwrap();
let backend = Backend::new(
&directory.path().join("sources"),
&directory.path().join("data"),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
"1",
RuntimeLimits::default(),
)
.unwrap();
backend.load_all().unwrap();
backend.set_enabled("example.audio", true).unwrap();
backend.download_manifest("example.audio").unwrap();
let output = directory.path().join("output");
std::fs::create_dir_all(&output).unwrap();
backend
.environment()
.set_allowed_download_directories(std::slice::from_ref(&output))
.unwrap();
let mut request = json!({"service":"example.audio","provider_track_id":"track",
"use_extensions":true,"use_fallback":false,"output_dir":output,
"track_name":"Track","artist_name":"Artist","album_name":"Album",
"filename_format":"{title}","output_ext":".flac","quality":"LOSSLESS",
"embed_metadata":returned_extension == "flac","embed_lyrics":false});
);
let mut request = container_request(&output, returned_extension == "flac");
if explicit_path {
request["output_path"] = output.join("working.flac").to_string_lossy().into();
}
@@ -140,3 +121,77 @@ fn downloads_publish_the_actual_container_for_regular_and_explicit_working_paths
}
}
}
#[test]
fn a_flac_label_on_unrecognized_bytes_fails_without_publishing() {
let directory = tempfile::tempdir().unwrap();
let encoded = base64::engine::general_purpose::STANDARD.encode(b"<!DOCTYPE html>");
let (backend, output) = container_fixture(
directory.path(),
&format!(
"file.writeBytes(outputPath,{encoded});
return {{success:true,file_path:outputPath,actual_extension:'.flac'}};",
encoded = json!(encoded),
),
);
let raw = backend
.download_by_strategy(&container_request(&output, true).to_string(), &|| Ok(()))
.unwrap();
let response: Value = serde_json::from_str(&raw).unwrap();
assert_eq!(response["success"], false, "{response}");
assert!(
text(&response, "error").contains("is not FLAC audio"),
"{response}"
);
assert_eq!(std::fs::read_dir(&output).unwrap().count(), 0);
backend.shutdown();
}
/// A download provider running `body` as its download function, with an
/// allowed output directory.
fn container_fixture(directory: &Path, body: &str) -> (Backend, std::path::PathBuf) {
let source = directory.join("sources/example.audio");
std::fs::create_dir_all(&source).unwrap();
std::fs::write(
source.join("manifest.json"),
json!({"name":"example.audio","version":"1",
"description":"Generic native-container fixture","type":["download_provider"],
"permissions":{"file":true},"skipMetadataEnrichment":true})
.to_string(),
)
.unwrap();
std::fs::write(
source.join("index.js"),
format!(
"registerExtension({{checkAvailability(){{return {{available:true,track_id:'track'}};}},
download(trackId,quality,outputPath){{{body}}}}});"
),
)
.unwrap();
let backend = Backend::new(
&directory.join("sources"),
&directory.join("data"),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
"1",
RuntimeLimits::default(),
)
.unwrap();
backend.load_all().unwrap();
backend.set_enabled("example.audio", true).unwrap();
backend.download_manifest("example.audio").unwrap();
let output = directory.join("output");
std::fs::create_dir_all(&output).unwrap();
backend
.environment()
.set_allowed_download_directories(std::slice::from_ref(&output))
.unwrap();
(backend, output)
}
fn container_request(output: &Path, embed_metadata: bool) -> Value {
json!({"service":"example.audio","provider_track_id":"track",
"use_extensions":true,"use_fallback":false,"output_dir":output,
"track_name":"Track","artist_name":"Artist","album_name":"Album",
"filename_format":"{title}","output_ext":".flac","quality":"LOSSLESS",
"embed_metadata":embed_metadata,"embed_lyrics":false})
}
@@ -6,32 +6,27 @@ use mornye_core::metadata::reenrich::{self, text};
use mornye_core::resolver::{artists_match, titles_match, track_identity_title};
use mornye_providers::resolver::Check;
use serde_json::{Value, json};
use std::io::{Read, Seek, SeekFrom};
use std::io::{Seek, SeekFrom};
pub(super) fn probe_result(
files: &crate::files::ExtensionFiles,
result: &mut Value,
album_pending: bool,
check: &Check<'_>,
) -> Result<(), String> {
) -> Result<&'static str, String> {
let path = text(result, "file_path").to_owned();
let file = files
.resolve_legacy(&path)?
.open(OpenOptions::new().read(true));
let mut container = "";
if let Ok(mut file) = file {
let mut header = [0; 8];
// Quality is probed only for FLAC/MP4. Missing album folders may use
// tags from any supported container, including undecoded audio.
let has_header = file.read_exact(&mut header).is_ok();
let container = if has_header && &header[..4] == b"fLaC" {
"flac"
} else if has_header && &header[4..] == b"ftyp" {
"m4a"
} else {
""
};
let quality = !container.is_empty();
if quality {
// Provider labels may disagree with the bytes, which decide the
// published suffix. Quality is probed only for FLAC/MP4. Missing
// album folders may use tags from any supported container,
// including undecoded audio.
container = mornye_core::tags::sniff_container(&mut file).unwrap_or_default();
let quality = matches!(container, "flac" | "m4a");
if !container.is_empty() {
result["actual_container"] = container.into();
}
let album = album_pending && text(result, "album").trim().is_empty();
@@ -49,7 +44,8 @@ pub(super) fn probe_result(
}
}
}
check()
check()?;
Ok(container)
}
pub(super) fn matches_track(request: &DownloadRequest, result: &Value, skip_names: bool) -> bool {
@@ -95,6 +95,13 @@ pub(super) fn success(
.find(|extension| !extension.is_empty())
.unwrap_or_default()
});
// A FLAC label on other audio bytes yields to the bytes, since FLAC tags
// cannot be written into them.
let extension = if extension.trim_start_matches('.') == "flac" && !container.is_empty() {
container.to_lowercase()
} else {
extension
};
if !extension.is_empty() {
let extension = format!(".{}", extension.trim_start_matches('.'));
response["actual_extension"] = if extension == ".mp4" {