Where does that email link really go? Checking links inside an email app

2026-12-04来源 dev.toEnglish

Most phishing advice ends with "hover over the link before you click". That works on a desktop with a mouse, for people who know what to look for, on links that aren't wrapped in three layers of redirects. In practice a lot of real email links look like this:

https://www.google.com/url?q=https%3A%2F%2Fexample-login.com%2Fa&sa=D https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2F...&data=... https://click.mailservice.com/ls/click?upn=very-long-token... 

While building MailHub, an email app for Windows, Linux and Android, we wrote a link checker that answers one question for every link in an email: where does this really go, and does that match what the email shows? This post walks through the checks, in the order we run them, with simplified Rust from the real code. None of it needs a network request: everything is decided from the email itself, so it's fast and private.

1. Collect the links, and skip the ones that can't hurt

An email's HTML has many kinds of href. Some need no checking at all:

let lower = href.to_ascii_lowercase(); // Links inside the email, or to write, call or text someone: nothing to check. if href.is_empty() || href.starts_with('#') || ["mailto:", "tel:", "sms:", "cid:"].iter().any(|p| lower.starts_with(p)) { continue; } // Links that run code or reach your files: never opened, but worth counting as a warning sign. if ["javascript:", "vbscript:", "data:", "file:"].iter().any(|p| lower.starts_with(p)) { odd += 1; continue; } 

Protocol-relative links (//example.com/x) get an https: prefix, and anything that isn't http or https after parsing is skipped. We also stop at 150 links: newsletters can have hundreds, and the dangerous one is almost never number 151.

2. Unwrap the redirects that hide the destination

Gmail rewrites links in some messages to google.com/url?q=..., and Microsoft Defender's Safe Links rewrites them to *.safelinks.protection.outlook.com/?url=.... Both are legitimate, but both hide the real destination from anyone who hovers. The destination is right there in a query parameter, so unwrap it before checking anything else:

fn unwrap_redirect(u: url::Url) -> url::Url { let host = u.host_str().unwrap_or("").to_ascii_lowercase(); let inner = if (host == "www.google.com" || host == "google.com") && u.path() == "/url" { u.query_pairs().find(|(k, _)| k == "q" || k == "url").map(|(_, v)| v.into_owned()) } else if host.ends_with("safelinks.protection.outlook.com") { u.query_pairs().find(|(k, _)| k == "url").map(|(_, v)| v.into_owned()) } else { None }; // Only accept a web address; anything else keeps the original link. inner .and_then(|i| url::Url::parse(&i).ok()) .filter(|i| matches!(i.scheme(), "http" | "https")) .unwrap_or(u) } 

Two details matter. query_pairs() already percent-decodes, so https%3A%2F%2F becomes https://. And the unwrapped value is parsed and limited to http/https, so a crafted ?q=javascript:... can't sneak through.

We deliberately don't try to unwrap marketing click-trackers. Their tokens are opaque; the only way to learn the destination is to request the link, which tells the sender you opened the email and clicked. That's exactly what a privacy-focused app shouldn't do.

3. Compare what the email shows with where the link goes

The most useful single check: if the link's visible text is itself a web address, does it match the real destination?

<a href="https://account-verify.example.net/login">https://www.paypal.com/signin</a> 

First, find a web address in the visible text. This needs care: "e.g." and "v1.2" aren't domains, and "paypal.com" without https:// still counts.

fn shown_host(shown: &str) -> Option<String> { shown.split_whitespace().find_map(|w| { let lower = w.to_ascii_lowercase(); let rest = lower.strip_prefix("https://").or_else(|| lower.strip_prefix("http://")); let explicit = rest.is_some() || lower.starts_with("www."); let w = rest.unwrap_or(&lower); let host: String = w.split(['/', '?', '#', ')', '(', '"', ',', ';', ':', '!', '<', '>']) .next().unwrap_or("").trim_end_matches('.').to_string(); let labels: Vec<&str> = host.split('.').collect(); let well_formed = labels.len() >= 2 && labels.iter().all(|l| !l.is_empty() && l.chars().all(|c| c.is_alphanumeric() || c == '-')) && labels.last().is_some_and(|t| t.len() >= 2 && t.chars().all(|c| c.is_ascii_alphabetic())); // A bare name needs a real-looking ending ("paypal.com", not "e.g"). (well_formed && (explicit || labels[labels.len() - 2].len() >= 2)).then_some(host) }) } 

Then compare at the level of the organisation, not the exact host. www.paypal.com and paypal.com are the same site; so are mail.example.co.uk and example.co.uk. But evilpaypal.com must not count as paypal.com, which is why a plain ends_with("paypal.com") is a bug: check ends_with(".paypal.com") with the dot.

How loudly to warn depends on what's being imitated. A mismatch that pretends to be a well-known brand (a bank, a payment service, a big email provider) is marked dangerous. A mismatch on an unknown site is only "be careful", because companies genuinely send links through another of their own domains all the time.

4. Don't cry wolf about email service providers

This is where a naive checker falls apart. A real newsletter from a shop says shop.example.com in the text and links to click.sendgrid.net/... or *.mjt.lu/.... That's not phishing; that's how bulk email works.

So the checker keeps a list of click-tracking and link-protection services (Mailjet, Proofpoint's URL Defense, Brevo's sendib* domains, app deep-link services and so on) and treats "shown site X, goes through a known mail service" as normal. One exception keeps it honest: if the text shows a well-known brand that isn't the sender itself, and the link goes through a service anyone can sign up for, it still gets a "be careful". Attackers use those services too.

The lesson from testing on real inboxes: false alarms are a security bug. If every newsletter gets a red banner, people learn to ignore red banners. When we tested on about a hundred real emails, the first version warned about scams in seven perfectly normal messages, mostly because of links in quoted earlier replies (often your own, with your own mail provider's tracking). Links inside a quoted message are now capped at "be careful", and that count went to zero without missing any of the planted scams.

5. The small checks that catch the classics

A handful of cheap checks catch tricks that have worked for twenty years:

// A bare server number instead of a name: http://203.0.113.7/login if host.parse::<std::net::IpAddr>().is_ok() || host.starts_with('[') { raise(Level::Danger, "Goes to a bare server number instead of a website name."); } // Everything before an @ is a username, not the site: https://paypal.com@evil.example/ if real.username() != "" || real.password().is_some() { raise(Level::Danger, "Hides the real website after an @ in the address."); } // Shorteners hide the destination, and expanding them means making a request. if SHORTENERS.contains(&organisation(host).as_str()) { raise(Level::Careful, "A shortened link: you can't see where it ends up."); } // Internationalised domain names: show the reader what the address really says. if host.split('.').any(|l| l.starts_with("xn--")) { let readable = idna::domain_to_unicode(host).0; raise(Level::Careful, format!("The address uses look-alike letters: it reads \"{readable}\".")); } 

The @ trick deserves a special mention because it still fools people: https://www.paypal.com@evil.example/ opens evil.example. The url crate parses it correctly, so the check is a single line, but only if you look at the parsed URL instead of the string.

Plain http isn't a warning by itself (plenty of harmless old links use it), but it's added as a note when a link is already suspicious.

6. Explain it in words people understand

The output of each check is a level (OK, careful, danger) and a sentence. "Shows paypal.com but opens account-verify.example.net" is something anyone can act on. "Domain mismatch, score 0.82" isn't. Links are sorted with the worst first, so the one that matters is at the top.

What we'd tell anyone building this

  • Unwrap known redirect wrappers locally; never "expand" unknown ones by requesting them.
  • Compare organisations, not raw hosts, and always match subdomains with a leading dot.
  • Keep a list of legitimate mail services, or your warnings will be ignored within a week.
  • Test on real inboxes, not just on phishing samples. Measure false alarms as carefully as catches.
  • Say what's wrong in a sentence. A warning nobody understands protects nobody.

If you build email tools and have found other link tricks worth checking, we'd like to hear about them in the comments.

This article was drafted with AI assistance from MailHub's own source code and test notes, and the code samples are simplified from the app.

原文:https://www.a8q9.cn/en/article/where-does-that-email-link-really-go-checking-links-inside-an-email-app-0aa514.html

相关文章