Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(allow_hosts): Use getaddrinfo instead of gethostbyname #209

Merged
merged 10 commits into from Jun 21, 2023
Merged

chore(allow_hosts): Use getaddrinfo instead of gethostbyname #209

merged 10 commits into from Jun 21, 2023

Conversation

hasier
Copy link
Contributor

@hasier hasier commented Apr 13, 2023

#189 introduced hostname resolution to allow passing actual host names rather than IPs to the allowlist. This used getaddrinfo to get the actual IP addresses of hosts, but this is limited to only 1 IPv4 address result, where the address may have more than 1, or maybe even IPv6. This is a limitation stated in the Python docs, and the suggested alternative is to use gethostbyname.

I assumed the same tests from #189 should apply, as the functionality itself is not changing. Please let me know if there are any other tests you'd like to see.

As a side effect, I have also slightly improved the error message for SocketConnectBlockedError, as I found it'd help me with some of the tests I've been building lately. Where the error message used to be like

A test tried to use socket.socket.connect() with host "172.217.169.3" (allowed: "google.com").

now will look more like

A test tried to use socket.socket.connect() with host "172.217.169.3" (allowed: "google.com (172.217.169.14)").

@hasier
Copy link
Contributor Author

hasier commented Apr 13, 2023

Seems like https://httpbin.org/ is having some issues, it's 504ing in the browser for me as well. I'll keep an eye on it to see if we can rerun when it's stable again.

@hasier
Copy link
Contributor Author

hasier commented Apr 13, 2023

@miketheman https://httpbin.org/ seems to be better now, feel free to approve the test run whenever you prefer 🙂

@miketheman
Copy link
Owner

@hasier Thanks for working through this - glad to see the existing behaviors are not affected.
There's enough code changes that don't have any new tests - can you please add some in another commit to detail what the behaviors that you're seeing trigger these new code paths?

@miketheman miketheman added the enhancement New feature or request label Apr 13, 2023
@hasier
Copy link
Contributor Author

hasier commented Apr 17, 2023

@hasier Thanks for working through this - glad to see the existing behaviors are not affected. There's enough code changes that don't have any new tests - can you please add some in another commit to detail what the behaviors that you're seeing trigger these new code paths?

@miketheman the underlying behaviour was not changed, so I could only think of a test that used a hostname directly as an argument, which I added in 75c0a8f. Let me know what you think 🙂

EDIT: pastebin seems to be having some issues again, but they don't seem to be related to the changes (as happened last time), a re-run when it's stable again should confirm this.

@hasier
Copy link
Contributor Author

hasier commented Apr 25, 2023

@miketheman thanks for re-running the tests! Any thoughts on the changes? 🙂

@miketheman
Copy link
Owner

Just got back from PyCon, gonna need a few days to recover. 😉

@hasier
Copy link
Contributor Author

hasier commented May 9, 2023

@miketheman any chance you can take a look at this again, whenever you have a bit of time? 😊

@hasier
Copy link
Contributor Author

hasier commented May 31, 2023

@miketheman I have updated the PR with the changes in main, could approve the workflow run and review again whenever you have some time, please? Thanks!

EDIT: pastebin seems to be having some issues once again, but they don't seem to be related to the changes (as happened in the previous times), a re-run when it's stable again should confirm this.

@hasier
Copy link
Contributor Author

hasier commented Jun 8, 2023

@miketheman seems like HTTPBin is somewhat stable now (at least for the few request I just tried), maybe we can re-run CI one last time? 🤞

@miketheman
Copy link
Owner

@hasier Thankfully, httpbin played nice today.
We got an interesting error: https://github.com/miketheman/pytest-socket/actions/runs/5305161721/jobs/9601944686#step:6:157

pytest_socket.SocketConnectBlockedError: A test tried to use socket.socket.connect() with host "192.30.255.113" (allowed: "github.com (192.30.255.112)").

Is this due to now how the functionality works with multiple DNS addresses?

@hasier
Copy link
Contributor Author

hasier commented Jun 20, 2023

@hasier Thankfully, httpbin played nice today. We got an interesting error: https://github.com/miketheman/pytest-socket/actions/runs/5305161721/jobs/9601944686#step:6:157

pytest_socket.SocketConnectBlockedError: A test tried to use socket.socket.connect() with host "192.30.255.113" (allowed: "github.com (192.30.255.112)").

Is this due to now how the functionality works with multiple DNS addresses?

Thanks for rerunning it @miketheman!

Hmm interesting, not sure it's directly related to the new functionality, the tests in the rest of Python versions succeeded, and it did locally for me too, so the resolution seems accurate most of the time 🤔 The new way of resolving IPs should simply yield multiple results, as opposed to just 1 from the previous method, but in the test there was only 1 result returned (note how there's only 1 IP allowed in the message (allowed: "github.com (192.30.255.112)")). Considering we are reaching out to a real address and the OS will be resolving DNSs each time, maybe we are more liable to this sort of tiny variations as Github will regularly change their external IPs, which will make this test flaky...

I have changed the test to check values only against "localhost", similar to what other tests do, also ensuring that it still resolves the hostname to its 127.0.0.1 and ::1 IPs to test the new functionality. This way we avoid the flakiness of a real address changing IPs, while still ensuring that "localhost" is correctly resolved to IP addresses. wdyt?

@codeclimate
Copy link

codeclimate bot commented Jun 21, 2023

Code Climate has analyzed commit 44636ea and detected 0 issues on this pull request.

View more on Code Climate.

@miketheman miketheman changed the title chore(allow_hosts): Use getaddrinfo in favour of gethostbyname chore(allow_hosts): Use getaddrinfo instead of gethostbyname Jun 21, 2023
@miketheman miketheman merged commit 13ca78c into miketheman:main Jun 21, 2023
18 checks passed
@miketheman
Copy link
Owner

@hasier thanks again for persisting! It's been a bit rocky for sure, but I think we got to a better solution overall.

@miketheman miketheman added this to the Next Release milestone Jun 21, 2023
@hasier
Copy link
Contributor Author

hasier commented Jun 21, 2023

Amazing, thanks very much to you @miketheman for bearing with all these reruns! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants