From f87d3fad9be11f1164137d1e28f2599dccab43fd Mon Sep 17 00:00:00 2001 From: hermes Date: Mon, 26 Oct 2026 16:09:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(qa):=20=E6=8E=88=E6=9D=83=E7=A0=81=E6=8C=89?= =?UTF-8?q?=E5=86=99=E5=85=A5=E7=89=88=E6=9C=AC=E8=A7=A3=E5=AF=86=EF=BC=8C?= =?UTF-8?q?=E6=94=B9=E5=85=B6=E5=AE=83=E8=AE=BE=E7=BD=AE=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 只调整收件人或时间也会递增设置版本,导致已保存的授权码无法解密、测试发送报存储不可用。 新增 password_revision 记录加密时的版本,迁移回填现有行。 --- ...064_weekly_mail_password_revision.down.sql | 1 + ...00064_weekly_mail_password_revision.up.sql | 3 +++ internal/platform/postgres/migrations_test.go | 4 ++-- internal/qawemail/store.go | 22 ++++++++++++------- 4 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 internal/platform/postgres/migrations/000064_weekly_mail_password_revision.down.sql create mode 100644 internal/platform/postgres/migrations/000064_weekly_mail_password_revision.up.sql diff --git a/internal/platform/postgres/migrations/000064_weekly_mail_password_revision.down.sql b/internal/platform/postgres/migrations/000064_weekly_mail_password_revision.down.sql new file mode 100644 index 0000000..ac426d7 --- /dev/null +++ b/internal/platform/postgres/migrations/000064_weekly_mail_password_revision.down.sql @@ -0,0 +1 @@ +alter table qa_weekly_email_settings drop column if exists password_revision; diff --git a/internal/platform/postgres/migrations/000064_weekly_mail_password_revision.up.sql b/internal/platform/postgres/migrations/000064_weekly_mail_password_revision.up.sql new file mode 100644 index 0000000..58708b9 --- /dev/null +++ b/internal/platform/postgres/migrations/000064_weekly_mail_password_revision.up.sql @@ -0,0 +1,3 @@ +-- 授权码按"写入它时的版本"解密:只改其它设置不再使已保存的密码失效。 +alter table qa_weekly_email_settings add column if not exists password_revision bigint not null default 0; +update qa_weekly_email_settings set password_revision = revision where password_revision = 0 and smtp_password_sealed is not null; diff --git a/internal/platform/postgres/migrations_test.go b/internal/platform/postgres/migrations_test.go index 7ea4cea..9c78216 100644 --- a/internal/platform/postgres/migrations_test.go +++ b/internal/platform/postgres/migrations_test.go @@ -11,8 +11,8 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) { if err != nil { t.Fatalf("loadMigrations() error = %v", err) } - if len(migrations) != 63 { - t.Fatalf("migration count = %d, want 63", len(migrations)) + if len(migrations) != 64 { + t.Fatalf("migration count = %d, want 64", len(migrations)) } if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") { t.Fatalf("migration 1 = %#v", migrations[0]) diff --git a/internal/qawemail/store.go b/internal/qawemail/store.go index 0de6a85..9073994 100644 --- a/internal/qawemail/store.go +++ b/internal/qawemail/store.go @@ -294,7 +294,8 @@ func (s *Store) SaveSettings(ctx context.Context, actor string, input SettingsIn } var current int64 var sealed []byte - err = tx.QueryRow(ctx, `SELECT revision,smtp_password_sealed FROM qa_weekly_email_settings WHERE tenant_key=$1 FOR UPDATE`, s.tenant).Scan(¤t, &sealed) + var passwordRevision int64 + err = tx.QueryRow(ctx, `SELECT revision,smtp_password_sealed,password_revision FROM qa_weekly_email_settings WHERE tenant_key=$1 FOR UPDATE`, s.tenant).Scan(¤t, &sealed, &passwordRevision) if err != nil && !errors.Is(err, pgx.ErrNoRows) { return Settings{}, ErrUnavailable } @@ -304,16 +305,17 @@ func (s *Store) SaveSettings(ctx context.Context, actor string, input SettingsIn next := current + 1 switch { case normalized.ClearPassword: - sealed = nil + sealed, passwordRevision = nil, 0 case strings.TrimSpace(normalized.Password) != "": if sealed, err = s.seal(normalized.Password, next); err != nil { return Settings{}, err } + passwordRevision = next } - if _, err = tx.Exec(ctx, `INSERT INTO qa_weekly_email_settings(tenant_key,smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,review_address,send_weekday,send_hour,enabled,revision,updated_by,updated_at) - VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,now()) - ON CONFLICT (tenant_key) DO UPDATE SET smtp_host=excluded.smtp_host,smtp_port=excluded.smtp_port,smtp_username=excluded.smtp_username,smtp_password_sealed=excluded.smtp_password_sealed,from_address=excluded.from_address,recipients=excluded.recipients,cc_recipients=excluded.cc_recipients,review_address=excluded.review_address,send_weekday=excluded.send_weekday,send_hour=excluded.send_hour,enabled=excluded.enabled,revision=excluded.revision,updated_by=excluded.updated_by,updated_at=now()`, - s.tenant, normalized.Host, normalized.Port, normalized.Username, sealed, normalized.From, normalized.Recipients, normalized.Cc, normalized.Review, normalized.Weekday, normalized.Hour, normalized.Enabled, next, actor); err != nil { + if _, err = tx.Exec(ctx, `INSERT INTO qa_weekly_email_settings(tenant_key,smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,review_address,send_weekday,send_hour,enabled,revision,updated_by,updated_at,password_revision) + VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,now(),$15) + ON CONFLICT (tenant_key) DO UPDATE SET smtp_host=excluded.smtp_host,smtp_port=excluded.smtp_port,smtp_username=excluded.smtp_username,smtp_password_sealed=excluded.smtp_password_sealed,from_address=excluded.from_address,recipients=excluded.recipients,cc_recipients=excluded.cc_recipients,review_address=excluded.review_address,send_weekday=excluded.send_weekday,send_hour=excluded.send_hour,enabled=excluded.enabled,revision=excluded.revision,updated_by=excluded.updated_by,updated_at=now(),password_revision=excluded.password_revision`, + s.tenant, normalized.Host, normalized.Port, normalized.Username, sealed, normalized.From, normalized.Recipients, normalized.Cc, normalized.Review, normalized.Weekday, normalized.Hour, normalized.Enabled, next, actor, passwordRevision); err != nil { return Settings{}, ErrUnavailable } if err = tx.Commit(ctx); err != nil { @@ -637,10 +639,14 @@ func (s *Store) inlineImages(ctx context.Context, week string, html *string) ([] func (s *Store) password(ctx context.Context, revision int64) (string, error) { var sealed []byte - if err := s.pool.QueryRow(ctx, `SELECT smtp_password_sealed FROM qa_weekly_email_settings WHERE tenant_key=$1`, s.tenant).Scan(&sealed); err != nil { + var sealedAt int64 + if err := s.pool.QueryRow(ctx, `SELECT smtp_password_sealed,password_revision FROM qa_weekly_email_settings WHERE tenant_key=$1`, s.tenant).Scan(&sealed, &sealedAt); err != nil { return "", ErrUnavailable } - return s.unseal(sealed, revision) + if sealedAt <= 0 { + sealedAt = revision + } + return s.unseal(sealed, sealedAt) } func renderReport(markdown string) (string, string) {