统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
This commit is contained in:
1 parent
28cd9d3406
commit
f60d3b0880
603 files changed
+2351
-660
No files matched your search
@@ -0,0 +1,87 @@
|
||||
package attachments
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto/parsemail"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/auth"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrAttachmentUnavailable = errors.New("attachment unavailable")
|
||||
|
||||
func GetAttachments(ctx *context.Context, emailId int, cid string) (string, []byte, error) {
|
||||
if emailId <= 0 || strings.TrimSpace(cid) == "" {
|
||||
return "", nil, ErrAttachmentUnavailable
|
||||
}
|
||||
|
||||
// 获取邮件内容
|
||||
email, err := loadAuthorizedEmail(ctx, emailId)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
// 检查权限
|
||||
|
||||
atts, err := decodeAttachments(email.Attachments)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
for _, att := range atts {
|
||||
if att.ContentID == cid {
|
||||
return att.ContentType, att.Content, nil
|
||||
}
|
||||
}
|
||||
return "", nil, ErrAttachmentUnavailable
|
||||
}
|
||||
|
||||
func GetAttachmentsByIndex(ctx *context.Context, emailId int, index int) (string, []byte, error) {
|
||||
if emailId <= 0 || index < 0 {
|
||||
return "", nil, ErrAttachmentUnavailable
|
||||
}
|
||||
|
||||
// 获取邮件内容
|
||||
email, err := loadAuthorizedEmail(ctx, emailId)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
// 检查权限
|
||||
|
||||
atts, err := decodeAttachments(email.Attachments)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
if index < len(atts) {
|
||||
return atts[index].Filename, atts[index].Content, nil
|
||||
}
|
||||
return "", nil, ErrAttachmentUnavailable
|
||||
}
|
||||
|
||||
func loadAuthorizedEmail(ctx *context.Context, emailID int) (models.Email, error) {
|
||||
var email models.Email
|
||||
has, err := db.Instance.ID(emailID).Get(&email)
|
||||
if err != nil {
|
||||
return models.Email{}, fmt.Errorf("load attachment email: %w", err)
|
||||
}
|
||||
if !has || !auth.HasAuth(ctx, &email) {
|
||||
return models.Email{}, ErrAttachmentUnavailable
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
func decodeAttachments(raw string) ([]parsemail.Attachment, error) {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return nil, nil
|
||||
}
|
||||
var atts []parsemail.Attachment
|
||||
if err := json.Unmarshal([]byte(raw), &atts); err != nil {
|
||||
return nil, fmt.Errorf("decode stored attachments: %w", err)
|
||||
}
|
||||
return atts, nil
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package attachments
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto/parsemail"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
func TestAttachmentAccessIsRestrictedToMailboxOwner(t *testing.T) {
|
||||
oldConfig := config.Instance
|
||||
oldDB := db.Instance
|
||||
config.Instance = &config.Config{DbType: config.DBTypeSQLite, DbDSN: filepath.Join(t.TempDir(), "attachments.db")}
|
||||
if err := db.Init(""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if db.Instance != nil {
|
||||
_ = db.Instance.Close()
|
||||
}
|
||||
db.Instance = oldDB
|
||||
config.Instance = oldConfig
|
||||
})
|
||||
|
||||
encoded, err := json.Marshal([]parsemail.Attachment{{
|
||||
Filename: "report.txt", ContentType: "text/plain", Content: []byte("secret"), ContentID: "cid-1",
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
email := models.Email{Attachments: string(encoded), MsgID: "attachment-access@example.com"}
|
||||
if _, err := db.Instance.Insert(&email); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
relation := models.UserEmail{UserID: 1, EmailID: email.Id}
|
||||
if _, err := db.Instance.Insert(&relation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
owner := &context.Context{UserID: 1}
|
||||
other := &context.Context{UserID: 2}
|
||||
admin := &context.Context{UserID: 3, IsAdmin: true}
|
||||
|
||||
if contentType, content, err := GetAttachments(owner, email.Id, "cid-1"); err != nil || contentType != "text/plain" || string(content) != "secret" {
|
||||
t.Fatalf("owner could not read attachment: type=%q content=%q", contentType, content)
|
||||
}
|
||||
if _, content, err := GetAttachments(other, email.Id, "cid-1"); err == nil || len(content) != 0 {
|
||||
t.Fatal("other user read attachment by content ID")
|
||||
}
|
||||
if _, content, err := GetAttachmentsByIndex(other, email.Id, 0); err == nil || len(content) != 0 {
|
||||
t.Fatal("other user downloaded attachment by index")
|
||||
}
|
||||
if _, content, err := GetAttachmentsByIndex(admin, email.Id, 0); err == nil || len(content) != 0 {
|
||||
t.Fatal("administrator read another user's attachment without mailbox ownership")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentDecodeRejectsCorruptedStoredJSON(t *testing.T) {
|
||||
if _, err := decodeAttachments("{"); err == nil {
|
||||
t.Fatal("corrupted attachment JSON was accepted")
|
||||
}
|
||||
if atts, err := decodeAttachments(" "); err != nil || atts != nil {
|
||||
t.Fatalf("blank attachment JSON was not accepted: atts=%#v err=%v", atts, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user