Don't store wildcards in the known hosts file
This commit is contained in:
parent
0343248952
commit
e01d59f8f6
13
client.go
13
client.go
@ -45,6 +45,11 @@ type Request struct {
|
|||||||
TLS tls.ConnectionState
|
TLS tls.ConnectionState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hostname returns the request host without the port.
|
||||||
|
func (r *Request) Hostname() string {
|
||||||
|
return hostname(r.Host)
|
||||||
|
}
|
||||||
|
|
||||||
// NewRequest returns a new request. The host is inferred from the provided url.
|
// NewRequest returns a new request. The host is inferred from the provided url.
|
||||||
func NewRequest(rawurl string) (*Request, error) {
|
func NewRequest(rawurl string) (*Request, error) {
|
||||||
u, err := url.Parse(rawurl)
|
u, err := url.Parse(rawurl)
|
||||||
@ -180,7 +185,7 @@ type Client struct {
|
|||||||
// TrustCertificate, if not nil, will be called to determine whether the
|
// TrustCertificate, if not nil, will be called to determine whether the
|
||||||
// client should trust the given certificate.
|
// client should trust the given certificate.
|
||||||
// If error is not nil, the connection will be aborted.
|
// If error is not nil, the connection will be aborted.
|
||||||
TrustCertificate func(cert *x509.Certificate, knownHosts *KnownHosts) error
|
TrustCertificate func(req *Request, cert *x509.Certificate, knownHosts *KnownHosts) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send sends a Gemini request and returns a Gemini response.
|
// Send sends a Gemini request and returns a Gemini response.
|
||||||
@ -205,15 +210,15 @@ func (c *Client) Send(req *Request) (*Response, error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Check that the certificate is valid for the hostname
|
// Check that the certificate is valid for the hostname
|
||||||
if err := cert.VerifyHostname(hostname(req.Host)); err != nil {
|
if err := cert.VerifyHostname(req.Hostname()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Check that the client trusts the certificate
|
// Check that the client trusts the certificate
|
||||||
if c.TrustCertificate == nil {
|
if c.TrustCertificate == nil {
|
||||||
if err := c.KnownHosts.Lookup(cert); err != nil {
|
if err := c.KnownHosts.Lookup(req.Hostname(), cert); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if err := c.TrustCertificate(cert, &c.KnownHosts); err != nil {
|
} else if err := c.TrustCertificate(req, cert, &c.KnownHosts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -22,8 +22,8 @@ func init() {
|
|||||||
client = &gemini.Client{}
|
client = &gemini.Client{}
|
||||||
client.KnownHosts.Load()
|
client.KnownHosts.Load()
|
||||||
|
|
||||||
client.TrustCertificate = func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
|
client.TrustCertificate = func(req *gemini.Request, cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
|
||||||
err := knownHosts.Lookup(cert)
|
err := knownHosts.Lookup(req.Hostname(), cert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err {
|
switch err {
|
||||||
case gemini.ErrCertificateNotTrusted:
|
case gemini.ErrCertificateNotTrusted:
|
||||||
|
@ -48,11 +48,11 @@ var DefaultClient *Client
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
DefaultClient = &Client{
|
DefaultClient = &Client{
|
||||||
TrustCertificate: func(cert *x509.Certificate, knownHosts *KnownHosts) error {
|
TrustCertificate: func(req *Request, cert *x509.Certificate, knownHosts *KnownHosts) error {
|
||||||
// Load the hosts only once. This is so that the hosts don't have to be loaded
|
// Load the hosts only once. This is so that the hosts don't have to be loaded
|
||||||
// for those using their own clients.
|
// for those using their own clients.
|
||||||
setupDefaultClientOnce.Do(setupDefaultClient)
|
setupDefaultClientOnce.Do(setupDefaultClient)
|
||||||
return knownHosts.Lookup(cert)
|
return knownHosts.Lookup(req.Hostname(), cert)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
3
tofu.go
3
tofu.go
@ -77,9 +77,8 @@ func (k *KnownHosts) Add(cert *x509.Certificate) {
|
|||||||
// Lookup returns ErrCertificateNotTrusted.
|
// Lookup returns ErrCertificateNotTrusted.
|
||||||
// If the hostname is not in the list, Lookup returns ErrCertificateUnknown.
|
// If the hostname is not in the list, Lookup returns ErrCertificateUnknown.
|
||||||
// If the certificate is found and the fingerprint matches, error will be nil.
|
// If the certificate is found and the fingerprint matches, error will be nil.
|
||||||
func (k *KnownHosts) Lookup(cert *x509.Certificate) error {
|
func (k *KnownHosts) Lookup(hostname string, cert *x509.Certificate) error {
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
hostname := cert.Subject.CommonName
|
|
||||||
fingerprint := Fingerprint(cert)
|
fingerprint := Fingerprint(cert)
|
||||||
for i := range k.hosts {
|
for i := range k.hosts {
|
||||||
if k.hosts[i].Hostname != hostname {
|
if k.hosts[i].Hostname != hostname {
|
||||||
|
Loading…
Reference in New Issue
Block a user