Skip to content

Core\Http::allowUrl

Checks a URL before your program sends a request to it.

A URL that comes from a user is tainted. The request methods of Core\Http\Client, such as Core\Http\Client::get, do not accept it. Core\Http::allowUrl checks the URL and returns a Core\Http\Target. You give this value to a request method in place of the URL.

The check has three parts. The scheme must be http, https, ws or wss. Any public host is allowed, unless net.connect in [capabilities.net] in nvs.toml lists the allowed hosts. And the host's addresses must not be inside your own network. Private and link-local addresses throw a RuntimeError, unless net.internal names them. Loopback is allowed from the command line and in development mode. A production server needs net.internal for it too.

The Core\Http\Target keeps the addresses that were checked, and the request connects to one of them. The host name is not looked up a second time.

Good to know: a refused URL throws a RuntimeError. The examples show one allowed URL, four refused URLs, and webhook URLs that customers save.

Parameters

  • $urlstringThe URL to check. A tainted URL is allowed here. No other outbound call accepts one.

Return value

Returns Core\Http\Target. A Core\Http\Target with every address of the host that the policy approved. It has at most eight addresses, in the order the name lookup returned them. The client connects only to these addresses, so a second name lookup cannot change them.

Errors

  • RuntimeErrorThe text is not a URL, its scheme is not http, https, ws or wss, or it has no host. Or net.connect does not grant the host, or the host resolves to no address. Or the host resolves to a loopback, private, link-local or unspecified address that net.internal does not list. When net.internal is not written, loopback is allowed on a command-line run and in development mode.

Examples

The examples below run with this configuration file.

nvs.toml
# The program may connect to public hosts only.
# `internal = []` allows no address on this computer or on a private network.
[capabilities.net]
internal = []

A url a user typed

<?nvs
// Shows how to check a URL that a user typed before your program sends a request to it.
// Text from a user is `tainted`. A request method does not accept a tainted URL.
tainted string $typed = 'https://203.0.113.10/news/feed.xml';
// `Core\Http::allowUrl` checks the URL and returns a `Core\Http\Target`.
// You give this value to a request method such as `Core\Http\Client::get`.
Core\Http\Target $feed = Core\Http::allowUrl($typed);
// This prints `The feed URL is allowed.` No request is sent in this example.
echo 'The feed URL is allowed.', "\n";
Output
The feed URL is allowed.

Urls that are refused

<?nvs
// Shows the URLs that `Core\Http::allowUrl` refuses, and the error message for each one.
array<string> $urls = [
// The scheme is not `http`, `https`, `ws` or `wss`.
'ftp://203.0.113.10/prices.csv',
// 10.0.0.7 is a private address inside your own network.
'http://10.0.0.7/admin',
// 169.254.169.254 is the address where many cloud servers keep their passwords.
'http://169.254.169.254/latest/meta-data/',
// The text before `@` is a user name. The real host is 127.0.0.1, this computer.
'http://203.0.113.10@127.0.0.1/',
];
// Each call throws a `RuntimeError`. This prints four messages.
foreach ($urls as string $url) {
try {
Core\Http\Target $target = Core\Http::allowUrl($url);
echo 'allowed: ', $url, "\n";
} catch (RuntimeError $refused) {
echo $refused->message, "\n";
}
}
Output
Core\Http::allowUrl: the scheme must be `http`, `https`, `ws` or `wss`, and this URL names `ftp`
Core\Http::allowUrl cannot connect to 10.0.0.7: it is private (10/8, 172.16/12, 192.168/16), which `net.internal` does not except
help: add `internal = ["10.0.0.7"]` under `[capabilities.net]` in nvs.toml
Core\Http::allowUrl cannot connect to 169.254.169.254: it is link-local (169.254.0.0/16), which `net.internal` does not except
help: add `internal = ["169.254.169.254"]` under `[capabilities.net]` in nvs.toml
Core\Http::allowUrl cannot connect to 127.0.0.1: it is loopback (127.0.0.0/8), which `net.internal` does not except
help: add `internal = ["127.0.0.1"]` under `[capabilities.net]` in nvs.toml

Saving webhook urls

<?nvs
// Shows how a service checks the webhook URLs its customers save. A webhook is a
// URL your service calls when something happens, for example when an order is paid.
array<tainted string> $saved = [
'shop' => 'https://203.0.113.10/hooks/order-paid',
'blog' => 'http://192.168.1.20/hooks/new-comment',
'chat' => 'wss://198.51.100.7/live',
];
// The checked URLs are kept here. Your program sends the requests to them later.
array<Core\Http\Target> $webhooks = [];
foreach ($saved as tainted string $customer => tainted string $url) {
try {
$webhooks[] = Core\Http::allowUrl($url);
echo $customer, ': saved', "\n";
} catch (RuntimeError $refused) {
// The customer sees why the URL was not saved.
echo $customer, ': not saved. ', $refused->message, "\n";
}
}
// This prints `2 webhooks are ready.`
echo Core\Arr::count($webhooks), ' webhooks are ready.', "\n";
Output
shop: saved
blog: not saved. Core\Http::allowUrl cannot connect to 192.168.1.20: it is private (10/8, 172.16/12, 192.168/16), which `net.internal` does not except
help: add `internal = ["192.168.1.20"]` under `[capabilities.net]` in nvs.toml
chat: saved
2 webhooks are ready.