web: every Copy button works over plain http; saved keys and passwords can be removed (#8508, #8511)
copyText (family idea "copyText"): - web/src/lib/utils/clipboard.ts tries the Clipboard API first. - Where the API is missing (plain http on a home network) or refuses, it copies through the copy command on a hidden textarea instead. - If that fails too, it selects the shown value. - All six Copy buttons go through it: an invite token, the needs-a-file list, diagnostics, connection details, a playlist link, and report details. Before, five of them only showed an error over http. SecretField (family idea "SecretField"): - One component for a write-only credential: a password box for a new value, which the card's own Save sends, and a Remove button while one is saved. - Used for the provider API keys, the SMTP password, the backup secret key and the AcoustID key. Only AcoustID had Remove before. - The server gains clear_password (SMTP) and clear_secret_key (backup). An empty value still keeps the stored one. Provider keys already clear when sent empty. - Remove sends the saved settings back with only the credential cleared, so unsaved edits elsewhere on the card stay unsaved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -41,7 +41,7 @@ func fieldErr(w http.ResponseWriter, err error) bool {
|
||||
}
|
||||
|
||||
// PUT /api/admin/backups — save the settings. An empty secret_key keeps the
|
||||
// stored one. Returns the new status.
|
||||
// stored one, and clear_secret_key removes it. Returns the new status.
|
||||
func (h *handlers) handleUpdateBackups(w http.ResponseWriter, r *http.Request) {
|
||||
u, ok := decodeBackupUpdate(w, r)
|
||||
if !ok {
|
||||
|
||||
@@ -33,6 +33,9 @@ type updateSMTPConfigReq struct {
|
||||
FromAddress string `json:"from_address"`
|
||||
FromName string `json:"from_name"`
|
||||
UseTLS bool `json:"use_tls"`
|
||||
// ClearPassword removes the stored password. An empty Password alone
|
||||
// keeps it, so the admin doesn't retype it on every save.
|
||||
ClearPassword bool `json:"clear_password"`
|
||||
}
|
||||
|
||||
func (h *handlers) handleGetSMTPConfig(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -80,6 +83,9 @@ func (h *handlers) handleUpdateSMTPConfig(w http.ResponseWriter, r *http.Request
|
||||
// Preserve the stored (sealed) password when the request sends "", so
|
||||
// the admin doesn't have to re-enter it on every save.
|
||||
password := current.Password
|
||||
if req.ClearPassword {
|
||||
password = ""
|
||||
}
|
||||
if req.Password != "" {
|
||||
password, err = secret.Seal(secret.SMTPPassword.Purpose(), "", req.Password)
|
||||
if err != nil {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/Fabled-Sword/minstrel/internal/auth"
|
||||
"github.com/Fabled-Sword/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
func newAdminSMTPRouter(h *handlers) chi.Router {
|
||||
@@ -92,6 +93,26 @@ func TestSMTPConfig_UpdatePreservesPasswordWhenEmpty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The password is removed only when the save asks for it.
|
||||
func TestSMTPConfig_UpdateClearsPasswordWhenAsked(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
admin := seedUser(t, pool, "smtpclear", "pw", true)
|
||||
fx := dbtest.On(t, pool)
|
||||
fx.Exec("UPDATE smtp_config SET password = 'original' WHERE id = true")
|
||||
|
||||
body := `{"enabled": false, "host": "", "port": 587, "username": "", "password": "",
|
||||
"from_address": "", "from_name": "T", "use_tls": true, "clear_password": true}`
|
||||
req := withUser(httptest.NewRequest(http.MethodPut, "/api/admin/smtp-config", bytes.NewReader([]byte(body))), admin)
|
||||
rec := httptest.NewRecorder()
|
||||
newAdminSMTPRouter(h).ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204; body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if n := fx.Count("SELECT count(*) FROM smtp_config WHERE password <> ''"); n != 0 {
|
||||
t.Error("the stored password survived a save with clear_password")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSMTPConfig_UpdateValidatesEnabled(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
admin := seedUser(t, pool, "smtpvalid", "pw", true)
|
||||
|
||||
@@ -99,7 +99,8 @@ type Config struct {
|
||||
}
|
||||
|
||||
// Update is a save from the admin card. An empty SecretKey keeps the stored
|
||||
// one, so the key doesn't have to be typed again on every save.
|
||||
// one, so the key doesn't have to be typed again on every save;
|
||||
// ClearSecretKey removes it.
|
||||
type Update struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Destination string `json:"destination"`
|
||||
@@ -108,6 +109,7 @@ type Update struct {
|
||||
Bucket string `json:"bucket"`
|
||||
AccessKey string `json:"access_key"`
|
||||
SecretKey string `json:"secret_key"`
|
||||
ClearSecretKey bool `json:"clear_secret_key"`
|
||||
RunHour int `json:"run_hour"`
|
||||
UploadLimitKiB int `json:"upload_limit_kib"`
|
||||
IncludeMusic bool `json:"include_music"`
|
||||
@@ -246,6 +248,9 @@ func merge(st dbq.BackupSetting, u Update) (dbq.BackupSetting, error) {
|
||||
st.RunHour = int16(u.RunHour)
|
||||
st.UploadLimitKib = int32(u.UploadLimitKiB)
|
||||
st.IncludeMusic = u.IncludeMusic
|
||||
if u.ClearSecretKey {
|
||||
st.SecretKey = ""
|
||||
}
|
||||
if u.SecretKey != "" {
|
||||
sealed, err := secret.Seal(secret.BackupSecretKey.Purpose(), "", u.SecretKey)
|
||||
if err != nil {
|
||||
|
||||
@@ -129,6 +129,16 @@ func TestMergeTidiesAndKeepsTheStoredSecret(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeRemovesTheSecretWhenAsked(t *testing.T) {
|
||||
st, err := merge(dbq.BackupSetting{SecretKey: "v1.stored"}, Update{Destination: DestinationS3, ClearSecretKey: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if st.SecretKey != "" {
|
||||
t.Errorf("secret key = %q after a clear, want it removed", st.SecretKey)
|
||||
}
|
||||
}
|
||||
|
||||
func TestThrottleKeepsToItsRate(t *testing.T) {
|
||||
data := bytes.Repeat([]byte("x"), 20*1024)
|
||||
start := time.Now()
|
||||
|
||||
Reference in New Issue
Block a user