修复邮件协议认证错误分类并补齐真实响应验收

This commit is contained in:
xuans committed 2026-09-17 13:05:39 +08:00
1 parent 0169676923
commit b04dce7bcf
8 files changed
+166 -14

No files matched your search

+6 -1
View File
@@ -17,6 +17,9 @@ const maxPOP3CommandLineBytes = 8 << 10
var errPOP3CommandLineTooLong = errors.New("POP3 command line exceeds the allowed size")
// ErrAuthenticationTemporary 仅用于映射固定的临时失败响应,不回显内部错误。
var ErrAuthenticationTemporary = errors.New("[SYS/TEMP] Authentication temporarily unavailable")
// Server Pop3服务实例
type Server struct {
Domain string // 域名
@@ -215,7 +218,9 @@ func (s *Server) handleClient(conn net.Conn) {
case "PASS":
password := getSafeArg(args, 0)
cmdError = s.Action.Pass(session, password)
if cmdError != nil {
if errors.Is(cmdError, ErrAuthenticationTemporary) {
fmt.Fprint(conn, "-ERR [SYS/TEMP] Authentication temporarily unavailable"+eol)
} else if cmdError != nil {
fmt.Fprint(conn, "-ERR Password incorrect!"+eol)
} else {
fmt.Fprint(conn, "+OK User signed in"+eol)
+52 -1
View File
@@ -18,7 +18,8 @@ import (
)
type securityTestAction struct {
quitErr error
quitErr error
passErrors []error
}
func (a *securityTestAction) User(session *Session, username string) error {
@@ -27,6 +28,13 @@ func (a *securityTestAction) User(session *Session, username string) error {
}
func (a *securityTestAction) Pass(session *Session, password string) error {
if len(a.passErrors) > 0 {
err := a.passErrors[0]
a.passErrors = a.passErrors[1:]
if err != nil {
return err
}
}
session.Status = TRANSACTION
return nil
}
@@ -100,6 +108,49 @@ func TestQuitReportsMailboxUpdateResult(t *testing.T) {
}
}
func TestPassReportsTemporaryFailureWithoutLeakingInternalErrors(t *testing.T) {
serverConn, clientConn := net.Pipe()
t.Cleanup(func() { _ = clientConn.Close() })
if err := clientConn.SetDeadline(time.Now().Add(3 * time.Second)); err != nil {
t.Fatal(err)
}
server := &Server{Domain: "test", Action: &securityTestAction{passErrors: []error{
ErrAuthenticationTemporary,
fmt.Errorf("private database detail: %w", ErrAuthenticationTemporary),
errors.New("[SYS/TEMP] untrusted detail\r\n+OK injected"),
errors.New("private credential detail"),
}}}
done := make(chan struct{})
go func() {
server.handleClient(serverConn)
close(done)
}()
reader := bufio.NewReader(clientConn)
readResponseLine(t, reader, "+OK")
writeCommand(t, clientConn, "USER user")
readResponseLine(t, reader, "+OK")
for _, want := range []string{
"-ERR [SYS/TEMP] Authentication temporarily unavailable\r\n",
"-ERR [SYS/TEMP] Authentication temporarily unavailable\r\n",
"-ERR Password incorrect!\r\n",
"-ERR Password incorrect!\r\n",
"+OK User signed in\r\n",
} {
writeCommand(t, clientConn, "PASS test-password")
line, err := reader.ReadString('\n')
if err != nil || line != want {
t.Fatalf("POP3 认证响应错误:%q,预期 %q,错误 %v", line, want, err)
}
}
writeCommand(t, clientConn, "QUIT")
readResponseLine(t, reader, "+OK Bye")
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("POP3 恢复认证后未正常退出")
}
}
func TestSTLSUpgradesConnectionBeforeAuthentication(t *testing.T) {
serverConn, clientConn := net.Pipe()
t.Cleanup(func() { _ = clientConn.Close() })