Skip to content

Commit 4be3fc3

Browse files
committed
[release-branch.go1.8] 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"). Cherry-pick of CL 68170. Change-Id: I1d3729bbd33aa2f11a03f4c000e6bb473164957b Reviewed-on: https://go-review.googlesource.com/68023 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Chris Broadfoot <[email protected]>
1 parent a4544a0 commit 4be3fc3

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

src/net/smtp/auth.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,29 @@ type plainAuth struct {
4444
}
4545

4646
// PlainAuth returns an Auth that implements the PLAIN authentication
47-
// mechanism as defined in RFC 4616.
48-
// The returned Auth uses the given username and password to authenticate
49-
// on TLS connections to host and act as identity. Usually identity will be
50-
// left blank to act as username.
47+
// mechanism as defined in RFC 4616. The returned Auth uses the given
48+
// username and password to authenticate to host and act as identity.
49+
// Usually identity should be the empty string, to act as username.
50+
//
51+
// PlainAuth will only send the credentials if the connection is using TLS
52+
// or is connected to localhost. Otherwise authentication will fail with an
53+
// error, without sending the credentials.
5154
func PlainAuth(identity, username, password, host string) Auth {
5255
return &plainAuth{identity, username, password, host}
5356
}
5457

58+
func isLocalhost(name string) bool {
59+
return name == "localhost" || name == "127.0.0.1" || name == "::1"
60+
}
61+
5562
func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) {
56-
if !server.TLS {
57-
advertised := false
58-
for _, mechanism := range server.Auth {
59-
if mechanism == "PLAIN" {
60-
advertised = true
61-
break
62-
}
63-
}
64-
if !advertised {
65-
return "", nil, errors.New("unencrypted connection")
66-
}
63+
// Must have TLS, or else localhost server.
64+
// Note: If TLS is not true, then we can't trust ANYTHING in ServerInfo.
65+
// In particular, it doesn't matter if the server advertises PLAIN auth.
66+
// That might just be the attacker saying
67+
// "it's ok, you can trust me with your password."
68+
if !server.TLS && !isLocalhost(server.Name) {
69+
return "", nil, errors.New("unencrypted connection")
6770
}
6871
if server.Name != a.host {
6972
return "", nil, errors.New("wrong host name")

src/net/smtp/smtp_test.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,41 @@ testLoop:
6060
}
6161

6262
func TestAuthPlain(t *testing.T) {
63-
auth := PlainAuth("foo", "bar", "baz", "servername")
6463

6564
tests := []struct {
66-
server *ServerInfo
67-
err string
65+
authName string
66+
server *ServerInfo
67+
err string
6868
}{
6969
{
70-
server: &ServerInfo{Name: "servername", TLS: true},
70+
authName: "servername",
71+
server: &ServerInfo{Name: "servername", TLS: true},
7172
},
7273
{
73-
// Okay; explicitly advertised by server.
74-
server: &ServerInfo{Name: "servername", Auth: []string{"PLAIN"}},
74+
// OK to use PlainAuth on localhost without TLS
75+
authName: "localhost",
76+
server: &ServerInfo{Name: "localhost", TLS: false},
7577
},
7678
{
77-
server: &ServerInfo{Name: "servername", Auth: []string{"CRAM-MD5"}},
78-
err: "unencrypted connection",
79+
// NOT OK on non-localhost, even if server says PLAIN is OK.
80+
// (We don't know that the server is the real server.)
81+
authName: "servername",
82+
server: &ServerInfo{Name: "servername", Auth: []string{"PLAIN"}},
83+
err: "unencrypted connection",
7984
},
8085
{
81-
server: &ServerInfo{Name: "attacker", TLS: true},
82-
err: "wrong host name",
86+
authName: "servername",
87+
server: &ServerInfo{Name: "servername", Auth: []string{"CRAM-MD5"}},
88+
err: "unencrypted connection",
89+
},
90+
{
91+
authName: "servername",
92+
server: &ServerInfo{Name: "attacker", TLS: true},
93+
err: "wrong host name",
8394
},
8495
}
8596
for i, tt := range tests {
97+
auth := PlainAuth("foo", "bar", "baz", tt.authName)
8698
_, _, err := auth.Start(tt.server)
8799
got := ""
88100
if err != nil {

0 commit comments

Comments
 (0)