整理前后端与测试构建目录并完善配置和安全引导
Security Checks / security (push) Waiting to run

统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
This commit is contained in:
xuans committed 2026-09-05 19:19:52 +08:00
1 parent 28cd9d3406
commit f60d3b0880
603 files changed
+2351 -660

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
package address
import "strings"
// IsValidEmailAddress 检查是否是有效的邮箱地址
func IsValidEmailAddress(str string) bool {
ars := strings.Split(str, "@")
if len(ars) != 2 {
return false
}
return strings.Contains(ars[1], ".")
}
+42
View File
@@ -0,0 +1,42 @@
package address
import "testing"
func TestIsValidEmailAddress(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want bool
}{
{
"",
args{"test@qq.com"},
true,
},
{
"",
args{"1000@qq.com"},
true,
},
{
"",
args{"1000@163.com"},
true,
},
{
"",
args{"1000@1631com"},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsValidEmailAddress(tt.args.str); got != tt.want {
t.Errorf("IsValidEmailAddress() = %v, want %v", got, tt.want)
}
})
}
}
+94
View File
@@ -0,0 +1,94 @@
package array
import (
"github.com/spf13/cast"
"strings"
)
func Join[T any](arg []T, str string) string {
var ret strings.Builder
for i, t := range arg {
if i == 0 {
ret.WriteString(cast.ToString(t))
} else {
ret.WriteString(str)
ret.WriteString(cast.ToString(t))
}
}
return ret.String()
}
// Unique 数组去重
func Unique[T comparable](slice []T) []T {
mp := map[T]bool{}
for _, v := range slice {
mp[v] = true
}
ret := []T{}
for t, _ := range mp {
ret = append(ret, t)
}
return ret
}
// Merge 求并集
func Merge[T any](slice1, slice2 []T) []T {
s1Len := len(slice1)
slice3 := make([]T, s1Len+len(slice2))
for i, t := range slice1 {
slice3[i] = t
}
for i, t := range slice2 {
slice3[s1Len+i] = t
}
return slice3
}
// Intersect 求交集
func Intersect[T comparable](slice1, slice2 []T) []T {
m := make(map[T]bool)
nn := make([]T, 0)
for _, v := range slice1 {
m[v] = true
}
for _, v := range slice2 {
exist, _ := m[v]
if exist {
nn = append(nn, v)
}
}
return nn
}
// Difference 求差集 slice1-并集
func Difference[T comparable](slice1, slice2 []T) []T {
m := make(map[T]bool)
nn := make([]T, 0)
inter := Intersect(slice1, slice2)
for _, v := range inter {
m[v] = true
}
for _, value := range slice1 {
exist, _ := m[value]
if !exist {
nn = append(nn, value)
}
}
return nn
}
// InArray 判断元素是否在数组中
func InArray[T comparable](needle T, haystack []T) bool {
for _, t := range haystack {
if needle == t {
return true
}
}
return false
}
+71
View File
@@ -0,0 +1,71 @@
package async
import (
"errors"
"git.pcux.cn/xuans/Smail/utils/context"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"runtime/debug"
"sync"
)
type Callback func(params any)
type Async struct {
wg *sync.WaitGroup
lastError error
ctx *context.Context
}
func New(ctx *context.Context) *Async {
return &Async{
ctx: ctx,
}
}
func (as *Async) LastError() error {
return as.lastError
}
func (as *Async) WaitProcess(callback Callback, params any) {
if as.wg == nil {
as.wg = &sync.WaitGroup{}
}
as.wg.Add(1)
as.Process(func(params any) {
defer as.wg.Done()
callback(params)
}, params)
}
func (as *Async) Process(callback Callback, params any) {
go func() {
defer func() {
if err := recover(); err != nil {
as.lastError = as.HandleErrRecover(err)
}
}()
callback(params)
}()
}
func (as *Async) Wait() {
if as.wg == nil {
return
}
as.wg.Wait()
}
// HandleErrRecover panic恢复处理
func (as *Async) HandleErrRecover(err interface{}) (returnErr error) {
switch err.(type) {
case error:
returnErr = err.(error)
default:
returnErr = errors.New(cast.ToString(err))
}
log.WithContext(as.ctx).Errorf("goroutine panic:%s \n %s", err, string(debug.Stack()))
return
}
+98
View File
@@ -0,0 +1,98 @@
package atomicfile
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"runtime"
)
func Write(path string, data []byte, perm os.FileMode) error {
directory := filepath.Dir(path)
root, err := os.OpenRoot(directory)
if err != nil {
return err
}
defer root.Close()
return WriteRoot(root, filepath.Base(path), data, perm)
}
func WriteRoot(root *os.Root, name string, data []byte, perm os.FileMode) error {
if root == nil || name == "" || name == "." || filepath.IsAbs(name) {
return fmt.Errorf("invalid atomic file path")
}
directory := filepath.Dir(name)
base := filepath.Base(name)
token := make([]byte, 16)
if _, err := rand.Read(token); err != nil {
return err
}
temporaryName := filepath.Join(directory, "."+base+"-"+hex.EncodeToString(token))
temporary, err := root.OpenFile(temporaryName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
if err != nil {
return err
}
closed := false
committed := false
defer func() {
if !closed {
_ = temporary.Close()
}
if !committed {
_ = root.Remove(temporaryName)
}
}()
if err = temporary.Chmod(perm); err != nil {
return err
}
written, err := temporary.Write(data)
if err != nil {
return err
}
if written != len(data) {
return fmt.Errorf("short atomic file write: wrote %d of %d bytes", written, len(data))
}
if err = temporary.Sync(); err != nil {
return err
}
if err = temporary.Close(); err != nil {
closed = true
return err
}
closed = true
if err = root.Rename(temporaryName, name); err != nil {
return err
}
committed = true
if err = root.Chmod(name, perm); err != nil {
return err
}
return syncRootDirectory(root, directory)
}
func SyncDirectory(path string) error {
if runtime.GOOS == "windows" {
return nil
}
root, err := os.OpenRoot(path)
if err != nil {
return err
}
defer root.Close()
return syncRootDirectory(root, ".")
}
func syncRootDirectory(root *os.Root, name string) error {
if runtime.GOOS == "windows" {
return nil
}
directory, err := root.Open(name)
if err != nil {
return err
}
defer directory.Close()
return directory.Sync()
}
@@ -0,0 +1,57 @@
package atomicfile
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestWriteReplacesFileAtomically(t *testing.T) {
directory := t.TempDir()
path := filepath.Join(directory, "config.json")
if err := os.WriteFile(path, []byte("old"), 0644); err != nil {
t.Fatal(err)
}
if err := Write(path, []byte("new"), 0600); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(data) != "new" {
t.Fatalf("atomic file content = %q, want new", data)
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if runtime.GOOS != "windows" && info.Mode().Perm() != 0600 {
t.Fatalf("atomic file permissions = %o, want 600", info.Mode().Perm())
}
entries, err := os.ReadDir(directory)
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), ".config.json-") {
t.Fatalf("temporary file was left behind: %s", entry.Name())
}
}
}
func TestWriteCreatesEmptyFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "empty")
if err := Write(path, nil, 0600); err != nil {
t.Fatal(err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if info.Size() != 0 {
t.Fatalf("empty atomic file size = %d, want 0", info.Size())
}
}
+5
View File
@@ -0,0 +1,5 @@
package consts
const (
TEST_DOMAIN = "test.domain"
)
+64
View File
@@ -0,0 +1,64 @@
package context
import (
"context"
"git.pcux.cn/xuans/Smail/services/access"
)
const (
LogID = "LogID"
)
type Context struct {
context.Context `json:"-"`
UserID int
UserAccount string
UserName string
Role access.Role
Values map[string]any
Lang string
IsAdmin bool
MustChangePassword int
SecuritySetupAcknowledged int
RecentAuth bool
RecoveryCodeConfigured bool
TargetRecoveryConfigured bool
PluginInstanceAuthorized bool
AuditScopeAuthorized bool
DeploymentChannel bool
}
// HasRole 判断当前请求是否属于指定角色。
func (c *Context) HasRole(role access.Role) bool {
return c != nil && c.Role == role
}
// EffectiveRole 兼容旧调用方,确保旧会话仍能得到明确角色。
func (c *Context) EffectiveRole() access.Role {
if c == nil {
return ""
}
if c.Role.Valid() {
return c.Role
}
if c.IsAdmin {
return access.RoleSuperAdmin
}
return access.RoleUser
}
func (c *Context) SetValue(key string, value any) {
if c.Values == nil {
c.Values = map[string]any{}
}
c.Values[key] = value
}
func (c *Context) GetValue(key string) any {
if c.Values == nil {
return nil
}
return c.Values[key]
}
+246
View File
@@ -0,0 +1,246 @@
package credential
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/netip"
"net/url"
"strconv"
"strings"
"time"
"unicode"
)
const (
defaultTimeoutMS = 3000
minTimeoutMS = 1000
maxTimeoutMS = 30000
)
func ValidateSecret(value string, maxLength int) (string, error) {
value = strings.TrimSpace(value)
if value == "" || len(value) > maxLength || strings.IndexFunc(value, unicode.IsControl) >= 0 {
return "", errors.New("invalid credential")
}
return value, nil
}
func NormalizeHTTPSURL(raw string) (string, error) {
raw = strings.TrimSpace(raw)
if raw == "" || len(raw) > 2048 || strings.Contains(raw, "#") {
return "", errors.New("invalid endpoint")
}
endpoint, err := url.ParseRequestURI(raw)
if err != nil || endpoint.Scheme != "https" || endpoint.Hostname() == "" || endpoint.User != nil || endpoint.Fragment != "" {
return "", errors.New("invalid endpoint")
}
if port := endpoint.Port(); port != "" {
value, err := strconv.Atoi(port)
if err != nil || value < 1 || value > 65535 {
return "", errors.New("invalid endpoint")
}
}
host := strings.TrimSuffix(strings.ToLower(endpoint.Hostname()), ".")
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
return "", errors.New("invalid endpoint")
}
if address, err := netip.ParseAddr(host); err == nil && !IsPublicRemoteIP(address) {
return "", errors.New("invalid endpoint")
}
return endpoint.String(), nil
}
func IsPublicRemoteIP(address netip.Addr) bool {
address = address.Unmap()
if !address.IsValid() || !address.IsGlobalUnicast() || address.IsPrivate() || address.IsLoopback() || address.IsLinkLocalUnicast() || address.IsLinkLocalMulticast() || address.IsMulticast() || address.IsUnspecified() {
return false
}
if address.Is6() && !globalIPv6UnicastPrefix.Contains(address) {
return false
}
for _, prefix := range restrictedRemotePrefixes {
if prefix.Contains(address) {
return false
}
}
return true
}
var globalIPv6UnicastPrefix = netip.MustParsePrefix("2000::/3")
var restrictedRemotePrefixes = []netip.Prefix{
netip.MustParsePrefix("0.0.0.0/8"),
netip.MustParsePrefix("100.64.0.0/10"),
netip.MustParsePrefix("192.0.0.0/24"),
netip.MustParsePrefix("192.0.2.0/24"),
netip.MustParsePrefix("192.88.99.0/24"),
netip.MustParsePrefix("198.18.0.0/15"),
netip.MustParsePrefix("198.51.100.0/24"),
netip.MustParsePrefix("203.0.113.0/24"),
netip.MustParsePrefix("240.0.0.0/4"),
netip.MustParsePrefix("2001::/23"),
netip.MustParsePrefix("2001:db8::/32"),
netip.MustParsePrefix("2002::/16"),
netip.MustParsePrefix("3fff::/20"),
}
func NewRestrictedHTTPClient(timeoutMS int) *http.Client {
return NewRestrictedHTTPClientForTargets(timeoutMS, nil)
}
// NetworkTarget 描述插件允许访问的网络目标。
type NetworkTarget struct {
Scheme string
Host string
Port int
}
// NewRestrictedHTTPClientForTargets 创建仅允许访问授权目标的 HTTP 客户端。
func NewRestrictedHTTPClientForTargets(timeoutMS int, targets []NetworkTarget) *http.Client {
timeout := time.Duration(NormalizeTimeoutMS(timeoutMS)) * time.Millisecond
allowedTargets := normalizeNetworkTargets(targets)
transport := &http.Transport{
Proxy: nil,
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return restrictedDialContext(ctx, network, address, allowedTargets)
},
ForceAttemptHTTP2: true,
TLSHandshakeTimeout: timeout,
ResponseHeaderTimeout: timeout,
IdleConnTimeout: 30 * time.Second,
}
return &http.Client{
Timeout: timeout,
Transport: restrictedRoundTripper{transport: transport, targets: allowedTargets},
CheckRedirect: func(request *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("too many redirects")
}
return validateRestrictedRequest(request, allowedTargets, "redirect target is not authorized")
},
}
}
type restrictedRoundTripper struct {
transport http.RoundTripper
targets []NetworkTarget
}
func (r restrictedRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
if err := validateRestrictedRequest(request, r.targets, "remote target is not authorized"); err != nil {
return nil, err
}
return r.transport.RoundTrip(request)
}
func validateRestrictedRequest(request *http.Request, targets []NetworkTarget, targetError string) error {
if request == nil || request.URL == nil {
return errors.New("invalid endpoint")
}
if _, err := NormalizeHTTPSURL(request.URL.String()); err != nil {
return err
}
if !matchesNetworkTarget(request.URL, targets) {
return errors.New(targetError)
}
return nil
}
func normalizeNetworkTargets(targets []NetworkTarget) []NetworkTarget {
result := make([]NetworkTarget, 0, len(targets))
for _, target := range targets {
scheme := strings.ToLower(strings.TrimSpace(target.Scheme))
host := strings.TrimSuffix(strings.ToLower(strings.TrimSpace(target.Host)), ".")
port := target.Port
if port == 0 {
if scheme == "https" {
port = 443
} else {
port = 80
}
}
if (scheme != "https" && scheme != "http") || host == "" || port < 1 || port > 65535 {
continue
}
result = append(result, NetworkTarget{Scheme: scheme, Host: host, Port: port})
}
return result
}
func matchesNetworkTarget(endpoint *url.URL, targets []NetworkTarget) bool {
if len(targets) == 0 {
return true
}
port := 443
if strings.EqualFold(endpoint.Scheme, "http") {
port = 80
}
if endpoint.Port() != "" {
port, _ = strconv.Atoi(endpoint.Port())
}
host := strings.TrimSuffix(strings.ToLower(endpoint.Hostname()), ".")
scheme := strings.ToLower(endpoint.Scheme)
for _, target := range targets {
if target.Scheme == scheme && target.Host == host && target.Port == port {
return true
}
}
return false
}
func restrictedDialContext(ctx context.Context, network, address string, targets []NetworkTarget) (net.Conn, error) {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, errors.New("invalid remote address")
}
if _, err := strconv.Atoi(port); err != nil || (len(targets) > 0 && !matchesNetworkTarget(&url.URL{Scheme: "https", Host: net.JoinHostPort(host, port)}, targets) && !matchesNetworkTarget(&url.URL{Scheme: "http", Host: net.JoinHostPort(host, port)}, targets)) {
return nil, errors.New("remote target is not authorized")
}
resolved, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil || len(resolved) == 0 {
return nil, errors.New("remote address resolution failed")
}
approved := make([]netip.Addr, 0, len(resolved))
for _, item := range resolved {
candidate, ok := netip.AddrFromSlice(item.IP)
if !ok || !IsPublicRemoteIP(candidate) {
return nil, errors.New("remote address is not public")
}
candidate = candidate.Unmap()
if network == "tcp4" && !candidate.Is4() || network == "tcp6" && !candidate.Is6() {
continue
}
approved = append(approved, candidate)
}
if len(approved) == 0 {
return nil, errors.New("remote address is unavailable")
}
dialer := &net.Dialer{Timeout: 5 * time.Second, KeepAlive: 30 * time.Second}
var lastErr error
for _, candidate := range approved {
connection, err := dialer.DialContext(ctx, network, net.JoinHostPort(candidate.String(), port))
if err == nil {
return connection, nil
}
lastErr = err
}
return nil, fmt.Errorf("connect to remote endpoint: %w", lastErr)
}
func NormalizeTimeoutMS(value int) int {
if value <= 0 {
return defaultTimeoutMS
}
if value < minTimeoutMS {
return minTimeoutMS
}
if value > maxTimeoutMS {
return maxTimeoutMS
}
return value
}
+132
View File
@@ -0,0 +1,132 @@
package credential
import (
"net/http"
"net/http/httptest"
"net/netip"
"testing"
)
func TestValidateSecret(t *testing.T) {
value, err := ValidateSecret(" token-value ", 64)
if err != nil || value != "token-value" {
t.Fatal("valid credential was rejected")
}
for _, input := range []string{"", "line\nbreak"} {
if _, err := ValidateSecret(input, 64); err == nil {
t.Fatal("invalid credential was accepted")
}
}
}
func TestNormalizeHTTPSURL(t *testing.T) {
value, err := NormalizeHTTPSURL("https://model.example.com/v1/predict?model=mail")
if err != nil || value != "https://model.example.com/v1/predict?model=mail" {
t.Fatal("valid HTTPS endpoint was rejected")
}
for _, input := range []string{
"http://model.example.com/v1/predict",
"https://user:password@model.example.com/v1/predict",
"https://model.example.com/v1/predict#fragment",
"https:///v1/predict",
"https://127.0.0.1/v1/predict",
"https://10.0.0.1/v1/predict",
"https://169.254.169.254/latest/meta-data",
"https://100.64.0.1/v1/predict",
"https://[::1]/v1/predict",
"https://localhost/v1/predict",
"https://service.localhost/v1/predict",
} {
if _, err := NormalizeHTTPSURL(input); err == nil {
t.Fatalf("unsafe endpoint was accepted: %s", input)
}
}
}
func TestIsPublicRemoteIPRejectsReservedAddresses(t *testing.T) {
for _, value := range []string{
"0.0.0.0",
"100.64.0.1",
"127.0.0.1",
"169.254.0.1",
"192.0.2.1",
"198.18.0.1",
"198.51.100.1",
"203.0.113.1",
"240.0.0.1",
"::1",
"64:ff9b::7f00:1",
"100::1",
"2001::1",
"2001:db8::1",
"2002:7f00:1::",
"3fff::1",
"4000::1",
"fc00::1",
"fe80::1",
} {
if IsPublicRemoteIP(netip.MustParseAddr(value)) {
t.Fatalf("reserved address was accepted: %s", value)
}
}
for _, value := range []string{"8.8.8.8", "1.1.1.1", "2606:4700:4700::1111"} {
if !IsPublicRemoteIP(netip.MustParseAddr(value)) {
t.Fatalf("public address was rejected: %s", value)
}
}
}
func TestRestrictedHTTPClientRejectsLocalEndpoint(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
client := NewRestrictedHTTPClient(1000)
if _, err := client.Get(server.URL); err == nil {
t.Fatal("restricted client connected to a local endpoint")
}
}
func TestRestrictedHTTPClientRejectsPrivateRedirect(t *testing.T) {
client := NewRestrictedHTTPClient(1000)
request := httptest.NewRequest(http.MethodGet, "https://127.0.0.1/internal", nil)
if err := client.CheckRedirect(request, nil); err == nil {
t.Fatal("restricted client accepted a private redirect")
}
}
func TestRestrictedHTTPClientRejectsCrossTargetRedirect(t *testing.T) {
client := NewRestrictedHTTPClientForTargets(1000, []NetworkTarget{{Scheme: "https", Host: "allowed.example.com", Port: 443}})
request := httptest.NewRequest(http.MethodGet, "https://other.example.com/internal", nil)
if err := client.CheckRedirect(request, nil); err == nil {
t.Fatal("restricted client accepted a redirect outside the authorized target")
}
}
func TestRestrictedHTTPClientRejectsInitialHTTPRequest(t *testing.T) {
client := NewRestrictedHTTPClientForTargets(1000, []NetworkTarget{{Scheme: "https", Host: "allowed.example.com", Port: 443}})
if _, err := client.Get("http://allowed.example.com:443/internal"); err == nil {
t.Fatal("restricted client accepted an initial HTTP request")
}
}
func TestRestrictedHTTPClientRejectsInitialUnauthorizedPort(t *testing.T) {
client := NewRestrictedHTTPClientForTargets(1000, []NetworkTarget{{Scheme: "https", Host: "allowed.example.com", Port: 443}})
if _, err := client.Get("https://allowed.example.com:8443/internal"); err == nil {
t.Fatal("restricted client accepted an unauthorized initial port")
}
}
func TestRestrictedHTTPClientAllowsAuthorizedRedirect(t *testing.T) {
client := NewRestrictedHTTPClientForTargets(1000, []NetworkTarget{{Scheme: "https", Host: "allowed.example.com", Port: 443}})
request := httptest.NewRequest(http.MethodGet, "https://allowed.example.com/next", nil)
if err := client.CheckRedirect(request, nil); err != nil {
t.Fatalf("authorized redirect was rejected: %v", err)
}
}
func TestNormalizeTimeoutMS(t *testing.T) {
if NormalizeTimeoutMS(0) != defaultTimeoutMS || NormalizeTimeoutMS(1) != minTimeoutMS || NormalizeTimeoutMS(60000) != maxTimeoutMS {
t.Fatal("timeout limit was not normalized")
}
}
+203
View File
@@ -0,0 +1,203 @@
package credential
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"git.pcux.cn/xuans/Smail/utils/atomicfile"
"io"
"os"
"path/filepath"
"runtime"
"strings"
)
const encryptedPrefix = "enc:v1:"
var credentialAAD = []byte("smail-config-v1")
func Encrypt(root string, plaintext string) (string, error) {
if plaintext == "" {
return plaintext, nil
}
key, err := loadOrCreateKey(root)
if err != nil {
return "", errors.New("credential protection failed")
}
block, err := aes.NewCipher(key)
if err != nil {
return "", errors.New("credential protection failed")
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", errors.New("credential protection failed")
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", errors.New("credential protection failed")
}
sealed := gcm.Seal(nonce, nonce, []byte(plaintext), credentialAAD)
return encryptedPrefix + base64.RawURLEncoding.EncodeToString(sealed), nil
}
func Decrypt(root string, value string) (string, error) {
if value == "" {
return value, nil
}
if !IsEncrypted(value) {
return "", errors.New("credential decryption failed")
}
encoded := strings.TrimPrefix(value, encryptedPrefix)
sealed, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil || base64.RawURLEncoding.EncodeToString(sealed) != encoded {
return "", errors.New("credential decryption failed")
}
key, err := readKey(root)
if err != nil {
return "", errors.New("credential decryption failed")
}
block, err := aes.NewCipher(key)
if err != nil {
return "", errors.New("credential decryption failed")
}
gcm, err := cipher.NewGCM(block)
if err != nil || len(sealed) < gcm.NonceSize() {
return "", errors.New("credential decryption failed")
}
plaintext, err := gcm.Open(nil, sealed[:gcm.NonceSize()], sealed[gcm.NonceSize():], credentialAAD)
if err != nil {
return "", errors.New("credential decryption failed")
}
return string(plaintext), nil
}
func IsEncrypted(value string) bool {
return strings.HasPrefix(value, encryptedPrefix)
}
func loadOrCreateKey(root string) ([]byte, error) {
key, err := readKey(root)
if err == nil {
return key, nil
}
if !os.IsNotExist(err) {
return nil, err
}
directory := filepath.Join(root, "config")
if err := os.MkdirAll(directory, 0700); err != nil {
return nil, err
}
if err := validateKeyDirectory(directory); err != nil {
return nil, err
}
rootHandle, err := os.OpenRoot(directory)
if err != nil {
return nil, err
}
defer rootHandle.Close()
key = make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
return nil, err
}
temporaryName, err := newCredentialKeyTemporaryName()
if err != nil {
return nil, err
}
file, err := rootHandle.OpenFile(temporaryName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return nil, err
}
closed := false
defer func() {
if !closed {
_ = file.Close()
}
_ = rootHandle.Remove(temporaryName)
}()
written, err := file.Write(key)
if err != nil {
return nil, err
}
if written != len(key) {
return nil, io.ErrShortWrite
}
if err := file.Sync(); err != nil {
return nil, err
}
if err := file.Close(); err != nil {
closed = true
return nil, err
}
closed = true
if err := rootHandle.Chmod(temporaryName, 0600); err != nil {
return nil, err
}
if err := rootHandle.Link(temporaryName, "credential.key"); err != nil {
if os.IsExist(err) {
return readKey(root)
}
return nil, err
}
if err := rootHandle.Chmod("credential.key", 0600); err != nil {
return nil, err
}
if err := atomicfile.SyncDirectory(directory); err != nil {
return nil, err
}
return key, nil
}
func newCredentialKeyTemporaryName() (string, error) {
token := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, token); err != nil {
return "", err
}
return ".credential.key-" + base64.RawURLEncoding.EncodeToString(token), nil
}
func readKey(root string) ([]byte, error) {
directory := filepath.Join(root, "config")
if err := validateKeyDirectory(directory); err != nil {
return nil, err
}
keyInfo, err := os.Lstat(filepath.Join(directory, "credential.key"))
if err != nil {
return nil, err
}
if keyInfo.Mode()&os.ModeSymlink != 0 || !keyInfo.Mode().IsRegular() {
return nil, errors.New("invalid credential key")
}
if runtime.GOOS != "windows" && keyInfo.Mode().Perm()&0077 != 0 {
return nil, errors.New("invalid credential key")
}
rootHandle, err := os.OpenRoot(directory)
if err != nil {
return nil, err
}
defer rootHandle.Close()
key, err := rootHandle.ReadFile("credential.key")
if err != nil {
return nil, err
}
if len(key) != 32 {
return nil, errors.New("invalid credential key")
}
return key, nil
}
func validateKeyDirectory(directory string) error {
info, err := os.Lstat(directory)
if err != nil {
return err
}
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
return errors.New("invalid credential directory")
}
if runtime.GOOS != "windows" && info.Mode().Perm()&0077 != 0 {
return errors.New("invalid credential directory")
}
return nil
}
+161
View File
@@ -0,0 +1,161 @@
package credential
import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
)
func TestEncryptDecrypt(t *testing.T) {
root := t.TempDir()
encrypted, err := Encrypt(root, "secret-value")
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(encrypted, encryptedPrefix) || strings.Contains(encrypted, "secret-value") {
t.Fatal("credential was not encrypted")
}
decrypted, err := Decrypt(root, encrypted)
if err != nil || decrypted != "secret-value" {
t.Fatal("credential could not be decrypted")
}
info, err := os.Stat(filepath.Join(root, "config", "credential.key"))
if err != nil {
t.Fatal(err)
}
if runtime.GOOS != "windows" && info.Mode().Perm()&0077 != 0 {
t.Fatalf("credential key permissions are too broad: %o", info.Mode().Perm())
}
}
func TestDecryptRejectsTampering(t *testing.T) {
root := t.TempDir()
encrypted, err := Encrypt(root, "secret-value")
if err != nil {
t.Fatal(err)
}
replacement := byte('A')
if encrypted[len(encrypted)-1] == replacement {
replacement = 'B'
}
encrypted = encrypted[:len(encrypted)-1] + string(replacement)
if _, err := Decrypt(root, encrypted); err == nil {
t.Fatal("tampered credential was accepted")
}
}
func TestDecryptRejectsPlaintext(t *testing.T) {
if _, err := Decrypt(t.TempDir(), "legacy-plaintext-secret"); err == nil {
t.Fatal("plaintext credential was accepted by strict decryption")
}
}
func TestEncryptKeepsEmptyAndProtectsPrefixValues(t *testing.T) {
root := t.TempDir()
if value, err := Encrypt(root, ""); err != nil || value != "" {
t.Fatal("empty credential changed")
}
plaintext := "enc:v1:third-party-secret"
encrypted, err := Encrypt(root, plaintext)
if err != nil {
t.Fatal(err)
}
if encrypted == plaintext || !strings.HasPrefix(encrypted, encryptedPrefix) {
t.Fatal("prefix credential was not encrypted")
}
decrypted, err := Decrypt(root, encrypted)
if err != nil || decrypted != plaintext {
t.Fatal("prefix credential did not round trip")
}
}
func TestCredentialKeyTemporaryNamesAreIndependentRandomValues(t *testing.T) {
seen := make(map[string]struct{})
for i := 0; i < 32; i++ {
name, err := newCredentialKeyTemporaryName()
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(name, ".credential.key-") {
t.Fatalf("unexpected temporary name: %s", name)
}
if _, ok := seen[name]; ok {
t.Fatalf("duplicate temporary name: %s", name)
}
seen[name] = struct{}{}
}
}
func TestReadKeyRejectsDirectorySymlink(t *testing.T) {
root := t.TempDir()
target := t.TempDir()
if err := os.WriteFile(filepath.Join(target, "credential.key"), make([]byte, 32), 0600); err != nil {
t.Fatal(err)
}
if err := os.Symlink(target, filepath.Join(root, "config")); err != nil {
t.Skipf("symlink is unavailable: %v", err)
}
if _, err := readKey(root); err == nil {
t.Fatal("credential directory symlink was accepted")
}
}
func TestReadKeyRejectsKeySymlink(t *testing.T) {
root := t.TempDir()
configDir := filepath.Join(root, "config")
if err := os.MkdirAll(configDir, 0700); err != nil {
t.Fatal(err)
}
target := filepath.Join(root, "outside.key")
if err := os.WriteFile(target, make([]byte, 32), 0600); err != nil {
t.Fatal(err)
}
if err := os.Symlink(target, filepath.Join(configDir, "credential.key")); err != nil {
t.Skipf("symlink is unavailable: %v", err)
}
if _, err := readKey(root); err == nil {
t.Fatal("credential key symlink was accepted")
}
}
func TestConcurrentEncryptUsesSingleCompleteKey(t *testing.T) {
root := t.TempDir()
const workers = 16
values := make(chan string, workers)
errors := make(chan error, workers)
var group sync.WaitGroup
for i := 0; i < workers; i++ {
group.Add(1)
go func() {
defer group.Done()
value, err := Encrypt(root, "shared-secret")
if err != nil {
errors <- err
return
}
values <- value
}()
}
group.Wait()
close(values)
close(errors)
for err := range errors {
t.Fatal(err)
}
for value := range values {
plaintext, err := Decrypt(root, value)
if err != nil || plaintext != "shared-secret" {
t.Fatalf("concurrent credential could not be decrypted: %v", err)
}
}
entries, err := os.ReadDir(filepath.Join(root, "config"))
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 || entries[0].Name() != "credential.key" {
t.Fatalf("unexpected credential directory contents: %#v", entries)
}
}
+87
View File
@@ -0,0 +1,87 @@
package credential
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"strings"
)
const CurrentVersion uint32 = 1
var (
ErrVaultUnavailable = errors.New("credential vault unavailable")
ErrUnsupportedVersion = errors.New("unsupported credential version")
)
type Vault struct {
root string
}
func NewVault(root string) (*Vault, error) {
root = strings.TrimSpace(root)
if root == "" {
return nil, ErrVaultUnavailable
}
key, err := loadOrCreateKey(root)
if err != nil {
return nil, ErrVaultUnavailable
}
for index := range key {
key[index] = 0
}
return &Vault{root: root}, nil
}
func (v *Vault) Version() uint32 {
if v == nil {
return 0
}
return CurrentVersion
}
func (v *Vault) Encrypt(plaintext string) (string, error) {
if v == nil {
return "", ErrVaultUnavailable
}
value, err := Encrypt(v.root, plaintext)
if err != nil {
return "", ErrVaultUnavailable
}
return value, nil
}
func (v *Vault) Decrypt(version uint32, ciphertext string) (string, error) {
if v == nil {
return "", ErrVaultUnavailable
}
if version != CurrentVersion {
return "", ErrUnsupportedVersion
}
value, err := Decrypt(v.root, ciphertext)
if err != nil {
return "", ErrVaultUnavailable
}
return value, nil
}
// Fingerprint 使用数据库外主密钥计算固定长度校验值,不能用于恢复原文。
func (v *Vault) Fingerprint(value string) (string, error) {
if v == nil {
return "", ErrVaultUnavailable
}
key, err := readKey(v.root)
if err != nil {
return "", ErrVaultUnavailable
}
defer func() {
for index := range key {
key[index] = 0
}
}()
mac := hmac.New(sha256.New, key)
_, _ = mac.Write([]byte("smail-plugin-setting-v1\x00"))
_, _ = mac.Write([]byte(value))
return hex.EncodeToString(mac.Sum(nil)), nil
}
+61
View File
@@ -0,0 +1,61 @@
package credential
import (
"errors"
"os"
"path/filepath"
"runtime"
"testing"
)
func TestVaultRejectsUnsupportedVersionAndWrongKey(t *testing.T) {
vault, err := NewVault(t.TempDir())
if err != nil {
t.Fatal(err)
}
ciphertext, err := vault.Encrypt("provider-secret")
if err != nil {
t.Fatal(err)
}
if _, err := vault.Decrypt(CurrentVersion+1, ciphertext); !errors.Is(err, ErrUnsupportedVersion) {
t.Fatalf("unexpected version error: %v", err)
}
other, err := NewVault(t.TempDir())
if err != nil {
t.Fatal(err)
}
if _, err := other.Decrypt(CurrentVersion, ciphertext); err == nil {
t.Fatal("wrong key was accepted")
}
}
func TestVaultRejectsBroadKeyPermissions(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("POSIX permission bits are unavailable")
}
root := t.TempDir()
configDir := filepath.Join(root, "config")
if err := os.Mkdir(configDir, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(configDir, "credential.key"), make([]byte, 32), 0644); err != nil {
t.Fatal(err)
}
if _, err := NewVault(root); err == nil {
t.Fatal("broad credential key permissions were accepted")
}
}
func TestVaultRejectsBroadDirectoryPermissions(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("POSIX permission bits are unavailable")
}
root := t.TempDir()
configDir := filepath.Join(root, "config")
if err := os.Mkdir(configDir, 0755); err != nil {
t.Fatal(err)
}
if _, err := NewVault(root); err == nil {
t.Fatal("broad credential directory permissions were accepted")
}
}
+57
View File
@@ -0,0 +1,57 @@
package csrf
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"net/http"
"git.pcux.cn/xuans/Smail/session"
)
const (
CookieName = "smail_csrf"
HeaderName = "X-CSRF-Token"
SessionKey = "csrf_token"
)
func Issue(w http.ResponseWriter, r *http.Request, secure bool) (string, error) {
raw := make([]byte, 32)
if _, err := rand.Read(raw); err != nil {
return "", err
}
token := base64.RawURLEncoding.EncodeToString(raw)
session.Instance.Put(r.Context(), SessionKey, token)
http.SetCookie(w, &http.Cookie{
Name: CookieName, Value: token, Path: "/", MaxAge: 86400,
Secure: secure, HttpOnly: false, SameSite: http.SameSiteStrictMode,
})
return token, nil
}
func Clear(w http.ResponseWriter, r *http.Request, secure bool) {
if session.Instance != nil {
session.Instance.Remove(r.Context(), SessionKey)
}
http.SetCookie(w, &http.Cookie{
Name: CookieName, Value: "", Path: "/", MaxAge: -1,
Secure: secure, HttpOnly: false, SameSite: http.SameSiteStrictMode,
})
}
func Valid(r *http.Request) bool {
if session.Instance == nil {
return false
}
header := r.Header.Get(HeaderName)
cookie, err := r.Cookie(CookieName)
if err != nil || header == "" || cookie.Value == "" {
return false
}
stored := session.Instance.GetString(r.Context(), SessionKey)
if stored == "" || len(stored) != len(header) || len(cookie.Value) != len(header) {
return false
}
return subtle.ConstantTimeCompare([]byte(stored), []byte(header)) == 1 &&
subtle.ConstantTimeCompare([]byte(cookie.Value), []byte(header)) == 1
}
+78
View File
@@ -0,0 +1,78 @@
package csrf
import (
"net/http"
"net/http/httptest"
"testing"
"git.pcux.cn/xuans/Smail/session"
"github.com/alexedwards/scs/v2"
)
func testRequestWithToken(t *testing.T, manager *scs.SessionManager, stored, header, cookie string) *http.Request {
t.Helper()
request := httptest.NewRequest(http.MethodPost, "https://mail.example.com/api/settings", nil)
loaded, err := manager.Load(request.Context(), "")
if err != nil {
t.Fatal(err)
}
request = request.WithContext(loaded)
if stored != "" {
manager.Put(request.Context(), SessionKey, stored)
}
if header != "" {
request.Header.Set(HeaderName, header)
}
if cookie != "" {
request.AddCookie(&http.Cookie{Name: CookieName, Value: cookie})
}
return request
}
func TestValidRejectsMissingAndMismatchedTokens(t *testing.T) {
old := session.Instance
manager := scs.New()
session.Instance = manager
t.Cleanup(func() { session.Instance = old })
cases := []struct {
name string
stored string
header string
cookie string
}{
{name: "missing-header", stored: "token", cookie: "token"},
{name: "missing-cookie", stored: "token", header: "token"},
{name: "header-mismatch", stored: "token", header: "other", cookie: "token"},
{name: "cookie-mismatch", stored: "token", header: "token", cookie: "other"},
{name: "missing-session", header: "token", cookie: "token"},
}
for _, item := range cases {
t.Run(item.name, func(t *testing.T) {
if Valid(testRequestWithToken(t, manager, item.stored, item.header, item.cookie)) {
t.Fatal("invalid CSRF token combination was accepted")
}
})
}
}
func TestClearInvalidatesExistingToken(t *testing.T) {
old := session.Instance
manager := scs.New()
session.Instance = manager
t.Cleanup(func() { session.Instance = old })
request := testRequestWithToken(t, manager, "token", "token", "token")
if !Valid(request) {
t.Fatal("valid CSRF token was rejected")
}
recorder := httptest.NewRecorder()
Clear(recorder, request, true)
if Valid(request) {
t.Fatal("cleared CSRF token remained valid")
}
cookies := recorder.Result().Cookies()
if len(cookies) != 1 || cookies[0].Name != CookieName || cookies[0].MaxAge >= 0 || !cookies[0].Secure {
t.Fatalf("unexpected CSRF clear cookie: %+v", cookies)
}
}
+34
View File
@@ -0,0 +1,34 @@
package errors
import (
oe "errors"
"fmt"
"runtime"
)
func New(text string) error {
_, file, line, _ := runtime.Caller(1)
return oe.New(fmt.Sprintf("%s at %s:%d", text, file, line))
}
func Wrap(err error) error {
_, file, line, _ := runtime.Caller(1)
return fmt.Errorf("at %s:%d\n%w", file, line, err)
}
func WrapWithMsg(err error, msg string) error {
_, file, line, _ := runtime.Caller(1)
return fmt.Errorf("%s at %s:%d\n%w", msg, file, line, err)
}
func Unwrap(err error) error {
return oe.Unwrap(err)
}
func Is(err, target error) bool {
return oe.Is(err, target)
}
func As(err error, target any) bool {
return oe.As(err, target)
}
+11
View File
@@ -0,0 +1,11 @@
package file
import "os"
func PathExist(_path string) bool {
_, err := os.Stat(_path)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
+60
View File
@@ -0,0 +1,60 @@
package file
import (
"errors"
"os"
"path/filepath"
"strings"
)
var ErrUnsafeRegularFile = errors.New("unsafe regular file")
// ReadRegularFile reads a regular file through a directory-scoped handle.
func ReadRegularFile(path string, maxSize int64) ([]byte, error) {
if strings.TrimSpace(path) == "" || maxSize <= 0 {
return nil, ErrUnsafeRegularFile
}
absolute, err := filepath.Abs(filepath.Clean(path))
if err != nil {
return nil, ErrUnsafeRegularFile
}
root, err := os.OpenRoot(filepath.Dir(absolute))
if err != nil {
return nil, ErrUnsafeRegularFile
}
defer root.Close()
name := filepath.Base(absolute)
info, err := root.Lstat(name)
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > maxSize {
return nil, ErrUnsafeRegularFile
}
data, err := root.ReadFile(name)
if err != nil || int64(len(data)) > maxSize {
return nil, ErrUnsafeRegularFile
}
return data, nil
}
func ControlledRelativePath(base, value string) (string, error) {
if strings.TrimSpace(value) == "" {
return "", errors.New("path is required")
}
basePath, err := filepath.Abs(filepath.Clean(base))
if err != nil {
return "", err
}
targetPath := value
if !filepath.IsAbs(targetPath) {
targetPath = filepath.Join(basePath, targetPath)
}
targetPath, err = filepath.Abs(filepath.Clean(targetPath))
if err != nil {
return "", err
}
relativePath, err := filepath.Rel(basePath, targetPath)
if err != nil || relativePath == "." || filepath.IsAbs(relativePath) || relativePath == ".." || strings.HasPrefix(relativePath, ".."+string(filepath.Separator)) {
return "", errors.New("path escapes controlled directory")
}
return relativePath, nil
}
+51
View File
@@ -0,0 +1,51 @@
package file
import (
"os"
"path/filepath"
"testing"
)
func TestControlledRelativePath(t *testing.T) {
base := t.TempDir()
inside := filepath.Join(base, "nested", "key.pem")
relative, err := ControlledRelativePath(base, inside)
if err != nil || relative != filepath.Join("nested", "key.pem") {
t.Fatalf("unexpected controlled path result: %q, %v", relative, err)
}
if _, err := ControlledRelativePath(base, filepath.Join(base, "..", "outside.pem")); err == nil {
t.Fatal("outside path must be rejected")
}
if _, err := ControlledRelativePath(base, ""); err == nil {
t.Fatal("empty path must be rejected")
}
}
func TestReadRegularFileRejectsSymlinkAndOversizedFile(t *testing.T) {
root := t.TempDir()
regular := filepath.Join(root, "regular.txt")
if err := os.WriteFile(regular, []byte("safe"), 0600); err != nil {
t.Fatal(err)
}
data, err := ReadRegularFile(regular, 16)
if err != nil || string(data) != "safe" {
t.Fatalf("regular file read failed: %q, %v", data, err)
}
if err := os.WriteFile(regular, []byte("too large"), 0600); err != nil {
t.Fatal(err)
}
if _, err := ReadRegularFile(regular, 4); err == nil {
t.Fatal("oversized file was accepted")
}
outside := filepath.Join(t.TempDir(), "outside.txt")
if err := os.WriteFile(outside, []byte("outside"), 0600); err != nil {
t.Fatal(err)
}
link := filepath.Join(root, "link.txt")
if err := os.Symlink(outside, link); err != nil {
t.Skipf("symlink is unavailable: %v", err)
}
if _, err := ReadRegularFile(link, 16); err == nil {
t.Fatal("symlink was accepted")
}
}
+58
View File
@@ -0,0 +1,58 @@
package id
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"net"
"os"
"time"
)
var ip_instance string
func getLocalIP() string {
if ip_instance != "" {
return ip_instance
}
ip := "127.0.0.1"
addrs, err := net.InterfaceAddrs()
if err != nil {
ip_instance = ip
return ip
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip = ipnet.IP.String()
break
}
}
}
ip_instance = ip
return ip
}
func GenLogID() string {
ip := getLocalIP()
now := time.Now()
timeNano := now.UnixNano()
pid := os.Getpid()
randomBytes := make([]byte, 3)
if _, err := rand.Read(randomBytes); err != nil {
randomBytes = []byte{byte(timeNano), byte(timeNano >> 8), byte(pid)}
}
b := bytes.Buffer{}
b.WriteString(hex.EncodeToString(net.ParseIP(ip).To4()))
b.WriteString(fmt.Sprintf("%x", now.Unix()&0xffffffff))
b.WriteString(fmt.Sprintf("%04x", timeNano&0xffff))
b.WriteString(fmt.Sprintf("%04x", pid&0xffff))
b.WriteString(hex.EncodeToString(randomBytes))
b.WriteString("b0")
return b.String()
}
+126
View File
@@ -0,0 +1,126 @@
package ip
import (
"errors"
"io"
"net"
"net/http"
"net/netip"
"strings"
"sync"
"time"
)
const (
publicIPv4Endpoint = "https://api.ipify.org"
publicIPv6Endpoint = "https://api6.ipify.org"
maxIPResponseBytes = 64
)
var (
publicIPClient = &http.Client{Timeout: 3 * time.Second}
cacheMu sync.Mutex
cachedIPv4 string
cachedIPv6 string
)
func GetIp() string {
value := GetIPv4()
if value == "" {
return "[Your Server IP]"
}
return value
}
func GetIPv4() string {
return getPublicIP(publicIPv4Endpoint, false)
}
func GetIPv6() string {
return getPublicIP(publicIPv6Endpoint, true)
}
func getPublicIP(endpoint string, wantIPv6 bool) string {
cacheMu.Lock()
if wantIPv6 && cachedIPv6 != "" {
value := cachedIPv6
cacheMu.Unlock()
return value
}
if !wantIPv6 && cachedIPv4 != "" {
value := cachedIPv4
cacheMu.Unlock()
return value
}
cacheMu.Unlock()
request, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return ""
}
value, err := fetchPublicIPForFamily(publicIPClient, request, wantIPv6)
if err != nil {
return ""
}
cacheMu.Lock()
defer cacheMu.Unlock()
if wantIPv6 {
if cachedIPv6 == "" {
cachedIPv6 = value
}
return cachedIPv6
}
if cachedIPv4 == "" {
cachedIPv4 = value
}
return cachedIPv4
}
func fetchPublicIPForFamily(client *http.Client, request *http.Request, wantIPv6 bool) (string, error) {
value, err := fetchPublicIP(client, request)
if err != nil {
return "", err
}
address, err := netip.ParseAddr(value)
if err != nil || address.Is6() != wantIPv6 {
return "", errors.New("public IP address family does not match")
}
return address.String(), nil
}
func fetchPublicIP(client *http.Client, request *http.Request) (string, error) {
if client == nil || request == nil || request.URL == nil || request.URL.Scheme != "https" {
return "", errors.New("invalid public IP request")
}
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", errors.New("public IP service returned an unexpected status")
}
body, err := io.ReadAll(io.LimitReader(response.Body, maxIPResponseBytes+1))
if err != nil {
return "", err
}
if len(body) > maxIPResponseBytes {
return "", errors.New("public IP response is too large")
}
value := strings.TrimSpace(string(body))
parsed, err := netip.ParseAddr(value)
if err != nil || !isPublicIP(parsed) {
return "", errors.New("public IP response is invalid")
}
return parsed.String(), nil
}
func isPublicIP(value netip.Addr) bool {
return value.IsValid() && value.IsGlobalUnicast() &&
!value.IsPrivate() && !value.IsLoopback() &&
!value.IsLinkLocalUnicast() && !value.IsLinkLocalMulticast() &&
!value.IsMulticast() && !value.IsUnspecified() &&
!net.IP(value.AsSlice()).IsInterfaceLocalMulticast()
}
+88
View File
@@ -0,0 +1,88 @@
package ip
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestFetchPublicIPAcceptsPublicAddress(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("8.8.8.8\n"))
}))
defer server.Close()
client := server.Client()
client.Timeout = time.Second
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatal(err)
}
value, err := fetchPublicIP(client, request)
if err != nil || value != "8.8.8.8" {
t.Fatalf("unexpected public IP result: value=%q err=%v", value, err)
}
}
func TestFetchPublicIPRejectsUnsafeResponses(t *testing.T) {
cases := []struct {
name string
status int
body string
}{
{name: "private", status: http.StatusOK, body: "192.168.1.10"},
{name: "loopback", status: http.StatusOK, body: "127.0.0.1"},
{name: "invalid", status: http.StatusOK, body: "not-an-ip"},
{name: "large", status: http.StatusOK, body: strings.Repeat("1", maxIPResponseBytes+1)},
{name: "status", status: http.StatusBadGateway, body: "8.8.8.8"},
}
for _, item := range cases {
t.Run(item.name, func(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(item.status)
_, _ = w.Write([]byte(item.body))
}))
defer server.Close()
client := server.Client()
client.Timeout = time.Second
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatal(err)
}
if _, err := fetchPublicIP(client, request); err == nil {
t.Fatal("unsafe public IP response was accepted")
}
})
}
}
func TestFetchPublicIPRejectsPlainHTTP(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, "http://example.com", nil)
if err != nil {
t.Fatal(err)
}
if _, err := fetchPublicIP(&http.Client{Timeout: time.Second}, request); err == nil {
t.Fatal("plain HTTP public IP request was accepted")
}
}
func TestFetchPublicIPForFamilyRequiresRequestedAddressFamily(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("2001:db8::1\n"))
}))
defer server.Close()
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatal(err)
}
value, err := fetchPublicIPForFamily(server.Client(), request, true)
if err != nil || value != "2001:db8::1" {
t.Fatalf("unexpected IPv6 result: value=%q err=%v", value, err)
}
if _, err := fetchPublicIPForFamily(server.Client(), request, false); err == nil {
t.Fatal("IPv6 result was accepted for IPv4 request")
}
}
+110
View File
@@ -0,0 +1,110 @@
package password
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
"strings"
"golang.org/x/crypto/argon2"
)
var ErrInvalidLength = errors.New("password length is invalid")
const (
format = "argon2id"
version = 19
memory = 64 * 1024
iterations = 3
parallelism = 1
saltLength = 16
keyLength = 32
maxHashSize = 255
minSecretSize = 8
maxSecretSize = 256
minPasswordSize = 8
maxPasswordSize = 12
)
// ValidateUserPassword 校验登录密码的统一长度边界。
// 长度按 UTF-8 编码后的字节数计算,避免不同入口采用不同规则。
func ValidateUserPassword(plain string) error {
passwordSize := len([]byte(plain))
if passwordSize < minPasswordSize || passwordSize > maxPasswordSize {
return ErrInvalidLength
}
return nil
}
// Hash 生成 Argon2id 密码哈希。
func Hash(plain string) (string, error) {
plainSize := len([]byte(plain))
if plainSize < minSecretSize || plainSize > maxSecretSize {
return "", ErrInvalidLength
}
salt := make([]byte, saltLength)
if _, err := rand.Read(salt); err != nil {
return "", fmt.Errorf("generate password salt: %w", err)
}
hash := argon2.IDKey([]byte(plain), salt, iterations, memory, parallelism, keyLength)
encoded := fmt.Sprintf("$%s$v=%d$m=%d,t=%d,p=%d$%s$%s",
format,
version,
memory,
iterations,
parallelism,
base64.RawStdEncoding.EncodeToString(salt),
base64.RawStdEncoding.EncodeToString(hash),
)
if len(encoded) > maxHashSize {
return "", errors.New("password hash is too long")
}
return encoded, nil
}
// Encode 保留统一的哈希生成入口,但不保留旧算法兼容逻辑。
func Encode(plain string) string {
hash, err := Hash(plain)
if err != nil {
return ""
}
return hash
}
// HashUserPassword 生成符合登录密码策略的 Argon2id 哈希。
func HashUserPassword(plain string) (string, error) {
if err := ValidateUserPassword(plain); err != nil {
return "", err
}
return Hash(plain)
}
// Verify 校验 Argon2id 密码哈希,不接受旧算法格式。
func Verify(encoded, plain string) bool {
if len([]byte(plain)) > maxSecretSize {
return false
}
parts := strings.Split(encoded, "$")
if len(parts) != 6 || parts[1] != format || parts[2] != fmt.Sprintf("v=%d", version) || parts[3] != fmt.Sprintf("m=%d,t=%d,p=%d", memory, iterations, parallelism) {
return false
}
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
if err != nil || len(salt) != saltLength {
return false
}
expected, err := base64.RawStdEncoding.DecodeString(parts[5])
if err != nil || len(expected) != keyLength {
return false
}
actual := argon2.IDKey([]byte(plain), salt, iterations, memory, parallelism, keyLength)
return subtle.ConstantTimeCompare(actual, expected) == 1
}
func VerifyDummy(plain string) bool {
salt := [saltLength]byte{0x53, 0x6d, 0x61, 0x69, 0x6c, 0x2d, 0x61, 0x75, 0x74, 0x68, 0x2d, 0x64, 0x75, 0x6d, 0x6d, 0x79}
expected := [keyLength]byte{0x91, 0xa4, 0x65, 0x82, 0x31, 0x07, 0x44, 0xc8, 0x7a, 0x22, 0xe1, 0x59, 0x3c, 0x5b, 0x8d, 0xf1, 0xd4, 0x3a, 0x68, 0x9c, 0xee, 0x20, 0x57, 0x61, 0x45, 0x3a, 0x71, 0x10, 0x5b, 0x26, 0xb6, 0xa0}
actual := argon2.IDKey([]byte(plain), salt[:], iterations, memory, parallelism, keyLength)
return subtle.ConstantTimeCompare(actual, expected[:]) == 1
}
+70
View File
@@ -0,0 +1,70 @@
package password
import (
"errors"
"strings"
"testing"
)
func TestHashAndVerify(t *testing.T) {
encoded, err := Hash("StrongPass123")
if err != nil {
t.Fatal(err)
}
if !Verify(encoded, "StrongPass123") || Verify(encoded, "WrongPass123") {
t.Fatal("password verification returned an invalid result")
}
}
func TestHashRejectsInvalidLengths(t *testing.T) {
for _, plain := range []string{"short", strings.Repeat("a", maxSecretSize+1)} {
if _, err := Hash(plain); err == nil {
t.Fatalf("invalid secret length was accepted: %d", len([]byte(plain)))
}
}
if Verify(mustHash(t, "StrongPass123"), strings.Repeat("a", maxSecretSize+1)) {
t.Fatal("oversized verification input was accepted")
}
}
func TestUserPasswordLengthBoundaries(t *testing.T) {
for _, plain := range []string{"12345678", "123456789012", "密码aa"} {
if err := ValidateUserPassword(plain); err != nil {
t.Fatalf("valid password length %d was rejected: %v", len([]byte(plain)), err)
}
if _, err := HashUserPassword(plain); err != nil {
t.Fatalf("valid user password was not hashed: %v", err)
}
}
for _, plain := range []string{"1234567", "1234567890123", "密码密码密码"} {
if err := ValidateUserPassword(plain); !errors.Is(err, ErrInvalidLength) {
t.Fatalf("invalid password length %d returned %v", len([]byte(plain)), err)
}
if _, err := HashUserPassword(plain); !errors.Is(err, ErrInvalidLength) {
t.Fatalf("invalid user password length %d returned %v", len([]byte(plain)), err)
}
}
}
func TestVerifyRejectsNonCanonicalArgon2Parameters(t *testing.T) {
encoded := mustHash(t, "StrongPass123")
for _, modified := range []string{
strings.Replace(encoded, "m=65536", "m=8192", 1),
strings.Replace(encoded, "t=3", "t=1", 1),
strings.Replace(encoded, "p=1", "p=2", 1),
strings.Replace(encoded, "v=19", "v=16", 1),
} {
if Verify(modified, "StrongPass123") {
t.Fatalf("non-canonical hash was accepted: %s", modified)
}
}
}
func mustHash(t *testing.T, plain string) string {
t.Helper()
encoded, err := Hash(plain)
if err != nil {
t.Fatal(err)
}
return encoded
}
+23
View File
@@ -0,0 +1,23 @@
package query
import "strings"
func IntList(values []int, max int) (string, []any, bool) {
if len(values) == 0 || max <= 0 || len(values) > max {
return "", nil, false
}
args := make([]any, len(values))
var placeholders strings.Builder
placeholders.Grow(len(values)*2 - 1)
for i, value := range values {
if value <= 0 {
return "", nil, false
}
if i > 0 {
placeholders.WriteByte(',')
}
placeholders.WriteByte('?')
args[i] = value
}
return placeholders.String(), args, true
}
+15
View File
@@ -0,0 +1,15 @@
package query
import "testing"
func TestIntList(t *testing.T) {
placeholders, args, ok := IntList([]int{1, 2, 3}, 3)
if !ok || placeholders != "?,?,?" || len(args) != 3 {
t.Fatalf("unexpected int list result: %q %#v %v", placeholders, args, ok)
}
for _, values := range [][]int{nil, {}, {0}, {-1}, {1, 2, 3, 4}} {
if _, _, ok := IntList(values, 3); ok {
t.Fatalf("invalid values accepted: %#v", values)
}
}
}
+328
View File
@@ -0,0 +1,328 @@
package send
import (
"crypto/tls"
"errors"
"fmt"
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/dto/parsemail"
"git.pcux.cn/xuans/Smail/models"
"git.pcux.cn/xuans/Smail/utils/array"
"git.pcux.cn/xuans/Smail/utils/async"
"git.pcux.cn/xuans/Smail/utils/consts"
"git.pcux.cn/xuans/Smail/utils/context"
"git.pcux.cn/xuans/Smail/utils/smtp"
log "github.com/sirupsen/logrus"
"net"
"net/mail"
"net/textproto"
"strings"
"sync"
)
type mxDomain struct {
domain string
mxHost string
localTest bool
}
type temporaryMXFallbackError struct {
lookupErr error
fallbackErr error
}
func (e *temporaryMXFallbackError) Error() string {
return fmt.Sprintf("temporary MX lookup failed (%v); fallback delivery failed: %v", e.lookupErr, e.fallbackErr)
}
func (e *temporaryMXFallbackError) Unwrap() error {
return e.lookupErr
}
// Forward 转发邮件
func Forward(ctx *context.Context, e *parsemail.Email, forwardAddress string, user *models.User) error {
log.WithContext(ctx).Debugf("开始转发邮件")
b, err := e.ForwardBuildBytes(ctx, user, forwardAddress)
if err != nil {
return fmt.Errorf("build forwarded message: %w", err)
}
log.WithContext(ctx).Debug("forward message prepared")
domains := config.CurrentMailDomains()
if len(domains.Accepted) == 0 {
return errors.New("runtime configuration is unavailable")
}
from := user.Account + "@" + domains.Primary
return forwardData(ctx, domains.Primary, b, forwardAddress, from)
}
func ForwardRaw(ctx *context.Context, e *parsemail.Email, rawEmailData []byte, forwardAddress string, user *models.User) error {
log.WithContext(ctx).Debugf("开始原始邮件转发")
domains := config.CurrentMailDomains()
if len(domains.Accepted) == 0 {
return errors.New("runtime configuration is unavailable")
}
from := user.Account + "@" + domains.Primary
return forwardData(ctx, domains.Primary, rawEmailData, forwardAddress, from)
}
func forwardData(ctx *context.Context, fromDomain string, data []byte, forwardAddress string, from string) error {
var to []*parsemail.User
to = []*parsemail.User{
{EmailAddress: forwardAddress},
}
err, _ := doSend(ctx, fromDomain, data, to, from)
return err
}
func Send(ctx *context.Context, e *parsemail.Email) (error, map[string]error) {
_, fromDomain := e.From.GetDomainAccount()
b, err := e.BuildBytes(ctx, true)
if err != nil {
return fmt.Errorf("build message: %w", err), nil
}
var to []*parsemail.User
to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
return doSend(ctx, fromDomain, b, to, e.From.EmailAddress)
}
func parseRecipientAddress(user *parsemail.User) (string, string, error) {
if user == nil {
return "", "", errors.New("recipient is nil")
}
address := strings.TrimSpace(user.EmailAddress)
if address == "" || address != user.EmailAddress {
return address, "", errors.New("recipient address is empty or contains surrounding whitespace")
}
parsed, err := mail.ParseAddress(address)
if err != nil || parsed.Address != address {
return address, "", errors.New("recipient address is invalid")
}
at := strings.LastIndexByte(address, '@')
if at <= 0 || at >= len(address)-1 {
return address, "", errors.New("recipient address has no valid domain")
}
domain := strings.ToLower(address[at+1:])
if strings.ContainsAny(domain, " \t\r\n") {
return address, "", errors.New("recipient domain is invalid")
}
return address, domain, nil
}
func doSend(ctx *context.Context, fromDomain string, data []byte, to []*parsemail.User, from string) (error, map[string]error) {
if len(to) == 0 {
err := errors.New("no recipients")
return err, map[string]error{"recipients": err}
}
invalidAddresses := make([]string, 0)
invalidErrors := make(map[string]error)
// 按域名整理
toByDomain := map[mxDomain][]*parsemail.User{}
mxLookupErrors := map[mxDomain]error{}
for _, s := range to {
address, domain, addressErr := parseRecipientAddress(s)
if addressErr != nil {
if address == "" {
address = "<empty>"
}
invalidAddresses = append(invalidAddresses, address)
invalidErrors[address] = addressErr
log.WithContext(ctx).WithError(addressErr).Error("invalid recipient address")
continue
}
args := []string{address, domain}
if len(args) == 2 {
if args[1] == consts.TEST_DOMAIN {
// 测试使用
address := mxDomain{
domain: "localhost",
mxHost: "127.0.0.1",
localTest: true,
}
toByDomain[address] = append(toByDomain[address], s)
} else {
//查询dns mx记录
mxInfo, lookupErr := net.LookupMX(args[1])
address := mxDomain{
domain: "smtp." + args[1],
mxHost: "smtp." + args[1],
}
if lookupErr != nil {
log.WithContext(ctx).Errorf("%s 域名mx记录查询失败,检查邮箱是否存在!", s.EmailAddress)
}
if len(mxInfo) > 0 {
address = mxDomain{
domain: args[1],
mxHost: mxInfo[0].Host,
}
}
if lookupErr != nil {
mxLookupErrors[address] = lookupErr
}
toByDomain[address] = append(toByDomain[address], s)
}
} else {
log.WithContext(ctx).Errorf("邮箱地址解析错误! %s", s)
continue
}
}
errEmailAddress := append([]string(nil), invalidAddresses...)
var errEmailAddressMu sync.Mutex
errMap := sync.Map{}
for address, err := range invalidErrors {
errMap.Store(address, err)
}
as := async.New(ctx)
for domain, tos := range toByDomain {
domain := domain
tos := tos
mxLookupErr := mxLookupErrors[domain]
as.WaitProcess(func(p any) {
recordFailure := func(err error) {
err = deliveryFailureCause(mxLookupErr, err)
log.WithContext(ctx).Errorf("%v 邮件投递失败%+v", tos, err)
errEmailAddressMu.Lock()
for _, user := range tos {
errEmailAddress = append(errEmailAddress, user.EmailAddress)
}
errEmailAddressMu.Unlock()
errMap.Store(domain.domain, err)
}
if allowUnsafeSMTP(domain) {
err := smtp.SendMailUnsafe("localhost", domain.mxHost+":25", nil, from, fromDomain, buildAddress(tos), data)
if err != nil {
recordFailure(err)
}
return
}
// 优先尝试25端口,starttls方式投递
err := smtp.SendMail("", domain.mxHost+":25", nil, from, fromDomain, buildAddress(tos), data)
if err == nil {
return
}
if isCertificateVerificationError(err) {
recordFailure(err)
return
}
if isPermanentSMTPResponse(err) {
recordFailure(err)
return
}
log.WithContext(ctx).Infof("SMTP STARTTLS on 25 Send Error. %s", err.Error())
// 再试用587投递
err = smtp.SendMailWithTls(strings.TrimSuffix(domain.mxHost, "."), domain.mxHost+":587", nil, from, fromDomain, buildAddress(tos), data)
if err == nil {
return
}
if isCertificateVerificationError(err) {
recordFailure(err)
return
}
if isPermanentSMTPResponse(err) {
recordFailure(err)
return
}
log.WithContext(ctx).Infof("SMTPS on 587 Send Error. %s", err.Error())
// 再次尝试465投递
err = smtp.SendMailWithTls(strings.TrimSuffix(domain.mxHost, "."), domain.mxHost+":465", nil, from, fromDomain, buildAddress(tos), data)
if err == nil {
return
}
if isCertificateVerificationError(err) {
recordFailure(err)
return
}
if isPermanentSMTPResponse(err) {
recordFailure(err)
return
}
log.WithContext(ctx).Infof("SMTPS on 465 Send Error. %s", err.Error())
// 所有安全投递方式均失败后记录错误,不允许回退到明文连接。
recordFailure(err)
}, nil)
}
as.Wait()
orgMap := map[string]error{}
errMap.Range(func(key, value any) bool {
if value != nil {
orgMap[key.(string)] = value.(error)
} else {
orgMap[key.(string)] = nil
}
return true
})
if len(errEmailAddress) > 0 {
return errors.New("以下收件人投递失败:" + array.Join(errEmailAddress, ",")), orgMap
}
return nil, orgMap
}
func allowUnsafeSMTP(domain mxDomain) bool {
return domain.localTest && domain.domain == "localhost" && domain.mxHost == "127.0.0.1"
}
func isCertificateVerificationError(err error) bool {
var certificateErr *tls.CertificateVerificationError
return errors.As(err, &certificateErr)
}
func isPermanentSMTPResponse(err error) bool {
var protocolErr *textproto.Error
return errors.As(err, &protocolErr) && protocolErr.Code >= 500 && protocolErr.Code <= 599
}
func deliveryFailureCause(mxLookupErr, fallbackErr error) error {
if fallbackErr == nil || isPermanentSMTPResponse(fallbackErr) {
return fallbackErr
}
var lookupDNSErr *net.DNSError
if !errors.As(mxLookupErr, &lookupDNSErr) || (!lookupDNSErr.IsTimeout && !lookupDNSErr.IsTemporary) {
return fallbackErr
}
var networkErr net.Error
if !errors.As(fallbackErr, &networkErr) {
return fallbackErr
}
return &temporaryMXFallbackError{
lookupErr: mxLookupErr,
fallbackErr: fallbackErr,
}
}
func buildAddress(u []*parsemail.User) []string {
var ret []string
for _, user := range u {
ret = append(ret, user.EmailAddress)
}
return ret
}
+160
View File
@@ -0,0 +1,160 @@
package send
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/textproto"
"testing"
"git.pcux.cn/xuans/Smail/dto/parsemail"
appcontext "git.pcux.cn/xuans/Smail/utils/context"
)
func TestIsPermanentSMTPResponse(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{name: "nil"},
{name: "network error", err: errors.New("connection refused")},
{name: "temporary SMTP response", err: &textproto.Error{Code: 451, Msg: "try later"}},
{name: "wrapped temporary SMTP response", err: fmt.Errorf("delivery: %w", &textproto.Error{Code: 421, Msg: "service unavailable"})},
{name: "permanent SMTP response", err: &textproto.Error{Code: 550, Msg: "mailbox unavailable"}, want: true},
{name: "wrapped permanent SMTP response", err: fmt.Errorf("delivery: %w", &textproto.Error{Code: 554, Msg: "transaction failed"}), want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isPermanentSMTPResponse(tt.err); got != tt.want {
t.Fatalf("isPermanentSMTPResponse() = %v, want %v", got, tt.want)
}
})
}
}
func TestDeliveryFailureCausePreservesTemporaryMXError(t *testing.T) {
tests := []struct {
name string
lookupErr *net.DNSError
}{
{
name: "SERVFAIL before fallback NXDOMAIN",
lookupErr: &net.DNSError{Err: "server misbehaving", Name: "example.com", IsTemporary: true},
},
{
name: "timeout before fallback NXDOMAIN",
lookupErr: &net.DNSError{Err: "i/o timeout", Name: "example.com", IsTimeout: true},
},
}
fallbackErr := &net.OpError{
Op: "dial",
Net: "tcp",
Err: &net.DNSError{Err: "no such host", Name: "smtp.example.com", IsNotFound: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := deliveryFailureCause(tt.lookupErr, fallbackErr)
var dnsErr *net.DNSError
if !errors.As(err, &dnsErr) {
t.Fatalf("deliveryFailureCause() = %T, want wrapped DNS error", err)
}
if dnsErr.IsNotFound {
t.Fatalf("deliveryFailureCause() selected fallback NXDOMAIN: %v", dnsErr)
}
if !dnsErr.IsTemporary && !dnsErr.IsTimeout {
t.Fatalf("deliveryFailureCause() selected non-temporary DNS error: %v", dnsErr)
}
})
}
}
func TestDeliveryFailureCauseKeepsExplicitSMTPRejection(t *testing.T) {
lookupErr := &net.DNSError{Err: "server misbehaving", Name: "example.com", IsTemporary: true}
rejection := &textproto.Error{Code: 550, Msg: "mailbox unavailable"}
if got := deliveryFailureCause(lookupErr, rejection); got != rejection {
t.Fatalf("deliveryFailureCause() = %v, want explicit SMTP rejection %v", got, rejection)
}
}
func TestCertificateVerificationErrorStopsFallback(t *testing.T) {
verificationErr := &tls.CertificateVerificationError{Err: errors.New("hostname mismatch")}
if !isCertificateVerificationError(fmt.Errorf("STARTTLS failed: %w", verificationErr)) {
t.Fatal("wrapped certificate verification error was not detected")
}
if isCertificateVerificationError(errors.New("connection refused")) {
t.Fatal("network error was treated as a certificate verification error")
}
}
func TestAllowUnsafeSMTPRequiresExplicitLoopbackTestTarget(t *testing.T) {
tests := []struct {
name string
domain mxDomain
want bool
}{
{name: "explicit local test", domain: mxDomain{domain: "localhost", mxHost: "127.0.0.1", localTest: true}, want: true},
{name: "external domain", domain: mxDomain{domain: "example.com", mxHost: "mx.example.com"}},
{name: "forged localhost name", domain: mxDomain{domain: "localhost", mxHost: "127.0.0.1"}},
{name: "test flag with external host", domain: mxDomain{domain: "localhost", mxHost: "192.0.2.1", localTest: true}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := allowUnsafeSMTP(tt.domain); got != tt.want {
t.Fatalf("allowUnsafeSMTP() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseRecipientAddressRejectsUnsafeInput(t *testing.T) {
tests := []struct {
name string
user *parsemail.User
}{
{name: "nil recipient"},
{name: "empty address", user: &parsemail.User{}},
{name: "missing domain", user: &parsemail.User{EmailAddress: "recipient"}},
{name: "surrounding whitespace", user: &parsemail.User{EmailAddress: " recipient@example.com"}},
{name: "header injection", user: &parsemail.User{EmailAddress: "recipient@example.com\r\nBcc: victim@example.com"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, _, err := parseRecipientAddress(tt.user); err == nil {
t.Fatal("unsafe recipient address was accepted")
}
})
}
}
func TestDoSendReportsInvalidRecipients(t *testing.T) {
err, failures := doSend(
&appcontext.Context{},
"example.com",
[]byte("message"),
[]*parsemail.User{{EmailAddress: "invalid"}, nil},
"sender@example.com",
)
if err == nil {
t.Fatal("invalid recipients were reported as delivered")
}
if len(failures) != 2 {
t.Fatalf("failure count = %d, want 2", len(failures))
}
}
func TestDoSendRejectsEmptyRecipientList(t *testing.T) {
err, failures := doSend(&appcontext.Context{}, "example.com", []byte("message"), nil, "sender@example.com")
if err == nil {
t.Fatal("empty recipient list was reported as delivered")
}
if len(failures) != 1 {
t.Fatalf("failure count = %d, want 1", len(failures))
}
}
+715
View File
@@ -0,0 +1,715 @@
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package smtp implements the Simple Mail Transfer Protocol as defined in RFC 5321.
// It also implements the following extensions:
//
// 8BITMIME RFC 1652
// AUTH RFC 2554
// STARTTLS RFC 3207
//
// Additional extensions may be handled by clients.
//
// The smtp package is frozen and is not accepting new features.
// Some external packages provide more functionality. See:
//
// https://godoc.org/?q=smtp
//
// 在go原始SMTP协议的基础上修复了TLS验证错误、支持了SMTPS协议、 支持自定义HELLO命令的域名信息
package smtp
import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/utils/credential"
log "github.com/sirupsen/logrus"
"io"
"net"
"net/netip"
"net/smtp"
"net/textproto"
"strings"
"time"
)
var NoSupportSTARTTLSError = errors.New("smtp: server doesn't support STARTTLS")
var EOFError = errors.New("EOF")
var ErrUnsafeRemoteAddress = errors.New("smtp: remote address is not public")
var ErrUnsafePlaintextTarget = errors.New("smtp: plaintext delivery is restricted to localhost")
const (
outboundSMTPDialTimeout = 2 * time.Second
outboundSMTPTLSHandshakeTimeout = 2 * time.Second
outboundSMTPTransactionTimeout = 2 * time.Minute
outboundSMTPQuitWriteTimeout = 250 * time.Millisecond
)
// A Client represents a client connection to an SMTP server.
type Client struct {
// Text is the textproto.Conn used by the Client. It is exported to allow for
// clients to add extensions.
Text *textproto.Conn
// keep a reference to the connection so it can be used to create a TLS
// connection later
conn net.Conn
// whether the Client is using TLS
tls bool
serverName string
// map of supported extensions
ext map[string]string
// supported auth mechanisms
auth []string
localName string // the name to use in HELO/EHLO
didHello bool // whether we've said HELO/EHLO
helloError error // the error from the hello
}
// Dial returns a new Client connected to an SMTP server at addr.
// The addr must include a port, as in "mail.example.com:smtp".
func Dial(addr, fromDomain string) (*Client, error) {
return dial(addr, fromDomain, true)
}
func dial(addr, fromDomain string, publicOnly bool) (*Client, error) {
conn, err := dialTCP(addr, publicOnly)
if err != nil {
return nil, err
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
_ = conn.Close()
return nil, err
}
return newClientWithTimeout(conn, host, fromDomain, outboundSMTPTransactionTimeout)
}
// with tls
func DialTls(addr, domain, fromDomain string) (*Client, error) {
return dialTLS(addr, domain, fromDomain, true)
}
func dialTLS(addr, domain, fromDomain string, publicOnly bool) (*Client, error) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
if domain == "" {
domain = strings.TrimSuffix(host, ".")
}
tlsconfig := &tls.Config{
MinVersion: tls.VersionTLS12,
ServerName: domain,
}
conn, err := dialTCP(addr, publicOnly)
if err != nil {
return nil, err
}
tlsConn, err := handshakeTLS(conn, tlsconfig, outboundSMTPTLSHandshakeTimeout)
if err != nil {
_ = conn.Close()
return nil, err
}
return newClientWithTimeout(tlsConn, host, fromDomain, outboundSMTPTransactionTimeout)
}
func handshakeTLS(conn net.Conn, config *tls.Config, timeout time.Duration) (*tls.Conn, error) {
tlsConn := tls.Client(conn, config)
if err := tlsConn.SetDeadline(time.Now().Add(timeout)); err != nil {
return nil, err
}
if err := tlsConn.Handshake(); err != nil {
return nil, err
}
return tlsConn, nil
}
func newClientWithTimeout(conn net.Conn, host, fromDomain string, timeout time.Duration) (*Client, error) {
if err := conn.SetDeadline(time.Now().Add(timeout)); err != nil {
_ = conn.Close()
return nil, err
}
return NewClient(conn, host, fromDomain)
}
func dialTCP(addr string, publicOnly bool) (net.Conn, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil || host == "" || port == "" {
return nil, errors.New("smtp: invalid remote address")
}
ctx, cancel := context.WithTimeout(context.Background(), outboundSMTPDialTimeout)
defer cancel()
addresses, err := resolveRemoteAddresses(ctx, host)
if err != nil {
return nil, err
}
for _, address := range addresses {
if publicOnly && !credential.IsPublicRemoteIP(address) {
return nil, fmt.Errorf("%w: %s", ErrUnsafeRemoteAddress, host)
}
}
dialer := &net.Dialer{Timeout: outboundSMTPDialTimeout, KeepAlive: 30 * time.Second}
var lastErr error
for _, address := range addresses {
conn, err := dialer.DialContext(ctx, "tcp", net.JoinHostPort(address.String(), port))
if err == nil {
return conn, nil
}
lastErr = err
}
if lastErr == nil {
return nil, errors.New("smtp: remote address is unavailable")
}
return nil, fmt.Errorf("smtp: connect to remote address: %w", lastErr)
}
func resolveRemoteAddresses(ctx context.Context, host string) ([]netip.Addr, error) {
if address, err := netip.ParseAddr(host); err == nil {
return []netip.Addr{address.Unmap()}, nil
}
resolved, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, fmt.Errorf("smtp: resolve remote address: %w", err)
}
addresses := make([]netip.Addr, 0, len(resolved))
for _, item := range resolved {
address, ok := netip.AddrFromSlice(item.IP)
if ok {
addresses = append(addresses, address.Unmap())
}
}
if len(addresses) == 0 {
return nil, errors.New("smtp: remote address is unavailable")
}
return addresses, nil
}
func validateUnsafePlaintextTarget(domain, addr string) error {
if strings.TrimSuffix(strings.ToLower(domain), ".") != "localhost" {
return ErrUnsafePlaintextTarget
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return ErrUnsafePlaintextTarget
}
address, err := netip.ParseAddr(host)
if err != nil || !address.Unmap().IsLoopback() {
return ErrUnsafePlaintextTarget
}
return nil
}
// NewClient returns a new Client using an existing connection and host as a
// server name to be used when authenticating.
func NewClient(conn net.Conn, host, fromDomain string) (*Client, error) {
text := textproto.NewConn(conn)
_, _, err := text.ReadResponse(220)
if err != nil {
closeErr := text.Close()
return nil, errors.Join(err, closeErr)
}
localName := "domain.com"
if fromDomain != "" {
localName = fromDomain
} else if domains := config.CurrentMailDomains(); domains.Primary != "" {
localName = domains.Primary
}
c := &Client{Text: text, conn: conn, serverName: host, localName: localName}
_, c.tls = conn.(*tls.Conn)
return c, nil
}
// Close closes the connection.
func (c *Client) Close() error {
return c.Text.Close()
}
// hello runs a hello exchange if needed.
func (c *Client) hello() error {
if !c.didHello {
c.didHello = true
err := c.ehlo()
if err != nil {
c.helloError = c.helo()
}
}
return c.helloError
}
// Hello sends a HELO or EHLO to the server as the given host name.
// Calling this method is only necessary if the client needs control
// over the host name used. The client will introduce itself as "localhost"
// automatically otherwise. If Hello is called, it must be called before
// any of the other methods.
func (c *Client) Hello(localName string) error {
if err := validateLine(localName); err != nil {
return err
}
if c.didHello {
return errors.New("smtp: Hello called after other methods")
}
c.localName = localName
return c.hello()
}
// cmd is a convenience function that sends a command and returns the response
func (c *Client) cmd(expectCode int, format string, args ...any) (int, string, error) {
id, err := c.Text.Cmd(format, args...)
if err != nil {
return 0, "", err
}
c.Text.StartResponse(id)
defer c.Text.EndResponse(id)
code, msg, err := c.Text.ReadResponse(expectCode)
return code, msg, err
}
// helo sends the HELO greeting to the server. It should be used only when the
// server does not support ehlo.
func (c *Client) helo() error {
c.ext = nil
_, _, err := c.cmd(250, "HELO %s", c.localName)
return err
}
// ehlo sends the EHLO (extended hello) greeting to the server. It
// should be the preferred greeting for servers that support it.
func (c *Client) ehlo() error {
_, msg, err := c.cmd(250, "EHLO %s", c.localName)
if err != nil {
return err
}
ext := make(map[string]string)
extList := strings.Split(msg, "\n")
if len(extList) > 1 {
extList = extList[1:]
for _, line := range extList {
k, v, _ := strings.Cut(line, " ")
ext[k] = v
}
}
if mechs, ok := ext["AUTH"]; ok {
c.auth = strings.Split(mechs, " ")
}
c.ext = ext
return err
}
// StartTLS sends the STARTTLS command and encrypts all further communication.
// Only servers that advertise the STARTTLS extension support this function.
func (c *Client) StartTLS(config *tls.Config) error {
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(220, "STARTTLS")
if err != nil {
return err
}
if config == nil {
config = &tls.Config{MinVersion: tls.VersionTLS12}
}
if config.ServerName == "" {
// Make a copy to avoid polluting argument
config = config.Clone()
config.ServerName = c.serverName
}
if config.MinVersion < tls.VersionTLS12 {
config = config.Clone()
config.MinVersion = tls.VersionTLS12
}
tlsConn, err := handshakeTLS(c.conn, config, outboundSMTPTLSHandshakeTimeout)
if err != nil {
return err
}
if err = tlsConn.SetDeadline(time.Now().Add(outboundSMTPTransactionTimeout)); err != nil {
return err
}
c.conn = tlsConn
c.Text = textproto.NewConn(c.conn)
c.tls = true
return c.ehlo()
}
// TLSConnectionState returns the client's TLS connection state.
// The return values are their zero values if StartTLS did
// not succeed.
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool) {
tc, ok := c.conn.(*tls.Conn)
if !ok {
return
}
return tc.ConnectionState(), true
}
// Verify checks the validity of an email address on the server.
// If Verify returns nil, the address is valid. A non-nil return
// does not necessarily indicate an invalid address. Many servers
// will not verify addresses for security reasons.
func (c *Client) Verify(addr string) error {
if err := validateLine(addr); err != nil {
return err
}
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(250, "VRFY %s", addr)
return err
}
// Auth authenticates a client using the provided authentication mechanism.
// A failed authentication closes the connection.
// Only servers that advertise the AUTH extension support this function.
func (c *Client) Auth(a smtp.Auth) error {
if err := c.hello(); err != nil {
return err
}
encoding := base64.StdEncoding
mech, resp, err := a.Start(&smtp.ServerInfo{Name: c.serverName, TLS: c.tls, Auth: c.auth})
if err != nil {
quitErr := c.Quit()
return errors.Join(err, quitErr)
}
resp64 := make([]byte, encoding.EncodedLen(len(resp)))
encoding.Encode(resp64, resp)
code, msg64, err := c.cmd(0, "AUTH %s %s", mech, resp64)
for err == nil {
var msg []byte
switch code {
case 334:
msg, err = encoding.DecodeString(msg64)
case 235:
// the last message isn't base64 because it isn't a challenge
msg = []byte(msg64)
default:
err = &textproto.Error{Code: code, Msg: msg64}
}
if err == nil {
resp, err = a.Next(msg, code == 334)
}
if err != nil {
// abort the AUTH
_, _, abortErr := c.cmd(501, "*")
err = errors.Join(err, abortErr, c.Quit())
break
}
if resp == nil {
break
}
resp64 = make([]byte, encoding.EncodedLen(len(resp)))
encoding.Encode(resp64, resp)
code, msg64, err = c.cmd(0, "%s", string(resp64))
}
return err
}
// Mail issues a MAIL command to the server using the provided email address.
// If the server supports the 8BITMIME extension, Mail adds the BODY=8BITMIME
// parameter. If the server supports the SMTPUTF8 extension, Mail adds the
// SMTPUTF8 parameter.
// This initiates a mail transaction and is followed by one or more Rcpt calls.
func (c *Client) Mail(from string) error {
if err := validateLine(from); err != nil {
return err
}
if err := c.hello(); err != nil {
return err
}
cmdStr := "MAIL FROM:<%s>"
if c.ext != nil {
if _, ok := c.ext["8BITMIME"]; ok {
cmdStr += " BODY=8BITMIME"
}
if _, ok := c.ext["SMTPUTF8"]; ok {
cmdStr += " SMTPUTF8"
}
}
_, _, err := c.cmd(250, cmdStr, from)
return err
}
// Rcpt issues a RCPT command to the server using the provided email address.
// A call to Rcpt must be preceded by a call to Mail and may be followed by
// a Data call or another Rcpt call.
func (c *Client) Rcpt(to string) error {
if err := validateLine(to); err != nil {
return err
}
_, _, err := c.cmd(25, "RCPT TO:<%s>", to)
return err
}
type dataCloser struct {
c *Client
io.WriteCloser
}
func (d *dataCloser) Close() error {
if err := d.WriteCloser.Close(); err != nil {
return err
}
_, _, err := d.c.Text.ReadResponse(250)
return err
}
// Data issues a DATA command to the server and returns a writer that
// can be used to write the mail headers and body. The caller should
// close the writer before calling any more methods on c. A call to
// Data must be preceded by one or more calls to Rcpt.
func (c *Client) Data() (io.WriteCloser, error) {
_, _, err := c.cmd(354, "DATA")
if err != nil {
return nil, err
}
return &dataCloser{c, c.Text.DotWriter()}, nil
}
func finishDelivery(c *Client, w io.WriteCloser, msg []byte) error {
if _, err := w.Write(msg); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
// The final 250 response to DATA transfers responsibility for the message.
// Send QUIT with a bounded write, but do not wait for 221: a failed or
// missing reply after acceptance must not block or trigger redelivery.
if err := c.conn.SetWriteDeadline(time.Now().Add(outboundSMTPQuitWriteTimeout)); err != nil {
log.Debugf("Could not bound SMTP QUIT write after message acceptance: %v", err)
}
if err := c.Text.PrintfLine("QUIT"); err != nil {
log.Debugf("SMTP QUIT failed after message acceptance: %v", err)
}
return nil
}
func SendMailWithTls(domain string, addr string, a smtp.Auth, from string, fromDomain string, to []string, msg []byte) error {
if err := validateLine(from); err != nil {
return err
}
for _, recp := range to {
if err := validateLine(recp); err != nil {
return err
}
}
c, err := DialTls(addr, domain, fromDomain)
if err != nil {
return err
}
defer c.Close()
if err = c.hello(); err != nil {
return err
}
if a != nil && c.ext != nil {
if _, ok := c.ext["AUTH"]; !ok {
return errors.New("smtp: server doesn't support AUTH")
}
if err = c.Auth(a); err != nil {
return err
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
return finishDelivery(c, w, msg)
}
// SendMail connects to the server at addr, switches to TLS if
// possible, authenticates with the optional mechanism a if possible,
// and then sends an email from address from, to addresses to, with
// message msg.
// The addr must include a port, as in "mail.example.com:smtp".
//
// The addresses in the to parameter are the SMTP RCPT addresses.
//
// The msg parameter should be an RFC 822-style email with headers
// first, a blank line, and then the message body. The lines of msg
// should be CRLF terminated. The msg headers should usually include
// fields such as "From", "To", "Subject", and "Cc". Sending "Bcc"
// messages is accomplished by including an email address in the to
// parameter but not including it in the msg headers.
//
// The SendMail function and the net/smtp package are low-level
// mechanisms and provide no support for DKIM signing, MIME
// attachments (see the mime/multipart package), or other mail
// functionality. Higher-level packages exist outside of the standard
// library.
// 修复TSL验证问题
func SendMail(domain string, addr string, a smtp.Auth, from string, fromDomain string, to []string, msg []byte) error {
log.Debugf("SMTP delivery started: recipient_count=%d", len(to))
if err := validateLine(from); err != nil {
return err
}
for _, recp := range to {
if err := validateLine(recp); err != nil {
return err
}
}
c, err := Dial(addr, fromDomain)
if err != nil {
return err
}
defer c.Close()
if err = c.hello(); err != nil {
return err
}
if ok, _ := c.Extension("STARTTLS"); !ok {
return NoSupportSTARTTLSError
}
var config *tls.Config
if domain != "" {
config = &tls.Config{
ServerName: domain,
MinVersion: tls.VersionTLS12,
}
}
if err = c.StartTLS(config); err != nil {
return err
}
if a != nil && c.ext != nil {
if _, ok := c.ext["AUTH"]; !ok {
return errors.New("smtp: server doesn't support AUTH")
}
if err = c.Auth(a); err != nil {
return err
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
return finishDelivery(c, w, msg)
}
// SendMailUnsafe 无TLS加密的邮件发送方式
func SendMailUnsafe(domain string, addr string, a smtp.Auth, from string, fromDomain string, to []string, msg []byte) error {
if err := validateUnsafePlaintextTarget(domain, addr); err != nil {
return err
}
if err := validateLine(from); err != nil {
return err
}
for _, recp := range to {
if err := validateLine(recp); err != nil {
return err
}
}
c, err := dial(addr, fromDomain, false)
if err != nil {
return err
}
defer c.Close()
if err = c.hello(); err != nil {
return err
}
if a != nil && c.ext != nil {
if _, ok := c.ext["AUTH"]; !ok {
return errors.New("smtp: server doesn't support AUTH")
}
if err = c.Auth(a); err != nil {
return err
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
return finishDelivery(c, w, msg)
}
// Extension reports whether an extension is support by the server.
// The extension name is case-insensitive. If the extension is supported,
// Extension also returns a string that contains any parameters the
// server specifies for the extension.
func (c *Client) Extension(ext string) (bool, string) {
if err := c.hello(); err != nil {
return false, ""
}
if c.ext == nil {
return false, ""
}
ext = strings.ToUpper(ext)
param, ok := c.ext[ext]
return ok, param
}
// Reset sends the RSET command to the server, aborting the current mail
// transaction.
func (c *Client) Reset() error {
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(250, "RSET")
return err
}
// Noop sends the NOOP command to the server. It does nothing but check
// that the connection to the server is okay.
func (c *Client) Noop() error {
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(250, "NOOP")
return err
}
// Quit sends the QUIT command and closes the connection to the server.
func (c *Client) Quit() error {
if err := c.hello(); err != nil {
return err
}
_, _, err := c.cmd(221, "QUIT")
if err != nil {
return err
}
return c.Text.Close()
}
// validateLine checks to see if a line has CR or LF as per RFC 5321.
func validateLine(line string) error {
if strings.ContainsAny(line, "\n\r") {
return errors.New("smtp: A line must not contain CR or LF")
}
return nil
}
+309
View File
@@ -0,0 +1,309 @@
package smtp
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"io"
"math/big"
"net"
"net/textproto"
"strings"
"testing"
"time"
)
func TestDialRejectsNonPublicRemoteAddress(t *testing.T) {
tests := []string{
"127.0.0.1",
"::1",
"10.0.0.1",
"172.16.0.1",
"192.168.0.1",
"169.254.0.1",
"100.64.0.1",
"192.0.2.1",
"198.51.100.1",
"203.0.113.1",
"2001:db8::1",
"0.0.0.0",
}
for _, host := range tests {
t.Run(host, func(t *testing.T) {
_, err := Dial(net.JoinHostPort(host, "25"), "example.com")
if !errors.Is(err, ErrUnsafeRemoteAddress) {
t.Fatalf("Dial() error = %v, want ErrUnsafeRemoteAddress", err)
}
})
}
}
func TestSendMailUnsafeRejectsNonLoopbackTargets(t *testing.T) {
tests := []struct {
name string
domain string
addr string
}{
{name: "external domain", domain: "example.com", addr: "127.0.0.1:25"},
{name: "private address", domain: "localhost", addr: "10.0.0.1:25"},
{name: "public address", domain: "localhost", addr: "8.8.8.8:25"},
{name: "hostname target", domain: "localhost", addr: "localhost:25"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := SendMailUnsafe(tt.domain, tt.addr, nil, "sender@example.com", "example.com", []string{"recipient@example.net"}, []byte("test"))
if !errors.Is(err, ErrUnsafePlaintextTarget) {
t.Fatalf("SendMailUnsafe() error = %v, want ErrUnsafePlaintextTarget", err)
}
})
}
}
func TestSendMailUnsafeUsesFinalDataResponseAsDeliveryResult(t *testing.T) {
tests := []struct {
name string
dataResponse string
holdWithoutQuitReply bool
wantCode int
maxDuration time.Duration
}{
{
name: "accepted message returns while server withholds QUIT reply",
dataResponse: "250 2.0.0 queued",
holdWithoutQuitReply: true,
maxDuration: time.Second,
},
{
name: "DATA rejection remains a delivery failure",
dataResponse: "550 5.0.0 rejected",
wantCode: 550,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
addr, serverDone := startSMTPTransactionServer(t, tt.dataResponse, tt.holdWithoutQuitReply)
start := time.Now()
err := SendMailUnsafe(
"localhost",
addr,
nil,
"sender@example.com",
"example.com",
[]string{"recipient@example.net"},
[]byte("From: sender@example.com\r\nTo: recipient@example.net\r\nSubject: test\r\n\r\nbody\r\n"),
)
duration := time.Since(start)
if tt.maxDuration > 0 && duration > tt.maxDuration {
t.Fatalf("SendMailUnsafe() took %v, want at most %v", duration, tt.maxDuration)
}
if tt.wantCode == 0 {
if err != nil {
t.Fatalf("SendMailUnsafe() = %v, want nil", err)
}
} else {
var protocolErr *textproto.Error
if !errors.As(err, &protocolErr) {
t.Fatalf("SendMailUnsafe() error type = %T, want *textproto.Error", err)
}
if protocolErr.Code != tt.wantCode {
t.Fatalf("SMTP code = %d, want %d", protocolErr.Code, tt.wantCode)
}
}
select {
case serverErr := <-serverDone:
if serverErr != nil {
t.Fatal(serverErr)
}
case <-time.After(2 * time.Second):
t.Fatal("fake SMTP server did not finish")
}
})
}
}
func startSMTPTransactionServer(t *testing.T, dataResponse string, holdWithoutQuitReply bool) (string, <-chan error) {
t.Helper()
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("net.Listen() failed: %v", err)
}
done := make(chan error, 1)
go func() {
defer listener.Close()
conn, err := listener.Accept()
if err != nil {
done <- err
return
}
defer conn.Close()
_ = conn.SetDeadline(time.Now().Add(2 * time.Second))
text := textproto.NewConn(conn)
defer text.Close()
if err := text.PrintfLine("220 test ESMTP ready"); err != nil {
done <- err
return
}
if err := expectSMTPCommand(text, "EHLO "); err != nil {
done <- err
return
}
if err := text.PrintfLine("250 test"); err != nil {
done <- err
return
}
if err := expectSMTPCommand(text, "MAIL FROM:"); err != nil {
done <- err
return
}
if err := text.PrintfLine("250 2.1.0 sender accepted"); err != nil {
done <- err
return
}
if err := expectSMTPCommand(text, "RCPT TO:"); err != nil {
done <- err
return
}
if err := text.PrintfLine("250 2.1.5 recipient accepted"); err != nil {
done <- err
return
}
if err := expectSMTPCommand(text, "DATA"); err != nil {
done <- err
return
}
if err := text.PrintfLine("354 end data with <CR><LF>.<CR><LF>"); err != nil {
done <- err
return
}
body, err := io.ReadAll(text.DotReader())
if err != nil {
done <- err
return
}
if !strings.Contains(string(body), "Subject: test") {
done <- fmt.Errorf("message body was not received: %q", body)
return
}
if err := text.PrintfLine("%s", dataResponse); err != nil {
done <- err
return
}
if holdWithoutQuitReply {
if err := expectSMTPCommand(text, "QUIT"); err != nil {
done <- err
return
}
// Keep the server side open without sending 221. The client must close
// the accepted transaction without waiting for a reply.
buf := make([]byte, 1)
if _, err := conn.Read(buf); err != io.EOF {
done <- fmt.Errorf("waiting for client close after QUIT: %w", err)
return
}
}
done <- nil
}()
return listener.Addr().String(), done
}
func expectSMTPCommand(conn *textproto.Conn, prefix string) error {
line, err := conn.ReadLine()
if err != nil {
return err
}
if !strings.HasPrefix(line, prefix) {
return fmt.Errorf("SMTP command = %q, want prefix %q", line, prefix)
}
return nil
}
func TestSMTPGreetingTimeout(t *testing.T) {
client, server := net.Pipe()
defer server.Close()
started := time.Now()
if _, err := newClientWithTimeout(client, "localhost", "example.com", 25*time.Millisecond); err == nil {
t.Fatal("silent SMTP server did not time out")
}
if elapsed := time.Since(started); elapsed > 500*time.Millisecond {
t.Fatalf("SMTP greeting timeout took too long: %v", elapsed)
}
}
func TestTLSHandshakeTimeout(t *testing.T) {
client, server := net.Pipe()
defer client.Close()
defer server.Close()
started := time.Now()
if _, err := handshakeTLS(client, &tls.Config{MinVersion: tls.VersionTLS12}, 25*time.Millisecond); err == nil {
t.Fatal("silent TLS peer did not time out")
}
if elapsed := time.Since(started); elapsed > 500*time.Millisecond {
t.Fatalf("TLS handshake timeout took too long: %v", elapsed)
}
}
func TestDialTlsRejectsUntrustedCertificate(t *testing.T) {
certificate := newUntrustedTestCertificate(t)
listener, err := tls.Listen("tcp", "127.0.0.1:0", &tls.Config{Certificates: []tls.Certificate{certificate}, MinVersion: tls.VersionTLS12})
if err != nil {
t.Fatal(err)
}
defer listener.Close()
accepted := make(chan struct{})
go func() {
conn, acceptErr := listener.Accept()
if acceptErr == nil {
close(accepted)
_ = conn.Close()
}
}()
if _, err = dialTLS(listener.Addr().String(), "localhost", "example.com", false); err == nil {
t.Fatal("DialTls accepted an untrusted certificate")
}
select {
case <-accepted:
case <-time.After(time.Second):
t.Fatal("TLS test server did not accept a connection")
}
}
func newUntrustedTestCertificate(t *testing.T) tls.Certificate {
t.Helper()
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "localhost"},
NotBefore: time.Now().Add(-time.Minute),
NotAfter: time.Now().Add(time.Hour),
DNSNames: []string{"localhost"},
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
der, err := x509.CreateCertificate(rand.Reader, template, template, &privateKey.PublicKey, privateKey)
if err != nil {
t.Fatal(err)
}
return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: privateKey}
}
+23
View File
@@ -0,0 +1,23 @@
The MIT License (MIT)
Copyright (c) 2013 The Go-IMAP Authors
Copyright (c) 2016 Proton Technologies AG
Copyright (c) 2023 Simon Ser
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+1
View File
@@ -0,0 +1 @@
COPY from https://github.com/emersion/go-imap/tree/v2/internal/utf7
+118
View File
@@ -0,0 +1,118 @@
package utf7
import (
"errors"
"strings"
"unicode/utf16"
"unicode/utf8"
)
// ErrInvalidUTF7 means that a decoder encountered invalid UTF-7.
var ErrInvalidUTF7 = errors.New("utf7: invalid UTF-7")
// Decode decodes a string encoded with modified UTF-7.
//
// Note, raw UTF-8 is accepted.
func Decode(src string) (string, error) {
if !utf8.ValidString(src) {
return "", errors.New("invalid UTF-8")
}
var sb strings.Builder
sb.Grow(len(src))
ascii := true
for i := 0; i < len(src); i++ {
ch := src[i]
if ch < min || (ch > max && ch < utf8.RuneSelf) {
// Illegal code point in ASCII mode. Note, UTF-8 codepoints are
// always allowed.
return "", ErrInvalidUTF7
}
if ch != '&' {
sb.WriteByte(ch)
ascii = true
continue
}
// Find the end of the Base64 or "&-" segment
start := i + 1
for i++; i < len(src) && src[i] != '-'; i++ {
if src[i] == '\r' || src[i] == '\n' { // base64 package ignores CR and LF
return "", ErrInvalidUTF7
}
}
if i == len(src) { // Implicit shift ("&...")
return "", ErrInvalidUTF7
}
if i == start { // Escape sequence "&-"
sb.WriteByte('&')
ascii = true
} else { // Control or non-ASCII code points in base64
if !ascii { // Null shift ("&...-&...-")
return "", ErrInvalidUTF7
}
b := decode([]byte(src[start:i]))
if len(b) == 0 { // Bad encoding
return "", ErrInvalidUTF7
}
sb.Write(b)
ascii = false
}
}
return sb.String(), nil
}
// Extracts UTF-16-BE bytes from base64 data and converts them to UTF-8.
// A nil slice is returned if the encoding is invalid.
func decode(b64 []byte) []byte {
var b []byte
// Allocate a single block of memory large enough to store the Base64 data
// (if padding is required), UTF-16-BE bytes, and decoded UTF-8 bytes.
// Since a 2-byte UTF-16 sequence may expand into a 3-byte UTF-8 sequence,
// double the space allocation for UTF-8.
if n := len(b64); b64[n-1] == '=' {
return nil
} else if n&3 == 0 {
b = make([]byte, b64Enc.DecodedLen(n)*3)
} else {
n += 4 - n&3
b = make([]byte, n+b64Enc.DecodedLen(n)*3)
copy(b[copy(b, b64):n], []byte("=="))
b64, b = b[:n], b[n:]
}
// Decode Base64 into the first 1/3rd of b
n, err := b64Enc.Decode(b, b64)
if err != nil || n&1 == 1 {
return nil
}
// Decode UTF-16-BE into the remaining 2/3rds of b
b, s := b[:n], b[n:]
j := 0
for i := 0; i < n; i += 2 {
r := rune(b[i])<<8 | rune(b[i+1])
if utf16.IsSurrogate(r) {
if i += 2; i == n {
return nil
}
r2 := rune(b[i])<<8 | rune(b[i+1])
if r = utf16.DecodeRune(r, r2); r == utf8.RuneError {
return nil
}
} else if min <= r && r <= max {
return nil
}
j += utf8.EncodeRune(s[j:], r)
}
return s[:j]
}
+118
View File
@@ -0,0 +1,118 @@
package utf7_test
import (
"git.pcux.cn/xuans/Smail/utils/utf7"
"strings"
"testing"
)
var decode = []struct {
in string
out string
ok bool
}{
// Basics (the inverse test on encode checks other valid inputs)
{"", "", true},
{"abc", "abc", true},
{"&-abc", "&abc", true},
{"abc&-", "abc&", true},
{"a&-b&-c", "a&b&c", true},
{"&ABk-", "\x19", true},
{"&AB8-", "\x1F", true},
{"ABk-", "ABk-", true},
{"&-,&-&AP8-&-", "&,&\u00FF&", true},
{"&-&-,&AP8-&-", "&&,\u00FF&", true},
{"abc &- &AP8A,wD,- &- xyz", "abc & \u00FF\u00FF\u00FF & xyz", true},
// Illegal code point in ASCII
{"\x00", "", false},
{"\x1F", "", false},
{"abc\n", "", false},
{"abc\x7Fxyz", "", false},
// Invalid UTF-8
{"\xc3\x28", "", false},
{"\xe2\x82\x28", "", false},
// Invalid Base64 alphabet
{"&/+8-", "", false},
{"&*-", "", false},
{"&ZeVnLIqe -", "", false},
// CR and LF in Base64
{"&ZeVnLIqe\r\n-", "", false},
{"&ZeVnLIqe\r\n\r\n-", "", false},
{"&ZeVn\r\n\r\nLIqe-", "", false},
// Padding not stripped
{"&AAAAHw=-", "", false},
{"&AAAAHw==-", "", false},
{"&AAAAHwB,AIA=-", "", false},
{"&AAAAHwB,AIA==-", "", false},
// One byte short
{"&2A-", "", false},
{"&2ADc-", "", false},
{"&AAAAHwB,A-", "", false},
{"&AAAAHwB,A=-", "", false},
{"&AAAAHwB,A==-", "", false},
{"&AAAAHwB,A===-", "", false},
{"&AAAAHwB,AI-", "", false},
{"&AAAAHwB,AI=-", "", false},
{"&AAAAHwB,AI==-", "", false},
// Implicit shift
{"&", "", false},
{"&Jjo", "", false},
{"Jjo&", "", false},
{"&Jjo&", "", false},
{"&Jjo!", "", false},
{"&Jjo+", "", false},
{"abc&Jjo", "", false},
// Null shift
{"&AGE-&Jjo-", "", false},
{"&U,BTFw-&ZeVnLIqe-", "", false},
// Long input with Base64 at the end
{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &2D3eCg- &2D3eCw- &2D3eDg-",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \U0001f60a \U0001f60b \U0001f60e", true},
// Long input in Base64 between short ASCII
{"00000000000000000000 &MEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEI- 00000000000000000000",
"00000000000000000000 " + strings.Repeat("\U00003042", 37) + " 00000000000000000000", true},
// ASCII in Base64
{"&AGE-", "", false}, // "a"
{"&ACY-", "", false}, // "&"
{"&AGgAZQBsAGwAbw-", "", false}, // "hello"
{"&JjoAIQ-", "", false}, // "\u263a!"
// Bad surrogate
{"&2AA-", "", false}, // U+D800
{"&2AD-", "", false}, // U+D800
{"&3AA-", "", false}, // U+DC00
{"&2AAAQQ-", "", false}, // U+D800 'A'
{"&2AD,,w-", "", false}, // U+D800 U+FFFF
{"&3ADYAA-", "", false}, // U+DC00 U+D800
// Chinese
{"&V4NXPpCuTvY-", "垃圾邮件", true},
{"&UXZO1mWHTvZZOQ-", "其他文件夹", true},
}
func TestDecoder(t *testing.T) {
for _, test := range decode {
out, err := utf7.Decode(test.in)
if out != test.out {
t.Errorf("UTF7Decode(%+q) expected %+q; got %+q", test.in, test.out, out)
}
if test.ok {
if err != nil {
t.Errorf("UTF7Decode(%+q) unexpected error; %v", test.in, err)
}
} else if err == nil {
t.Errorf("UTF7Decode(%+q) expected error", test.in)
}
}
}
+88
View File
@@ -0,0 +1,88 @@
package utf7
import (
"strings"
"unicode/utf16"
"unicode/utf8"
)
// Encode encodes a string with modified UTF-7.
func Encode(src string) string {
var sb strings.Builder
sb.Grow(len(src))
for i := 0; i < len(src); {
ch := src[i]
if min <= ch && ch <= max {
sb.WriteByte(ch)
if ch == '&' {
sb.WriteByte('-')
}
i++
} else {
start := i
// Find the next printable ASCII code point
i++
for i < len(src) && (src[i] < min || src[i] > max) {
i++
}
sb.Write(encode([]byte(src[start:i])))
}
}
return sb.String()
}
// Converts string s from UTF-8 to UTF-16-BE, encodes the result as base64,
// removes the padding, and adds UTF-7 shifts.
func encode(s []byte) []byte {
// len(s) is sufficient for UTF-8 to UTF-16 conversion if there are no
// control code points (see table below).
b := make([]byte, 0, len(s)+4)
for len(s) > 0 {
r, size := utf8.DecodeRune(s)
if r > utf8.MaxRune {
r, size = utf8.RuneError, 1 // Bug fix (issue 3785)
}
s = s[size:]
if r1, r2 := utf16.EncodeRune(r); r1 != utf8.RuneError {
b = append(b, byte(r1>>8), byte(r1))
r = r2
}
b = append(b, byte(r>>8), byte(r))
}
// Encode as base64
n := b64Enc.EncodedLen(len(b)) + 2
b64 := make([]byte, n)
b64Enc.Encode(b64[1:], b)
// Strip padding
n -= 2 - (len(b)+2)%3
b64 = b64[:n]
// Add UTF-7 shifts
b64[0] = '&'
b64[n-1] = '-'
return b64
}
// Escape passes through raw UTF-8 as-is and escapes the special UTF-7 marker
// (the ampersand character).
func Escape(src string) string {
var sb strings.Builder
sb.Grow(len(src))
for _, ch := range src {
sb.WriteRune(ch)
if ch == '&' {
sb.WriteByte('-')
}
}
return sb.String()
}
+123
View File
@@ -0,0 +1,123 @@
package utf7_test
import (
"git.pcux.cn/xuans/Smail/utils/utf7"
"testing"
)
var encode = []struct {
in string
out string
ok bool
}{
// Printable ASCII
{"", "", true},
{"a", "a", true},
{"ab", "ab", true},
{"-", "-", true},
{"&", "&-", true},
{"&&", "&-&-", true},
{"&&&-&", "&-&-&--&-", true},
{"-&*&-", "-&-*&--", true},
{"a&b", "a&-b", true},
{"a&", "a&-", true},
{"&b", "&-b", true},
{"-a&", "-a&-", true},
{"&b-", "&-b-", true},
// Unicode range
{"\u0000", "&AAA-", true},
{"\n", "&AAo-", true},
{"\r", "&AA0-", true},
{"\u001F", "&AB8-", true},
{"\u0020", " ", true},
{"\u0025", "%", true},
{"\u0026", "&-", true},
{"\u0027", "'", true},
{"\u007E", "~", true},
{"\u007F", "&AH8-", true},
{"\u0080", "&AIA-", true},
{"\u00FF", "&AP8-", true},
{"\u07FF", "&B,8-", true},
{"\u0800", "&CAA-", true},
{"\uFFEF", "&,+8-", true},
{"\uFFFF", "&,,8-", true},
{"\U00010000", "&2ADcAA-", true},
{"\U0010FFFF", "&2,,f,w-", true},
// Padding
{"\x00\x1F", "&AAAAHw-", true}, // 2
{"\x00\x1F\x7F", "&AAAAHwB,-", true}, // 0
{"\x00\x1F\x7F\u0080", "&AAAAHwB,AIA-", true}, // 1
{"\x00\x1F\x7F\u0080\u00FF", "&AAAAHwB,AIAA,w-", true}, // 2
// Mix
{"a\x00", "a&AAA-", true},
{"\x00a", "&AAA-a", true},
{"&\x00", "&-&AAA-", true},
{"\x00&", "&AAA-&-", true},
{"a\x00&", "a&AAA-&-", true},
{"a&\x00", "a&-&AAA-", true},
{"&a\x00", "&-a&AAA-", true},
{"&\x00a", "&-&AAA-a", true},
{"\x00&a", "&AAA-&-a", true},
{"\x00a&", "&AAA-a&-", true},
{"ab&\uFFFF", "ab&-&,,8-", true},
{"a&b\uFFFF", "a&-b&,,8-", true},
{"&ab\uFFFF", "&-ab&,,8-", true},
{"ab\uFFFF&", "ab&,,8-&-", true},
{"a\uFFFFb&", "a&,,8-b&-", true},
{"\uFFFFab&", "&,,8-ab&-", true},
{"\x20\x25&\x27\x7E", " %&-'~", true},
{"\x1F\x20&\x7E\x7F", "&AB8- &-~&AH8-", true},
{"&\x00\x19\x7F\u0080", "&-&AAAAGQB,AIA-", true},
{"\x00&\x19\x7F\u0080", "&AAA-&-&ABkAfwCA-", true},
{"\x00\x19&\x7F\u0080", "&AAAAGQ-&-&AH8AgA-", true},
{"\x00\x19\x7F&\u0080", "&AAAAGQB,-&-&AIA-", true},
{"\x00\x19\x7F\u0080&", "&AAAAGQB,AIA-&-", true},
{"&\x00\x1F\x7F\u0080", "&-&AAAAHwB,AIA-", true},
{"\x00&\x1F\x7F\u0080", "&AAA-&-&AB8AfwCA-", true},
{"\x00\x1F&\x7F\u0080", "&AAAAHw-&-&AH8AgA-", true},
{"\x00\x1F\x7F&\u0080", "&AAAAHwB,-&-&AIA-", true},
{"\x00\x1F\x7F\u0080&", "&AAAAHwB,AIA-&-", true},
// Russian
{"\u041C\u0430\u043A\u0441\u0438\u043C \u0425\u0438\u0442\u0440\u043E\u0432",
"&BBwEMAQ6BEEEOAQ8- &BCUEOARCBEAEPgQy-", true},
// RFC 3501
{"~peter/mail/\u53F0\u5317/\u65E5\u672C\u8A9E", "~peter/mail/&U,BTFw-/&ZeVnLIqe-", true},
{"~peter/mail/\u53F0\u5317/\u65E5\u672C\u8A9E", "~peter/mail/&U,BTFw-/&ZeVnLIqe-", true},
{"\u263A!", "&Jjo-!", true},
{"\u53F0\u5317\u65E5\u672C\u8A9E", "&U,BTF2XlZyyKng-", true},
// RFC 2152 (modified)
{"\u0041\u2262\u0391\u002E", "A&ImIDkQ-.", true},
{"Hi Mom -\u263A-!", "Hi Mom -&Jjo--!", true},
{"\u65E5\u672C\u8A9E", "&ZeVnLIqe-", true},
// 8->16 and 24->16 byte UTF-8 to UTF-16 conversion
{"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007", "&AAAAAQACAAMABAAFAAYABw-", true},
{"\u0800\u0801\u0802\u0803\u0804\u0805\u0806\u0807", "&CAAIAQgCCAMIBAgFCAYIBw-", true},
// Invalid UTF-8 (bad bytes are converted to U+FFFD)
{"\xC0\x80", "&,,3,,Q-", false}, // U+0000
{"\xF4\x90\x80\x80", "&,,3,,f,9,,0-", false}, // U+110000
{"\xF7\xBF\xBF\xBF", "&,,3,,f,9,,0-", false}, // U+1FFFFF
{"\xF8\x88\x80\x80\x80", "&,,3,,f,9,,3,,Q-", false}, // U+200000
{"\xF4\x8F\xBF\x3F", "&,,3,,f,9-?", false}, // U+10FFFF (bad byte)
{"\xF4\x8F\xBF", "&,,3,,f,9-", false}, // U+10FFFF (short)
{"\xF4\x8F", "&,,3,,Q-", false},
{"\xF4", "&,,0-", false},
{"\x00\xF4\x00", "&AAD,,QAA-", false},
}
func TestEncoder(t *testing.T) {
for _, test := range encode {
out := utf7.Encode(test.in)
if out != test.out {
t.Errorf("UTF7Encode(%+q) expected %+q; got %+q", test.in, test.out, out)
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// Package utf7 implements modified UTF-7 encoding defined in RFC 3501 section 5.1.3
package utf7
import (
"encoding/base64"
)
const (
min = 0x20 // Minimum self-representing UTF-7 value
max = 0x7E // Maximum self-representing UTF-7 value
)
var b64Enc = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,")
+95
View File
@@ -0,0 +1,95 @@
package validation
import (
"errors"
"net/mail"
"strings"
"git.pcux.cn/xuans/Smail/dto/parsemail"
)
var (
ErrInvalidEmail = errors.New("invalid email")
ErrTooManyRecipients = errors.New("too many recipients")
ErrTooManyAttachments = errors.New("too many attachments")
ErrAttachmentTooLarge = errors.New("attachment too large")
ErrEmailTooLarge = errors.New("email too large")
)
type EmailLimits struct {
MaxRecipients int
MaxAttachments int
MaxAttachmentBytes int64
MaxTotalAttachmentBytes int64
MaxBodyBytes int64
MaxEstimatedBytes int64
}
func ValidateEmail(email *parsemail.Email, limits EmailLimits) error {
if email == nil || email.From == nil || email.Sender == nil {
return ErrInvalidEmail
}
recipients := len(email.To) + len(email.Cc) + len(email.Bcc) + len(email.ReplyTo)
if recipients == 0 {
return ErrInvalidEmail
}
if limits.MaxRecipients > 0 && recipients > limits.MaxRecipients {
return ErrTooManyRecipients
}
for _, list := range [][]*parsemail.User{email.To, email.Cc, email.Bcc, email.ReplyTo} {
for _, user := range list {
if user == nil || !IsMailboxAddress(user.EmailAddress) {
return ErrInvalidEmail
}
}
}
bodyBytes := int64(len(email.Text)) + int64(len(email.HTML))
if limits.MaxBodyBytes > 0 && bodyBytes > limits.MaxBodyBytes {
return ErrEmailTooLarge
}
if limits.MaxAttachments > 0 && len(email.Attachments) > limits.MaxAttachments {
return ErrTooManyAttachments
}
totalAttachmentBytes := int64(0)
estimatedBytes := base64Size(int64(len(email.Text))) + base64Size(int64(len(email.HTML))) + 64<<10
for _, attachment := range email.Attachments {
if attachment == nil || strings.ContainsAny(attachment.ContentType, "\r\n") {
return ErrInvalidEmail
}
size := int64(len(attachment.Content))
if limits.MaxAttachmentBytes > 0 && size > limits.MaxAttachmentBytes {
return ErrAttachmentTooLarge
}
if size > 0 && totalAttachmentBytes > int64(^uint64(0)>>1)-size {
return ErrEmailTooLarge
}
totalAttachmentBytes += size
if limits.MaxTotalAttachmentBytes > 0 && totalAttachmentBytes > limits.MaxTotalAttachmentBytes {
return ErrEmailTooLarge
}
estimatedBytes += base64Size(size) + int64(len(attachment.Filename)) + int64(len(attachment.ContentType)) + 1024
if limits.MaxEstimatedBytes > 0 && estimatedBytes > limits.MaxEstimatedBytes {
return ErrEmailTooLarge
}
}
if limits.MaxEstimatedBytes > 0 && estimatedBytes > limits.MaxEstimatedBytes {
return ErrEmailTooLarge
}
return nil
}
func IsMailboxAddress(address string) bool {
address = strings.TrimSpace(address)
parsed, err := mail.ParseAddress(address)
return err == nil && parsed.Address == address
}
func base64Size(size int64) int64 {
if size <= 0 {
return 0
}
return ((size + 2) / 3) * 4
}
+58
View File
@@ -0,0 +1,58 @@
package validation
import (
"errors"
"testing"
"git.pcux.cn/xuans/Smail/dto/parsemail"
)
func TestValidateEmail(t *testing.T) {
valid := func() *parsemail.Email {
return &parsemail.Email{
From: &parsemail.User{EmailAddress: "alice@example.com"},
Sender: &parsemail.User{EmailAddress: "alice@example.com"},
To: []*parsemail.User{{EmailAddress: "bob@example.net"}},
Text: []byte("hello"),
}
}
limits := EmailLimits{
MaxRecipients: 2,
MaxAttachments: 1,
MaxAttachmentBytes: 4,
MaxTotalAttachmentBytes: 4,
MaxBodyBytes: 16,
MaxEstimatedBytes: 128 << 10,
}
if err := ValidateEmail(valid(), limits); err != nil {
t.Fatalf("ValidateEmail() error = %v", err)
}
tests := []struct {
name string
edit func(*parsemail.Email)
want error
}{
{name: "invalid recipient", edit: func(email *parsemail.Email) { email.To[0].EmailAddress = "Bob <bob@example.net>" }, want: ErrInvalidEmail},
{name: "too many recipients", edit: func(email *parsemail.Email) {
email.Cc = []*parsemail.User{{EmailAddress: "c@example.net"}, {EmailAddress: "d@example.net"}}
}, want: ErrTooManyRecipients},
{name: "too many attachments", edit: func(email *parsemail.Email) { email.Attachments = []*parsemail.Attachment{{}, {}} }, want: ErrTooManyAttachments},
{name: "single attachment too large", edit: func(email *parsemail.Email) { email.Attachments = []*parsemail.Attachment{{Content: make([]byte, 5)}} }, want: ErrAttachmentTooLarge},
{name: "body too large", edit: func(email *parsemail.Email) { email.HTML = make([]byte, 17) }, want: ErrEmailTooLarge},
{name: "invalid content type", edit: func(email *parsemail.Email) {
email.Attachments = []*parsemail.Attachment{{ContentType: "text/plain\r\nX-Test: 1"}}
}, want: ErrInvalidEmail},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
email := valid()
tt.edit(email)
if err := ValidateEmail(email, limits); !errors.Is(err, tt.want) {
t.Fatalf("ValidateEmail() error = %v, want %v", err, tt.want)
}
})
}
}
+92
View File
@@ -0,0 +1,92 @@
package validation
import (
"errors"
"net"
"strings"
"unicode"
"unicode/utf8"
)
const (
MaxAccountBytes = 20
MaxNameRunes = 10
MaxDomainCount = 100
)
func NormalizeAccount(value string) (string, error) {
value = strings.TrimSpace(value)
if len(value) == 0 || len(value) > MaxAccountBytes {
return "", errors.New("invalid account length")
}
for index, char := range []byte(value) {
isLetter := char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z'
isDigit := char >= '0' && char <= '9'
if !isLetter && !isDigit && char != '.' && char != '_' && char != '-' {
return "", errors.New("invalid account character")
}
if (index == 0 || index == len(value)-1) && !isLetter && !isDigit {
return "", errors.New("invalid account boundary")
}
}
return value, nil
}
func NormalizeDisplayName(value string) (string, error) {
value = strings.TrimSpace(value)
if value == "" || !utf8.ValidString(value) || utf8.RuneCountInString(value) > MaxNameRunes {
return "", errors.New("invalid display name length")
}
for _, char := range value {
if unicode.IsControl(char) {
return "", errors.New("invalid display name character")
}
}
return value, nil
}
func NormalizeDomain(value string) (string, error) {
value = strings.ToLower(strings.TrimSpace(value))
value = strings.TrimSuffix(value, ".")
if value == "" || len(value) > 253 || net.ParseIP(value) != nil || strings.ContainsAny(value, ":/@\\?#") {
return "", errors.New("invalid domain")
}
labels := strings.Split(value, ".")
for _, label := range labels {
if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
return "", errors.New("invalid domain label")
}
for _, char := range []byte(label) {
isLetter := char >= 'a' && char <= 'z'
isDigit := char >= '0' && char <= '9'
if !isLetter && !isDigit && char != '-' {
return "", errors.New("invalid domain character")
}
}
}
return value, nil
}
func NormalizeDomainList(value string) ([]string, error) {
if strings.TrimSpace(value) == "" {
return nil, nil
}
parts := strings.Split(value, ",")
if len(parts) > MaxDomainCount {
return nil, errors.New("too many domains")
}
seen := make(map[string]struct{}, len(parts))
result := make([]string, 0, len(parts))
for _, part := range parts {
domain, err := NormalizeDomain(part)
if err != nil {
return nil, err
}
if _, exists := seen[domain]; exists {
continue
}
seen[domain] = struct{}{}
result = append(result, domain)
}
return result, nil
}
+59
View File
@@ -0,0 +1,59 @@
package validation
import (
"strings"
"testing"
)
func TestNormalizeAccount(t *testing.T) {
for _, value := range []string{"user", "user.name", "user_name", "user-name", "User123"} {
if normalized, err := NormalizeAccount(value); err != nil || normalized != value {
t.Fatalf("valid account rejected: %q %v", value, err)
}
}
for _, value := range []string{"", ".user", "user-", "user@example.com", "user name", strings.Repeat("a", MaxAccountBytes+1)} {
if _, err := NormalizeAccount(value); err == nil {
t.Fatalf("invalid account accepted: %q", value)
}
}
}
func TestNormalizeDisplayName(t *testing.T) {
if value, err := NormalizeDisplayName(" Test User "); err != nil || value != "Test User" {
t.Fatalf("valid display name rejected: %q %v", value, err)
}
for _, value := range []string{"", "bad\nname", strings.Repeat("a", MaxNameRunes+1), string([]byte{0xff})} {
if _, err := NormalizeDisplayName(value); err == nil {
t.Fatalf("invalid display name accepted: %q", value)
}
}
}
func TestNormalizeDomain(t *testing.T) {
for input, expected := range map[string]string{
"Mail.Example.COM.": "mail.example.com",
"mx-1.example.com": "mx-1.example.com",
} {
if value, err := NormalizeDomain(input); err != nil || value != expected {
t.Fatalf("valid domain rejected: %q %q %v", input, value, err)
}
}
for _, value := range []string{"", "https://example.com", "example.com:443", "user@example.com", "127.0.0.1", "-bad.example", "bad-.example", "bad_name.example", strings.Repeat("a", 64) + ".example"} {
if _, err := NormalizeDomain(value); err == nil {
t.Fatalf("invalid domain accepted: %q", value)
}
}
}
func TestNormalizeDomainListDeduplicates(t *testing.T) {
values, err := NormalizeDomainList("a.example.com, A.EXAMPLE.COM ,b.example.com")
if err != nil {
t.Fatal(err)
}
if len(values) != 2 || values[0] != "a.example.com" || values[1] != "b.example.com" {
t.Fatalf("unexpected normalized domain list: %#v", values)
}
if _, err := NormalizeDomainList(strings.Repeat("a.example.com,", MaxDomainCount) + "b.example.com"); err == nil {
t.Fatal("oversized domain list was accepted")
}
}
+37
View File
@@ -0,0 +1,37 @@
package validation
import (
"net/mail"
"strings"
)
func IsAuthorizedSender(address, account string, isAdmin bool, domains []string) bool {
address = strings.TrimSpace(address)
parsed, err := mail.ParseAddress(address)
if err != nil || parsed.Address != address {
return false
}
at := strings.LastIndexByte(address, '@')
if at <= 0 || at == len(address)-1 || strings.Contains(address[:at], "@") {
return false
}
domain, err := NormalizeDomain(address[at+1:])
if err != nil {
return false
}
configured := false
for _, candidate := range domains {
normalized, normalizeErr := NormalizeDomain(candidate)
if normalizeErr == nil && domain == normalized {
configured = true
break
}
}
if !configured {
return false
}
return isAdmin || strings.EqualFold(address[:at], strings.TrimSpace(account))
}
+31
View File
@@ -0,0 +1,31 @@
package validation
import "testing"
func TestIsAuthorizedSender(t *testing.T) {
domains := []string{"example.com", "mail.example.net"}
tests := []struct {
name string
address string
account string
admin bool
want bool
}{
{name: "matching account", address: "alice@example.com", account: "alice", want: true},
{name: "case insensitive account", address: "Alice@EXAMPLE.COM", account: "alice", want: true},
{name: "external domain", address: "alice@external.example", account: "alice", want: false},
{name: "different account", address: "bob@example.com", account: "alice", want: false},
{name: "admin local alias", address: "alias@mail.example.net", account: "admin", admin: true, want: true},
{name: "admin external domain", address: "alias@external.example", account: "admin", admin: true, want: false},
{name: "display name rejected", address: "Alice <alice@example.com>", account: "alice", want: false},
{name: "missing local part", address: "@example.com", account: "alice", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsAuthorizedSender(tt.address, tt.account, tt.admin, domains); got != tt.want {
t.Fatalf("IsAuthorizedSender(%q) = %v, want %v", tt.address, got, tt.want)
}
})
}
}
+16
View File
@@ -0,0 +1,16 @@
package version
func LT(version1, version2 string) bool {
if version2 == "test" {
return true
}
return version1 < version2
}
func GT(version1, version2 string) bool {
if version2 == "test" {
return false
}
return version1 > version2
}
+55
View File
@@ -0,0 +1,55 @@
package version
import "testing"
func TestLT(t *testing.T) {
type args struct {
version1 string
version2 string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "test1",
args: args{
version1: "1.0.0",
version2: "1.0.0",
},
want: false,
},
{
name: "test1",
args: args{
version1: "2.0.0",
version2: "1.0.0",
},
want: false,
},
{
name: "test1",
args: args{
version1: "1.0.0",
version2: "2.0.0",
},
want: true,
},
{
name: "test1",
args: args{
version1: "",
version2: "1.0.0",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := LT(tt.args.version1, tt.args.version2); got != tt.want {
t.Errorf("LT() = %v, want %v", got, tt.want)
}
})
}
}