Reject unreopenable source before checked save stages files
This commit is contained in:
@@ -3,6 +3,9 @@ use serde::{Deserialize, Serialize};
|
||||
use std::{io::Write, path::Path};
|
||||
use thiserror::Error;
|
||||
mod json_codec;
|
||||
#[cfg(test)]
|
||||
#[path = "persistence/source_save_tests.rs"]
|
||||
mod source_save_tests;
|
||||
#[path = "migration_v2/mod.rs"]
|
||||
pub(crate) mod v2;
|
||||
pub(crate) use v2::adapter::migrate as migrate_v2_limited;
|
||||
@@ -948,19 +951,54 @@ fn save_checked_profile(
|
||||
expected: Option<&[u8]>,
|
||||
profile: json_codec::Profile,
|
||||
depth_128: bool,
|
||||
) -> Result<Vec<u8>, FileError> {
|
||||
save_checked_profile_with_source(path, p, expected, profile, depth_128, |project| {
|
||||
crate::language::format(project).into_bytes()
|
||||
})
|
||||
}
|
||||
// Keep the source writer replaceable only at this private seam so regressions
|
||||
// can prove that malformed or lossy output never reaches filesystem staging.
|
||||
fn save_checked_profile_with_source(
|
||||
path: &Path,
|
||||
p: &Project,
|
||||
expected: Option<&[u8]>,
|
||||
profile: json_codec::Profile,
|
||||
depth_128: bool,
|
||||
source: impl FnOnce(&Project) -> Vec<u8>,
|
||||
) -> Result<Vec<u8>, FileError> {
|
||||
validate_project(p)?;
|
||||
let mut bytes = if path
|
||||
let is_source = path
|
||||
.extension()
|
||||
.is_some_and(|ext| ext.eq_ignore_ascii_case("arcadia"))
|
||||
{
|
||||
crate::language::format(p).into_bytes()
|
||||
.is_some_and(|ext| ext.eq_ignore_ascii_case("arcadia"));
|
||||
let mut bytes = if is_source {
|
||||
source(p)
|
||||
} else {
|
||||
json_codec::encode(p, profile, depth_128)?
|
||||
};
|
||||
if !bytes.ends_with(b"\n") {
|
||||
bytes.push(b'\n');
|
||||
}
|
||||
if is_source {
|
||||
// Check exactly the final bytes, including their newline, through the
|
||||
// public reader before creating a temp file or replacing a destination.
|
||||
let decoded = decode_with_report_limited(
|
||||
&bytes,
|
||||
crate::language::ParseLimits {
|
||||
max_source_bytes: profile.bytes,
|
||||
..crate::language::ParseLimits::UNLIMITED
|
||||
},
|
||||
)?;
|
||||
if decoded.project != *p {
|
||||
return Err(FileError::Invalid(
|
||||
"Generated source does not reproduce the complete requested project.".into(),
|
||||
));
|
||||
}
|
||||
if decoded.migration.is_some() {
|
||||
return Err(FileError::Invalid(
|
||||
"Generated source unexpectedly requires migration.".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
let parent = path
|
||||
.parent()
|
||||
.filter(|p| !p.as_os_str().is_empty())
|
||||
|
||||
Reference in New Issue
Block a user