mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 15:22:43 +00:00
Plug-in Library: a package with an unreadable repo URL downloads nothing
When a package names a `repo` that isn't a GitHub repository URL (`https://github.com/<owner>/<repo>`, a trailing `/` or `.git` allowed, or a folder in one, `.../tree/<ref>/<path>`), Install, Update and Preview now refuse its release URLs with an error naming the package. Before, such a `repo` skipped the repository check and any GitHub release asset was downloaded. A package without a `repo` keeps any repository's release asset. The example catalog's schema now holds `repo` and release `url` to the same forms, and `scripts/validate.sh` checks that every release is an asset of the package's `repo` before it downloads anything.
This commit is contained in:
1 parent
75fed052e3
commit
5dfcadd09b
6 files changed
+117
-29
No files matched your search
@@ -266,20 +266,33 @@ fn strip_prefix_ignore_case<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
|
||||
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 == ".."
|
||||
}
|
||||
|
||||
/// `(owner, name)` in lower case of a `https://github.com/<owner>/<name>` repository URL (an
|
||||
/// optional trailing `/` or `.git`); `None` for anything else.
|
||||
/// 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(['?', '#', '\\']) {
|
||||
return None;
|
||||
}
|
||||
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()))
|
||||
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's
|
||||
/// `repo` is a GitHub repository URL, `<owner>/<name>` must be that repository.
|
||||
/// `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() };
|
||||
@@ -287,11 +300,9 @@ fn release_url_problem(repo: &str, url: &str) -> Option<String> {
|
||||
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 == ".."
|
||||
};
|
||||
// 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();
|
||||
}
|
||||
@@ -299,11 +310,16 @@ fn release_url_problem(repo: &str, url: &str) -> Option<String> {
|
||||
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() => {
|
||||
Some(format!("`{url}` isn't a release asset of the package's repository {}", repo.trim()))
|
||||
}
|
||||
_ => None,
|
||||
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"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -854,7 +870,7 @@ fn pick(s: &Session, c: &str, p: &Value) -> Result<(String, Package, Release)> {
|
||||
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));
|
||||
return Err(bad(c, format!("{}: {why}", pkg.name)));
|
||||
}
|
||||
Ok((url, pkg, rel))
|
||||
}
|
||||
@@ -1191,7 +1207,7 @@ mod tests {
|
||||
#[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"] {
|
||||
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");
|
||||
@@ -1221,11 +1237,57 @@ mod tests {
|
||||
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("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/..",
|
||||
] {
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user