Skip to content

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

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

nicolas-grekas wants to merge 1 commit into
amphp:5.xfrom
nicolas-grekas:connect-callback-5.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::connectionAcquired() comes too late for that.
A request that has a callback reuses only the pooled connections opened with the same callback.
This needs ConnectContext::withConnectCallback() from amphp/socket#122, hence the ^2.5 constraint.

$privateNetwork = new CidrMatcher('10.0.0.0/8');

$request->setConnectCallback(function (InternetAddress $address) use ($privateNetwork): void {
    if ($privateNetwork->match($address->getAddress())) {
        throw new \Exception('Blocked ' . $address->toString());
    }
});

The callback is called with the IP address of each connection attempt, before the connection is opened.
@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

Not needed anymore, see amphp/socket#122.

@nicolas-grekas
nicolas-grekas deleted the connect-callback-5.x branch November 5, 2026 09:16
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