Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Clear legacy plaintext tokens in batches

Neeraj Guptad1aa8eb991af
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index 79db0607bb..8eb002662e 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -1097,6 +1097,12 @@ func main() {
if err := remoteStoreRepository.MigrateCustomDomainCanonicalValues(context.Background()); err != nil {
log.WithError(err).Error("Failed to backfill custom domain canonical values")
}
+ migrated, err := userAuthRepo.MigratePlaintextTokens(context.Background())
+ if err != nil {
+ log.WithError(err).Error("Failed to clear plaintext tokens")
+ } else if migrated > 0 {
+ log.WithField("tokens", migrated).Info("Cleared plaintext tokens")
+ }
})
setupAndStartCrons(
userAuthRepo, collectionLinkRepo, fileLinkRepo, pasteRepo, twoFactorRepo, passkeysRepo, fileController, taskLockingRepo, emailNotificationCtrl,
diff --git a/server/pkg/repo/userauth_migration.go b/server/pkg/repo/userauth_migration.go
new file mode 100644
index 0000000000..fe29cc9c37
--- /dev/null
+++ b/server/pkg/repo/userauth_migration.go
@@ -0,0 +1,39 @@
+package repo
+
+import (
+ "context"
+ "time"
+
+ "github.com/ente/stacktrace"
+)
+
+const plaintextTokenBatchSize = 5000
+
+// MigratePlaintextTokens clears legacy plaintext tokens (2026-09-02).
+func (repo *UserAuthRepository) MigratePlaintextTokens(ctx context.Context) (int64, error) {
+ var migrated int64
+ for {
+ result, err := repo.DB.ExecContext(ctx, `WITH batch AS (
+ SELECT token_hash FROM tokens
+ WHERE token IS NOT NULL
+ ORDER BY token
+ LIMIT $1
+ FOR UPDATE SKIP LOCKED
+ )
+ UPDATE tokens SET token = NULL
+ FROM batch
+ WHERE tokens.token_hash = batch.token_hash`, plaintextTokenBatchSize)
+ if err != nil {
+ return migrated, stacktrace.Propagate(err, "failed to clear plaintext tokens")
+ }
+ count, err := result.RowsAffected()
+ if err != nil {
+ return migrated, stacktrace.Propagate(err, "failed to count cleared plaintext tokens")
+ }
+ migrated += count
+ if count < plaintextTokenBatchSize {
+ return migrated, nil
+ }
+ time.Sleep(time.Second)
+ }
+}