Improve host validation for download link detection

Refactored isDownloadLink to use URL parsing for host checks on GitHub and Hugging Face links, improving accuracy and robustness. Added error handling for invalid URLs and clarified host-specific download patterns.
This commit is contained in:
xixu-me committed 2025-07-11 17:56:26 +08:00
1 parent b08a854da6
commit aa590fcd9a
1 file changed
+39 -24
+39 -24
View File
@@ -158,26 +158,32 @@ function isDownloadLink(link) {
} }
// Third check: GitHub-specific patterns // Third check: GitHub-specific patterns
if (href.includes("github.com")) { const allowedGitHubHosts = ["github.com"];
// GitHub release asset download URLs follow pattern: /releases/download/ try {
if (pathname.includes("/releases/download/")) { const parsedUrl = new URL(href);
return true; if (allowedGitHubHosts.includes(parsedUrl.host)) {
} // GitHub release asset download URLs follow pattern: /releases/download/
// GitHub archive download URLs if (pathname.includes("/releases/download/")) {
if ( return true;
pathname.includes("/archive/") && }
(pathname.endsWith(".zip") || pathname.endsWith(".tar.gz")) // GitHub archive download URLs
) { if (
return true; pathname.includes("/archive/") &&
} (pathname.endsWith(".zip") || pathname.endsWith(".tar.gz"))
// GitHub raw file URLs - NEW: support for raw file links ) {
if (pathname.includes("/raw/")) { return true;
return true; }
} // GitHub raw file URLs - NEW: support for raw file links
// Exclude navigation to releases page (just /releases or /releases/) if (pathname.includes("/raw/")) {
if (pathname.endsWith("/releases") || pathname.endsWith("/releases/")) { return true;
return false; }
// Exclude navigation to releases page (just /releases or /releases/)
if (pathname.endsWith("/releases") || pathname.endsWith("/releases/")) {
return false;
}
} }
} catch (e) {
console.error("Invalid URL:", href, e);
} }
// Fourth check: GitLab-specific patterns // Fourth check: GitLab-specific patterns
@@ -190,7 +196,10 @@ function isDownloadLink(link) {
return true; return true;
} }
// GitLab release downloads // GitLab release downloads
if (pathname.includes("/-/releases/") && pathname.includes("/downloads/")) { if (
pathname.includes("/-/releases/") &&
pathname.includes("/downloads/")
) {
return true; return true;
} }
} }
@@ -199,11 +208,17 @@ function isDownloadLink(link) {
} }
// Fifth check: Hugging Face file downloads // Fifth check: Hugging Face file downloads
if (href.includes("huggingface.co")) { const allowedHuggingFaceHosts = ["huggingface.co"];
// HF file download URLs contain /resolve/ try {
if (pathname.includes("/resolve/")) { const parsedUrl = new URL(href);
return true; if (allowedHuggingFaceHosts.includes(parsedUrl.host)) {
// HF file download URLs contain /resolve/
if (pathname.includes("/resolve/")) {
return true;
}
} }
} catch (e) {
console.error("Invalid URL:", href, e);
} }
// Seventh check: explicit download text indicators (be more specific) // Seventh check: explicit download text indicators (be more specific)