-
Notifications
You must be signed in to change notification settings - Fork 18k
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
net/http/httptest: make it possible to use a Server (TLS or not) to test cookies #31054
Comments
This is a key pair that is available to the general public. Ie. its private part is known to all. Making it match on localhost or loopback interface would be a huge security hole for millions of developers who would add it to the trusted certs store then their machines would be then susceptible to a wide class of threats via localhost MITM. Please follow howto and make a cert for yourself. |
It already matches IPv4 and IPv6 loopback addresses. Adding a cert from a well-known key pair to ones trusted certs is unwise for the reasons you mentioned. Safer to generate your own to trust. |
Dup of #30774, it seems? That's not supported. The httptest.NewTLSServer is for testing and you can only hit it with the URL it gives you. |
Then this is a feature request, I guess. The goal being to make it easier use |
Feature request SGTM. I'm fine if the returned Client would have a special-cased dialer that makes a certain name(s) ("*.test.example") dial towards that IP, and make the TLS work. Anybody want to work on it? |
I'm interested. |
Change https://golang.org/cl/182917 mentions this issue: |
httptest.NewTLSServer uses a cert that is not valid for localhost
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm trying to test something that involves cookies that set the
Domain
attribute. As discussed in #12610,*net/http/cookiejar.Jar
won't return cookies for an IP (as per the relevant RFCs).(*httptest.Server).URL
has the host set to an IP address (defaults to127.0.0.1
or::1
).To do the test I needed, I spun up an
*httptest.Server
usinghttptest.NewTLSServer(...)
, replaced the IP in(*httptest.Server).URL
withlocalhost
and attempted to send a request to it with(*httptest.Server).Client()
.What did you expect to see?
I expected the
httptest.NewTLSServer(...)
to use a TLS cert that could be valid forlocalhost
, as well as the loopback IP addresses.I expected to be able to successfully make an HTTPS request to
localhost
at the correct port that the*httptest.Server
was listening on by using(*httptest.Server).Client()
.What did you see instead?
x509: certificate is valid for example.com, not localhost
Example
For completeness, I'm including the suite of tests showing the different behaviors with
*cookiejar.Jar
and the various combinations of*httptest.Server
. The problematic test here isTestCookies/tls/localhost/default_cert
(line 174). The test at line 185 shows that the original issue with cookies is resolved if I send requests tolocalhost
with a cert valid for that hostname.I attempted to have a conversation about the hosts included in the cert used by
httptest.NewTLSServer()
in the golang-nuts group, but it went nowhere.The text was updated successfully, but these errors were encountered: