mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-17 15:47:34 +00:00
fix(engine): refuse linked files when publishing a plugin folder
The assets of a plugin folder were read through whatever links they pointed at, so a folder handed to the publisher could put any file the publisher can read into the bundle the directory then serves to everyone. A linked asset, or a linked assets folder, is refused by name before anything is sent.
This commit is contained in:
1 parent
02c38a6d95
commit
ac184c1ca8
3 files changed
+47
-5
No files matched your search
@@ -292,6 +292,12 @@ pub fn open(path: impl AsRef<Path>) -> Result<std::fs::File> {
|
||||
std::fs::File::open(allow(path, Access::Read)?).map_err(io(path))
|
||||
}
|
||||
|
||||
/// What the file system says about `path` itself (a link is not followed).
|
||||
pub fn link_metadata(path: impl AsRef<Path>) -> Result<std::fs::Metadata> {
|
||||
let path = path.as_ref();
|
||||
std::fs::symlink_metadata(allow(path, Access::Read)?).map_err(io(path))
|
||||
}
|
||||
|
||||
/// What the file system says about `path` (links followed).
|
||||
pub fn metadata(path: impl AsRef<Path>) -> Result<std::fs::Metadata> {
|
||||
let path = path.as_ref();
|
||||
|
||||
@@ -221,12 +221,23 @@ fn bundle_from_disk(cmd: &str, path: &str) -> Result<Bundle> {
|
||||
let wasm = module_file(&file, &m)?;
|
||||
let mut assets = Vec::new();
|
||||
let dir = file.parent().map(|d| d.join(ASSETS));
|
||||
// Links are not followed: a folder handed to the publisher could otherwise put any file the
|
||||
// publisher can read (a key, a token) in the bundle the directory then serves to everyone.
|
||||
let linked = |p: &std::path::Path| crate::file_access::link_metadata(p).is_ok_and(|m| m.is_symlink());
|
||||
if dir.as_ref().is_some_and(|d| linked(d)) {
|
||||
return Err(bad(cmd, format!("the {ASSETS} folder is a link: only files inside the plugin's folder are published")));
|
||||
}
|
||||
if let Some(entries) = dir.as_ref().and_then(|d| crate::file_access::read_dir(d).ok()) {
|
||||
let mut names: Vec<(String, std::path::PathBuf)> = entries
|
||||
.into_iter()
|
||||
.filter(|p| crate::file_access::metadata(p).is_ok_and(|m| m.is_file()))
|
||||
.map(|p| (p.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(), p))
|
||||
.collect();
|
||||
let mut names: Vec<(String, std::path::PathBuf)> = Vec::new();
|
||||
for p in entries {
|
||||
let name = p.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
if linked(&p) {
|
||||
return Err(bad(cmd, format!("{ASSETS}/{name} is a link: only files inside the plugin's folder are published")));
|
||||
}
|
||||
if crate::file_access::link_metadata(&p).is_ok_and(|m| m.is_file()) {
|
||||
names.push((name, p));
|
||||
}
|
||||
}
|
||||
names.sort();
|
||||
if names.len() > MAX_ASSETS {
|
||||
return Err(bad(cmd, format!("a plugin's assets folder holds at most {MAX_ASSETS} files")));
|
||||
|
||||
@@ -252,3 +252,28 @@ fn publishing_sends_a_checked_folder() {
|
||||
assert_eq!(r["id"], ID);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn publishing_refuses_a_linked_asset_instead_of_bundling_what_it_points_at() {
|
||||
let (mut s, d) = session();
|
||||
let dir = temp("publish-link");
|
||||
std::fs::create_dir_all(dir.join("build")).unwrap();
|
||||
std::fs::create_dir_all(dir.join("assets")).unwrap();
|
||||
std::fs::write(dir.join("build").join("tiny.wasm"), wasm()).unwrap();
|
||||
std::fs::write(dir.join("figcraft-plugin.json"), json!({"id": ID, "name": "Tiny", "version": "1.0.0", "main": "build/tiny.wasm"}).to_string())
|
||||
.unwrap();
|
||||
// A private file outside the plugin folder, reached through a link in its assets.
|
||||
let secret = temp("publish-link-secret").join("token.txt");
|
||||
std::fs::write(&secret, b"per-user-secret").unwrap();
|
||||
std::os::unix::fs::symlink(&secret, dir.join("assets").join("notes.txt")).unwrap();
|
||||
let e = s.execute("plugin.directory.publish", &json!({"path": dir.to_string_lossy(), "server": "ws://h:1/"})).unwrap_err().to_string();
|
||||
assert!(e.contains("link"), "{e}");
|
||||
assert!(d.lock().unwrap().plugins.is_empty(), "nothing was sent");
|
||||
// The whole assets folder as a link is refused the same way.
|
||||
std::fs::remove_file(dir.join("assets").join("notes.txt")).unwrap();
|
||||
std::fs::remove_dir(dir.join("assets")).unwrap();
|
||||
std::os::unix::fs::symlink(secret.parent().unwrap(), dir.join("assets")).unwrap();
|
||||
let e = s.execute("plugin.directory.publish", &json!({"path": dir.to_string_lossy(), "server": "ws://h:1/"})).unwrap_err().to_string();
|
||||
assert!(e.contains("link"), "{e}");
|
||||
}
|
||||
Reference in new issue
Block a user