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
+18 -3
+18 -3
View File
@@ -158,7 +158,10 @@ function isDownloadLink(link) {
} }
// Third check: GitHub-specific patterns // Third check: GitHub-specific patterns
if (href.includes("github.com")) { const allowedGitHubHosts = ["github.com"];
try {
const parsedUrl = new URL(href);
if (allowedGitHubHosts.includes(parsedUrl.host)) {
// GitHub release asset download URLs follow pattern: /releases/download/ // GitHub release asset download URLs follow pattern: /releases/download/
if (pathname.includes("/releases/download/")) { if (pathname.includes("/releases/download/")) {
return true; return true;
@@ -179,6 +182,9 @@ function isDownloadLink(link) {
return false; return false;
} }
} }
} catch (e) {
console.error("Invalid URL:", href, e);
}
// Fourth check: GitLab-specific patterns // Fourth check: GitLab-specific patterns
const allowedGitLabHosts = ["gitlab.com"]; const allowedGitLabHosts = ["gitlab.com"];
@@ -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,12 +208,18 @@ function isDownloadLink(link) {
} }
// Fifth check: Hugging Face file downloads // Fifth check: Hugging Face file downloads
if (href.includes("huggingface.co")) { const allowedHuggingFaceHosts = ["huggingface.co"];
try {
const parsedUrl = new URL(href);
if (allowedHuggingFaceHosts.includes(parsedUrl.host)) {
// HF file download URLs contain /resolve/ // HF file download URLs contain /resolve/
if (pathname.includes("/resolve/")) { if (pathname.includes("/resolve/")) {
return true; 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)
const downloadTextIndicators = ["download", "download file", "get file"]; const downloadTextIndicators = ["download", "download file", "get file"];