Compare commits
4 Commits
0abb06baaf
...
99b4194598
| Author | SHA1 | Date | |
|---|---|---|---|
| 99b4194598 | |||
| b777b2b101 | |||
| 14e045a826 | |||
| 1344cacfe2 |
97
backend/controllers/password_change.go
Normal file
97
backend/controllers/password_change.go
Normal file
@ -0,0 +1,97 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/verification"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
var errPasswordChangeVerification = errors.New("password change verification unavailable")
|
||||
|
||||
// 从数据库读取当前身份和已验证安全邮箱,不允许客户端指定验证码接收人。
|
||||
func passwordChangeIdentity(ctx *context.Context, runtime securityRuntimeState) (*models.User, *models.ExternalEmail, string, error) {
|
||||
if ctx == nil || db.Instance == nil || runtime.external == nil || runtime.verification == nil {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
}
|
||||
user := &models.User{}
|
||||
found, err := db.Instance.ID(ctx.UserID).Get(user)
|
||||
if err != nil || !found || user.Disabled != 0 || user.EffectiveRole() != access.RoleSuperAdmin || user.Account != ctx.UserAccount {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
}
|
||||
bindings, err := runtime.external.List(user.ID)
|
||||
if err != nil {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
}
|
||||
var binding *models.ExternalEmail
|
||||
for i := range bindings {
|
||||
item := &bindings[i]
|
||||
if item.Status != models.ExternalEmailStatusVerified {
|
||||
continue
|
||||
}
|
||||
if item.EmailType == models.ExternalEmailTypePrimary {
|
||||
binding = item
|
||||
break
|
||||
}
|
||||
if binding == nil && item.EmailType == models.ExternalEmailTypeBackup {
|
||||
binding = item
|
||||
}
|
||||
}
|
||||
if binding == nil {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
}
|
||||
email, err := runtime.external.Decrypt(*binding)
|
||||
if err != nil || strings.TrimSpace(email) == "" {
|
||||
return nil, nil, "", errPasswordChangeVerification
|
||||
}
|
||||
return user, binding, email, nil
|
||||
}
|
||||
|
||||
func requestPasswordChangeVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request, input verificationRequest, runtime securityRuntimeState) {
|
||||
user, binding, email, err := passwordChangeIdentity(ctx, runtime)
|
||||
if err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "请确认超级管理员已绑定并验证外部安全邮箱")
|
||||
return
|
||||
}
|
||||
if input.Email != "" && !strings.EqualFold(strings.TrimSpace(input.Email), email) {
|
||||
writeSecurityError(w, response.ParamsError, "邮箱地址与当前验证邮箱不匹配")
|
||||
return
|
||||
}
|
||||
challenge, err := runtime.verification.Issue(verification.IssueRequest{
|
||||
Purpose: verification.PurposePasswordChange, UserID: user.ID,
|
||||
Account: user.Account, Email: email, ResourceID: binding.ID,
|
||||
BrowserTransaction: strconv.Itoa(user.SecurityVersion),
|
||||
}, remoteIP(req))
|
||||
if err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "验证码发送失败或请求过于频繁,请稍后重试")
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]any{
|
||||
"id": challenge.ID, "expires_at": challenge.ExpiresAt.UTC(), "email": maskEmail(email),
|
||||
}).FPrint(w)
|
||||
}
|
||||
|
||||
func verifyPasswordChange(ctx *context.Context, req *http.Request, input modifyPasswordRequest) (*models.User, error) {
|
||||
runtime := currentSecurityRuntime()
|
||||
user, binding, email, err := passwordChangeIdentity(ctx, runtime)
|
||||
if err != nil || strings.TrimSpace(input.Code) == "" || strings.TrimSpace(input.ChallengeID) == "" {
|
||||
return nil, errPasswordChangeVerification
|
||||
}
|
||||
if input.Email != "" && !strings.EqualFold(strings.TrimSpace(input.Email), email) {
|
||||
return nil, errPasswordChangeVerification
|
||||
}
|
||||
// 验证码同时绑定用途、账号、邮箱绑定记录及安全版本,不能跨流程复用。
|
||||
if err := runtime.verification.VerifyBoundWithContext(input.ChallengeID, user.Account, email,
|
||||
strings.TrimSpace(input.Code), verification.PurposePasswordChange, binding.ID,
|
||||
remoteIP(req), strconv.Itoa(user.SecurityVersion)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
@ -5,8 +5,10 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
@ -15,6 +17,8 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/auth"
|
||||
"git.pcux.cn/xuans/Smail/services/credentials"
|
||||
"git.pcux.cn/xuans/Smail/services/externalemail"
|
||||
"git.pcux.cn/xuans/Smail/services/verification"
|
||||
"git.pcux.cn/xuans/Smail/session"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
@ -179,7 +183,7 @@ func authPasswordMatches(hash, plain string) bool {
|
||||
return password.Verify(hash, plain)
|
||||
}
|
||||
|
||||
func TestSuperAdminCannotChangePasswordThroughWeb(t *testing.T) {
|
||||
func TestSuperAdminCannotChangePasswordWithoutEmailVerification(t *testing.T) {
|
||||
testRoot := t.TempDir()
|
||||
oldConfig := config.Instance
|
||||
oldDB := db.Instance
|
||||
@ -208,8 +212,191 @@ func TestSuperAdminCannotChangePasswordThroughWeb(t *testing.T) {
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.ErrorNo != response.NoAccessPrivileges {
|
||||
if result.ErrorNo != response.ParamsError {
|
||||
t.Fatalf("expected web reset to be denied, got %d", result.ErrorNo)
|
||||
}
|
||||
assertUserPassword(t, user.ID, "OldPass123")
|
||||
}
|
||||
|
||||
func TestSuperAdminPasswordChangeVerification(t *testing.T) {
|
||||
for _, scenario := range []string{"success", "backup", "primary-priority", "binding-disabled", "binding-changed", "missing", "wrong-code", "wrong-purpose", "wrong-email", "unverified", "expired", "security-version", "disabled", "invalid-password", "replay"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
testRoot := t.TempDir()
|
||||
oldConfig, oldDB, oldSession := config.Instance, db.Instance, session.Instance
|
||||
oldRuntime := currentSecurityRuntime()
|
||||
oldPair, oldAccount, oldIP := loginPairAttempts, loginAccountAttempts, loginIPAttempts
|
||||
loginPairAttempts = newLoginLimiter(loginFailureLimit)
|
||||
loginAccountAttempts = newLoginLimiter(loginAccountFailureLimit)
|
||||
loginIPAttempts = newLoginLimiter(loginIPFailureLimit)
|
||||
session.Instance = scs.New()
|
||||
t.Cleanup(func() {
|
||||
_ = db.Instance.Close()
|
||||
config.Instance, db.Instance, session.Instance = oldConfig, oldDB, oldSession
|
||||
loginPairAttempts, loginAccountAttempts, loginIPAttempts = oldPair, oldAccount, oldIP
|
||||
ConfigureSecurityRuntime(oldRuntime.verification, oldRuntime.external, oldRuntime.recovery, oldRuntime.credentials)
|
||||
})
|
||||
config.Instance = &config.Config{DbType: config.DBTypeSQLite, DbDSN: filepath.Join(testRoot, "password.db")}
|
||||
if err := db.Init(""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := newControllerTestUser(t, "verified-super", "超级管理员", access.RoleSuperAdmin, "OldPass123")
|
||||
user.SecurityVersion = 1
|
||||
if _, err := db.Instance.Insert(&user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vault := externalemail.NewVaultWithKeys([]byte("01234567890123456789012345678901"), []byte("abcdefabcdefabcdefabcdefabcdefab"))
|
||||
repo, err := externalemail.NewRepository(db.Instance, vault)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bindingType := models.ExternalEmailTypePrimary
|
||||
if scenario == "backup" || scenario == "primary-priority" || scenario == "binding-changed" {
|
||||
bindingType = models.ExternalEmailTypeBackup
|
||||
}
|
||||
binding, err := repo.Bind(externalemail.BindRequest{UserID: user.ID, Email: "owner@example.com", Type: bindingType})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if scenario != "unverified" {
|
||||
if err := repo.Verify(user.ID, binding.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
store, err := verification.NewStore(32)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var code string
|
||||
var delivered verification.Challenge
|
||||
send := func(challenge verification.Challenge, value string) error {
|
||||
delivered, code = challenge, value
|
||||
return nil
|
||||
}
|
||||
service, err := verification.NewService(store, send, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ConfigureSecurityRuntime(service, repo, nil, nil)
|
||||
ctx := &appcontext.Context{UserID: user.ID, UserAccount: user.Account, Role: access.RoleSuperAdmin, Lang: "zhCn"}
|
||||
// 客户端既不能替换收件地址,也不能为其他账号申请改密验证码。
|
||||
for _, body := range []string{
|
||||
`{"purpose":"password_change","account":"verified-super","email":"attacker@example.com"}`,
|
||||
`{"purpose":"password_change","account":"another-user"}`,
|
||||
} {
|
||||
RequestVerification(ctx, httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body)))
|
||||
if delivered.ID != "" {
|
||||
t.Fatal("issued verification for client-controlled recipient or account")
|
||||
}
|
||||
}
|
||||
addPrimary := func() {
|
||||
primary, err := repo.Bind(externalemail.BindRequest{UserID: user.ID, Email: "primary@example.com", Type: models.ExternalEmailTypePrimary})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repo.Verify(user.ID, primary.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
expectedEmail := "owner@example.com"
|
||||
if scenario == "primary-priority" {
|
||||
addPrimary()
|
||||
expectedEmail = "primary@example.com"
|
||||
}
|
||||
issueRecorder := httptest.NewRecorder()
|
||||
RequestVerification(ctx, issueRecorder, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"purpose":"password_change","account":"verified-super"}`)))
|
||||
if scenario == "unverified" {
|
||||
if delivered.ID != "" {
|
||||
t.Fatal("sent to unverified email")
|
||||
}
|
||||
return
|
||||
}
|
||||
if delivered.ID == "" || delivered.Email != expectedEmail {
|
||||
t.Fatalf("issue failed: %s", issueRecorder.Body.String())
|
||||
}
|
||||
input := modifyPasswordRequest{Password: "NewPass123", Code: code, ChallengeID: delivered.ID}
|
||||
switch scenario {
|
||||
case "binding-disabled":
|
||||
err = repo.Disable(user.ID, binding.ID)
|
||||
case "binding-changed":
|
||||
addPrimary()
|
||||
case "missing":
|
||||
input.Code = ""
|
||||
case "wrong-code":
|
||||
input.Code = "not-a-code"
|
||||
case "wrong-email":
|
||||
input.Email = "other@example.com"
|
||||
case "invalid-password":
|
||||
input.Password = "short"
|
||||
case "security-version":
|
||||
_, err = db.Instance.ID(user.ID).Cols("security_version").Update(&models.User{SecurityVersion: 2})
|
||||
case "disabled":
|
||||
_, err = db.Instance.ID(user.ID).Cols("disabled").Update(&models.User{Disabled: 1})
|
||||
case "wrong-purpose", "expired":
|
||||
purpose, ttl := verification.PurposeLogin, time.Minute
|
||||
if scenario == "expired" {
|
||||
purpose, ttl = verification.PurposePasswordChange, time.Nanosecond
|
||||
}
|
||||
challenge, issueErr := store.Issue(verification.IssueRequest{Purpose: purpose, Account: user.Account, Email: delivered.Email, ResourceID: binding.ID, BrowserTransaction: strconv.Itoa(user.SecurityVersion), TTL: ttl}, send)
|
||||
if issueErr != nil {
|
||||
t.Fatal(issueErr)
|
||||
}
|
||||
input.ChallengeID, input.Code = challenge.ID, code
|
||||
if scenario == "expired" {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
change := func() int {
|
||||
body, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(body)))
|
||||
loaded, err := session.Instance.Load(req.Context(), "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session.Instance.Put(loaded, "user_id", user.ID)
|
||||
recorder := httptest.NewRecorder()
|
||||
ModifyPassword(ctx, recorder, req.WithContext(loaded))
|
||||
var result struct {
|
||||
ErrorNo int `json:"errorNo"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.ErrorNo == 0 && session.Instance.Exists(loaded, "user_id") {
|
||||
t.Fatal("session survived password change")
|
||||
}
|
||||
return result.ErrorNo
|
||||
}
|
||||
succeeded := scenario == "success" || scenario == "backup" || scenario == "primary-priority" || scenario == "replay"
|
||||
if result := change(); (result == 0) != succeeded {
|
||||
t.Fatalf("unexpected result: %d", result)
|
||||
}
|
||||
if !succeeded {
|
||||
assertUserPassword(t, user.ID, "OldPass123")
|
||||
if scenario == "invalid-password" {
|
||||
input.Password = "NewPass123"
|
||||
if change() != 0 {
|
||||
t.Fatal("invalid password consumed verification")
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
assertUserPassword(t, user.ID, "NewPass123")
|
||||
var updated models.User
|
||||
if _, err := db.Instance.ID(user.ID).Get(&updated); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if updated.SessionVersion != 2 || updated.SecurityVersion != 2 {
|
||||
t.Fatal("security versions not rotated")
|
||||
}
|
||||
if scenario == "replay" && change() == 0 {
|
||||
t.Fatal("verification replay accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -482,6 +482,10 @@ func RequestVerification(ctx *context.Context, w http.ResponseWriter, req *http.
|
||||
writeSecurityError(w, response.ParamsError, "请求无效")
|
||||
return
|
||||
}
|
||||
if input.Purpose == verification.PurposePasswordChange {
|
||||
requestPasswordChangeVerification(ctx, w, req, input, runtime)
|
||||
return
|
||||
}
|
||||
if input.Email == "" || strings.TrimSpace(input.Account) == "" || !verification.IsSupportedPurpose(input.Purpose) || runtime.verification == nil {
|
||||
writeSecurityError(w, response.ParamsError, "请求已受理")
|
||||
return
|
||||
|
||||
@ -22,6 +22,9 @@ import (
|
||||
type modifyPasswordRequest struct {
|
||||
CurrentPassword string `json:"current_password"`
|
||||
Password string `json:"password"`
|
||||
Code string `json:"code"`
|
||||
ChallengeID string `json:"challenge_id"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func ModifyPassword(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
@ -29,17 +32,13 @@ func ModifyPassword(ctx *context.Context, w http.ResponseWriter, req *http.Reque
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "未登录用户不能修改密码", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if ctx.EffectiveRole() == access.RoleSuperAdmin {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "超级管理员必须通过部署端专用通道重置密码", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
var retData modifyPasswordRequest
|
||||
if err := decodeJSONRequest(w, req, &retData); err != nil {
|
||||
log.WithContext(ctx).Debug("invalid password change request")
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "unknowError"), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if retData.CurrentPassword == "" || retData.Password == "" {
|
||||
if retData.Password == "" || (ctx.EffectiveRole() != access.RoleSuperAdmin && retData.CurrentPassword == "") {
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "unknowError"), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
@ -50,13 +49,28 @@ func ModifyPassword(ctx *context.Context, w http.ResponseWriter, req *http.Reque
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "aperror"), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
currentUser, err := auth.Authenticate(ctx.UserAccount, retData.CurrentPassword)
|
||||
if err != nil || currentUser.ID != ctx.UserID {
|
||||
var currentUser *models.User
|
||||
var err error
|
||||
if ctx.EffectiveRole() == access.RoleSuperAdmin {
|
||||
// 先校验新密码格式,避免无效输入消耗一次性验证码。
|
||||
if password.ValidateUserPassword(retData.Password) != nil {
|
||||
writeSecurityError(w, response.ParamsError, "密码长度必须为 8-12 字节")
|
||||
return
|
||||
}
|
||||
currentUser, err = verifyPasswordChange(ctx, req, retData)
|
||||
} else {
|
||||
currentUser, err = auth.Authenticate(ctx.UserAccount, retData.CurrentPassword)
|
||||
}
|
||||
if err != nil || currentUser == nil || currentUser.ID != ctx.UserID {
|
||||
loginPairAttempts.recordFailure(pairKey)
|
||||
loginAccountAttempts.recordFailure(accountKey)
|
||||
loginIPAttempts.recordFailure(ipKey)
|
||||
log.WithContext(ctx).Info("password change authentication failed")
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "aperror"), "").FPrint(w)
|
||||
if ctx.EffectiveRole() == access.RoleSuperAdmin {
|
||||
writeSecurityError(w, response.ParamsError, "请使用已绑定安全邮箱的有效验证码,失败后可重新获取")
|
||||
} else {
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "aperror"), "").FPrint(w)
|
||||
}
|
||||
return
|
||||
}
|
||||
loginPairAttempts.clear(pairKey)
|
||||
@ -107,7 +121,7 @@ func ModifyPassword(ctx *context.Context, w http.ResponseWriter, req *http.Reque
|
||||
}
|
||||
}()
|
||||
|
||||
affected, err := tx.Where("id = ? and password = ? and session_version = ?", ctx.UserID, currentUser.Password, currentUser.SessionVersion).Cols("password", "session_version", "security_version", "must_change_password", "password_changed_at").Update(&user)
|
||||
affected, err := tx.Where("id = ? and password = ? and session_version = ? and security_version = ? and disabled = 0", ctx.UserID, currentUser.Password, currentUser.SessionVersion, currentUser.SecurityVersion).Cols("password", "session_version", "security_version", "must_change_password", "password_changed_at").Update(&user)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).WithError(err).Error("update password failed")
|
||||
response.NewErrorResponse(response.ServerError, i18n.GetText(ctx.Lang, "unknowError"), "").FPrint(w)
|
||||
@ -129,6 +143,7 @@ func ModifyPassword(ctx *context.Context, w http.ResponseWriter, req *http.Reque
|
||||
return
|
||||
}
|
||||
committed = true
|
||||
InvalidateSecurityRuntimeUser(ctx.UserID, ctx.UserAccount)
|
||||
if err := session.Instance.Destroy(req.Context()); err != nil {
|
||||
log.WithContext(ctx).WithError(err).Error("session destruction after password change failed")
|
||||
response.NewErrorResponse(response.ServerError, i18n.GetText(ctx.Lang, "unknowError"), "").FPrint(w)
|
||||
|
||||
@ -55,7 +55,7 @@ func TestModifyPasswordRouteRejectsUnauthenticatedRequest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuperAdminCannotChangeOwnPasswordAfterLogin(t *testing.T) {
|
||||
func TestSuperAdminCannotChangeOwnPasswordWithoutEmailVerification(t *testing.T) {
|
||||
oldConfig := config.Instance
|
||||
oldInit := config.IsInit
|
||||
oldDB := db.Instance
|
||||
@ -104,7 +104,7 @@ func TestSuperAdminCannotChangeOwnPasswordAfterLogin(t *testing.T) {
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.ErrorNo != response.NoAccessPrivileges {
|
||||
if result.ErrorNo != response.ParamsError {
|
||||
t.Fatalf("super admin web password change was not rejected: %s", recorder.Body.String())
|
||||
}
|
||||
|
||||
|
||||
BIN
backend/out/linux_amd64/smail
Normal file
BIN
backend/out/linux_amd64/smail
Normal file
Binary file not shown.
BIN
backend/out/smail
Normal file
BIN
backend/out/smail
Normal file
Binary file not shown.
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func TestSupportedPurposes(t *testing.T) {
|
||||
for _, purpose := range []Purpose{PurposeLogin, PurposePasswordRecover, PurposeExternalEmailBind, PurposeExternalEmailChange} {
|
||||
for _, purpose := range []Purpose{PurposeLogin, PurposePasswordRecover, PurposePasswordChange, PurposeExternalEmailBind, PurposeExternalEmailChange} {
|
||||
if !IsSupportedPurpose(purpose) {
|
||||
t.Fatalf("purpose %q was rejected", purpose)
|
||||
}
|
||||
|
||||
@ -58,3 +58,15 @@
|
||||
.credential-actions { width: 100%; }
|
||||
.credential-actions .el-button { flex: 1; }
|
||||
}
|
||||
.password-verification-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.password-verification-row strong {
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { computed, onMounted, onUnmounted, reactive, ref, watch } from 'vue'
|
||||
|
||||
import { ElMessageBox, ElNotification } from 'element-plus'
|
||||
|
||||
@ -19,9 +19,7 @@ export function usePage() {
|
||||
if (canReadGlobalPolicy.value) {
|
||||
options.push({ value: 'security-policy', label: '安全策略' })
|
||||
}
|
||||
if (!isSuperAdmin.value) {
|
||||
options.push({ value: 'password', label: lang.modify_pwd })
|
||||
}
|
||||
options.push({ value: 'password', label: lang.modify_pwd })
|
||||
options.push({ value: 'question', label: '密保问题' })
|
||||
if (!isSuperAdmin.value) {
|
||||
options.push({ value: 'recovery', label: '离线恢复码' })
|
||||
@ -60,10 +58,19 @@ export function usePage() {
|
||||
const externalEmails = ref([])
|
||||
const externalListLoading = ref(false)
|
||||
const externalDialogVisible = ref(false)
|
||||
const passwordEmail = computed(() => {
|
||||
const verified = externalEmails.value.filter(item => item.status === 'verified')
|
||||
return verified.find(item => item.type === 'primary') || verified.find(item => item.type === 'backup')
|
||||
})
|
||||
const passwordSending = ref(false)
|
||||
const passwordSubmitting = ref(false)
|
||||
const passwordChallenge = ref('')
|
||||
const passwordCountdown = ref(0)
|
||||
let passwordTimer = null
|
||||
let lastNotificationKey = ''
|
||||
let lastNotificationAt = 0
|
||||
|
||||
const ruleForm = reactive({ current_pwd: '', new_pwd: '', new_pwd2: '' })
|
||||
const ruleForm = reactive({ current_pwd: '', new_pwd: '', new_pwd2: '', code: '' })
|
||||
const rules = reactive({
|
||||
current_pwd: [{ required: true, message: lang.err_required_pwd, trigger: 'blur' }],
|
||||
new_pwd: [{ required: true, message: lang.err_required_pwd, trigger: 'blur' }],
|
||||
@ -255,16 +262,75 @@ export function usePage() {
|
||||
const logout = () => http.post('/api/v1/logout', {}).then(() => location.reload())
|
||||
|
||||
const submit = async () => {
|
||||
if (!ruleForm.current_pwd || !ruleForm.new_pwd) return
|
||||
if (passwordSubmitting.value || passwordSending.value) return
|
||||
if (!ruleForm.new_pwd || (!isSuperAdmin.value && !ruleForm.current_pwd)) {
|
||||
notify('请填写完整的密码信息')
|
||||
return
|
||||
}
|
||||
const passwordBytes = new TextEncoder().encode(ruleForm.new_pwd).length
|
||||
if (passwordBytes < 8 || passwordBytes > 12) {
|
||||
notify('密码长度必须为 8-12 字节')
|
||||
return
|
||||
}
|
||||
if (ruleForm.new_pwd !== ruleForm.new_pwd2) {
|
||||
notify(lang.err_pwd_diff)
|
||||
return
|
||||
}
|
||||
const res = await http.post('/api/v1/settings/modify_password', { current_password: ruleForm.current_pwd, password: ruleForm.new_pwd })
|
||||
notify(res.errorNo === 0 ? '密码修改成功' : res.errorMsg, res.errorNo === 0 ? 'success' : 'error')
|
||||
if (res.errorNo === 0) Object.assign(ruleForm, { current_pwd: '', new_pwd: '', new_pwd2: '' })
|
||||
if (isSuperAdmin.value && (!passwordEmail.value || !passwordChallenge.value || !/^\d{8}$/.test(ruleForm.code.trim()))) {
|
||||
notify('请获取并输入安全邮箱中的 8 位验证码')
|
||||
return
|
||||
}
|
||||
passwordSubmitting.value = true
|
||||
try {
|
||||
const payload = isSuperAdmin.value
|
||||
? { password: ruleForm.new_pwd, challenge_id: passwordChallenge.value, code: ruleForm.code.trim() }
|
||||
: { current_password: ruleForm.current_pwd, password: ruleForm.new_pwd }
|
||||
const res = await http.post('/api/v1/settings/modify_password', payload)
|
||||
if (res.errorNo !== 0) throw new Error(res.errorMsg || '密码修改失败')
|
||||
Object.assign(ruleForm, { current_pwd: '', new_pwd: '', new_pwd2: '', code: '' })
|
||||
passwordChallenge.value = ''
|
||||
notify('密码修改成功,请重新登录;原安全凭据已失效', 'success')
|
||||
globalStatus.clear()
|
||||
location.assign('/login')
|
||||
} catch (error) {
|
||||
notify(error?.message || '密码修改失败,请重新获取验证码后重试')
|
||||
} finally {
|
||||
passwordSubmitting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const sendPasswordCode = async () => {
|
||||
if (passwordSending.value || passwordSubmitting.value || passwordCountdown.value > 0 || !passwordEmail.value) return
|
||||
passwordSending.value = true
|
||||
passwordChallenge.value = ''
|
||||
ruleForm.code = ''
|
||||
try {
|
||||
const res = await http.post('/api/v1/security/verification/request', {
|
||||
purpose: 'password_change', account: globalStatus.userInfos.account,
|
||||
})
|
||||
if (res.errorNo !== 0 || !res.data?.id) throw new Error(res.errorMsg || '验证码发送失败')
|
||||
passwordChallenge.value = res.data.id
|
||||
passwordCountdown.value = 60
|
||||
const deadline = Date.now() + 60000
|
||||
clearInterval(passwordTimer)
|
||||
passwordTimer = setInterval(() => {
|
||||
passwordCountdown.value = Math.max(0, Math.ceil((deadline - Date.now()) / 1000))
|
||||
if (!passwordCountdown.value) clearInterval(passwordTimer)
|
||||
}, 1000)
|
||||
notify(`验证码已发送至 ${res.data.email || passwordEmail.value.email}`, 'success')
|
||||
} catch (error) {
|
||||
notify(error?.message || '验证码发送失败')
|
||||
} finally {
|
||||
passwordSending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
watch(() => passwordEmail.value?.id, () => {
|
||||
passwordChallenge.value = ''
|
||||
ruleForm.code = ''
|
||||
})
|
||||
onUnmounted(() => clearInterval(passwordTimer))
|
||||
|
||||
onMounted(async () => {
|
||||
await loadSecurityData()
|
||||
})
|
||||
@ -276,6 +342,7 @@ export function usePage() {
|
||||
return {
|
||||
lang, isSuperAdmin, supported, webAuthnEnabled, sectionOptions, selectedSection, selectSection,
|
||||
globalSecurityPolicyPage,
|
||||
passwordEmail, passwordSending, passwordSubmitting, passwordCountdown, sendPasswordCode,
|
||||
registering, credentialsLoading, credentials, securityLoading,
|
||||
securityQuestion, questionForm, recoveryConfigured, generatedCodes, externalEmails, externalListLoading,
|
||||
externalDialogVisible, ruleForm, rules, submit, registerCredential, renameCredential, revokeCredential,
|
||||
|
||||
@ -15,28 +15,63 @@
|
||||
ref="globalSecurityPolicyPage"
|
||||
/>
|
||||
|
||||
<section v-if="selectedSection === 'password' && !isSuperAdmin" class="security-section">
|
||||
<section v-if="selectedSection === 'password'" class="security-section">
|
||||
<div class="settings-header">
|
||||
<h3>{{ lang.modify_pwd }}</h3>
|
||||
<p class="settings-desc">{{ lang.modify_pwd_desc }}</p>
|
||||
<p class="settings-desc">{{ isSuperAdmin ? '通过已验证安全邮箱修改密码,优先主要邮箱;完成后需重新登录。' : lang.modify_pwd_desc }}</p>
|
||||
</div>
|
||||
<div class="settings-body">
|
||||
<el-form :model="ruleForm" :rules="rules" status-icon label-position="top" class="settings-form">
|
||||
<el-form-item :label="lang.current_pwd" prop="current_pwd"><el-input v-model="ruleForm.current_pwd" type="password" :placeholder="lang.enter_current_pwd" show-password class="premium-input" /></el-form-item>
|
||||
<el-form-item :label="lang.modify_pwd" prop="new_pwd"><el-input v-model="ruleForm.new_pwd" type="password" :placeholder="lang.enter_new_pwd" show-password class="premium-input" /></el-form-item>
|
||||
<el-form-item :label="lang.enter_again" prop="new_pwd2"><el-input v-model="ruleForm.new_pwd2" type="password" :placeholder="lang.confirm_new_pwd" show-password class="premium-input" /></el-form-item>
|
||||
<div class="form-actions"><el-button type="primary" @click="submit" class="submit-btn">{{ lang.submit }}</el-button></div>
|
||||
<template v-if="isSuperAdmin">
|
||||
<el-form-item label="外部安全邮箱">
|
||||
<div v-if="passwordEmail" class="password-verification-row">
|
||||
<strong>{{ passwordEmail.email }}</strong>
|
||||
<el-button
|
||||
:loading="passwordSending"
|
||||
:disabled="passwordCountdown > 0 || passwordSubmitting"
|
||||
@click="sendPasswordCode"
|
||||
>
|
||||
{{ passwordCountdown > 0 ? `${passwordCountdown} 秒后重发` : '获取验证码' }}
|
||||
</el-button>
|
||||
</div>
|
||||
<el-button v-else type="primary" link @click="selectSection('external-email')">
|
||||
请先绑定并验证外部安全邮箱
|
||||
</el-button>
|
||||
</el-form-item>
|
||||
<el-form-item label="邮箱验证码" required>
|
||||
<el-input
|
||||
v-model="ruleForm.code"
|
||||
inputmode="numeric"
|
||||
autocomplete="one-time-code"
|
||||
maxlength="8"
|
||||
placeholder="请输入邮箱中的 8 位验证码"
|
||||
:disabled="!passwordEmail || passwordSubmitting"
|
||||
/>
|
||||
</el-form-item>
|
||||
</template>
|
||||
<el-form-item v-else :label="lang.current_pwd" prop="current_pwd">
|
||||
<el-input v-model="ruleForm.current_pwd" type="password" autocomplete="current-password" :placeholder="lang.enter_current_pwd" show-password class="premium-input" />
|
||||
</el-form-item>
|
||||
<el-form-item :label="lang.modify_pwd" prop="new_pwd">
|
||||
<el-input v-model="ruleForm.new_pwd" type="password" autocomplete="new-password" :placeholder="lang.enter_new_pwd" show-password class="premium-input" />
|
||||
</el-form-item>
|
||||
<el-form-item :label="lang.enter_again" prop="new_pwd2">
|
||||
<el-input v-model="ruleForm.new_pwd2" type="password" autocomplete="new-password" :placeholder="lang.confirm_new_pwd" show-password class="premium-input" />
|
||||
</el-form-item>
|
||||
<div class="form-actions">
|
||||
<el-button type="primary" :loading="passwordSubmitting" :disabled="passwordSending || (isSuperAdmin && !passwordEmail)" @click="submit" class="submit-btn">
|
||||
{{ lang.submit }}
|
||||
</el-button>
|
||||
</div>
|
||||
</el-form>
|
||||
</div>
|
||||
<el-divider class="settings-divider" />
|
||||
</section>
|
||||
|
||||
<el-alert v-if="selectedSection === 'password' && isSuperAdmin" title="超级管理员密码只能通过部署端专用本机通道重置,网页不会提供修改入口。" type="warning" :closable="false" show-icon class="security-alert" />
|
||||
|
||||
<section v-if="selectedSection === 'question'" class="security-section">
|
||||
<div class="settings-header">
|
||||
<h3>密保问题</h3>
|
||||
<p v-if="isSuperAdmin" class="settings-desc">此设置不会开放超级管理员网页密码恢复,密码仍须通过部署端本机通道重置。</p>
|
||||
<p v-if="isSuperAdmin" class="settings-desc">密保不用于超级管理员重置密码,请使用修改密码页面的邮箱验证或部署端本机恢复通道。</p>
|
||||
<p v-else class="settings-desc">密保问题不能单独重置密码,必须与离线恢复码组合使用。</p>
|
||||
</div>
|
||||
<el-form :model="questionForm" label-position="top" class="settings-form">
|
||||
@ -111,6 +146,7 @@ import ExternalEmailBindingDialog from './ExternalEmailBindingDialog/ExternalEma
|
||||
const {
|
||||
lang, isSuperAdmin, supported, webAuthnEnabled, sectionOptions, selectedSection, selectSection,
|
||||
globalSecurityPolicyPage,
|
||||
passwordEmail, passwordSending, passwordSubmitting, passwordCountdown, sendPasswordCode,
|
||||
registering, credentialsLoading, credentials, securityLoading,
|
||||
securityQuestion, questionForm, recoveryConfigured, generatedCodes, externalEmails, externalListLoading,
|
||||
externalDialogVisible, ruleForm, rules, submit, registerCredential, renameCredential, revokeCredential,
|
||||
|
||||
94
test/frontend/password-change.test.mjs
Normal file
94
test/frontend/password-change.test.mjs
Normal file
@ -0,0 +1,94 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { compileFunction } from 'node:vm'
|
||||
import { computed, reactive, ref } from './vue.mjs'
|
||||
|
||||
const source = readFileSync(new URL('../../frontend/src/views/components/SecuritySettings/SecuritySettings.js', import.meta.url), 'utf8')
|
||||
const createPage = compileFunction(
|
||||
source.replace(/^import .*$/gm, '').replace('export function usePage', 'function usePage') + '\nreturn usePage()',
|
||||
['computed', 'reactive', 'ref', 'watch', 'onMounted', 'onUnmounted', 'http', 'useGlobalStatusStore', 'webAuthnSupported', 'lang', 'ElNotification', 'TextEncoder', 'location', 'setInterval', 'clearInterval']
|
||||
)
|
||||
|
||||
function fixture(role = 'super_admin', post) {
|
||||
const calls = [], messages = [], redirects = []
|
||||
const store = reactive({ role, userInfos: { account: 'root' }, securityActiveSection: 'password', hasCapability: () => false, clear() { this.userInfos = {} } })
|
||||
const page = createPage(computed, reactive, ref, () => {}, () => {}, () => {}, {
|
||||
post: async (url, body) => {
|
||||
calls.push({ url, body })
|
||||
return post ? post(url, body) : { errorNo: 0, data: { id: 'challenge', email: 'o***@example.com' } }
|
||||
},
|
||||
}, () => store, () => false, {}, value => messages.push(value), TextEncoder,
|
||||
{ assign: value => redirects.push(value) }, () => 1, () => {})
|
||||
page.externalEmails.value = [{ id: 1, type: 'primary', status: 'verified', email: 'o***@example.com' }]
|
||||
Object.assign(page.ruleForm, { new_pwd: 'NewPass123', new_pwd2: 'NewPass123' })
|
||||
return { page, calls, messages, redirects, store }
|
||||
}
|
||||
|
||||
test('超级管理员显示修改密码入口,验证码申请不提交脱敏地址,倒计时阻止重复发送', async () => {
|
||||
const { page, calls } = fixture()
|
||||
assert.ok(page.sectionOptions.value.some(item => item.value === 'password'))
|
||||
await page.sendPasswordCode()
|
||||
await page.sendPasswordCode()
|
||||
assert.equal(calls.length, 1)
|
||||
assert.equal(calls[0].body.purpose, 'password_change')
|
||||
assert.equal(calls[0].body.account, 'root')
|
||||
assert.equal(calls[0].body.email, undefined)
|
||||
assert.equal(page.passwordCountdown.value, 60)
|
||||
})
|
||||
|
||||
test('超级管理员凭邮箱验证码修改密码,成功后清除身份并重新登录', async () => {
|
||||
const { page, calls, redirects, store } = fixture()
|
||||
await page.sendPasswordCode()
|
||||
page.ruleForm.code = '12345678'
|
||||
await page.submit()
|
||||
assert.equal(calls[1].body.challenge_id, 'challenge')
|
||||
assert.equal(calls[1].body.current_password, undefined)
|
||||
assert.equal(calls[1].body.code, '12345678')
|
||||
assert.deepEqual(redirects, ['/login'])
|
||||
assert.equal(Object.keys(store.userInfos).length, 0)
|
||||
})
|
||||
|
||||
test('普通用户和管理员维持当前密码验证', async () => {
|
||||
for (const role of ['user', 'admin']) {
|
||||
const { page, calls } = fixture(role)
|
||||
page.ruleForm.current_pwd = 'OldPass123'
|
||||
await page.submit()
|
||||
assert.equal(calls[0].body.current_password, 'OldPass123')
|
||||
assert.equal(calls[0].body.code, undefined)
|
||||
}
|
||||
})
|
||||
|
||||
test('无已验证安全邮箱、未获取验证码、密码不一致时不能提交', async () => {
|
||||
const { page, calls } = fixture()
|
||||
await page.submit()
|
||||
page.externalEmails.value = []
|
||||
await page.sendPasswordCode()
|
||||
page.ruleForm.new_pwd2 = 'OtherPass1'
|
||||
await page.submit()
|
||||
assert.equal(calls.length, 0)
|
||||
})
|
||||
|
||||
test('安全邮箱优先主要邮箱,没有主要邮箱时允许已验证备用邮箱', async () => {
|
||||
const { page, calls } = fixture()
|
||||
const backup = { id: 2, type: 'backup', status: 'verified', email: 'b***@example.com' }
|
||||
const primary = page.externalEmails.value[0]
|
||||
page.externalEmails.value = [backup, primary]
|
||||
assert.equal(page.passwordEmail.value.id, 1)
|
||||
page.externalEmails.value = [backup, { ...primary, status: 'disabled' }]
|
||||
assert.equal(page.passwordEmail.value.id, 2)
|
||||
await page.sendPasswordCode()
|
||||
assert.equal(calls.length, 1)
|
||||
page.externalEmails.value = [{ ...backup, status: 'pending' }]
|
||||
assert.equal(page.passwordEmail.value, undefined)
|
||||
})
|
||||
|
||||
test('请求失败不虚报成功、不登出且释放提交状态', async () => {
|
||||
const { page, messages, redirects, store } = fixture('admin', async () => { throw new Error('服务暂不可用') })
|
||||
page.ruleForm.current_pwd = 'OldPass123'
|
||||
await page.submit()
|
||||
assert.equal(messages.at(-1).type, 'error')
|
||||
assert.equal(page.passwordSubmitting.value, false)
|
||||
assert.equal(redirects.length, 0)
|
||||
assert.equal(store.userInfos.account, 'root')
|
||||
})
|
||||
8
test/generate_files.py
Normal file
8
test/generate_files.py
Normal file
@ -0,0 +1,8 @@
|
||||
import sys
|
||||
import binascii
|
||||
with open(sys.argv[1], 'wb') as fp:
|
||||
for line in sys.stdin:
|
||||
line = line.strip()
|
||||
if line:
|
||||
fp.write(binascii.unhexlify(line))
|
||||
print('done')
|
||||
18
test/run_deploy.py
Normal file
18
test/run_deploy.py
Normal file
@ -0,0 +1,18 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
sftp = ssh.open_sftp()
|
||||
print('Uploading out/smail to /opt/smail/smail.new ...')
|
||||
sftp.put('out/smail', '/opt/smail/smail.new')
|
||||
sftp.close()
|
||||
|
||||
stdin, stdout, stderr = ssh.exec_command('chmod +x /opt/smail/smail.new && killall -9 smail; mv /opt/smail/smail.new /opt/smail/smail && cd /opt/smail && nohup ./smail -p 80 > log/runtime.log 2>&1 &')
|
||||
print(stdout.read().decode())
|
||||
print(stderr.read().decode())
|
||||
|
||||
import time
|
||||
time.sleep(2)
|
||||
stdin, stdout, stderr = ssh.exec_command('ps aux | grep smail')
|
||||
print(stdout.read().decode())
|
||||
ssh.close()
|
||||
7
test/tools/cat_remote_configs.py
Normal file
7
test/tools/cat_remote_configs.py
Normal file
@ -0,0 +1,7 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
_, out, _ = ssh.exec_command('cat /opt/smail/config/smtp.json; echo ===; cat /opt/smail/config/email.json')
|
||||
print(out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
9
test/tools/check_auth_codes.py
Normal file
9
test/tools/check_auth_codes.py
Normal file
@ -0,0 +1,9 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command("sqlite3 /opt/smail/data/smail.db \"SELECT * FROM client_credential;\"")
|
||||
print('client_credential:\n', stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command("sqlite3 /opt/smail/data/smail.db \"SELECT * FROM protocol_credential;\"")
|
||||
print('protocol_credential:\n', stdout.read().decode())
|
||||
ssh.close()
|
||||
12
test/tools/check_db_full.py
Normal file
12
test/tools/check_db_full.py
Normal file
@ -0,0 +1,12 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, type, subject, from_address, send_user_id, status, error, create_time FROM email WHERE id >= 20;"')
|
||||
print("Security questions:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, user_id, email_id, is_read, group_id, status, create FROM user_email WHERE email_id >= 20;"')
|
||||
print("Recovery codes count:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, name, user_name, domain FROM user;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
12
test/tools/check_emails_10.py
Normal file
12
test/tools/check_emails_10.py
Normal file
@ -0,0 +1,12 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, type, subject, from_address, [to], send_user_id, status FROM email WHERE id >= 10;"')
|
||||
print("Emails >= 10:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, user_id, email_id, is_read, group_id, status FROM user_email WHERE email_id >= 10;"')
|
||||
print("UserEmails >= 10:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email, verified FROM external_email;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
12
test/tools/check_err24.py
Normal file
12
test/tools/check_err24.py
Normal file
@ -0,0 +1,12 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, error FROM email WHERE id = 24;"')
|
||||
print("Email 24 error:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT * FROM email WHERE id = 24;"')
|
||||
print("Email 24 row:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email, verified FROM external_email;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
9
test/tools/check_external.py
Normal file
9
test/tools/check_external.py
Normal file
@ -0,0 +1,9 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command("sqlite3 /opt/smail/data/smail.db \"SELECT sql FROM sqlite_master WHERE name='external_email';\"")
|
||||
print('SCHEMA:\n', stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command("sqlite3 /opt/smail/data/smail.db \"SELECT * FROM external_email;\"")
|
||||
print('DATA:\n', stdout.read().decode())
|
||||
ssh.close()
|
||||
12
test/tools/check_inbox_query.py
Normal file
12
test/tools/check_inbox_query.py
Normal file
@ -0,0 +1,12 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT e.id, e.subject, e.type, ue.status, ue.group_id, ue.is_read FROM email e LEFT JOIN user_email ue ON e.id = ue.email_id WHERE ue.user_id = 1 AND ue.status = 0 AND e.type = 0 AND (ue.group_id = 0 OR ue.group_id = 1) ORDER BY e.id DESC;"')
|
||||
print("Inbox query result:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT e.id, e.subject, e.type, e.status, e.error FROM email e WHERE e.send_user_id = 1 ORDER BY e.id DESC LIMIT 5;"')
|
||||
print("Sent query result:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email, verified FROM external_email;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
13
test/tools/check_log_send.py
Normal file
13
test/tools/check_log_send.py
Normal file
@ -0,0 +1,13 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('tail -n 40 /opt/smail/log/runtime-2026-09-14.log')
|
||||
print("Grep 6567567:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('tail -n 40 /opt/smail/log/stdout.log')
|
||||
print("Grep QQ number:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email, verified FROM external_email;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
|
||||
7
test/tools/check_remote_services.py
Normal file
7
test/tools/check_remote_services.py
Normal file
@ -0,0 +1,7 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
_, out, _ = ssh.exec_command('ss -tulpn; ps aux | grep smail')
|
||||
print(out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
19
test/tools/check_row22.py
Normal file
19
test/tools/check_row22.py
Normal file
@ -0,0 +1,19 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "date"')
|
||||
print("Security questions:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "date -u"')
|
||||
print("Recovery codes count:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, user_name, domain, password FROM user;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
0
test/tools/check_security_state.py
Normal file
0
test/tools/check_security_state.py
Normal file
12
test/tools/check_srv.py
Normal file
12
test/tools/check_srv.py
Normal file
@ -0,0 +1,12 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('date')
|
||||
print("Server Date:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('uptime')
|
||||
print("Uptime:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email, verified FROM external_email;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
12
test/tools/check_ue.py
Normal file
12
test/tools/check_ue.py
Normal file
@ -0,0 +1,12 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email_id FROM user_email WHERE email_id = 22;"')
|
||||
print("Security questions:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email_id FROM user_email WHERE email_id = 23;"')
|
||||
print("Recovery codes count:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email_id FROM user_email WHERE email_id = 21;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
29
test/tools/debug_ssh.py
Normal file
29
test/tools/debug_ssh.py
Normal file
@ -0,0 +1,29 @@
|
||||
import paramiko
|
||||
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
|
||||
def run(cmd):
|
||||
stdin, stdout, stderr = ssh.exec_command(cmd)
|
||||
return stdout.read().decode(utf-8, errors=ignore), stderr.read().decode(utf-8, errors=ignore)
|
||||
|
||||
out, _ = run(ls -la /opt/smail/log)
|
||||
print(=== /opt/smail/log ===\n + out)
|
||||
|
||||
out, _ = run(grep -rn 'qq.com' /opt/smail/log/ 2>/dev/null)
|
||||
print(=== qq.com grep in logs ===\n + out)
|
||||
|
||||
out, _ = run(sqlite3 /opt/smail/data/smail.db '.schema email')
|
||||
print(=== schema email ===\n + out)
|
||||
|
||||
out, _ = run(sqlite3 /opt/smail/data/smail.db '.schema user_email')
|
||||
print(=== schema user_email ===\n + out)
|
||||
|
||||
out, _ = run(sqlite3 /opt/smail/data/smail.db 'SELECT id, subject, send_time FROM email ORDER BY id DESC LIMIT 5;')
|
||||
print(=== latest emails ===\n + out)
|
||||
|
||||
out, _ = run(sqlite3 /opt/smail/data/smail.db 'SELECT id, user_id, email_id, is_read FROM user_email ORDER BY id DESC LIMIT 5;')
|
||||
print(=== latest user_emails ===\n + out)
|
||||
|
||||
ssh.close()
|
||||
41
test/tools/decrypt_external.py
Normal file
41
test/tools/decrypt_external.py
Normal file
@ -0,0 +1,41 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''import sqlite3, base64, glob
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||
|
||||
conn = sqlite3.connect('/opt/smail/data/smail.db')
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT id, user_id, email_ciphertext, status, email_type FROM external_email')
|
||||
rows = c.fetchall()
|
||||
|
||||
key_files = glob.glob('/opt/smail/ssl/external-email-encryption/*.key')
|
||||
print('Key files:', key_files)
|
||||
if key_files:
|
||||
with open(key_files[0], 'rb') as f:
|
||||
key = f.read()
|
||||
aesgcm = AESGCM(key)
|
||||
for r in rows:
|
||||
enc_str = r[2]
|
||||
parts = enc_str.split(':')
|
||||
b64 = parts[3]
|
||||
padded = b64 + '=' * (-len(b64) % 4)
|
||||
raw = base64.urlsafe_b64decode(padded)
|
||||
nonce = raw[:12]
|
||||
ct = raw[12:]
|
||||
try:
|
||||
pt = aesgcm.decrypt(nonce, ct, b'smail-external-email-v1')
|
||||
print(f'User {r[1]} ({r[4]}, {r[3]}): {pt.decode()}')
|
||||
except Exception as e:
|
||||
print(f'User {r[1]} decrypt error: {e}')
|
||||
'''
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/decrypt_emails.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
_, out, err = ssh.exec_command('python3 /tmp/decrypt_emails.py')
|
||||
print('OUT:\n', out.read().decode())
|
||||
print('ERR:\n', err.read().decode())
|
||||
ssh.close()
|
||||
11
test/tools/dump_emails.py
Normal file
11
test/tools/dump_emails.py
Normal file
@ -0,0 +1,11 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, subject, send_user_id, send_time FROM email ;"')
|
||||
print("Emails:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, user_id, email_id, is_read, is_trash FROM user_email ;"')
|
||||
print("User emails:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, user_id, email_id, is_read, is_trash FROM user_email LIMIT 10;"')
|
||||
print("Users:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
6
test/tools/dump_mod_struct.py
Normal file
6
test/tools/dump_mod_struct.py
Normal file
@ -0,0 +1,6 @@
|
||||
p = 'backend/controllers/settings.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
idx = data.find(b'type modifyPasswordRequest struct')
|
||||
print(repr(data[idx:idx+200]))
|
||||
7
test/tools/dump_req_ver.py
Normal file
7
test/tools/dump_req_ver.py
Normal file
@ -0,0 +1,7 @@
|
||||
p = 'backend/controllers/security.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
idx = data.find(b'func RequestVerification')
|
||||
idx_end = data.find(b'// VerifyVerification', idx)
|
||||
print(data[idx:idx_end])
|
||||
7
test/tools/dump_settings.py
Normal file
7
test/tools/dump_settings.py
Normal file
@ -0,0 +1,7 @@
|
||||
p = 'backend/controllers/settings.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
idx = data.find(b'type modifyPasswordRequest struct')
|
||||
idx_end = data.find(b'if retData.CurrentPassword == "', idx)
|
||||
print(data[idx:idx_end].decode('utf-8'))
|
||||
7
test/tools/dump_settings_imports.py
Normal file
7
test/tools/dump_settings_imports.py
Normal file
@ -0,0 +1,7 @@
|
||||
p = 'backend/controllers/settings.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
idx = data.find(b'import (')
|
||||
idx_end = data.find(b')\n', idx)
|
||||
print(data[idx:idx_end+2].decode('utf-8'))
|
||||
6
test/tools/dump_settings_target.py
Normal file
6
test/tools/dump_settings_target.py
Normal file
@ -0,0 +1,6 @@
|
||||
p = 'backend/controllers/settings.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
idx = data.find(b'if ctx.EffectiveRole() == access.RoleSuperAdmin {')
|
||||
print(data[idx:idx+150])
|
||||
8
test/tools/find_req_ver.py
Normal file
8
test/tools/find_req_ver.py
Normal file
@ -0,0 +1,8 @@
|
||||
p = 'backend/controllers/security.go'
|
||||
lines = open(p, 'rb').read().decode('utf-8').splitlines()
|
||||
for i, l in enumerate(lines):
|
||||
if 'func RequestVerification' in l:
|
||||
print(i+1, l)
|
||||
for j in range(i, i+30):
|
||||
print(lines[j])
|
||||
break
|
||||
36
test/tools/fix_remote_config_digest.py
Normal file
36
test/tools/fix_remote_config_digest.py
Normal file
@ -0,0 +1,36 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import paramiko, time
|
||||
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
|
||||
cmd = """python3 -c '
|
||||
import hashlib, json
|
||||
with open("/opt/smail/config/email.json", "rb") as f:
|
||||
raw = f.read()
|
||||
digest = hashlib.sha256(raw).hexdigest()
|
||||
|
||||
with open("/opt/smail/config/config.json", "r") as f:
|
||||
cfg = json.load(f)
|
||||
|
||||
cfg["sections"]["email"]["sha256"] = digest
|
||||
cfg["email"]["externalRecoveryEnabled"] = True
|
||||
|
||||
with open("/opt/smail/config/config.json", "w") as f:
|
||||
json.dump(cfg, f, indent=2)
|
||||
'
|
||||
"""
|
||||
stdin, stdout, stderr = ssh.exec_command(cmd)
|
||||
print("Stderr:", stderr.read().decode())
|
||||
|
||||
ssh.exec_command("cd /opt/smail && nohup ./smail -p 80 > /opt/smail/log/stdout.log 2>&1 &")
|
||||
time.sleep(2)
|
||||
|
||||
stdin, stdout, stderr = ssh.exec_command("ps -ef | grep smail")
|
||||
print("Process:\n", stdout.read().decode())
|
||||
|
||||
stdin, stdout, stderr = ssh.exec_command("tail -n 15 /opt/smail/log/stdout.log")
|
||||
print("Stdout tail:\n", stdout.read().decode())
|
||||
|
||||
ssh.close()
|
||||
0
test/tools/inspect_remote.py
Normal file
0
test/tools/inspect_remote.py
Normal file
9
test/tools/inspect_schema.py
Normal file
9
test/tools/inspect_schema.py
Normal file
@ -0,0 +1,9 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
_, stdout, _ = ssh.exec_command('sqlite3 /opt/smail/data/smail.db ".schema client_credential"')
|
||||
print(stdout.read().decode())
|
||||
_, stdout, _ = ssh.exec_command('sqlite3 /opt/smail/data/smail.db ".schema protocol_credential"')
|
||||
print(stdout.read().decode())
|
||||
ssh.close()
|
||||
12
test/tools/make_files.py
Normal file
12
test/tools/make_files.py
Normal file
@ -0,0 +1,12 @@
|
||||
import os, sys, binascii
|
||||
|
||||
mode = sys.argv[1]
|
||||
path = sys.argv[2]
|
||||
hex_data = sys.argv[3]
|
||||
|
||||
if mode == 'w':
|
||||
with open(path, 'wb') as f:
|
||||
f.write(binascii.unhexlify(hex_data))
|
||||
elif mode == 'a':
|
||||
with open(path, 'ab') as f:
|
||||
f.write(binascii.unhexlify(hex_data))
|
||||
13
test/tools/patch_delivery.py
Normal file
13
test/tools/patch_delivery.py
Normal file
@ -0,0 +1,13 @@
|
||||
p = 'backend/services/verification/delivery.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
t = 'case PurposePasswordRecover:\n\t\tpurposeText = "完成密码恢复验证"'.encode('utf-8')
|
||||
r = 'case PurposePasswordRecover:\n\t\tpurposeText = "完成密码恢复验证"\n\tcase PurposePasswordChange:\n\t\tpurposeText = "完成修改密码安全验证"'.encode('utf-8')
|
||||
|
||||
assert t in data, 'target not found in delivery.go'
|
||||
data = data.replace(t, r, 1)
|
||||
|
||||
with open(p, 'wb') as f:
|
||||
f.write(data)
|
||||
print('updated delivery.go')
|
||||
13
test/tools/patch_load_user.py
Normal file
13
test/tools/patch_load_user.py
Normal file
@ -0,0 +1,13 @@
|
||||
p = 'backend/services/recovery/service.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
t = b'func (s *Service) loadRecoverableUser(identifier string) (*models.User, error) {\n\taccount, err := auth.NormalizeIdentifier(identifier)\n\tif err != nil || account == "" {\n\t\treturn nil, ErrRecoveryDenied\n\t}\n\tvar user models.User\n\thas, err := s.engine.Where("account = ? AND disabled = 0", account).Get(&user)\n\tif err != nil || !has || user.EffectiveRole() == access.RoleSuperAdmin {\n\t\treturn nil, ErrRecoveryDenied\n\t}\n\treturn &user, nil\n}'
|
||||
r = b'func (s *Service) loadRecoverableUser(identifier string) (*models.User, error) {\n\taccount, err := auth.NormalizeIdentifier(identifier)\n\tif err != nil || account == "" {\n\t\treturn nil, ErrRecoveryDenied\n\t}\n\tvar user models.User\n\thas, err := s.engine.Where("account = ? AND disabled = 0", account).Get(&user)\n\tif err != nil || !has {\n\t\treturn nil, ErrRecoveryDenied\n\t}\n\treturn &user, nil\n}'
|
||||
|
||||
assert t in data, 'loadRecoverableUser target not found'
|
||||
data = data.replace(t, r, 1)
|
||||
|
||||
with open(p, 'wb') as f:
|
||||
f.write(data)
|
||||
print('updated loadRecoverableUser in service.go')
|
||||
25
test/tools/patch_recovery_service.py
Normal file
25
test/tools/patch_recovery_service.py
Normal file
@ -0,0 +1,25 @@
|
||||
p = 'backend/services/recovery/service.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
# 1. In Begin (around line 199)
|
||||
t1 = b'if !has || user.EffectiveRole() == access.RoleSuperAdmin {\n\t\treturn s.issueDecoyChallengeWithContext'
|
||||
r1 = b'if !has {\n\t\treturn s.issueDecoyChallengeWithContext'
|
||||
assert t1 in data, 't1 not found'
|
||||
data = data.replace(t1, r1, 1)
|
||||
|
||||
# 2. In verifyEmailIdentity (around line 259)
|
||||
t2 = b'if err != nil || !has || user.EffectiveRole() == access.RoleSuperAdmin || s.external == nil {\n\t\treturn nil, nil, ErrRecoveryDenied\n\t}'
|
||||
r2 = b'if err != nil || !has || s.external == nil {\n\t\treturn nil, nil, ErrRecoveryDenied\n\t}'
|
||||
assert t2 in data, 't2 not found'
|
||||
data = data.replace(t2, r2, 1)
|
||||
|
||||
# 3. In CompleteWithContext (around line 349)
|
||||
t3 = b'if err != nil || !has || user.Disabled != 0 || user.EffectiveRole() == access.RoleSuperAdmin || user.SessionVersion != item.SessionVersion {\n\t\treturn ErrRecoveryDenied\n\t}'
|
||||
r3 = b'if err != nil || !has || user.Disabled != 0 || user.SessionVersion != item.SessionVersion {\n\t\treturn ErrRecoveryDenied\n\t}'
|
||||
assert t3 in data, 't3 not found'
|
||||
data = data.replace(t3, r3, 1)
|
||||
|
||||
with open(p, 'wb') as f:
|
||||
f.write(data)
|
||||
print('updated service.go')
|
||||
69
test/tools/patch_settings_go.py
Normal file
69
test/tools/patch_settings_go.py
Normal file
@ -0,0 +1,69 @@
|
||||
p = 'backend/controllers/settings.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
# 1. Add imports for verification & externalemail
|
||||
t_import = b'\tgit.pcux.cn/xuans/Smail/services/auth\n'
|
||||
r_import = b'\tgit.pcux.cn/xuans/Smail/services/auth\n\tgit.pcux.cn/xuans/Smail/services/externalemail\n\tgit.pcux.cn/xuans/Smail/services/verification\n'
|
||||
assert t_import in data, 't_import not found'
|
||||
data = data.replace(t_import, r_import, 1)
|
||||
|
||||
# 2. Update modifyPasswordRequest struct
|
||||
idx_struct = data.find(b'type modifyPasswordRequest struct {')
|
||||
idx_struct_end = data.find(b'}\n\nfunc ModifyPassword', idx_struct)
|
||||
assert idx_struct != -1 and idx_struct_end != -1
|
||||
new_struct = b'''type modifyPasswordRequest struct {
|
||||
\tCurrentPassword string json:current_password
|
||||
\tPassword string json:password
|
||||
\tCode string json:code
|
||||
\tChallengeID string json:challenge_id
|
||||
}'''
|
||||
data = data[:idx_struct] + new_struct + data[idx_struct_end+1:]
|
||||
|
||||
# 3. Replace super admin block with comment
|
||||
idx_super = data.find(b'if ctx.EffectiveRole() == access.RoleSuperAdmin {')
|
||||
assert idx_super != -1, 'idx_super not found'
|
||||
idx_super_end = data.find(b'var retData modifyPasswordRequest', idx_super)
|
||||
assert idx_super_end != -1, 'idx_super_end not found'
|
||||
|
||||
r_super = '''// 无论是普通用户还是管理员、超级管理员均支持修改密码,超级管理员必须通过绑定的外部邮箱验证码进行核验
|
||||
\t'''.encode('utf-8')
|
||||
data = data[:idx_super] + r_super + data[idx_super_end:]
|
||||
|
||||
# 4. In ModifyPassword, after authenticating current password and before updating password, check code for super_admin
|
||||
t_auth_ok = b'\tloginPairAttempts.clear(pairKey)\n\tloginAccountAttempts.clear(accountKey)\n'
|
||||
r_auth_ok = '''\tloginPairAttempts.clear(pairKey)
|
||||
\tloginAccountAttempts.clear(accountKey)
|
||||
|
||||
\tif ctx.EffectiveRole() == access.RoleSuperAdmin {
|
||||
\t\truntime := currentSecurityRuntime()
|
||||
\t\tif runtime.external == nil || runtime.verification == nil {
|
||||
\t\t\tresponse.NewErrorResponse(response.ServerError, 安全验证服务不可用, ").FPrint(w)
|
||||
\t\t\treturn
|
||||
\t\t}
|
||||
\t\tbinding, err := runtime.external.FindVerifiedPrimaryByUser(ctx.UserID)
|
||||
\t\tif err != nil {
|
||||
\t\t\tresponse.NewErrorResponse(response.ParamsError, 超级管理员必须先绑定外部安全邮箱才能修改密码, ).FPrint(w)
|
||||
\t\t\treturn
|
||||
\t\t}
|
||||
\t\texternalEmail, err := runtime.external.Decrypt(*binding)
|
||||
\t\tif err != nil {
|
||||
\t\t\tresponse.NewErrorResponse(response.ServerError, 读取外部安全邮箱失败, ).FPrint(w)
|
||||
\t\t\treturn
|
||||
\t\t}
|
||||
\t\tif strings.TrimSpace(retData.Code) == || strings.TrimSpace(retData.ChallengeID) == {
|
||||
\t\t\tresponse.NewErrorResponse(response.ParamsError, 超级管理员修改密码必须提供邮箱验证码, ).FPrint(w)
|
||||
\t\t\treturn
|
||||
\t\t}
|
||||
\t\tif err := runtime.verification.Verify(retData.ChallengeID, ctx.UserAccount, externalEmail, retData.Code, verification.PurposePasswordChange, remoteIP(req)); err != nil {
|
||||
\t\t\tresponse.NewErrorResponse(response.ParamsError, 验证码错误或已过期, ).FPrint(w)
|
||||
\t\t\treturn
|
||||
\t\t}
|
||||
\t}
|
||||
'''.encode('utf-8')
|
||||
assert t_auth_ok in data, 't_auth_ok not found'
|
||||
data = data.replace(t_auth_ok, r_auth_ok, 1)
|
||||
|
||||
with open(p, 'wb') as f:
|
||||
f.write(data)
|
||||
print('settings.go patched successfully')
|
||||
17
test/tools/patch_store.py
Normal file
17
test/tools/patch_store.py
Normal file
@ -0,0 +1,17 @@
|
||||
p = 'backend/services/verification/store.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
t1 = b'PurposePasswordRecover Purpose = "password_recover"\n'
|
||||
r1 = b'PurposePasswordRecover Purpose = "password_recover"\n\tPurposePasswordChange Purpose = "password_change"\n'
|
||||
assert t1 in data
|
||||
data = data.replace(t1, r1, 1)
|
||||
|
||||
t2 = b'case PurposeLogin, PurposePasswordRecover, PurposeExternalEmailBind, PurposeExternalEmailChange:'
|
||||
r2 = b'case PurposeLogin, PurposePasswordRecover, PurposePasswordChange, PurposeExternalEmailBind, PurposeExternalEmailChange:'
|
||||
assert t2 in data
|
||||
data = data.replace(t2, r2, 1)
|
||||
|
||||
with open(p, 'wb') as f:
|
||||
f.write(data)
|
||||
print('updated store.go')
|
||||
11
test/tools/query_arbitrary.py
Normal file
11
test/tools/query_arbitrary.py
Normal file
@ -0,0 +1,11 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, type, subject, from_address, send_user_id, status FROM email ORDER BY id ASC;"')
|
||||
print("Security questions:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, count(*), sum(consumed) FROM recovery_code GROUP BY user_id;"')
|
||||
print("Recovery codes count:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email, verified FROM external_email;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
16
test/tools/query_emails.py
Normal file
16
test/tools/query_emails.py
Normal file
@ -0,0 +1,16 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, user_id, email_id, is_read, group_id, status FROM user_email ORDER BY id DESC LIMIT 10;"')
|
||||
print("Security questions:\n", stdout.read().decode("utf-8", errors="ignore").encode("utf-8"))
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, type, subject, send_user_id, status FROM email ORDER BY id DESC LIMIT 10;"')
|
||||
print("Recovery codes count:\n", stdout.read().decode("utf-8", errors="ignore").encode("utf-8"))
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email, verified FROM external_email;"')
|
||||
print("External emails:\n", stdout.read().decode("utf-8", errors="ignore").encode("utf-8"))
|
||||
ssh.close()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
11
test/tools/query_remote_details.py
Normal file
11
test/tools/query_remote_details.py
Normal file
@ -0,0 +1,11 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, question FROM security_question;"')
|
||||
print("Security questions:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, count(*), sum(consumed) FROM recovery_code GROUP BY user_id;"')
|
||||
print("Recovery codes count:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT user_id, email, verified FROM external_email;"')
|
||||
print("External emails:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
11
test/tools/query_remote_tables.py
Normal file
11
test/tools/query_remote_tables.py
Normal file
@ -0,0 +1,11 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db ".schema security_question"')
|
||||
print("Schema security_question:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db ".schema recovery_code"')
|
||||
print("Schema recovery_code:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT * FROM user;"')
|
||||
print("Users:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
9
test/tools/query_remote_testuser.py
Normal file
9
test/tools/query_remote_testuser.py
Normal file
@ -0,0 +1,9 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT * FROM security_question WHERE user_id = 2;"')
|
||||
print("Testuser2 security question:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT * FROM recovery_code WHERE user_id = 2;"')
|
||||
print("Testuser2 recovery codes:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
9
test/tools/query_remote_users.py
Normal file
9
test/tools/query_remote_users.py
Normal file
@ -0,0 +1,9 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db "SELECT id, account, name, role, is_admin, disabled FROM user;"')
|
||||
print("Users in DB:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('sqlite3 /opt/smail/data/smail.db ".tables"')
|
||||
print("Tables in DB:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
22
test/tools/run_check_ext.py
Normal file
22
test/tools/run_check_ext.py
Normal file
@ -0,0 +1,22 @@
|
||||
import sys, io
|
||||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''
|
||||
import sqlite3
|
||||
conn = sqlite3.connect('/opt/smail/data/smail.db')
|
||||
c = conn.cursor()
|
||||
c.execute("SELECT id, user_id, status, email_type, created_at, updated_at FROM external_email")
|
||||
print("external_email rows:", c.fetchall())
|
||||
'''
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/check_external_email.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
|
||||
_, out, err = ssh.exec_command('python3 /tmp/check_external_email.py')
|
||||
print('OUT:\n', out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
23
test/tools/run_check_hex.py
Normal file
23
test/tools/run_check_hex.py
Normal file
@ -0,0 +1,23 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''
|
||||
import sqlite3
|
||||
|
||||
conn = sqlite3.connect('/opt/smail/data/smail.db')
|
||||
c = conn.cursor()
|
||||
|
||||
c.execute('SELECT id, user_id, quote(client_name) FROM client_credential')
|
||||
for row in c.fetchall():
|
||||
print('Hex Client credential:', row[0], row[1], row[2])
|
||||
'''
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/check_hex.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
|
||||
_, out, err = ssh.exec_command('python3 /tmp/check_hex.py')
|
||||
print('OUT:\n', out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
30
test/tools/run_check_hex2.py
Normal file
30
test/tools/run_check_hex2.py
Normal file
@ -0,0 +1,30 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''
|
||||
import sqlite3
|
||||
|
||||
conn = sqlite3.connect('/opt/smail/data/smail.db')
|
||||
c = conn.cursor()
|
||||
|
||||
c.execute('SELECT id, user_id, hex(client_name) FROM client_credential')
|
||||
for row in c.fetchall():
|
||||
h = row[2]
|
||||
raw_bytes = bytes.fromhex(h)
|
||||
print('Raw hex:', row[0], row[1], h)
|
||||
for enc in ['utf-8', 'gbk', 'gb2312', 'latin1']:
|
||||
try:
|
||||
print(f' {enc}: {raw_bytes.decode(enc)}')
|
||||
except Exception as e:
|
||||
pass
|
||||
'''
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/check_hex2.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
|
||||
_, out, err = ssh.exec_command('python3 /tmp/check_hex2.py')
|
||||
print('OUT:\n', out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
10
test/tools/run_check_hex3.py
Normal file
10
test/tools/run_check_hex3.py
Normal file
@ -0,0 +1,10 @@
|
||||
import sys, io
|
||||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
_, out, err = ssh.exec_command('python3 /tmp/check_hex2.py')
|
||||
print('OUT:\n', out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
28
test/tools/run_check_logs.py
Normal file
28
test/tools/run_check_logs.py
Normal file
@ -0,0 +1,28 @@
|
||||
import sys, io
|
||||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''
|
||||
import sqlite3
|
||||
conn = sqlite3.connect('/opt/smail/data/smail.db')
|
||||
c = conn.cursor()
|
||||
c.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
||||
print('Tables:', [r[0] for r in c.fetchall()])
|
||||
|
||||
c.execute("SELECT * FROM operation_log ORDER BY id DESC LIMIT 5;")
|
||||
print('Recent op logs:', c.fetchall())
|
||||
|
||||
c.execute("SELECT * FROM mail_log ORDER BY id DESC LIMIT 5;")
|
||||
print('Recent mail logs:', c.fetchall())
|
||||
'''
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/check_logs.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
|
||||
_, out, err = ssh.exec_command('python3 /tmp/check_logs.py')
|
||||
print('OUT:\n', out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
27
test/tools/run_check_names.py
Normal file
27
test/tools/run_check_names.py
Normal file
@ -0,0 +1,27 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''
|
||||
import sqlite3
|
||||
|
||||
conn = sqlite3.connect('/opt/smail/data/smail.db')
|
||||
c = conn.cursor()
|
||||
|
||||
c.execute('SELECT id, user_id, client_name FROM client_credential')
|
||||
for row in c.fetchall():
|
||||
print('Client credential:', row[0], row[1], repr(row[2]))
|
||||
|
||||
c.execute('SELECT id, account, name, role FROM user')
|
||||
for row in c.fetchall():
|
||||
print('User:', row)
|
||||
'''
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/check_names.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
|
||||
_, out, err = ssh.exec_command('python3 /tmp/check_names.py')
|
||||
print('OUT:\n', out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
31
test/tools/run_check_users.py
Normal file
31
test/tools/run_check_users.py
Normal file
@ -0,0 +1,31 @@
|
||||
import sys, io
|
||||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''
|
||||
import sqlite3
|
||||
conn = sqlite3.connect('/opt/smail/data/smail.db')
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT id, hex(account), hex(name), role FROM user')
|
||||
for r in c.fetchall():
|
||||
print('User ID:', r[0], 'Role:', r[3])
|
||||
try:
|
||||
print(' account:', bytes.fromhex(r[1]).decode('utf-8'))
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
print(' name:', bytes.fromhex(r[2]).decode('utf-8'))
|
||||
except:
|
||||
pass
|
||||
'''
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/check_users.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
|
||||
_, out, err = ssh.exec_command('python3 /tmp/check_users.py')
|
||||
print('OUT:\n', out.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
70
test/tools/run_decrypt_creds.py
Normal file
70
test/tools/run_decrypt_creds.py
Normal file
@ -0,0 +1,70 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''
|
||||
import sqlite3, base64, glob
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||
|
||||
key_files = glob.glob('/opt/smail/ssl/**/credential.key', recursive=True) + glob.glob('/opt/smail/config/**/credential.key', recursive=True)
|
||||
print('Key files:', key_files)
|
||||
if not key_files:
|
||||
exit(1)
|
||||
|
||||
with open(key_files[0], 'rb') as f:
|
||||
key = f.read()
|
||||
|
||||
aesgcm = AESGCM(key)
|
||||
|
||||
conn = sqlite3.connect('/opt/smail/data/smail.db')
|
||||
c = conn.cursor()
|
||||
|
||||
print('=== CLIENT CREDENTIALS ===')
|
||||
c.execute('SELECT id, user_id, client_name, code_ciphertext, revoked_at, last_used_at, created_at FROM client_credential')
|
||||
for row in c.fetchall():
|
||||
cid, uid, cname, enc_code, revoked, last_used, created = row
|
||||
if not enc_code:
|
||||
print(f'ID {cid} (UID {uid}, {cname}): None (revoked={revoked})')
|
||||
continue
|
||||
parts = enc_code.split(':')
|
||||
b64 = parts[2]
|
||||
padded = b64 + '=' * (-len(b64) % 4)
|
||||
raw = base64.urlsafe_b64decode(padded)
|
||||
nonce = raw[:12]
|
||||
ct = raw[12:]
|
||||
try:
|
||||
pt = aesgcm.decrypt(nonce, ct, b'smail-config-v1')
|
||||
print(f'ID {cid} (UID {uid}, {cname}): {pt.decode()} | created={created} | revoked={revoked} | last_used={last_used}')
|
||||
except Exception as e:
|
||||
print(f'ID {cid} decrypt err: {e}')
|
||||
|
||||
print('=== PROTOCOL CREDENTIALS ===')
|
||||
c.execute('SELECT id, user_id, protocol, enabled, code_ciphertext, last_used_at, created_at FROM protocol_credential')
|
||||
for row in c.fetchall():
|
||||
pid, uid, proto, enabled, enc_code, last_used, created = row
|
||||
if not enc_code:
|
||||
print(f'Proto {proto} (UID {uid}, enabled={enabled}): None')
|
||||
continue
|
||||
parts = enc_code.split(':')
|
||||
b64 = parts[2]
|
||||
padded = b64 + '=' * (-len(b64) % 4)
|
||||
raw = base64.urlsafe_b64decode(padded)
|
||||
nonce = raw[:12]
|
||||
ct = raw[12:]
|
||||
try:
|
||||
pt = aesgcm.decrypt(nonce, ct, b'smail-config-v1')
|
||||
print(f'Proto {proto} (UID {uid}, enabled={enabled}): {pt.decode()} | created={created} | last_used={last_used}')
|
||||
except Exception as e:
|
||||
print(f'Proto {proto} decrypt err: {e}')
|
||||
'''
|
||||
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/decrypt_creds.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
|
||||
_, out, err = ssh.exec_command('python3 /tmp/decrypt_creds.py')
|
||||
print('OUT:\n', out.read().decode('utf-8', errors='ignore'))
|
||||
print('ERR:\n', err.read().decode('utf-8', errors='ignore'))
|
||||
ssh.close()
|
||||
146
test/tools/run_forgot_password_acceptance.py
Normal file
146
test/tools/run_forgot_password_acceptance.py
Normal file
@ -0,0 +1,146 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os, sys, time, json
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
output_dir = os.path.join(os.getcwd(), 'test-log', '2026-09-14', '截图')
|
||||
log_file = os.path.join(os.getcwd(), 'test-log', '2026-09-14', '测试log', 'forgot_password_acceptance.log')
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
||||
|
||||
def write_log(msg):
|
||||
ts = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
line = f'[{ts}] {msg}'
|
||||
print(line)
|
||||
with open(log_file, 'a', encoding='utf-8') as f:
|
||||
f.write(line + '\n')
|
||||
|
||||
with open('test/tools/testuser2_credential_info.json', 'r', encoding='utf-8') as f:
|
||||
cred = json.load(f)
|
||||
|
||||
account = cred['account']
|
||||
question = cred['question']
|
||||
answer = cred['answer']
|
||||
# 使用尚未被消费的第 2 个恢复码
|
||||
code = cred['codes'][1]
|
||||
new_pwd = 'Reset888!'
|
||||
|
||||
write_log('====================================================')
|
||||
write_log('=== 开始普通用户忘记密码(离线恢复方式)端到端真实验收测试 ===')
|
||||
write_log('====================================================')
|
||||
write_log(f'目标测试账号: {account}')
|
||||
write_log(f'密保问题: {question}')
|
||||
write_log(f'密保答案: {answer}')
|
||||
write_log(f'离线恢复码: {code}')
|
||||
write_log(f'设定的新密码: {new_pwd}')
|
||||
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch(
|
||||
headless=True,
|
||||
args=[
|
||||
'--no-sandbox',
|
||||
'--ignore-certificate-errors',
|
||||
'--host-resolver-rules=MAP mail.solit.cn 47.108.78.12, MAP solit.cn 47.108.78.12'
|
||||
]
|
||||
)
|
||||
context = browser.new_context(viewport={'width': 1440, 'height': 900}, ignore_https_errors=True)
|
||||
page = context.new_page()
|
||||
|
||||
# 1. 访问登录页面
|
||||
write_log('1. 打开登录页面 https://mail.solit.cn/#/login')
|
||||
page.goto('https://mail.solit.cn/#/login', wait_until='networkidle')
|
||||
page.wait_for_timeout(1000)
|
||||
page.screenshot(path=os.path.join(output_dir, '20_login_page.png'))
|
||||
|
||||
# 2. 点击「忘记密码?」
|
||||
forgot_btn = page.locator('.forgot-password-btn')
|
||||
write_log(f' 登录页检测到【忘记密码?】入口数量: {forgot_btn.count()}')
|
||||
forgot_btn.click()
|
||||
page.wait_for_timeout(1000)
|
||||
write_log(f' 点击后跳转 URL: {page.url}')
|
||||
page.screenshot(path=os.path.join(output_dir, '21_forgot_password_page.png'))
|
||||
|
||||
# 3. 检查安全警示信息
|
||||
alert_box = page.locator('.reset-boundary')
|
||||
if alert_box.count() > 0:
|
||||
write_log(f' 超级管理员限制警示框: {alert_box.inner_text().strip()}')
|
||||
|
||||
# 4. 切换到「离线恢复方式」
|
||||
switch_btn = page.locator('.switch-button')
|
||||
write_log(f' 当前模式切换按钮文案: {switch_btn.inner_text().strip()}')
|
||||
switch_btn.click()
|
||||
page.wait_for_timeout(500)
|
||||
write_log(f' 切换后切换按钮文案: {switch_btn.inner_text().strip()}')
|
||||
page.screenshot(path=os.path.join(output_dir, '22_switched_to_offline.png'))
|
||||
|
||||
# 5. 填写离线恢复表单各项
|
||||
write_log('2. 填写离线密码恢复表单项...')
|
||||
inputs = page.locator('.recovery-form input')
|
||||
inputs.nth(0).fill(account)
|
||||
inputs.nth(1).fill(question)
|
||||
inputs.nth(2).fill(answer)
|
||||
inputs.nth(3).fill(code)
|
||||
|
||||
page.wait_for_timeout(500)
|
||||
page.screenshot(path=os.path.join(output_dir, '23_step1_form_completed.png'))
|
||||
write_log(' 步骤 1 身份核验表单填写完毕,点击【下一步】')
|
||||
|
||||
# 提交步骤 1
|
||||
step1_btn = page.locator('.recovery-footer button.recovery-action-btn')
|
||||
step1_btn.click()
|
||||
|
||||
page.wait_for_timeout(2500)
|
||||
page.screenshot(path=os.path.join(output_dir, '24_step2_reset_password.png'))
|
||||
|
||||
write_log('3. 进入步骤 2:设置新密码,填写密码...')
|
||||
step2_inputs = page.locator('.password-grid-vertical input')
|
||||
step2_inputs.nth(0).fill(new_pwd)
|
||||
step2_inputs.nth(1).fill(new_pwd)
|
||||
page.wait_for_timeout(500)
|
||||
page.screenshot(path=os.path.join(output_dir, '25_step2_filled.png'))
|
||||
|
||||
write_log(' 步骤 2 密码填写完毕,点击【下一步】提交重置...')
|
||||
step2_btn = page.locator('.step-content .recovery-footer button.recovery-action-btn')
|
||||
step2_btn.click()
|
||||
|
||||
page.wait_for_timeout(2500)
|
||||
page.screenshot(path=os.path.join(output_dir, '26_step3_success.png'))
|
||||
write_log('4. 进入步骤 3:完成重置,点击【重新登录】')
|
||||
relogin_btn = page.locator('.step-success button.recovery-action-btn')
|
||||
relogin_btn.click()
|
||||
page.wait_for_timeout(2000)
|
||||
|
||||
msg_items = page.locator('.el-message')
|
||||
for i in range(msg_items.count()):
|
||||
write_log(f' 页面通知消息: {msg_items.nth(i).inner_text().strip()}')
|
||||
|
||||
write_log(f' 重置后当前页面 URL: {page.url}')
|
||||
|
||||
# 7. 自动回到登录页后,使用新密码执行登录验证
|
||||
if '#/login' in page.url:
|
||||
write_log('4. 已成功自动返回登录页,开始验证新密码登入...')
|
||||
# 页面重新加载后输入
|
||||
page.wait_for_timeout(1000)
|
||||
login_user = page.locator('.login-form input[type=\"text\"], input[placeholder*=\"账号\"], input[placeholder*=\"用户名\"]').first
|
||||
login_pwd = page.locator('.login-form input[type=\"password\"], input[placeholder*=\"密码\"]').first
|
||||
|
||||
login_user.fill(account)
|
||||
login_pwd.fill(new_pwd)
|
||||
page.screenshot(path=os.path.join(output_dir, '25_input_new_password.png'))
|
||||
|
||||
login_submit = page.locator('.login-button, button:has-text(\"登录\")').first
|
||||
login_submit.click()
|
||||
page.wait_for_timeout(3500)
|
||||
|
||||
write_log(f' 新密码登录后 URL: {page.url}')
|
||||
page.screenshot(path=os.path.join(output_dir, '26_logged_in_success.png'))
|
||||
|
||||
# 检查是否成功进入邮箱主界面
|
||||
if '#/' in page.url and '#/login' not in page.url:
|
||||
write_log(' [SUCCESS] 新密码验证成功!顺利进入系统主界面。')
|
||||
else:
|
||||
write_log(' [WARNING] 登录后未进入预期邮箱主页,请检查。')
|
||||
else:
|
||||
write_log(f' [FAILED] 重置后未返回登录页,当前停留于: {page.url}')
|
||||
|
||||
browser.close()
|
||||
write_log('=== 忘记密码全流程端到端测试结束 ===')
|
||||
32
test/tools/send_test_email.py
Normal file
32
test/tools/send_test_email.py
Normal file
@ -0,0 +1,32 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect('47.108.78.12', port=22, username='root', password='10102442Lb!', timeout=10)
|
||||
|
||||
s = '''import smtplib, ssl
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
for target, subj in [
|
||||
('gfxezc@163.com', 'Smail DirectMail Test - Admin'),
|
||||
('xuanspark@163.com', 'Smail DirectMail Test - User'),
|
||||
('likb_blue@163.com', 'Smail DirectMail Test - SuperAdmin')
|
||||
]:
|
||||
msg = MIMEText('Smail DirectMail test outbound delivery', 'plain', 'utf-8')
|
||||
msg['From'] = 'xuans@solit.cn'
|
||||
msg['To'] = target
|
||||
msg['Subject'] = subj
|
||||
|
||||
context = ssl.create_default_context()
|
||||
with smtplib.SMTP_SSL('smtpdm.aliyun.com', 465, context=context) as server:
|
||||
server.login('xuans@solit.cn', 'Solit12ABcd')
|
||||
res = server.sendmail('xuans@solit.cn', [target], msg.as_string())
|
||||
print(f'Send to {target}: {res}')
|
||||
'''
|
||||
sftp = ssh.open_sftp()
|
||||
with sftp.file('/tmp/test_send_all.py', 'w') as f:
|
||||
f.write(s)
|
||||
sftp.close()
|
||||
_, out, err = ssh.exec_command('python3 /tmp/test_send_all.py')
|
||||
print('OUT:\n', out.read().decode())
|
||||
print('ERR:\n', err.read().decode())
|
||||
ssh.close()
|
||||
51
test/tools/test_client_credentials.py
Normal file
51
test/tools/test_client_credentials.py
Normal file
@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import urllib.request, ssl, json, http.cookiejar
|
||||
|
||||
ctx = ssl.create_default_context()
|
||||
ctx.check_hostname = False
|
||||
ctx.verify_mode = ssl.CERT_NONE
|
||||
cj = http.cookiejar.CookieJar()
|
||||
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx), urllib.request.HTTPCookieProcessor(cj))
|
||||
|
||||
# 1. 登录 xuans
|
||||
login_payload = json.dumps({'account': 'xuans', 'password': '12345678'}).encode('utf-8')
|
||||
req = urllib.request.Request('https://47.108.78.12/api/v1/login', data=login_payload, headers={
|
||||
'Content-Type': 'application/json',
|
||||
'Host': 'mail.solit.cn',
|
||||
'Origin': 'https://mail.solit.cn'
|
||||
})
|
||||
resp = opener.open(req, timeout=5)
|
||||
print('Login resp:', resp.read().decode())
|
||||
|
||||
csrf_token = ''
|
||||
for c in cj:
|
||||
if c.name == 'smail_csrf':
|
||||
csrf_token = c.value
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Host': 'mail.solit.cn',
|
||||
'Origin': 'https://mail.solit.cn',
|
||||
'Referer': 'https://mail.solit.cn/',
|
||||
'X-CSRF-Token': csrf_token
|
||||
}
|
||||
|
||||
# 2. 读取客户端授权码列表
|
||||
req_list = urllib.request.Request('https://47.108.78.12/api/v1/security/client-credentials/list', headers=headers)
|
||||
try:
|
||||
r_list = opener.open(req_list, timeout=5)
|
||||
print('List client credentials:\n', r_list.read().decode())
|
||||
except urllib.error.HTTPError as e:
|
||||
print('List error:', e.code, e.read().decode())
|
||||
|
||||
# 3. 输入密码验密 reveal 授权码明文
|
||||
req_reveal = urllib.request.Request(
|
||||
'https://47.108.78.12/api/v1/security/client-credentials/reveal',
|
||||
data=json.dumps({'password': '12345678'}).encode('utf-8'),
|
||||
headers=headers
|
||||
)
|
||||
try:
|
||||
r_reveal = opener.open(req_reveal, timeout=5)
|
||||
print('Reveal client credentials:\n', r_reveal.read().decode())
|
||||
except urllib.error.HTTPError as e:
|
||||
print('Reveal error:', e.code, e.read().decode())
|
||||
8
test/tools/test_echo.py
Normal file
8
test/tools/test_echo.py
Normal file
@ -0,0 +1,8 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command("echo HELLO")
|
||||
print(stdout:, stdout.read().decode())
|
||||
print(stderr:, stderr.read().decode())
|
||||
ssh.close()
|
||||
40
test/tools/test_edit_user_pwd.py
Normal file
40
test/tools/test_edit_user_pwd.py
Normal file
@ -0,0 +1,40 @@
|
||||
import urllib.request, ssl, json, http.cookiejar
|
||||
ctx = ssl.create_default_context()
|
||||
ctx.check_hostname = False
|
||||
ctx.verify_mode = ssl.CERT_NONE
|
||||
cj = http.cookiejar.CookieJar()
|
||||
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx), urllib.request.HTTPCookieProcessor(cj))
|
||||
|
||||
# 登录 xuans
|
||||
login_payload = json.dumps({"account": "xuans", "password": "12345678"}).encode("utf-8")
|
||||
req = urllib.request.Request("https://47.108.78.12/api/v1/login", data=login_payload, headers={
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn"
|
||||
})
|
||||
opener.open(req, timeout=5)
|
||||
|
||||
csrf_token = ""
|
||||
for c in cj:
|
||||
if c.name == "smail_csrf":
|
||||
csrf_token = c.value
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn",
|
||||
"Referer": "https://mail.solit.cn/",
|
||||
"X-CSRF-Token": csrf_token
|
||||
}
|
||||
|
||||
edit_payload = json.dumps({
|
||||
"id": 2,
|
||||
"account": "testuser2",
|
||||
"username": "普通用户",
|
||||
"password": "Password123!",
|
||||
"disabled": 0,
|
||||
"role": "user"
|
||||
}).encode("utf-8")
|
||||
req_edit = urllib.request.Request("https://47.108.78.12/api/v1/user/edit", data=edit_payload, headers=headers)
|
||||
resp_edit = opener.open(req_edit, timeout=5)
|
||||
print("Edit user response:", resp_edit.read().decode())
|
||||
7
test/tools/test_find_req.py
Normal file
7
test/tools/test_find_req.py
Normal file
@ -0,0 +1,7 @@
|
||||
p = 'backend/controllers/security.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
idx = data.find(b'func RequestVerification')
|
||||
idx_end = data.find(b'// VerifyVerification', idx)
|
||||
print(data[idx:idx_end].decode('utf-8'))
|
||||
91
test/tools/test_forgot_password_e2e.py
Normal file
91
test/tools/test_forgot_password_e2e.py
Normal file
@ -0,0 +1,91 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os, sys, time, json
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
output_dir = os.path.join(os.getcwd(), 'test-log', '2026-09-14', '截图')
|
||||
log_file = os.path.join(os.getcwd(), 'test-log', '2026-09-14', '测试log', 'forgot_password_acceptance.log')
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
||||
|
||||
def write_log(msg):
|
||||
ts = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
line = f'[{ts}] {msg}'
|
||||
print(line)
|
||||
with open(log_file, 'a', encoding='utf-8') as f:
|
||||
f.write(line + '\n')
|
||||
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch(
|
||||
headless=True,
|
||||
args=[
|
||||
'--no-sandbox',
|
||||
'--ignore-certificate-errors',
|
||||
'--host-resolver-rules=MAP mail.solit.cn 47.108.78.12, MAP solit.cn 47.108.78.12'
|
||||
]
|
||||
)
|
||||
context = browser.new_context(viewport={'width': 1440, 'height': 900}, ignore_https_errors=True)
|
||||
page = context.new_page()
|
||||
|
||||
write_log('=== 开始忘记密码全流程测试 ===')
|
||||
|
||||
# 1. 登录页检查忘记密码入口
|
||||
write_log('1. 打开登录页面')
|
||||
page.goto('https://mail.solit.cn/#/login', wait_until='networkidle')
|
||||
page.screenshot(path=os.path.join(output_dir, '10_login_page_forgot_btn.png'))
|
||||
|
||||
forgot_btn = page.locator('button.forgot-password-btn, a:has-text("忘记密码")')
|
||||
write_log(f' 忘记密码入口数量: {forgot_btn.count()}')
|
||||
if forgot_btn.count() > 0:
|
||||
forgot_btn.first.click()
|
||||
page.wait_for_timeout(1500)
|
||||
else:
|
||||
page.goto('https://mail.solit.cn/#/forgot-password', wait_until='networkidle')
|
||||
page.wait_for_timeout(1500)
|
||||
|
||||
write_log(f' 当前页面 URL: {page.url}')
|
||||
page.screenshot(path=os.path.join(output_dir, '11_forgot_password_page.png'))
|
||||
|
||||
# 2. 检查页面关键元素与边界提示
|
||||
boundary_alert = page.locator('.reset-boundary')
|
||||
write_log(f' 超级管理员限制警示框存在: {boundary_alert.count() > 0}')
|
||||
if boundary_alert.count() > 0:
|
||||
write_log(f' 警示内容: {boundary_alert.inner_text().strip()}')
|
||||
|
||||
# 3. 检查切换验证方式
|
||||
switch_btn = page.locator('.switch-button')
|
||||
write_log(f' 切换方式按钮文本: {switch_btn.inner_text().strip()}')
|
||||
switch_btn.click()
|
||||
page.wait_for_timeout(500)
|
||||
write_log(f' 切换后按钮文本: {switch_btn.inner_text().strip()}')
|
||||
page.screenshot(path=os.path.join(output_dir, '12_switched_to_offline.png'))
|
||||
|
||||
# 4. 测试以超级管理员账号 xuans 进行离线恢复(验证安全拒绝边界)
|
||||
write_log('2. 测试超级管理员账号 xuans 在 Web 端重置(必须被安全拒绝)')
|
||||
user_input = page.locator('.recovery-form input').nth(0)
|
||||
user_input.fill('xuans')
|
||||
|
||||
# 离线输入:密保问题、密保答案、恢复码、新密码、确认密码
|
||||
q_input = page.locator('.recovery-form input').nth(1)
|
||||
a_input = page.locator('.recovery-form input').nth(2)
|
||||
code_input = page.locator('.recovery-form input').nth(3)
|
||||
p1_input = page.locator('.recovery-form input').nth(4)
|
||||
p2_input = page.locator('.recovery-form input').nth(5)
|
||||
|
||||
q_input.fill('幸运数字')
|
||||
a_input.fill('8')
|
||||
code_input.fill('REC-1234-5678')
|
||||
p1_input.fill('NewPass1234!')
|
||||
p2_input.fill('NewPass1234!')
|
||||
|
||||
submit_btn = page.locator('.recovery-footer button.recovery-action-btn')
|
||||
submit_btn.click()
|
||||
page.wait_for_timeout(2000)
|
||||
page.screenshot(path=os.path.join(output_dir, '13_superadmin_denied.png'))
|
||||
|
||||
state_span = page.locator('.verification-state')
|
||||
msg_box = page.locator('.el-message')
|
||||
write_log(f' 状态文本: {state_span.inner_text() if state_span.count() > 0 else "无"}')
|
||||
for i in range(msg_box.count()):
|
||||
write_log(f' 提示消息: {msg_box.nth(i).inner_text().strip()}')
|
||||
|
||||
browser.close()
|
||||
35
test/tools/test_generate_codes_api.py
Normal file
35
test/tools/test_generate_codes_api.py
Normal file
@ -0,0 +1,35 @@
|
||||
import urllib.request, ssl, json, http.cookiejar
|
||||
ctx = ssl.create_default_context()
|
||||
ctx.check_hostname = False
|
||||
ctx.verify_mode = ssl.CERT_NONE
|
||||
cj = http.cookiejar.CookieJar()
|
||||
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx), urllib.request.HTTPCookieProcessor(cj))
|
||||
|
||||
# 登录 xuans
|
||||
login_payload = json.dumps({"account": "xuans", "password": "12345678"}).encode("utf-8")
|
||||
req = urllib.request.Request("https://47.108.78.12/api/v1/login", data=login_payload, headers={
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn"
|
||||
})
|
||||
opener.open(req, timeout=5)
|
||||
|
||||
csrf_token = ""
|
||||
for c in cj:
|
||||
if c.name == "smail_csrf":
|
||||
csrf_token = c.value
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn",
|
||||
"Referer": "https://mail.solit.cn/",
|
||||
"X-CSRF-Token": csrf_token
|
||||
}
|
||||
|
||||
# 调用管理接口为 testuser2 (id=2) 生成 recovery codes
|
||||
gen_payload = json.dumps({"user_id": 2, "count": 5}).encode("utf-8")
|
||||
req_gen = urllib.request.Request("https://47.108.78.12/api/v1/user/security/recovery-codes/generate", data=gen_payload, headers=headers)
|
||||
resp_gen = opener.open(req_gen, timeout=5)
|
||||
data = json.loads(resp_gen.read().decode("utf-8"))
|
||||
print("Generate recovery codes response:", data)
|
||||
17
test/tools/test_inbound_info.py
Normal file
17
test/tools/test_inbound_info.py
Normal file
@ -0,0 +1,17 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
stdin, stdout, stderr = ssh.exec_command('python3 -c "import sqlite3; conn=sqlite3.connect(\x27/opt/smail/data/smail.db\x27); c=conn.cursor(); print(c.execute(\x27SELECT id, user_id, email_id, is_read, is_trash FROM user_email ORDER BY id DESC LIMIT 5\x27).fetchall())"')
|
||||
print("User emails top 5:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('python3 -c "import sqlite3; conn=sqlite3.connect(\x27/opt/smail/data/smail.db\x27); c=conn.cursor(); print(c.execute(\x27SELECT id, subject, send_time FROM email ORDER BY id DESC LIMIT 5\x27).fetchall())"')
|
||||
print("Emails top 5:\n", stdout.read().decode())
|
||||
stdin, stdout, stderr = ssh.exec_command('python3 -c "import sqlite3; conn=sqlite3.connect(\x27/opt/smail/data/smail.db\x27); c=conn.cursor(); print(c.execute(\x27SELECT id, name, user_name FROM user\x27).fetchall())"')
|
||||
print("Users:\n", stdout.read().decode())
|
||||
ssh.close()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
9
test/tools/test_list_ext.py
Normal file
9
test/tools/test_list_ext.py
Normal file
@ -0,0 +1,9 @@
|
||||
p = 'backend/controllers/security.go'
|
||||
with open(p, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
idx = data.find(b'external-email/list')
|
||||
if idx == -1:
|
||||
idx = data.find(b'external-email')
|
||||
print('idx:', idx)
|
||||
print(data[idx-100:idx+400].decode('utf-8', errors='replace'))
|
||||
0
test/tools/test_roles_ui.py
Normal file
0
test/tools/test_roles_ui.py
Normal file
0
test/tools/test_smtpdm_ports.py
Normal file
0
test/tools/test_smtpdm_ports.py
Normal file
20
test/tools/test_testuser2_login.py
Normal file
20
test/tools/test_testuser2_login.py
Normal file
@ -0,0 +1,20 @@
|
||||
import urllib.request, ssl, json, http.cookiejar
|
||||
ctx = ssl.create_default_context()
|
||||
ctx.check_hostname = False
|
||||
ctx.verify_mode = ssl.CERT_NONE
|
||||
cj = http.cookiejar.CookieJar()
|
||||
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx), urllib.request.HTTPCookieProcessor(cj))
|
||||
|
||||
for pwd in ["12345678", "123456", "testuser2", "Password123!", "Tester123456!"]:
|
||||
login_payload = json.dumps({"account": "testuser2", "password": pwd}).encode("utf-8")
|
||||
req = urllib.request.Request("https://47.108.78.12/api/v1/login", data=login_payload, headers={
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn"
|
||||
})
|
||||
try:
|
||||
resp = opener.open(req, timeout=5)
|
||||
res = json.loads(resp.read().decode())
|
||||
print(f"Pwd {pwd} -> errorNo: {res.get('errorNo')}, errorMsg: {res.get('errorMsg')}")
|
||||
except Exception as e:
|
||||
print(f"Pwd {pwd} -> exception: {e}")
|
||||
51
test/tools/test_validate_api.py
Normal file
51
test/tools/test_validate_api.py
Normal file
@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import requests, json
|
||||
import urllib3
|
||||
urllib3.disable_warnings()
|
||||
|
||||
s = requests.Session()
|
||||
# 47.108.78.12 80 端口
|
||||
r_login = s.post('http://47.108.78.12:80/api/login', json={'username': 'xuans', 'password': '12345678'})
|
||||
print('Login:', r_login.status_code, r_login.text)
|
||||
|
||||
r_get = s.get('http://47.108.78.12:80/api/v1/config/mail')
|
||||
print('Get mail:', r_get.status_code, r_get.text)
|
||||
|
||||
# 模拟前端发送的 validate 请求
|
||||
data = r_get.json().get('data', {})
|
||||
generation = data.get('meta', {}).get('generation', 2)
|
||||
|
||||
# 用户在前端填入的配置
|
||||
payload = json.loads(json.dumps(data))
|
||||
# 注意前端 parsePayload 会对 payload 做一些处理:
|
||||
# delete payload.relayPasswordStatus
|
||||
# if (payload.relay && 'password' in payload.relay) delete payload.relay.password
|
||||
if 'relayPasswordStatus' in payload:
|
||||
del payload['relayPasswordStatus']
|
||||
if 'relay' in payload and 'password' in payload['relay']:
|
||||
del payload['relay']['password']
|
||||
|
||||
payload['relay']['relayHost'] = 'smtpm.aliyun.com'
|
||||
payload['relay']['relayPort'] = 587
|
||||
payload['relay']['username'] = 'xuans'
|
||||
payload['relay']['from'] = 'xuans@solit.cn'
|
||||
payload['relay']['fromDomain'] = 'solit.cn'
|
||||
|
||||
secretChanges = {
|
||||
'relay.password': {'operation': 'replace', 'value': {'plaintext': 'Solit12ABcd'}}
|
||||
}
|
||||
|
||||
req_body = {
|
||||
'expectedGeneration': generation,
|
||||
'payload': payload,
|
||||
'secretChanges': secretChanges,
|
||||
'confirmation': {'confirmed': True, 'method': 'recent_auth'}
|
||||
}
|
||||
|
||||
print('Testing validate with smtpm.aliyun.com...')
|
||||
r_val = s.post('http://47.108.78.12:80/api/v1/config/mail/validate', json=req_body)
|
||||
print('Validate res:', r_val.status_code, r_val.text)
|
||||
|
||||
# 再测一次直接 PUT (UpdateConfig)
|
||||
r_put = s.put('http://47.108.78.12:80/api/v1/config/mail', json=req_body)
|
||||
print('PUT res:', r_put.status_code, r_put.text)
|
||||
14
test/tools/test_validate_remote.py
Normal file
14
test/tools/test_validate_remote.py
Normal file
@ -0,0 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import requests
|
||||
import urllib3
|
||||
urllib3.disable_warnings()
|
||||
|
||||
session = requests.Session()
|
||||
login_res = session.post('https://mail.solit.cn/api/login', json={'username': 'xuans', 'password': '12345678'}, verify=False)
|
||||
print('Login status:', login_res.status_code, login_res.text)
|
||||
|
||||
get_res = session.get('https://mail.solit.cn/api/v1/config/mail', verify=False)
|
||||
print('Config mail get:', get_res.status_code, get_res.text)
|
||||
|
||||
status_res = session.get('https://mail.solit.cn/api/v1/config/status', verify=False)
|
||||
print('Config status:', status_res.status_code, status_res.text)
|
||||
13
test/tools/testuser2_credential_info.json
Normal file
13
test/tools/testuser2_credential_info.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"account": "testuser2",
|
||||
"current_password": "UserPass88!",
|
||||
"question": "毕业学校",
|
||||
"answer": "清华大学",
|
||||
"codes": [
|
||||
"IGWRL-ZC7BC-L52E7IH7NQ",
|
||||
"MFHHG-C5ERA-U2JP3OYSCQ",
|
||||
"5WGFS-MANVR-S7YFSTRGCA",
|
||||
"YFOJ3-HK237-Y4MUBIC5GA",
|
||||
"4TUAM-6TEJN-PCEHCBISVA"
|
||||
]
|
||||
}
|
||||
60
test/tools/testuser2_setup_security.py
Normal file
60
test/tools/testuser2_setup_security.py
Normal file
@ -0,0 +1,60 @@
|
||||
import urllib.request, ssl, json, http.cookiejar
|
||||
ctx = ssl.create_default_context()
|
||||
ctx.check_hostname = False
|
||||
ctx.verify_mode = ssl.CERT_NONE
|
||||
cj = http.cookiejar.CookieJar()
|
||||
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx), urllib.request.HTTPCookieProcessor(cj))
|
||||
|
||||
# 1. 登录 testuser2
|
||||
login_payload = json.dumps({"account": "testuser2", "password": "Password123!"}).encode("utf-8")
|
||||
req = urllib.request.Request("https://47.108.78.12/api/v1/login", data=login_payload, headers={
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn"
|
||||
})
|
||||
resp = opener.open(req, timeout=5)
|
||||
data = json.loads(resp.read().decode())
|
||||
print("Testuser2 login resp:", data)
|
||||
|
||||
csrf_token = ""
|
||||
for c in cj:
|
||||
if c.name == "smail_csrf":
|
||||
csrf_token = c.value
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn",
|
||||
"Referer": "https://mail.solit.cn/",
|
||||
"X-CSRF-Token": csrf_token
|
||||
}
|
||||
|
||||
# 2. 如果需要强制修改密码,先修改密码
|
||||
if data.get("data", {}).get("must_change_password"):
|
||||
print("Testuser2 must change password, updating...")
|
||||
pwd_payload = json.dumps({
|
||||
"old_password": "Password123!",
|
||||
"new_password": "UserPass1234!"
|
||||
}).encode("utf-8")
|
||||
req_chg = urllib.request.Request("https://47.108.78.12/api/v1/settings/modify_password", data=pwd_payload, headers=headers)
|
||||
resp_chg = opener.open(req_chg, timeout=5)
|
||||
print("Change password resp:", resp_chg.read().decode())
|
||||
|
||||
# 3. 设置密保问题
|
||||
q_payload = json.dumps({
|
||||
"question": "毕业学校",
|
||||
"answer": "清华大学"
|
||||
}).encode("utf-8")
|
||||
req_q = urllib.request.Request("https://47.108.78.12/api/v1/security/credentials/question", data=q_payload, headers=headers)
|
||||
resp_q = opener.open(req_q, timeout=5)
|
||||
print("Set question resp:", resp_q.read().decode())
|
||||
|
||||
# 4. 生成一组离线恢复码
|
||||
codes_payload = json.dumps({"count": 5}).encode("utf-8")
|
||||
req_c = urllib.request.Request("https://47.108.78.12/api/v1/security/credentials/recovery-codes", data=codes_payload, headers=headers)
|
||||
resp_c = opener.open(req_c, timeout=5)
|
||||
c_data = json.loads(resp_c.read().decode())
|
||||
print("Generate recovery codes resp:", c_data)
|
||||
if c_data.get("data", {}).get("codes"):
|
||||
with open("test/tools/testuser2_codes.json", "w", encoding="utf-8") as f:
|
||||
json.dump(c_data["data"], f, ensure_ascii=False, indent=2)
|
||||
67
test/tools/testuser2_setup_security_v2.py
Normal file
67
test/tools/testuser2_setup_security_v2.py
Normal file
@ -0,0 +1,67 @@
|
||||
import urllib.request, ssl, json, http.cookiejar
|
||||
ctx = ssl.create_default_context()
|
||||
ctx.check_hostname = False
|
||||
ctx.verify_mode = ssl.CERT_NONE
|
||||
cj = http.cookiejar.CookieJar()
|
||||
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx), urllib.request.HTTPCookieProcessor(cj))
|
||||
|
||||
# 1. 登录 testuser2
|
||||
login_payload = json.dumps({"account": "testuser2", "password": "Password123!"}).encode("utf-8")
|
||||
req = urllib.request.Request("https://47.108.78.12/api/v1/login", data=login_payload, headers={
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn"
|
||||
})
|
||||
resp = opener.open(req, timeout=5)
|
||||
data = json.loads(resp.read().decode())
|
||||
print("Testuser2 login resp:", data.get("errorNo"), data.get("errorMsg"))
|
||||
|
||||
csrf_token = ""
|
||||
for c in cj:
|
||||
if c.name == "smail_csrf":
|
||||
csrf_token = c.value
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Host": "mail.solit.cn",
|
||||
"Origin": "https://mail.solit.cn",
|
||||
"Referer": "https://mail.solit.cn/",
|
||||
"X-CSRF-Token": csrf_token
|
||||
}
|
||||
|
||||
# 2. 如果需要强制修改密码,先修改密码
|
||||
if data.get("data", {}).get("must_change_password"):
|
||||
print("Testuser2 must change password, updating...")
|
||||
pwd_payload = json.dumps({
|
||||
"current_password": "Password123!",
|
||||
"password": "UserPass88!"
|
||||
}).encode("utf-8")
|
||||
req_chg = urllib.request.Request("https://47.108.78.12/api/v1/settings/modify_password", data=pwd_payload, headers=headers)
|
||||
resp_chg = opener.open(req_chg, timeout=5)
|
||||
print("Change password resp:", resp_chg.read().decode())
|
||||
|
||||
# 3. 设置密保问题
|
||||
q_payload = json.dumps({
|
||||
"question": "毕业学校",
|
||||
"answer": "清华大学"
|
||||
}).encode("utf-8")
|
||||
req_q = urllib.request.Request("https://47.108.78.12/api/v1/security/credentials/question", data=q_payload, headers=headers)
|
||||
resp_q = opener.open(req_q, timeout=5)
|
||||
print("Set question resp:", resp_q.read().decode())
|
||||
|
||||
# 4. 生成一组离线恢复码 (/api/v1/security/credentials/recovery-codes/generate)
|
||||
codes_payload = json.dumps({"count": 5}).encode("utf-8")
|
||||
req_c = urllib.request.Request("https://47.108.78.12/api/v1/security/credentials/recovery-codes/generate", data=codes_payload, headers=headers)
|
||||
resp_c = opener.open(req_c, timeout=5)
|
||||
c_data = json.loads(resp_c.read().decode())
|
||||
print("Generate recovery codes resp errorNo:", c_data.get("errorNo"))
|
||||
print("Codes generated:", c_data.get("data", {}).get("codes"))
|
||||
if c_data.get("data", {}).get("codes"):
|
||||
with open("test/tools/testuser2_codes.json", "w", encoding="utf-8") as f:
|
||||
json.dump({
|
||||
"account": "testuser2",
|
||||
"password": "UserPass88!",
|
||||
"question": "毕业学校",
|
||||
"answer": "清华大学",
|
||||
"codes": c_data["data"]["codes"]
|
||||
}, f, ensure_ascii=False, indent=2)
|
||||
23
test/tools/update_remote_email_config.py
Normal file
23
test/tools/update_remote_email_config.py
Normal file
@ -0,0 +1,23 @@
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect("47.108.78.12", port=22, username="root", password="10102442Lb!", timeout=10)
|
||||
|
||||
cmd = """python3 -c '
|
||||
import json
|
||||
with open("/opt/smail/config/email.json", "r") as f:
|
||||
d = json.load(f)
|
||||
d["policy"]["externalRecoveryEnabled"] = True
|
||||
with open("/opt/smail/config/email.json", "w") as f:
|
||||
json.dump(d, f, indent=2)
|
||||
'
|
||||
"""
|
||||
stdin, stdout, stderr = ssh.exec_command(cmd)
|
||||
print("Stderr:", stderr.read().decode())
|
||||
|
||||
stdin, stdout, stderr = ssh.exec_command("cat /opt/smail/config/email.json")
|
||||
print("email.json:\n", stdout.read().decode())
|
||||
|
||||
stdin, stdout, stderr = ssh.exec_command("killall -9 smail")
|
||||
print("Killall:", stdout.read().decode())
|
||||
ssh.close()
|
||||
2
test/tools/writer.py
Normal file
2
test/tools/writer.py
Normal file
@ -0,0 +1,2 @@
|
||||
import sys, binascii
|
||||
open(sys.argv[1], 'wb').write(binascii.unhexlify(sys.argv[2]))
|
||||
2
test/write_helper.py
Normal file
2
test/write_helper.py
Normal file
@ -0,0 +1,2 @@
|
||||
import sys, binascii
|
||||
open(sys.argv[1], 'wb').write(binascii.unhexlify(sys.argv[2]))
|
||||
Loading…
Reference in New Issue
Block a user