fix(subscription): separate deep link parameters from nested URLs

This commit is contained in:
Tunglies
2026-12-03 17:59:25 +08:00
parent 390231a043
commit c53e005f25
@@ -13,7 +13,11 @@ pub fn decode_subscription_link(link: &Url) -> Option<SubscriptionLink<'_>> {
}
// The embedded URL can contain unescaped query separators of its own.
let raw_url = link.query()?.split_once("url=")?.1.trim();
let query = link.query()?;
let raw_url = query
.strip_prefix("url=")
.or_else(|| query.split_once("&url=").map(|(_, url)| url))?
.trim();
let name = link.query_pairs().find(|(key, _)| key == "name").map(|(_, name)| name);
Some(SubscriptionLink {
url: decode_subscription_url(raw_url),
@@ -22,10 +26,16 @@ pub fn decode_subscription_link(link: &Url) -> Option<SubscriptionLink<'_>> {
}
fn decode_subscription_url(raw_url: &str) -> Cow<'_, str> {
let mut candidate = Cow::Borrowed(raw_url);
if Url::parse(raw_url).is_ok() {
return candidate;
return Cow::Borrowed(raw_url);
}
// Only the outer name has meaning here. Partly encoded URLs can still contain literal path and query delimiters.
let mut candidate = Cow::Borrowed(
raw_url
.split_once("&name=")
.filter(|(value, _)| !value.contains(['/', '?', '&']))
.map_or(raw_url, |(value, _)| value),
);
for _ in 0..2 {
let decoded = percent_decode_str(&candidate).decode_utf8_lossy();
@@ -51,7 +61,22 @@ mod tests {
assert_eq!(link.name.as_deref(), Some("Work VPN"));
assert!(matches!(link.url, Cow::Borrowed("https://example.invalid/sub?a=1&b=2")));
let encoded = Url::parse("clash-verge://install-config?url=https%253A%252F%252Fexample.invalid%252Fsub")?;
let partial = Url::parse("clash://install-config?url=https%3A%2F%2Fexample.invalid/a&b/file?token=abc")?;
assert_eq!(
decode_subscription_link(&partial).expect("partially encoded link").url,
"https://example.invalid/a&b/file?token=abc"
);
let missing = Url::parse("clash://install-config?name=myurl=notes")?;
assert!(decode_subscription_link(&missing).is_none());
let named = Url::parse("clash://install-config?name=myurl=notes&url=https://example.invalid/work")?;
assert_eq!(
decode_subscription_link(&named).expect("named link").url,
"https://example.invalid/work"
);
let encoded =
Url::parse("clash-verge://install-config?url=https%253A%252F%252Fexample.invalid%252Fsub&name=Work")?;
let link = decode_subscription_link(&encoded).expect("double-encoded subscription link");
assert_eq!(link.url, "https://example.invalid/sub");
Ok(())