fix(anatomy): R5.3 shapes refused as Zod did (struct as array, enum as map), reads bounded to regular files, --check never follows links, plain names in --out
This commit is contained in:
1 parent
314cf04e6a
commit
776c7095b3
9 files changed
+258
-25
No files matched your search
@@ -57,8 +57,21 @@ struct Folder {
|
||||
files: Vec<FolderFile>,
|
||||
}
|
||||
|
||||
/// A bound no source or generated file approaches (the largest, the draft CHANGELOG, is about
|
||||
/// 1.1 MB): a resource cap on what the tool reads.
|
||||
const MAX_FILE_BYTES: u64 = 16 * 1024 * 1024;
|
||||
|
||||
/// A regular file of the seed (never a symbolic link, a device or a pipe), read under the cap.
|
||||
fn read(path: &Path) -> Result<Vec<u8>, String> {
|
||||
fs::read(path).map_err(|error| format!("{}: {error}", path.display()))
|
||||
let refuse = |why: &str| format!("{}: {why}", path.display());
|
||||
let metadata = fs::symlink_metadata(path).map_err(|error| refuse(&error.to_string()))?;
|
||||
if !metadata.file_type().is_file() {
|
||||
return Err(refuse("not a regular file"));
|
||||
}
|
||||
if metadata.len() > MAX_FILE_BYTES {
|
||||
return Err(refuse(&format!("larger than {MAX_FILE_BYTES} bytes")));
|
||||
}
|
||||
fs::read(path).map_err(|error| refuse(&error.to_string()))
|
||||
}
|
||||
|
||||
/// `readFile(path, 'utf8')`: invalid UTF-8 becomes U+FFFD, as the TypeScript read the
|
||||
@@ -145,7 +158,9 @@ fn listed(dir: &Path, prefix: &str, out: &mut Vec<String>) -> Result<(), String>
|
||||
for entry in entries {
|
||||
let entry = entry.map_err(|error| error.to_string())?;
|
||||
let name = format!("{prefix}{}", entry.file_name().to_string_lossy());
|
||||
if entry.path().is_dir() {
|
||||
// `file_type` does not follow a symbolic link: a link is listed, never walked into.
|
||||
let is_dir = entry.file_type().is_ok_and(|kind| kind.is_dir());
|
||||
if is_dir {
|
||||
listed(&entry.path(), &format!("{name}/"), out)?;
|
||||
} else {
|
||||
out.push(name);
|
||||
@@ -160,7 +175,7 @@ fn check(folder: &Folder) -> Result<Vec<String>, String> {
|
||||
let target = Path::new(SEED).join(folder.name);
|
||||
let mut problems = Vec::new();
|
||||
for (name, bytes) in &folder.files {
|
||||
match fs::read(target.join(name)) {
|
||||
match read(&target.join(name)) {
|
||||
Ok(committed) if committed == *bytes => {}
|
||||
Ok(_) => problems.push(format!("{}: differs: {name}", folder.name)),
|
||||
Err(_) => problems.push(format!("{}: missing: {name}", folder.name)),
|
||||
@@ -205,7 +220,7 @@ fn claim_destination(typed: &Path) -> Result<PathBuf, String> {
|
||||
None => return Err(refuse("not a destination folder")),
|
||||
};
|
||||
let seed = fs::canonicalize(SEED).map_err(|error| format!("{SEED}: {error}"))?;
|
||||
if parent.starts_with(&seed) {
|
||||
if inside(&parent, &seed) {
|
||||
return Err(refuse("inside the seed folder"));
|
||||
}
|
||||
let name = out
|
||||
@@ -227,6 +242,11 @@ fn claim_destination(typed: &Path) -> Result<PathBuf, String> {
|
||||
Ok(root)
|
||||
}
|
||||
|
||||
/// Whether a canonical folder is the seed folder or lies under it.
|
||||
fn inside(folder: &Path, seed: &Path) -> bool {
|
||||
folder.starts_with(seed)
|
||||
}
|
||||
|
||||
/// Where a relative destination is typed from: `INIT_CWD` when pnpm set it to an absolute
|
||||
/// path, else the current folder (the repository root when the script is run directly: it
|
||||
/// moves there first).
|
||||
@@ -242,8 +262,18 @@ fn base_dir() -> PathBuf {
|
||||
/// appeared since the claim, file, folder or symbolic link, is refused, never followed or
|
||||
/// truncated (`create_dir`, `create_new`).
|
||||
fn write_exclusive(root: &Path, relative: &Path, bytes: &[u8]) -> Result<(), String> {
|
||||
// The claimed folder is checked again: a swap for a symbolic link since the claim is refused.
|
||||
if !fs::symlink_metadata(root).is_ok_and(|m| m.file_type().is_dir()) {
|
||||
return Err(format!("{}: no longer the claimed folder", root.display()));
|
||||
}
|
||||
let mut folder = root.to_path_buf();
|
||||
let components: Vec<_> = relative.components().collect();
|
||||
if !components.iter().all(|c| matches!(c, Component::Normal(_))) {
|
||||
return Err(format!(
|
||||
"{}: not a plain relative file name",
|
||||
relative.display()
|
||||
));
|
||||
}
|
||||
let (file, folders) = components.split_last().ok_or("an empty file name")?;
|
||||
for component in folders {
|
||||
folder.push(component);
|
||||
@@ -327,3 +357,25 @@ fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_folder_under_the_seed_or_the_seed_itself_is_inside_it() {
|
||||
let seed = Path::new("/repo/supabase/seed/anatomical-sheets/equine");
|
||||
assert!(inside(seed, seed));
|
||||
assert!(inside(&seed.join("draft"), seed));
|
||||
assert!(inside(&seed.join("x/y"), seed));
|
||||
assert!(!inside(
|
||||
Path::new("/repo/supabase/seed/anatomical-sheets"),
|
||||
seed
|
||||
));
|
||||
assert!(!inside(
|
||||
Path::new("/repo/supabase/seed/anatomical-sheets/equine-copy"),
|
||||
seed
|
||||
));
|
||||
assert!(!inside(Path::new("/tmp/out"), seed));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user