Skip to content

Commit 67cf010

Browse files
committed
fix(labrinth): use Url for cdn check
1 parent 46211a1 commit 67cf010

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

‎packages/app-lib/src/util/fetch.rs‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,17 @@ async fn fetch_advanced_with_client_and_progress(
914914
}
915915
}
916916

917+
/// Whether `url` points at the Modrinth CDN, compared by scheme and exact
918+
/// domain so that look-alike hosts such as `cdn.modrinth.com.example.com`
919+
/// never receive the user's credentials.
920+
fn is_modrinth_cdn_url(url: &str) -> bool {
921+
let url = url::Url::parse(url);
922+
923+
url.is_ok_and(|url| {
924+
url.domain() == Some("cdn.modrinth.com") && url.scheme() == "https"
925+
})
926+
}
927+
917928
async fn fetch_advanced_with_target(
918929
method: Method,
919930
url: &str,
@@ -944,7 +955,7 @@ async fn fetch_advanced_with_target(
944955
let creds = if header
945956
.as_ref()
946957
.is_none_or(|x| &*x.0.to_lowercase() != "authorization")
947-
&& (url.starts_with("https://cdn.modrinth.com") || is_api_url)
958+
&& (is_modrinth_cdn_url(url) || is_api_url)
948959
{
949960
crate::state::ModrinthCredentials::get_active(exec).await?
950961
} else {
@@ -1344,6 +1355,25 @@ mod tests {
13441355
use super::*;
13451356
use chrono::{TimeDelta, Utc};
13461357

1358+
#[test]
1359+
fn test_modrinth_cdn_url_matches_exact_domain() {
1360+
assert!(is_modrinth_cdn_url(
1361+
"https://cdn.modrinth.com/data/AANobbMI/versions/1/sodium.jar"
1362+
));
1363+
assert!(is_modrinth_cdn_url("https://cdn.modrinth.com:443/data/x"));
1364+
assert!(is_modrinth_cdn_url("https://CDN.modrinth.com/data/x"));
1365+
1366+
assert!(!is_modrinth_cdn_url(
1367+
"https://cdn.modrinth.com.example.com/data/x"
1368+
));
1369+
assert!(!is_modrinth_cdn_url(
1370+
"https://cdn.modrinth.com@example.com/"
1371+
));
1372+
assert!(!is_modrinth_cdn_url("https://cdn.modrinth.comexample.com/"));
1373+
assert!(!is_modrinth_cdn_url("http://cdn.modrinth.com/data/x"));
1374+
assert!(!is_modrinth_cdn_url("not a url"));
1375+
}
1376+
13471377
#[test]
13481378
fn test_fence_block_after_4_fails() {
13491379
// Update tests if the FenceInner constants change

0 commit comments

Comments
 (0)