Skip to content

Fix DNS pinning - #27

Open
jtojnar wants to merge 2 commits into
j0k3r:masterfrom
jtojnar:pinning-fix
Open

Fix DNS pinning#27
jtojnar wants to merge 2 commits into
j0k3r:masterfrom
jtojnar:pinning-fix

Conversation

@jtojnar

@jtojnar jtojnar commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Previously, the pinning would create a pinned URI but then overwrite the resolved IP address in its host part with the original host, reverting the pinning. This was the case since the beginning (504a338).

Not sure why the original uri had the host part replaced. I thought it might have been to set the Host header but any PSR-7 implementation should have already been setting that:

https://www.php-fig.org/psr/psr-7/#host-header

During construction, implementations MUST attempt to set the Host header from a provided URI if no Host header is provided.

And then, the Request-URI would remain unpinned.

Let’s replace it while keeping the Host header.
If it is set to a different value, it has probably been done intentionally.

Also test that the headers are properly updated. This required introducing a DNS resolver abstraction.

jtojnar added 2 commits July 28, 2026 03:35
Allows running tests without internet and also makes them reproducible.
Previously, the pinning would create a pinned URI but then overwrite the resolved IP address in its host part with the original host, reverting the pinning. This was the case since the beginning (504a338).

Not sure why the original `uri` had the `host` part replaced. I thought it might have been to set the `Host` header but any PSR-7 implementation should have already been setting that:

https://www.php-fig.org/psr/psr-7/#host-header
> During construction, implementations MUST attempt to set the Host header from a provided URI if no Host header is provided.

And then, the `Request-URI` would remain unpinned.

Let’s replace it while keeping the `Host` header.
If it is set to a different value, it has probably been done intentionally.

Also test that the headers are properly updated.
@jtojnar

jtojnar commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

I am not actually sure how helpful DNS pinning is here. It might prevent some TOCTOU-based attacks but since the plug-in is scoped per request, the usefulness will be limited.


if ((string) $uri !== (string) $request->getUri()) {
$request = $request->withUri($uri->withHost($urlData['host']));
$request = $request->withUri($uri, true);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

We should instead first rewrite the validated host and then apply the pinned URI while asking PSR-7 to preserve that Host. This is to avoid a forced host to persist.

Suggested change
$request = $request->withUri($uri, true);
$request = $request
->withUri($uri->withHost($urlData['host']))
->withUri($uri, true);

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants