Merge pull request #3148 from dotCooCoo/fix/plugin-library-release-urls

Plug-in Library: a package downloads only release assets of its repository
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 10:25:12 +01:00
commit af069369fc
6 files changed
+267 -11

No files matched your search

+173 -2
View File
@@ -255,6 +255,80 @@ 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()
}
/// A `.` or `..` path segment, also percent-encoded.
fn dot_segment(p: &str) -> bool {
let p = p.to_ascii_lowercase().replace("%2e", ".");
p == "." || p == ".."
}
/// A character a release or repository URL may not contain: `?`, `#`, `\`, Unicode white space,
/// or U+FEFF (the catalog schema's `\s` matches it too).
fn bad_url_char(c: char) -> bool {
matches!(c, '?' | '#' | '\\' | '\u{feff}') || c.is_whitespace()
}
/// `(owner, name)` in lower case of a `https://github.com/<owner>/<name>` repository URL (an
/// optional trailing `/` or `.git`) or of a folder in one, `https://github.com/<owner>/<name>/tree/<ref>/<path>`;
/// `None` for anything else.
fn github_repo(repo: &str) -> Option<(String, String)> {
let rest = strip_prefix_ignore_case(repo.trim(), "https://github.com/")?;
if rest.contains(bad_url_char) {
return None;
}
let rest = rest.strip_suffix('/').unwrap_or(rest);
let parts: Vec<&str> = rest.split('/').collect();
let (owner, name) = match parts.as_slice() {
[owner, name] => (*owner, name.strip_suffix(".git").unwrap_or(name)),
[owner, name, "tree", folder @ ..] if !folder.is_empty() && folder.iter().all(|p| !p.is_empty() && !dot_segment(p)) => (*owner, *name),
_ => return 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
/// names a `repo`, that must be a GitHub repository URL (see [`github_repo`]) of `<owner>/<name>`.
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(bad_url_char) {
return not_asset();
}
let parts: Vec<&str> = rest.split('/').collect();
// At least six segments, not exactly six: a tag may contain `/` (`release/v1`), and its assets are
// then at `releases/download/release/v1/<file>`. `.` and `..` (also percent-encoded) would move
// the download to another path.
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();
}
let repo = repo.trim();
if repo.is_empty() {
return None;
}
match github_repo(repo) {
Some((o, n)) if o == owner.to_ascii_lowercase() && n == name.to_ascii_lowercase() => None,
Some(_) => Some(format!("`{url}` isn't a release asset of the package's repository {repo}")),
None => Some(format!(
"the package's `repo` {repo} isn't a GitHub repository URL (https://github.com/<owner>/<repo>), so `{url}` can't be checked against it"
)),
}
}
/// 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> {
@@ -801,7 +875,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, format!("{}: {why}", pkg.name)));
}
Ok((url, pkg, rel))
}
@@ -1051,7 +1129,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
),
@@ -1135,6 +1213,99 @@ 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", ""] {
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/me/glow/releases/download/v 1/x.wasm",
"https://github.com/me/glow/releases/download/v1/x\t.wasm",
"https://github.com/me/glow/releases/download/v1/x.wasm\n",
"https://github.com/me/glow/releases/download/v1\u{a0}/x.wasm",
"https://github.com/me/glow/releases/download/v1/\u{85}x.wasm",
"https://github.com/me/glow/releases/download/v1/x\u{feff}.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())));
assert_eq!(github_repo("https://github.com/Me/Glow/tree/main/plugins/glow"), Some(("me".into(), "glow".into())));
for no in ["https://github.com/me", "https://github.com/me/glow/tree", "http://github.com/me/glow", "me/glow"] {
assert_eq!(github_repo(no), None, "{no}");
}
}
#[test]
fn a_repo_that_isnt_a_github_repository_url_refuses_every_release() {
let asset = "https://github.com/me/glow/releases/download/v1/glow.wasm";
for repo in [
"https://github.com/me/glow",
"https://github.com/me/glow/",
"https://github.com/me/glow.git",
"https://github.com/me/glow.git/",
" https://github.com/ME/glow ",
"https://github.com/me/glow/tree/main",
"https://github.com/me/glow/tree/main/plugins/glow/",
"",
" ",
] {
assert_eq!(release_url_problem(repo, asset), None, "{repo:?}");
}
let elsewhere = release_url_problem("https://github.com/me/glow/tree/main", "https://github.com/you/evil/releases/download/v1/glow.wasm");
assert!(elsewhere.as_deref().is_some_and(|w| w.contains("package's repository")), "{elsewhere:?}");
for repo in [
"https://github.com/me/glow/tree",
"https://github.com/me/glow/tree/",
"https://github.com/me/glow/tree//x",
"https://github.com/me/glow/tree/main/../../you/evil",
"https://github.com/me/glow/tree/main/%2e%2e",
"https://github.com/me/glow/blob/main/README.md",
"https://github.com/me/glow.git/x",
"https://github.com/me/glow/releases",
"https://github.com/me/glow//",
"https://github.com/me",
"https://github.com/me/",
"http://github.com/me/glow",
"github.com/me/glow",
"me/glow",
"https://www.github.com/me/glow",
"https://gitlab.com/me/glow",
"https://github.com/me/glow?tab=readme",
"https://github.com/me/glow#readme",
"https://github.com/me/gl ow",
"https://github.com/me/glow/tree/main/my plug-in",
"https://github.com/me/glow/tree/ma\tin",
"https://github.com/me/glow/tree/main/x\u{feff}",
"https://github.com/me/..",
] {
let why = release_url_problem(repo, asset);
assert!(why.as_deref().is_some_and(|w| w.contains("isn't a GitHub repository URL")), "{repo:?}: {why:?}");
}
}
#[test]
fn catalogs_leave_out_bad_entries() {
let good = "a".repeat(64);
+38
View File
@@ -156,6 +156,44 @@ 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"),
pkg("org.test.tree", "https://github.com/me/glow/tree/main", "https://github.com/someone/else/releases/download/v1/glow.wasm"),
pkg("org.test.http", "http://github.com/me/glow", "https://github.com/me/glow/releases/download/v1/glow.wasm"),
pkg("org.test.blob", "https://github.com/me/glow/blob/main/README.md", "https://github.com/me/glow/releases/download/v1/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"),
("org.test.tree", "isn't a release asset of the package's repository https://github.com/me/glow/tree/main"),
("org.test.http", "org.test.http: the package's `repo` http://github.com/me/glow isn't a GitHub repository URL"),
("org.test.blob", "org.test.blob: the package's `repo` https://github.com/me/glow/blob/main/README.md isn't a GitHub repository URL"),
] {
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");
+9 -1
View File
@@ -393,7 +393,15 @@ 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/…`). When the package names a `repo`, the
asset must be in that repository. A `repo` that isn't `https://github.com/<owner>/<repo>` (with
an optional trailing `/` or `.git`) or a folder in one (`https://github.com/<owner>/<repo>/tree/<ref>/…`)
makes Install and Preview refuse every `https://` release of the package. These rules don't apply
to a release whose `url` is a local `file://` path, which only a local catalog may name (to test
an entry before its release is published). 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
+2 -2
View File
@@ -181,7 +181,7 @@ Package fields:
| `author` | no | You, or your group. |
| `description` | no, but reviewed | One or two plain sentences that say what the effect does. |
| `license` | no, but reviewed | The licence's SPDX id (`MIT`, `Apache-2.0`, `MIT OR Apache-2.0`…). |
| `repo` | no, but reviewed | The public source repository. The details' "Open on GitHub" opens it. |
| `repo` | no, but reviewed | The public source repository, as `https://github.com/<owner>/<repo>` (a trailing `/` or `.git` is allowed), or a folder in it, `https://github.com/<owner>/<repo>/tree/<ref>/<path>`. The details' "Open on GitHub" opens it. When `repo` is given in any other form, Install and Preview refuse every release of the package. |
| `tags` | no | Lower-case words for the tag filter (`blur`, `stylize`, `light`, `distort`…). Reuse tags the catalog already has. |
| `preview.video` | no | An `https://` link to a short demo video on YouTube, Vimeo or GitHub. "Watch video" opens it in the browser. A link that isn't `https://` (or is longer than 500 characters) is left out. The live preview doesn't need it. |
| `versions` | yes | One entry per release, newest or oldest first. |
@@ -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 the package has a `repo`, 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. |
+4 -4
View File
@@ -12,9 +12,9 @@
},
"$defs": {
"version": {"type": "string", "maxLength": 32, "pattern": "^[0-9]+(\\.[0-9]+)*$"},
"github_url": {
"release_url": {
"type": "string",
"pattern": "^https://(github\\.com|raw\\.githubusercontent\\.com|objects\\.githubusercontent\\.com|release-assets\\.githubusercontent\\.com)/[^\\s]+$"
"pattern": "^(?!.*/(\\.|%2[eE]){1,2}(/|$))https://[Gg][Ii][Tt][Hh][Uu][Bb]\\.[Cc][Oo][Mm]/[A-Za-z0-9._-]+/[A-Za-z0-9._-]+/releases/download/[^/?#\\\\\\s\\u0085]+(/[^/?#\\\\\\s\\u0085]+)+$"
},
"package": {
"type": "object",
@@ -26,7 +26,7 @@
"author": {"type": "string"},
"description": {"type": "string"},
"license": {"type": "string"},
"repo": {"type": "string", "pattern": "^https://"},
"repo": {"type": "string", "pattern": "^(?!.*/(\\.|%2[eE]){1,2}(/|$))[Hh][Tt][Tt][Pp][Ss]://[Gg][Ii][Tt][Hh][Uu][Bb]\\.[Cc][Oo][Mm]/[A-Za-z0-9._-]+/[A-Za-z0-9._-]+(/tree(/[^/?#\\\\\\s\\u0085]+)+)?/?$"},
"tags": {"type": "array", "items": {"type": "string"}},
"preview": {
"type": "object",
@@ -44,7 +44,7 @@
"version": {"$ref": "#/$defs/version"},
"api": {"type": "integer", "minimum": 0, "maximum": 4294967295},
"minApp": {"$ref": "#/$defs/version"},
"url": {"$ref": "#/$defs/github_url"},
"url": {"$ref": "#/$defs/release_url"},
"sha256": {"type": "string", "pattern": "^[0-9a-fA-F]{64}$"},
"size": {"type": "integer", "minimum": 1, "maximum": 67108864},
"effects": {"type": "array", "minItems": 1, "items": {"type": "string", "pattern": "^(?!ec\\.)[A-Za-z0-9._-]+$"}}
+41 -2
View File
@@ -42,7 +42,46 @@ dups=$(jq -r '.packages[].id' "$catalog" | sort | uniq -d)
[[ -z "$dups" ]] || fail "duplicate package ids: $dups"
echo "ok"
echo "== 3. downloads"
echo "== 3. release URLs"
# Every release that isn't a local file:// path must be a release asset, and when its package has
# a `repo`, an asset of that repository, as Install and Preview require (the loader step installs
# from file:// copies, which skip this check).
bad=$(jq -r '
# A path segment EffectCraft refuses: empty, `.` or `..` (also percent-encoded).
def bad_segment: . == "" or (ascii_downcase | gsub("%2e"; ".") | . == "." or . == "..");
# A `?`, `#`, `\` or white space character (Unicode, and U+FEFF), which EffectCraft refuses in a URL.
def bad_char: test("[?#\\\\\t\n\u000b\f\r \u0085   - 

   ]");
# "owner/name" in lower case of a release asset,
# https://github.com/<owner>/<name>/releases/download/<tag>/<file> (a tag may contain `/`), with
# no query, fragment, backslash, white space, or empty, `.` or `..` segment; "?" for anything else.
def asset_of:
if startswith("https://") and (.[8:19] | ascii_downcase) == "github.com/" then
.[19:] as $rest
| ($rest | split("/")) as $p
| if ($rest | bad_char) or ($p | length) < 6 or ($p | any(bad_segment))
or ($p[0] | test("^[A-Za-z0-9._-]+$") | not) or ($p[1] | test("^[A-Za-z0-9._-]+$") | not)
or $p[2] != "releases" or $p[3] != "download"
then "?" else "\($p[0])/\($p[1])" | ascii_downcase end
else "?" end;
# Only the scheme, host, owner and name compare without case; the `.git` suffix is lower case.
def repo_of:
if bad_char then "?" else
(capture("^(?<h>[^/]+//[^/]+)/(?<o>[A-Za-z0-9._-]+)/(?<n>[A-Za-z0-9._-]+?)(\\.git)?/?$")
// (capture("^(?<h>[^/]+//[^/]+)/(?<o>[A-Za-z0-9._-]+)/(?<n>[A-Za-z0-9._-]+)/tree/(?<f>[^?#\\\\]+?)/?$")
| select(.f | split("/") | any(bad_segment) | not))
| select((.h | ascii_downcase) == "https://github.com" and (.o | bad_segment | not) and (.n | bad_segment | not))
| "\(.o)/\(.n)" | ascii_downcase) // "?" end;
.packages[] | . as $pkg | ((.repo // "") | gsub("^\\s+|\\s+$"; "")) as $repo
| (if $repo == "" then "" else $repo | repo_of end) as $want
| .versions[] | select(.url | startswith("file://") | not)
| (.url | asset_of) as $got
| if $got == "?" then "\($pkg.id) \(.version): \(.url) is not a release asset (https://github.com/<owner>/<repo>/releases/download/<tag>/<file>)"
elif $want != "" and ($want == "?" or $got != $want) then "\($pkg.id) \(.version): \(.url) is not a release asset of \($repo)"
else empty end' "$catalog")
[[ -z "$bad" ]] || fail "release URLs that Install and Preview refuse:"$'\n'"$bad"
echo "ok"
echo "== 4. downloads"
# The checked files are what the loader step installs: a copy of the catalog names them by file://.
local_catalog=$(jq '.' "$catalog")
while IFS=$'\t' read -r p r id version url sha size; do
@@ -58,7 +97,7 @@ done < <(jq -r '.packages | to_entries[] | .key as $p | .value as $pkg | $pkg.ve
| [$p, .key, $pkg.id, .value.version, .value.url, .value.sha256, (.value.size // "")] | @tsv' "$catalog")
echo "ok"
echo "== 4. load with EffectCraft"
echo "== 5. load with EffectCraft"
if [[ -z "${EFFECTCRAFT_CLI:-}" ]]; then
echo "skipped (set EFFECTCRAFT_CLI to an effectcraft-cli build)"
exit 0