Skip to content

Add ConnectPolicy to check addresses before DnsConnector connects - #123

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

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

Conversation

@nicolas-grekas

@nicolas-grekas nicolas-grekas commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds a ConnectPolicy argument to DnsConnector, to check the address of each connection attempt before connecting.
Denied addresses are skipped, and the connection fails when all of them are denied.
SSRF filters need this to reject internal addresses after DNS resolution, IP literals included.
It mirrors #122 for 2.x.

$connector = new DnsConnector(null, new class implements ConnectPolicy {
    public function isAllowed(SocketAddress $address): bool
    {
        return (bool) \filter_var($address->getHost(), \FILTER_VALIDATE_IP, \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE);
    }
});

@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

@kelunik it'd be great if my PRs here and on http-cllient could be merged quickly - I'd rebuild Symfony HttpClient SSRF's protection on it.

@kelunik

kelunik commented Nov 5, 2026

Copy link
Copy Markdown
Member

@nicolas-grekas I don't think this is the right approach. The callback would have to be supported by all existing connectors to be reliable, which would be a BC break if we make this a requirement. I'd rather inject a policy object into DnsConnector and control it that way. It needs a custom connector set then, but I think it's a much better approach.

What do you think about that?

@nicolas-grekas nicolas-grekas changed the title Add ConnectContext::withConnectCallback() to check addresses before connecting Add ConnectPolicy to check addresses before DnsConnector connects Nov 5, 2026
@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

PR updated, #122 too.

Comment thread src/ConnectPolicy.php Resolved
Comment thread src/DnsConnector.php Resolved
Comment thread test/DnsConnectorTest.php Resolved
Comment thread test/DnsConnectorTest.php Resolved
Comment thread test/DnsConnectorTest.php Resolved
@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

PR updated, #122 too.

Comment thread src/ConnectPolicy.php
/**
* A denied address is skipped. The connection fails when no address is left.
*/
public function isAllowed(SocketAddress $address): bool;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SocketAddress works differently for 1.x. Do you need 1.x support for Symfony?

This branch has not been deployed

No deployments
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.

2 participants