Why a deep-link host allow-list compares whole hosts, not suffixes
A deep link is a URL anyone can send, and it picks a screen before any session check runs. If the router decides whether that URL is ours with hostname.endsWith("example.com"), then example.com.evil.net is ours too, and so is notexample.com — both end with the trusted string, and both belong to somebody who is not us. The fix is unglamorous: compare the host whole, and refuse to start with an allow-list that cannot mean what it says.
We build mobile clients against payment, lending and marketplace backends, which means the links arriving at our apps routinely carry a password-reset token, a signed-document reference, or an order id that selects a screen behind a login. In that setting the routing table is an authorisation boundary — it is the thing that decides what a stranger's URL is allowed to reach — and a chain of prefix and suffix checks is not a boundary, it is a sequence of near-misses. The host handling below is from deeplinkmap, the React Native deep-link router we maintain in the open.
The suffix check and the domain that ends with yours#
The check that almost works looks like this.
// Not an allow-list.
if (url.hostname.endsWith("example.com")) {
route(url);
}It passes every test anyone writes for it, because the tests are written by someone who already knows which hosts are supposed to pass. The hosts that defeat it are the ones nobody thinks to type: a registrant who owns evil.net can publish example.com.evil.net this afternoon, and a suffix test reads that as home. Prefixing a dot to the comparison fixes the first case and not the second — evil.example.net still has to be rejected on different grounds, and example.com itself now fails against its own entry.
What the suffix test is really missing is that a hostname is a sequence of labels read right to left, not a string. So the comparison has to be done on labels, and the allow-list has to say how deep it reaches.
Compare whole, and let a wildcard stand for exactly one label#
The router takes its hosts up front:
const router = new Router(routes, {
schemes: ["myapp"],
hosts: ["example.com", "*.example.com"],
});Each entry compiles to a pattern that is either exact or wildcard, and the matching rule is two sentences long. An exact pattern matches only by equality. A wildcard pattern requires the hostname to end with a dot plus the declared host, and requires the remaining prefix to be non-empty and to contain no dot. That second clause is the whole design: one label, no more, and not zero.
| incoming hostname | example.com |
*.example.com |
|---|---|---|
example.com |
matches | no — a wildcard does not cover the bare domain |
app.example.com |
no | matches |
a.b.example.com |
no | no — two labels below the declared one |
example.com.evil.net |
no — ends with it, is not it | no |
evil.example.net |
no | no |
A hostname that matches no pattern is refused with the reason foreign-host, and that happens before any route pattern is tried. This matters more than it looks: the refusal is not "no screen wanted this link", it is "this link is not addressed to us", and the two deserve different entries in an audit trail. The demo in the repository shows both coming out of the same loop:
https://example.com.evil.net/order/1 refused: foreign-host
https://evil.example.net/order/1 refused: foreign-host
https://example.com/admin/danger refused: no-routeA list that cannot mean what it says never gets to run#
An allow-list that reads as a boundary while letting everything through is worse than none at all, so compileHost in src/routes.ts refuses a handful of entries outright, at construction, rather than quietly interpreting them:
function compileHost(raw: string): HostPattern {
const fail = (detail: string): never => {
throw new InvalidPolicy(`host ${JSON.stringify(raw)} ${detail}`);
};
const value = bareHost(raw.trim());
if (value === "") fail("is empty");
if (value === "*") fail("would allow every host, which is not an allow-list");
if (/[:/?#@\\]/.test(value)) fail("is not a bare hostname");
const wildcard = value.startsWith("*.");
const host = wildcard ? value.slice(2) : value;
if (host.includes("*")) fail("may only wildcard its first label");
if (wildcard && !host.includes(".")) fail("wildcards a whole top-level domain");
if (host === "" || !host.split(".").every((label) => LABEL.test(label))) {
fail("is not a hostname");
}
return { host, wildcard };
}Each of those lines answers a plausible mistake. A lone * is somebody writing "all of ours" and getting all of everyone's. *.com is a wildcard over a whole top-level domain, which is the same mistake with a longer blast radius. A * in a middle label (a.*.example.com) reads as a pattern language the matcher does not implement, so it is rejected rather than silently treated as literal. And the punctuation class catches entries that are not hostnames at all: https://example.com pasted in from a browser bar, example.com:8080 with a port, example.com@evil.net with userinfo. bareHost normalises what remains — trim, lowercase, drop one trailing dot — so the list does not depend on how it was typed.
The same discipline applies to custom schemes. compileScheme rejects http and https there, because whether a web link is ours is decided by the host allow-list and nowhere else; two places to say yes is one place to forget.
All of it is pinned by a test that reads as a list of near-misses:
test("a host or scheme that cannot be an allow-list is a build-time error", () => {
for (const host of [
"",
"*",
"*.com",
"a.*.example.com",
"https://example.com",
"example.com:8080",
"exa mple.com",
]) {
assert.throws(() => new Router([], { hosts: [host] }), InvalidPolicy, host);
}
for (const scheme of ["", "http", "https", "my app", "myapp://", "1app"]) {
assert.throws(() => new Router([], { schemes: [scheme] }), InvalidPolicy, scheme);
}
});What the hostname is not#
Comparing whole hosts only helps if both sides agree on where the hostname ends. Three places in a URL look like a host and are not one, and each has its own test.
Userinfo. In https://example.com@evil.net/order/1, the authority before the @ is a username; the host is evil.net. The parser is asked, not the string, so the link is refused and the refusal's detail names evil.net and does not contain example.com — a log that said otherwise would be actively misleading during an incident. Written the other way, https://example.com/@evil.net/order/1, the @evil.net is a path segment and the link is refused as no-route.
Ports. A port is not part of the host for the purpose of the comparison: https://example.com:8443/order/1 matches, while https://evil.net:8443/order/1 is a foreign-host. The port survives in the recorded URL, because the record is for reading, not for routing. An unparseable port (example.com:abc) is refused as unparseable rather than guessed at.
Trailing dots and non-ASCII lookalikes. https://example.com./order/1 is the same host written differently and matches; https://example.com.evil.net./order/1 is still foreign. A Cyrillic homograph of example.com is refused, and the refusal names the host in its punycode form, so two hosts a human reader cannot tell apart are distinguishable in a log.
assert.equal(matched("https://example.com./order/1").params.id, 1);
assert.equal(refused("https://example.com.evil.net./order/1").reason, "foreign-host");
assert.equal(refused("https://evil.net:8443/order/1").reason, "foreign-host");The same list decides outbound links#
One table, read in both directions. When the router builds a universal link it uses the first pattern whose wildcard is false, because a wildcard names no host anyone can be sent to. A router configured with *.example.com alone can still accept links from a subdomain, but asked to build one it falls back to a custom scheme where the route allows it, and throws InvalidLink where it does not:
const wildcardOnly = new Router(routes, { hosts: ["*.example.com"], schemes: ["myapp"] });
assert.equal(wildcardOnly.link("order", { id: 1 }), "myapp://order/1");
assert.throws(
() => wildcardOnly.link("file", { name: "notes.txt" }),
/no host that is not a wildcard/,
);That refusal lands on the developer building the link, during a test run, rather than on a recipient holding a URL that goes nowhere.
What it costs to run#
The price of comparing whole hosts is that subdomain depth becomes a thing you declare on purpose. An app served from app.example.com and www.example.com lists *.example.com or both names; one served from eu.app.example.com lists that host, because a single wildcard will not reach two labels down. A staging host is a line in the config, not an accident of a suffix that happened to match. In exchange, the config is reviewable: the set of hosts whose links can select a screen in the app is a short literal array in the diff, and anything ambiguous enough to argue about throws InvalidPolicy on the first render instead of behaving well in QA and badly in a stranger's email.
We would rather pay that in review than discover the other arrangement in production, where the failure mode is an app that opens somebody else's link on a screen holding a session.