Commit ec3b6131 authored by Russ Cox's avatar Russ Cox

net/smtp: fix PlainAuth to refuse to send passwords to non-TLS servers

PlainAuth originally refused to send passwords to non-TLS servers
and was documented as such.

In 2013, issue #5184 was filed objecting to the TLS requirement,
despite the fact that it is spelled out clearly in RFC 4954.
The only possibly legitimate use case raised was using PLAIN auth
for connections to localhost, and the suggested fix was to let the
server decide: if it advertises that PLAIN auth is OK, believe it.
That approach was adopted in CL 8279043 and released in Go 1.1.

Unfortunately, this is exactly wrong. The whole point of the TLS
requirement is to make sure not to send the password to the wrong
server or to a man-in-the-middle. Instead of implementing this rule,
CL 8279043 blindly trusts the server, so that if a man-in-the-middle
says "it's OK, you can send me your password," PlainAuth does.
And the documentation was not updated to reflect any of this.

This CL restores the original TLS check, as required by RFC 4954
and as promised in the documentation for PlainAuth.
It then carves out a documented exception for connections made
to localhost (defined as "localhost", "127.0.0.1", or "::1").

Change-Id: I1d3729bbd33aa2f11a03f4c000e6bb473164957b
Reviewed-on: https://go-review.googlesource.com/68170
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
parent b2e7eae7
...@@ -44,26 +44,29 @@ type plainAuth struct { ...@@ -44,26 +44,29 @@ type plainAuth struct {
} }
// PlainAuth returns an Auth that implements the PLAIN authentication // PlainAuth returns an Auth that implements the PLAIN authentication
// mechanism as defined in RFC 4616. // mechanism as defined in RFC 4616. The returned Auth uses the given
// The returned Auth uses the given username and password to authenticate // username and password to authenticate to host and act as identity.
// on TLS connections to host and act as identity. Usually identity will be // Usually identity should be the empty string, to act as username.
// left blank to act as username. //
// PlainAuth will only send the credentials if the connection is using TLS
// or is connected to localhost. Otherwise authentication will fail with an
// error, without sending the credentials.
func PlainAuth(identity, username, password, host string) Auth { func PlainAuth(identity, username, password, host string) Auth {
return &plainAuth{identity, username, password, host} return &plainAuth{identity, username, password, host}
} }
func isLocalhost(name string) bool {
return name == "localhost" || name == "127.0.0.1" || name == "::1"
}
func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) { func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) {
if !server.TLS { // Must have TLS, or else localhost server.
advertised := false // Note: If TLS is not true, then we can't trust ANYTHING in ServerInfo.
for _, mechanism := range server.Auth { // In particular, it doesn't matter if the server advertises PLAIN auth.
if mechanism == "PLAIN" { // That might just be the attacker saying
advertised = true // "it's ok, you can trust me with your password."
break if !server.TLS && !isLocalhost(server.Name) {
} return "", nil, errors.New("unencrypted connection")
}
if !advertised {
return "", nil, errors.New("unencrypted connection")
}
} }
if server.Name != a.host { if server.Name != a.host {
return "", nil, errors.New("wrong host name") return "", nil, errors.New("wrong host name")
......
...@@ -62,29 +62,41 @@ testLoop: ...@@ -62,29 +62,41 @@ testLoop:
} }
func TestAuthPlain(t *testing.T) { func TestAuthPlain(t *testing.T) {
auth := PlainAuth("foo", "bar", "baz", "servername")
tests := []struct { tests := []struct {
server *ServerInfo authName string
err string server *ServerInfo
err string
}{ }{
{ {
server: &ServerInfo{Name: "servername", TLS: true}, authName: "servername",
server: &ServerInfo{Name: "servername", TLS: true},
}, },
{ {
// Okay; explicitly advertised by server. // OK to use PlainAuth on localhost without TLS
server: &ServerInfo{Name: "servername", Auth: []string{"PLAIN"}}, authName: "localhost",
server: &ServerInfo{Name: "localhost", TLS: false},
}, },
{ {
server: &ServerInfo{Name: "servername", Auth: []string{"CRAM-MD5"}}, // NOT OK on non-localhost, even if server says PLAIN is OK.
err: "unencrypted connection", // (We don't know that the server is the real server.)
authName: "servername",
server: &ServerInfo{Name: "servername", Auth: []string{"PLAIN"}},
err: "unencrypted connection",
}, },
{ {
server: &ServerInfo{Name: "attacker", TLS: true}, authName: "servername",
err: "wrong host name", server: &ServerInfo{Name: "servername", Auth: []string{"CRAM-MD5"}},
err: "unencrypted connection",
},
{
authName: "servername",
server: &ServerInfo{Name: "attacker", TLS: true},
err: "wrong host name",
}, },
} }
for i, tt := range tests { for i, tt := range tests {
auth := PlainAuth("foo", "bar", "baz", tt.authName)
_, _, err := auth.Start(tt.server) _, _, err := auth.Start(tt.server)
got := "" got := ""
if err != nil { if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment