mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 21:15:29 +00:00
Plug-in Library: a package downloads only release assets of its repository
A catalog release's url was checked only for its host: any https:// URL on GitHub's hosts was downloaded, from any repository and any path, including a raw file on a branch or a path with dot segments that curl resolves to another repository. library.install, library.update and library.preview now refuse a url that isn't https://github.com/<owner>/<repo>/releases/download/<tag>/<file>. When the package's repo is a https://github.com/<owner>/<repo> URL, the asset must be in that repository (owner and name compared without case). Dot segments, also percent-encoded, empty segments, backslashes, queries and fragments are refused. file:// releases from a local catalog are read as before, and the SHA-256 and size checks are unchanged. A catalog other than the community one is still read only when library.refresh names it (https:// on GitHub's hosts, or a local file://). docs/plugins.md, docs/publishing-plugins.md and the library.install params text describe the rules.
This commit is contained in:
@@ -255,6 +255,58 @@ fn release_problem(r: &Release) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// An owner or repository name on GitHub: letters, digits, `.`, `-`, `_`, and not `.` or `..`.
|
||||
fn valid_repo_part(p: &str) -> bool {
|
||||
!p.is_empty() && p != "." && p != ".." && p.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
|
||||
}
|
||||
|
||||
/// The rest of `s` after `prefix`, ignoring ASCII case.
|
||||
fn strip_prefix_ignore_case<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
|
||||
let head = s.get(..prefix.len())?;
|
||||
head.eq_ignore_ascii_case(prefix).then(|| s.get(prefix.len()..)).flatten()
|
||||
}
|
||||
|
||||
/// `(owner, name)` in lower case of a `https://github.com/<owner>/<name>` repository URL (an
|
||||
/// optional trailing `/` or `.git`); `None` for anything else.
|
||||
fn github_repo(repo: &str) -> Option<(String, String)> {
|
||||
let rest = strip_prefix_ignore_case(repo.trim(), "https://github.com/")?;
|
||||
let rest = rest.strip_suffix('/').unwrap_or(rest);
|
||||
let rest = rest.strip_suffix(".git").unwrap_or(rest);
|
||||
let mut parts = rest.split('/');
|
||||
let (owner, name) = (parts.next()?, parts.next()?);
|
||||
(parts.next().is_none() && valid_repo_part(owner) && valid_repo_part(name)).then(|| (owner.to_ascii_lowercase(), name.to_ascii_lowercase()))
|
||||
}
|
||||
|
||||
/// Why an `https://` release URL can't be used, if it can't: it must be a release asset,
|
||||
/// `https://github.com/<owner>/<name>/releases/download/<tag>/<file>`, and when the package's
|
||||
/// `repo` is a GitHub repository URL, `<owner>/<name>` must be that repository.
|
||||
fn release_url_problem(repo: &str, url: &str) -> Option<String> {
|
||||
let not_asset = || Some(format!("`{url}` isn't a release asset (https://github.com/<owner>/<repo>/releases/download/<tag>/<file>)"));
|
||||
let Some(rest) = strip_prefix_ignore_case(url, "https://github.com/") else { return not_asset() };
|
||||
if rest.contains(['?', '#', '\\']) {
|
||||
return not_asset();
|
||||
}
|
||||
let parts: Vec<&str> = rest.split('/').collect();
|
||||
// `.` and `..` (also percent-encoded) would move the download to another path.
|
||||
let dot_segment = |p: &&str| {
|
||||
let p = p.to_ascii_lowercase().replace("%2e", ".");
|
||||
p == "." || p == ".."
|
||||
};
|
||||
if parts.len() < 6 || parts.iter().any(|p| p.is_empty() || dot_segment(p)) {
|
||||
return not_asset();
|
||||
}
|
||||
let (Some(owner), Some(name), Some(releases), Some(download)) = (parts.first(), parts.get(1), parts.get(2), parts.get(3)) else { return not_asset() };
|
||||
if !valid_repo_part(owner) || !valid_repo_part(name) || *releases != "releases" || *download != "download" {
|
||||
return not_asset();
|
||||
}
|
||||
match github_repo(repo) {
|
||||
Some((o, n)) if o != owner.to_ascii_lowercase() || n != name.to_ascii_lowercase() => {
|
||||
Some(format!("`{url}` isn't a release asset of the package's repository {}", repo.trim()))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Read a catalog: packages and releases that are malformed are left out (and reported), so one
|
||||
/// bad entry doesn't hide the others.
|
||||
pub fn parse_catalog(bytes: &[u8]) -> std::result::Result<(Catalog, Vec<String>), String> {
|
||||
@@ -799,7 +851,11 @@ fn pick(s: &Session, c: &str, p: &Value) -> Result<(String, Package, Release)> {
|
||||
),
|
||||
));
|
||||
}
|
||||
source(&rel.url, Some(&url)).map_err(|e| bad(c, e))?;
|
||||
if let Source::Https(u) = source(&rel.url, Some(&url)).map_err(|e| bad(c, e))?
|
||||
&& let Some(why) = release_url_problem(&pkg.repo, &u)
|
||||
{
|
||||
return Err(bad(c, why));
|
||||
}
|
||||
Ok((url, pkg, rel))
|
||||
}
|
||||
|
||||
@@ -1049,7 +1105,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Install Plug-in",
|
||||
[],
|
||||
None,
|
||||
"{id, version?, wait?} — downloads the release, checks its SHA-256 against the catalog, loads it",
|
||||
"{id, version?, wait?} — downloads the release (a GitHub release asset, in the package's repo when it names one), checks its SHA-256 against the catalog, loads it",
|
||||
always,
|
||||
install
|
||||
),
|
||||
@@ -1132,6 +1188,44 @@ mod tests {
|
||||
assert!(latest_compatible(&Package::default()).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_urls_are_assets_of_the_declared_repository() {
|
||||
let asset = "https://github.com/Me/Glow/releases/download/v1.0.0/glow.wasm";
|
||||
for repo in ["https://github.com/me/glow", "https://github.com/Me/Glow/", "https://GitHub.com/me/glow.git", "", "https://gitlab.com/me/glow"] {
|
||||
assert_eq!(release_url_problem(repo, asset), None, "{repo}");
|
||||
}
|
||||
assert_eq!(release_url_problem("", "https://github.com/a/b/releases/download/tags/v1/x.wasm"), None, "a tag with a slash");
|
||||
assert!(
|
||||
release_url_problem("https://github.com/me/glow", "https://github.com/me/other/releases/download/v1/x.wasm")
|
||||
.unwrap()
|
||||
.contains("package's repository")
|
||||
);
|
||||
assert!(release_url_problem("https://github.com/me/glow", "https://github.com/you/glow/releases/download/v1/x.wasm").is_some());
|
||||
for no in [
|
||||
"https://raw.githubusercontent.com/me/glow/main/x.wasm",
|
||||
"https://objects.githubusercontent.com/x.wasm",
|
||||
"https://github.com/me/glow/blob/main/x.wasm",
|
||||
"https://github.com/me/glow/releases/download/v1",
|
||||
"https://github.com/me/glow/releases/download//x.wasm",
|
||||
"https://github.com/me/glow/releases/download/v1/x.wasm?x=1",
|
||||
"https://github.com/me/glow/releases/download/v1/x.wasm#x",
|
||||
"https://github.com/me/glow/releases/download/../../../you/evil/releases/download/v1/x.wasm",
|
||||
"https://github.com/me/glow/releases/download/%2e%2e/%2E./x/y/x.wasm",
|
||||
"https://github.com/me/glow/releases/download/./v1/x.wasm",
|
||||
"https://github.com/me/glow/releases/download/v1\\x.wasm",
|
||||
"https://github.com/m e/glow/releases/download/v1/x.wasm",
|
||||
"https://github.com/../glow/releases/download/v1/x.wasm",
|
||||
"https://github.com.evil.com/me/glow/releases/download/v1/x.wasm",
|
||||
] {
|
||||
assert!(release_url_problem("https://github.com/me/glow", no).is_some(), "{no}");
|
||||
assert!(release_url_problem("", no).is_some(), "{no}");
|
||||
}
|
||||
assert_eq!(github_repo("https://github.com/Me/Glow.git"), Some(("me".into(), "glow".into())));
|
||||
for no in ["https://github.com/me", "https://github.com/me/glow/tree/main", "http://github.com/me/glow", "me/glow"] {
|
||||
assert_eq!(github_repo(no), None, "{no}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalogs_leave_out_bad_entries() {
|
||||
let good = "a".repeat(64);
|
||||
|
||||
@@ -156,6 +156,38 @@ fn a_wrong_checksum_or_a_bad_module_installs_nothing() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn downloads_are_release_assets_of_the_package_repository() {
|
||||
let dir = tmp("assets");
|
||||
let sum = "0".repeat(64);
|
||||
let pkg =
|
||||
|id: &str, repo: &str, url: &str| json!({"id": id, "name": id, "repo": repo, "versions": [{"version": "1", "api": 1, "url": url, "sha256": sum}]});
|
||||
let catalog = write_catalog(
|
||||
&dir,
|
||||
json!([
|
||||
pkg("org.test.other", "https://github.com/me/glow", "https://github.com/someone/else/releases/download/v1/glow.wasm"),
|
||||
pkg("org.test.raw", "https://github.com/me/glow", "https://raw.githubusercontent.com/me/glow/main/glow.wasm"),
|
||||
pkg("org.test.page", "", "https://github.com/me/glow/blob/main/glow.wasm"),
|
||||
pkg("org.test.dots", "https://github.com/me/glow", "https://github.com/me/glow/releases/download/%2e%2e/%2E%2E/x/y/glow.wasm"),
|
||||
]),
|
||||
);
|
||||
let mut s = session(&dir);
|
||||
s.execute("library.refresh", json!({"catalog": catalog, "wait": true})).unwrap();
|
||||
for (id, why) in [
|
||||
("org.test.other", "isn't a release asset of the package's repository https://github.com/me/glow"),
|
||||
("org.test.raw", "isn't a release asset"),
|
||||
("org.test.page", "isn't a release asset"),
|
||||
("org.test.dots", "isn't a release asset"),
|
||||
] {
|
||||
for c in ["library.install", "library.preview"] {
|
||||
let e = s.execute(c, json!({"id": id, "wait": true})).unwrap_err().to_string();
|
||||
assert!(e.contains(why), "{c} {id}: {e}");
|
||||
}
|
||||
}
|
||||
assert!(!dir.join("Plug-ins").join("Library").join("installed.json").exists());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn installed_packages_load_at_start_and_changed_files_are_refused() {
|
||||
let dir = tmp("startup");
|
||||
|
||||
+5
-1
@@ -374,7 +374,11 @@ package can't read files, open the network or crash the app.
|
||||
Install (`library.install`) downloads the package's `.wasm`, checks its SHA-256 (and its size, when the
|
||||
catalog gives one) against the catalog and loads it. Downloads use the system's `curl`,
|
||||
`https://` only and starting on GitHub's hosts (redirects stay on `https://`; the SHA-256 check
|
||||
is what guarantees the file).
|
||||
is what guarantees the file). A package's `url` must be a release asset
|
||||
(`https://github.com/<owner>/<repo>/releases/download/…`), in the package's `repo` when that is
|
||||
a GitHub repository URL. A catalog other than the community one is read only when
|
||||
`library.refresh {catalog}` names it: an `index.json` on GitHub's hosts over `https://`, or a
|
||||
local `file://` path.
|
||||
* **Where it goes.** `<Plug-ins>/Library/<id>/<version>/<id>.wasm` (the `Plug-ins` folder next to
|
||||
the settings), listed in `Library/installed.json`. Installed packages load at start-up, each
|
||||
checked against the SHA-256 it was installed with. The catalog last fetched is kept as
|
||||
|
||||
@@ -193,7 +193,7 @@ Release fields (one per entry in `versions`):
|
||||
| `version` | yes | Dotted numbers only, at most 32 characters: `1.0.0`, `1.2`. No `v` and no suffixes such as `-beta`. |
|
||||
| `api` | yes | The plug-in API the module implements: `1`. |
|
||||
| `minApp` | no | The oldest EffectCraft version the release needs (`0.17.0`). Leave it out if any version with this API works. |
|
||||
| `url` | yes | The release asset's `https://` URL on GitHub. |
|
||||
| `url` | yes | The release asset's `https://` URL on GitHub: `https://github.com/<owner>/<repo>/releases/download/<tag>/<file>`. When `repo` is a `https://github.com/<owner>/<repo>` URL, the asset must be in that repository. Install and Preview refuse any other `https://` URL. |
|
||||
| `sha256` | yes | The file's SHA-256, 64 hex digits. |
|
||||
| `size` | no, but reviewed | The file's size in bytes. When it is given, a download of another size is refused. |
|
||||
| `effects` | yes | The effect ids the module registers (its manifest's `id`). EffectCraft uses them to find the package that provides an effect a project needs. |
|
||||
|
||||
Reference in New Issue
Block a user