Skip to content

Add Request::setConnectCallback() to check addresses before connecting - #409

Closed
nicolas-grekas wants to merge 1 commit into
amphp:4.xfrom
nicolas-grekas:connect-callback-4.x
Closed

nicolas-grekas wants to merge 1 commit into
amphp:4.xfrom
nicolas-grekas:connect-callback-4.x

Conversation

@nicolas-grekas

Copy link
Copy Markdown
Contributor

This PR adds a per-request callback that's called with the IP address of each connection attempt, before the connection is opened.
SSRF filters need to check the resolved address before connecting, and EventListener::startConnectionCreation() runs before DNS resolution.
A request that has a callback reuses only the pooled connections opened with the same callback, like in #408 for 5.x.
This needs ConnectContext::withConnectCallback() from amphp/socket#123, hence the ^1.3 constraint.

$request->setConnectCallback(function (SocketAddress $address): void {
    if (!\filter_var($address->getHost(), \FILTER_VALIDATE_IP, \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE)) {
        throw new \Exception('Blocked ' . $address->toString());
    }
});

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant