Sub topic 14
Validating and Securing Incoming Links
Assume you know basic Dart, Flutter navigation, URL parts, and how go_router receives a Uri. A deep link is external input. Treat it like form data or an API request: parse it, check it, and use only the values your app expects.
A valid-looking URL can still be unsafe. It might use an unexpected host, contain an unsupported route, include a misleading redirect target, or pass data that your app later uses in a dangerous way.
Define an allowlist
Validate against a small set of permitted values. Do not accept every host, scheme, path, and query parameter merely because Dart can parse them.
| Part | Safe rule | Example |
|---|---|---|
| Scheme | Require HTTPS for production links | `https` |
| Host | Compare with an exact allowlist | `example.com` |
| Path | Accept only known patterns | `/products/42` |
| Query | Accept known keys and validate values | `ref=email` |
| Fragment | Usually reject or ignore it | empty |
Custom schemes such as myapp:// cannot prove which application registered them. Another installed app may claim the same scheme. Prefer verified HTTPS links for sensitive actions. Android App Links use assetlinks.json, and iOS Universal Links use an Associated Domains entitlement plus apple-app-site-association.
Validate before routing
The following function accepts one product route and one optional tracking parameter. It returns a safe internal route for go_router, or null when the input is invalid.
String? safeTarget(String raw) {
final uri = Uri.tryParse(raw);
if (uri == null) return null;
final validPort = !uri.hasPort || uri.port == 443;
if (uri.scheme != 'https' ||
uri.host != 'example.com' ||
!validPort ||
uri.fragment.isNotEmpty) {
return null;
}
final segments = uri.pathSegments;
if (segments.length != 2 || segments.first != 'products') {
return null;
}
final id = segments[1];
if (!RegExp(r'^[A-Za-z0-9_-]+$').hasMatch(id)) {
return null;
}
for (final key in uri.queryParameters.keys) {
if (key != 'ref') return null;
}
final ref = uri.queryParameters['ref'];
if (ref != null && !RegExp(r'^[A-Za-z0-9_-]{1,40}$').hasMatch(ref)) {
return null;
}
return Uri(
path: '/products/$id',
queryParameters: ref == null ? null : {'ref': ref},
).toString();
}
void main() {
print(safeTarget('https://example.com/products/42?ref=email'));
print(safeTarget('https://evil.example/products/42'));
}The validation checks the decoded path segments and query values, rather than trusting the original string. The regular expression for the product ID also prevents unexpected characters from becoming part of a route.
Avoid dangerous redirect behavior
Do not route to an arbitrary URL supplied in a query parameter such as next=https://other.example. If login must return to a deep link, store an internal route such as /products/42, validate it against the same allowlist, and discard it after one use.
Never place tokens, passwords, or private data in links. URLs can appear in browser history, analytics, logs, screenshots, and messages. Use short-lived server-side state instead.
Test both accepted and rejected cases. Include malformed URLs, unexpected hosts, alternate ports, extra query keys, encoded characters, empty IDs, and links received while the app is already running.
Exercise
Why should an app validate the host and path before calling router.go with an incoming URI?
Show answer
Because an external URI is untrusted input. Host and path validation prevents unexpected domains and routes from reaching screens or redirect logic that the app did not intend to expose.