Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(cloud): harden account activity tracking and cleanup retries

Timothy Yin834e8780743a
diff --git a/apps/cloud/server/plugins/account-lifecycle.ts b/apps/cloud/server/plugins/account-lifecycle.ts
index 85cbe370..2133a99a 100644
--- a/apps/cloud/server/plugins/account-lifecycle.ts
+++ b/apps/cloud/server/plugins/account-lifecycle.ts
@@ -1,5 +1,6 @@
import {
scanAccounts,
+ lifecycleStages,
deliverLifecycleMail,
} from '../../../../packages/platform/src/account-lifecycle'
import { purgeAccounts } from '../../../../packages/platform/src/account-purge'
@@ -14,90 +15,106 @@ export default defineNitroPlugin((app) => {
const db = env.DB as PlatformD1 | undefined
if (!db) return
const now = Date.now()
- await scanAccounts(db, now)
- await deliverLifecycleMail(
- db,
- env.EMAIL as MailBinding,
- String(env.NUXT_PLATFORM_PORTAL_ORIGIN || 'https://app.chronoframe.art'),
- now,
- )
- const runtime = {
- public: {
- siteBaseDomain: String(
- env.NUXT_PUBLIC_SITE_BASE_DOMAIN || 'chronoframe.art',
- ),
- },
- platform: {
- domainEnabled: env.NUXT_PLATFORM_DOMAIN_ENABLED,
- domainRoutingReady: env.NUXT_PLATFORM_DOMAIN_ROUTING_READY,
- domainProviderToken: env.NUXT_PLATFORM_DOMAIN_PROVIDER_TOKEN,
- domainZoneId: env.NUXT_PLATFORM_DOMAIN_ZONE_ID,
- domainCnameTarget: env.NUXT_PLATFORM_DOMAIN_CNAME_TARGET,
- domainSiteWorker: env.NUXT_PLATFORM_DOMAIN_SITE_WORKER,
- },
- }
- const domains = domainServices(db, runtime)
- await purgeAccounts(
+ await lifecycleStages(
db,
{
- async domains(siteId) {
+ scan: () => scanAccounts(db, now),
+ mail: () =>
+ deliverLifecycleMail(
+ db,
+ env.EMAIL as MailBinding,
+ String(
+ env.NUXT_PLATFORM_PORTAL_ORIGIN || 'https://app.chronoframe.art',
+ ),
+ now,
+ ),
+ purge: async () => {
+ const runtime = {
+ public: {
+ siteBaseDomain: String(
+ env.NUXT_PUBLIC_SITE_BASE_DOMAIN || 'chronoframe.art',
+ ),
+ },
+ platform: {
+ domainEnabled: env.NUXT_PLATFORM_DOMAIN_ENABLED,
+ domainRoutingReady: env.NUXT_PLATFORM_DOMAIN_ROUTING_READY,
+ domainProviderToken: env.NUXT_PLATFORM_DOMAIN_PROVIDER_TOKEN,
+ domainZoneId: env.NUXT_PLATFORM_DOMAIN_ZONE_ID,
+ domainCnameTarget: env.NUXT_PLATFORM_DOMAIN_CNAME_TARGET,
+ domainSiteWorker: env.NUXT_PLATFORM_DOMAIN_SITE_WORKER,
+ },
+ }
+ const domains = domainServices(db, runtime)
+ for (let i = 0; i < 3; i++)
+ await purgeAccounts(
+ db,
+ {
+ async domains(siteId) {
+ await db
+ .prepare(
+ "UPDATE platform_custom_domains SET state='deleting',purge_requested=1,next_check_at=0,revision=revision+1,lease_token=NULL,lease_until=NULL WHERE site_id=? AND (state<>'deleted' OR purge_requested=0 OR provider_id IS NOT NULL OR route_id IS NOT NULL OR provider_attempted=1 OR route_attempted=1)",
+ )
+ .bind(siteId)
+ .run()
+ const domain = await db
+ .prepare(
+ 'SELECT hostname FROM platform_custom_domains WHERE site_id=? LIMIT 1',
+ )
+ .bind(siteId)
+ .first<{ hostname: string }>()
+ if (domain) {
+ if (!domains.config.enabled)
+ throw new Error('Domain service required')
+ await domains.manager.reconcile(domain.hostname, 1)
+ }
+ return !(await db
+ .prepare(
+ 'SELECT hostname FROM platform_custom_domains WHERE site_id=? LIMIT 1',
+ )
+ .bind(siteId)
+ .first())
+ },
+ async storage(siteId, connectionId, revision) {
+ const key = String(env.NUXT_PLATFORM_STORAGE_SECRET || '')
+ if (key.length < 32)
+ throw new Error('Storage credentials required')
+ const vault = createStorageVault(
+ db,
+ siteId,
+ await createStorageCipher(key),
+ )
+ const connection = await vault.resolve(connectionId, revision)
+ if (!connection)
+ throw new Error('Storage connection unavailable')
+ return createCloudS3(
+ { ...connection.config, ...connection.credentials },
+ {
+ allowedHosts: [
+ ...String(env.NUXT_PLATFORM_S3_ENDPOINT_HOSTS || '')
+ .split(',')
+ .map((h) => h.trim())
+ .filter(Boolean),
+ ...('poolId' in connection
+ ? [new URL(connection.config.endpoint).hostname]
+ : []),
+ ],
+ },
+ ).purgeTenant(siteId)
+ },
+ },
+ now,
+ )
+ },
+ outbox: async () => {
await db
.prepare(
- "UPDATE platform_custom_domains SET state='deleting',purge_requested=1,next_check_at=0,revision=revision+1,lease_token=NULL,lease_until=NULL WHERE site_id=? AND (state<>'deleted' OR purge_requested=0 OR provider_id IS NOT NULL OR route_id IS NOT NULL OR provider_attempted=1 OR route_attempted=1)",
+ "DELETE FROM platform_mail_outbox WHERE state IN('sent','cancelled') AND available_at<?",
)
- .bind(siteId)
+ .bind(now - 30 * 86400000)
.run()
- const domain = await db
- .prepare(
- 'SELECT hostname FROM platform_custom_domains WHERE site_id=? LIMIT 1',
- )
- .bind(siteId)
- .first<{ hostname: string }>()
- if (domain) {
- if (!domains.config.enabled)
- throw new Error('Domain service required')
- await domains.manager.reconcile(domain.hostname, 1)
- }
- return !(await db
- .prepare(
- 'SELECT hostname FROM platform_custom_domains WHERE site_id=? LIMIT 1',
- )
- .bind(siteId)
- .first())
- },
- async storage(siteId, connectionId, revision) {
- const key = String(env.NUXT_PLATFORM_STORAGE_SECRET || '')
- if (key.length < 32) throw new Error('Storage credentials required')
- const vault = createStorageVault(
- db,
- siteId,
- await createStorageCipher(key),
- )
- const connection = await vault.resolve(connectionId, revision)
- if (!connection) throw new Error('Storage connection unavailable')
- return createCloudS3(
- { ...connection.config, ...connection.credentials },
- {
- allowedHosts: [
- ...String(env.NUXT_PLATFORM_S3_ENDPOINT_HOSTS || '')
- .split(',')
- .map((h) => h.trim())
- .filter(Boolean),
- ...('poolId' in connection
- ? [new URL(connection.config.endpoint).hostname]
- : []),
- ],
- },
- ).purgeTenant(siteId)
},
},
now,
)
- await db
- .prepare(
- "DELETE FROM platform_mail_outbox WHERE state IN('sent','cancelled') AND available_at<?",
- )
- .bind(now - 30 * 86400000)
- .run()
})
})
diff --git a/apps/cloud/server/utils/account-auth.ts b/apps/cloud/server/utils/account-auth.ts
index a992b864..0965d0b6 100644
--- a/apps/cloud/server/utils/account-auth.ts
+++ b/apps/cloud/server/utils/account-auth.ts
@@ -25,6 +25,28 @@ export function accountAuthOptions(
) {
const origin = new URL(config.portalOrigin)
const db = database as unknown as PlatformD1
+ const recordSessionActivity = async (session: {
+ userId: string
+ id: string
+ }) => {
+ const updated = await db
+ .prepare(
+ 'UPDATE platform_users SET last_login_at=? WHERE id=? AND NOT EXISTS(SELECT 1 FROM platform_account_deletions WHERE user_id=platform_users.id) RETURNING id',
+ )
+ .bind(Date.now(), Number(session.userId))
+ .first()
+ if (!updated) {
+ await db
+ .prepare('DELETE FROM platform_auth_sessions WHERE id=?')
+ .bind(Number(session.id))
+ .run()
+ throw new APIError('FORBIDDEN', {
+ code: 'ACCOUNT_DELETED',
+ message:
+ 'This account has been deleted. Contact [email protected] to request restoration.',
+ })
+ }
+ }
return {
appName: 'ChronoFrame Cloud',
databaseHooks: {
@@ -71,26 +93,9 @@ export function accountAuthOptions(
)
return { data: session }
},
- after: async (session) => {
- const updated = await db
- .prepare(
- 'UPDATE platform_users SET last_login_at=? WHERE id=? AND NOT EXISTS(SELECT 1 FROM platform_account_deletions WHERE user_id=platform_users.id) RETURNING id',
- )
- .bind(Date.now(), Number(session.userId))
- .first()
- if (!updated) {
- await db
- .prepare('DELETE FROM platform_auth_sessions WHERE id=?')
- .bind(Number(session.id))
- .run()
- throw new APIError('FORBIDDEN', {
- code: 'ACCOUNT_DELETED',
- message:
- 'This account has been deleted. Contact [email protected] to request restoration.',
- })
- }
- },
+ after: recordSessionActivity,
},
+ update: { after: recordSessionActivity },
},
},
baseURL: origin.origin,
diff --git a/packages/platform/migrations/0020_account_lifecycle_mail.sql b/packages/platform/migrations/0020_account_lifecycle_mail.sql
index 811bf178..9341d2a9 100644
--- a/packages/platform/migrations/0020_account_lifecycle_mail.sql
+++ b/packages/platform/migrations/0020_account_lifecycle_mail.sql
@@ -1,6 +1,6 @@
ALTER TABLE platform_users ADD COLUMN last_login_at INTEGER;
ALTER TABLE platform_users ADD COLUMN lifecycle_grace_until INTEGER NOT NULL DEFAULT 0;
-UPDATE platform_users SET last_login_at=COALESCE((SELECT MAX(CASE WHEN typeof(created_at)='integer' THEN created_at ELSE CAST(strftime('%s',created_at) AS INTEGER)*1000 END) FROM platform_auth_sessions WHERE user_id=platform_users.id),CASE WHEN typeof(created_at)='integer' THEN created_at ELSE CAST(strftime('%s',created_at) AS INTEGER)*1000 END);
+UPDATE platform_users SET last_login_at=COALESCE((SELECT MAX(CASE WHEN typeof(created_at)='integer' THEN created_at ELSE CAST(strftime('%s',created_at) AS INTEGER)*1000 END) FROM platform_auth_sessions WHERE user_id=platform_users.id),CAST(strftime('%s','now') AS INTEGER)*1000);
CREATE TABLE platform_lifecycle_settings(id INTEGER PRIMARY KEY CHECK(id=1),enabled INTEGER NOT NULL DEFAULT 1 CHECK(enabled IN(0,1)),unverified_days INTEGER NOT NULL DEFAULT 14 CHECK(unverified_days BETWEEN 1 AND 3650),inactive_days INTEGER NOT NULL DEFAULT 90 CHECK(inactive_days BETWEEN 1 AND 3650),retention_days INTEGER NOT NULL DEFAULT 15 CHECK(retention_days BETWEEN 1 AND 3650),warning_days_json TEXT NOT NULL DEFAULT '[7,3]' CHECK(json_valid(warning_days_json)),activated_at INTEGER NOT NULL,revision INTEGER NOT NULL DEFAULT 1);
INSERT INTO platform_lifecycle_settings(id,activated_at) VALUES(1,CAST(strftime('%s','now') AS INTEGER)*1000);
CREATE TABLE platform_account_deletions(user_id INTEGER PRIMARY KEY REFERENCES platform_users(id),generation TEXT NOT NULL,state TEXT NOT NULL DEFAULT 'marked' CHECK(state IN('marked','purging')),source TEXT NOT NULL,reason TEXT NOT NULL,marked_at INTEGER NOT NULL,purge_at INTEGER NOT NULL,previous_suspended INTEGER NOT NULL,previous_reason TEXT NOT NULL,revision INTEGER NOT NULL DEFAULT 1,error TEXT,next_attempt_at INTEGER NOT NULL DEFAULT 0,lease_token TEXT,lease_until INTEGER);
@@ -16,3 +16,4 @@ CREATE TABLE platform_deletion_sites(user_id INTEGER NOT NULL,site_id INTEGER PR
ALTER TABLE platform_lifecycle_settings ADD COLUMN exemptions_json TEXT NOT NULL DEFAULT '{"enabled":true,"match":"any","rules":[{"kind":"active-subscription","sources":["manual","activation","payment"],"planIds":[]}]}' CHECK(json_valid(exemptions_json));
ALTER TABLE platform_users ADD COLUMN lifecycle_was_exempt INTEGER NOT NULL DEFAULT 0 CHECK(lifecycle_was_exempt IN(0,1));
+CREATE TABLE platform_lifecycle_errors(scope TEXT PRIMARY KEY,user_id INTEGER REFERENCES platform_users(id) ON DELETE CASCADE,error TEXT NOT NULL,updated_at INTEGER NOT NULL);
diff --git a/packages/platform/src/account-lifecycle.ts b/packages/platform/src/account-lifecycle.ts
index 391cf9c9..254f0d32 100644
--- a/packages/platform/src/account-lifecycle.ts
+++ b/packages/platform/src/account-lifecycle.ts
@@ -79,10 +79,10 @@ export function deletionDeadline(
user.lifecycle_grace_until ?? 0,
)
}
-export const automaticReason = (verified: boolean) =>
+export const automaticReason = (verified: boolean, days: number) =>
verified
- ? 'No recent sign-in was recorded within the inactivity period.'
- : 'Your email address has not been verified within the verification period.'
+ ? `No sign-in has been recorded for ${days} days.`
+ : `Your email address is still unverified ${days} days after registration.`
const date = (v: number) =>
new Date(v).toISOString().replace('T', ' ').replace('.000Z', ' UTC')
export async function markAccount(
@@ -275,6 +275,41 @@ export async function restoreAccount(
.bind(id)
.first())
}
+export async function lifecycleFailure(
+ db: PlatformD1,
+ scope: string,
+ now: number,
+ userId: number | null = null,
+) {
+ console.error('account-lifecycle stage failed', scope)
+ try {
+ await db
+ .prepare(
+ "INSERT INTO platform_lifecycle_errors(scope,user_id,error,updated_at) VALUES(?,?,'lifecycle_stage_failed',?) ON CONFLICT(scope) DO UPDATE SET error=excluded.error,updated_at=excluded.updated_at",
+ )
+ .bind(scope, userId, now)
+ .run()
+ } catch {
+ /* A database outage must not prevent independent stages from running. */
+ }
+}
+export async function lifecycleStages(
+ db: PlatformD1,
+ stages: Record<string, () => Promise<unknown>>,
+ now = Date.now(),
+) {
+ for (const [scope, run] of Object.entries(stages)) {
+ try {
+ await run()
+ await db
+ .prepare('DELETE FROM platform_lifecycle_errors WHERE scope=?')
+ .bind(scope)
+ .run()
+ } catch {
+ await lifecycleFailure(db, scope, now)
+ }
+ }
+}
export async function scanAccounts(
db: PlatformD1,
now = Date.now(),
@@ -293,13 +328,13 @@ export async function scanAccounts(
)
.bind(state?.value ?? 0, limit),
)
- for (const u of users) {
+ const scanUser = async (u: any) => {
if (await accountExemption(db, u.id, s.exemptions!, now)) {
await db
.prepare('UPDATE platform_users SET lifecycle_was_exempt=1 WHERE id=?')
.bind(u.id)
.run()
- continue
+ return
}
if (u.lifecycle_was_exempt) {
u.lifecycle_grace_until = Math.max(
@@ -314,7 +349,10 @@ export async function scanAccounts(
.run()
}
const deadline = deletionDeadline(u, s),
- reason = automaticReason(!!u.email_verified)
+ reason = automaticReason(
+ !!u.email_verified,
+ u.email_verified ? s.inactiveDays : s.unverifiedDays,
+ )
if (deadline <= now) {
await markAccount(
db,
@@ -329,7 +367,7 @@ export async function scanAccounts(
settingsRevision: s.revision,
},
)
- continue
+ return
}
const warning = s.warningDays.find(
(days, index) =>
@@ -361,6 +399,18 @@ export async function scanAccounts(
)
.run()
}
+ for (const u of users) {
+ const scope = 'scan-user:' + u.id
+ try {
+ await scanUser(u)
+ await db
+ .prepare('DELETE FROM platform_lifecycle_errors WHERE scope=?')
+ .bind(scope)
+ .run()
+ } catch {
+ await lifecycleFailure(db, scope, now, u.id)
+ }
+ }
await db
.prepare('UPDATE platform_lifecycle_cursor SET value=? WHERE id=1')
.bind(users.length === limit ? users.at(-1).id : 0)
@@ -389,34 +439,34 @@ export async function deliverLifecycleMail(
.bind(token, now + 120000, row.id, now)
.first()
if (!claimed) continue
- const p = JSON.parse(row.payload_json),
- u = await db
- .prepare('SELECT * FROM platform_users WHERE id=?')
- .bind(row.user_id)
- .first<any>(),
- marked = await db
- .prepare('SELECT * FROM platform_account_deletions WHERE user_id=?')
- .bind(row.user_id)
- .first<any>()
- const valid =
- u &&
- (row.kind === 'account-deleted'
- ? marked?.generation === row.cycle
- : s.enabled &&
- !marked &&
- !(await accountExemption(db, row.user_id, s.exemptions!, now)) &&
- deletionDeadline(u, s) === p.deadline &&
- p.deadline > now)
- if (!valid) {
- await db
- .prepare(
- "UPDATE platform_mail_outbox SET state='cancelled',lease_token=NULL,lease_until=NULL WHERE id=? AND lease_token=?",
- )
- .bind(row.id, token)
- .run()
- continue
- }
try {
+ const p = JSON.parse(row.payload_json),
+ u = await db
+ .prepare('SELECT * FROM platform_users WHERE id=?')
+ .bind(row.user_id)
+ .first<any>(),
+ marked = await db
+ .prepare('SELECT * FROM platform_account_deletions WHERE user_id=?')
+ .bind(row.user_id)
+ .first<any>()
+ const valid =
+ u &&
+ (row.kind === 'account-deleted'
+ ? marked?.generation === row.cycle
+ : s.enabled &&
+ !marked &&
+ !(await accountExemption(db, row.user_id, s.exemptions!, now)) &&
+ deletionDeadline(u, s) === p.deadline &&
+ p.deadline > now)
+ if (!valid) {
+ await db
+ .prepare(
+ "UPDATE platform_mail_outbox SET state='cancelled',lease_token=NULL,lease_until=NULL WHERE id=? AND lease_token=?",
+ )
+ .bind(row.id, token)
+ .run()
+ continue
+ }
await sendMail(
db,
email,
diff --git a/packages/platform/src/account-purge.ts b/packages/platform/src/account-purge.ts
index 40440b85..63980478 100644
--- a/packages/platform/src/account-purge.ts
+++ b/packages/platform/src/account-purge.ts
@@ -16,7 +16,7 @@ export async function purgeAccounts(
) {
const row = await db
.prepare(
- 'SELECT * FROM platform_account_deletions WHERE purge_at<=? AND next_attempt_at<=? AND (lease_until IS NULL OR lease_until<?) ORDER BY purge_at,user_id LIMIT 1',
+ 'SELECT * FROM platform_account_deletions WHERE purge_at<=? AND next_attempt_at<=? AND (lease_until IS NULL OR lease_until<?) ORDER BY next_attempt_at,purge_at,user_id LIMIT 1',
)
.bind(now, now, now)
.first<any>()
@@ -80,7 +80,7 @@ export async function purgeAccounts(
.prepare(
'UPDATE platform_account_deletions SET lease_token=NULL,lease_until=NULL,next_attempt_at=?,error=NULL WHERE user_id=? AND lease_token=?',
)
- .bind(now + 60000, id, token)
+ .bind(now + 1000, id, token)
.run()
return
}
@@ -210,9 +210,25 @@ export async function purgeAccounts(
statements.push(
db
.prepare(
- 'DELETE FROM platform_verifications WHERE instr(identifier,(SELECT email FROM platform_users WHERE id=?))>0',
+ "DELETE FROM platform_verifications WHERE identifier IN ('email-verification-otp-' || (SELECT email FROM platform_users WHERE id=?), 'sign-in-otp-' || (SELECT email FROM platform_users WHERE id=?), 'forget-password-otp-' || (SELECT email FROM platform_users WHERE id=?))",
)
- .bind(id),
+ .bind(id, id, id),
+ )
+ const factorKeys =
+ "SELECT identifier FROM platform_verifications WHERE value=? AND identifier LIKE '2fa-%' AND identifier NOT LIKE '2fa-otp-%' AND identifier NOT LIKE '2fa-attempts-%'"
+ statements.push(
+ db
+ .prepare(
+ `DELETE FROM platform_verifications WHERE identifier IN(SELECT '2fa-attempts-'||identifier FROM (${factorKeys})) OR identifier IN(SELECT '2fa-otp-'||identifier FROM (${factorKeys}))`,
+ )
+ .bind(String(id), String(id)),
+ )
+ statements.push(
+ db
+ .prepare(
+ "DELETE FROM platform_verifications WHERE value=? AND (identifier LIKE 'reset-password:%' OR identifier LIKE 'trust-device-%' OR (identifier LIKE '2fa-%' AND identifier NOT LIKE '2fa-otp-%' AND identifier NOT LIKE '2fa-attempts-%'))",
+ )
+ .bind(String(id)),
)
statements.push(
db
diff --git a/packages/platform/src/mail.ts b/packages/platform/src/mail.ts
index 326c8468..2f6c1e2a 100644
--- a/packages/platform/src/mail.ts
+++ b/packages/platform/src/mail.ts
@@ -39,13 +39,13 @@ const content: Record<MailKind, [string, string, string, string]> = {
'deletion-warning': [
'Your ChronoFrame Cloud account is scheduled for deletion',
'Keep your account',
- '{{reason}} Your account will be marked as deleted and sign-in disabled on {{deletionAt}}. All owned workspaces, sites, domains and files will be permanently removed on {{purgeAt}}. {{action}} Contact [email protected] if you need help.',
+ '{{reason}} Your account will be marked as deleted and sign-in disabled on {{deletionAt}}. Permanent cleanup of all owned workspaces, sites, domains and files will begin on {{purgeAt}}. {{action}} Contact [email protected] if you need help.',
'Open ChronoFrame Cloud',
],
'account-deleted': [
'Your ChronoFrame Cloud account has been deleted',
'Your account has been marked as deleted',
- '{{reason}} Sign-in is now disabled. After the {{retentionDays}}-day recovery period, all owned workspaces, sites, domains and files will be permanently removed on {{purgeAt}}. Until then, contact [email protected] to request restoration.',
+ '{{reason}} Sign-in is now disabled. After the {{retentionDays}}-day recovery period, permanent cleanup of all owned workspaces, sites, domains and files will begin on {{purgeAt}}. Until then, contact [email protected] to request restoration.',
'Contact support',
],
}
diff --git a/packages/storage/src/s3-edge.ts b/packages/storage/src/s3-edge.ts
index eaf3c079..5bfbe0cf 100644
--- a/packages/storage/src/s3-edge.ts
+++ b/packages/storage/src/s3-edge.ts
@@ -4,6 +4,7 @@ import {
GetObjectCommand,
HeadObjectCommand,
DeleteObjectCommand,
+ DeleteObjectsCommand,
CreateMultipartUploadCommand,
UploadPartCommand,
CompleteMultipartUploadCommand,
@@ -181,63 +182,80 @@ export function createCloudS3(
)
throw error
}
- if (versioned) {
- const versions = await client.send(
- new ListObjectVersionsCommand({
+ const deadline = Date.now() + 15000
+ async function removeBatch(
+ entries: { Key: string; VersionId?: string }[],
+ ) {
+ for (const entry of entries)
+ if (!entry.Key.startsWith(prefix))
+ throw new Error('Invalid object scope')
+ if (!entries.length) return
+ const result = await client.send(
+ new DeleteObjectsCommand({
Bucket,
- Prefix: prefix,
- MaxKeys: 20,
+ Delete: { Objects: entries, Quiet: true },
}),
)
- const entries = [
- ...(versions.Versions ?? []),
- ...(versions.DeleteMarkers ?? []),
- ]
- for (const entry of entries) {
- if (!entry.Key?.startsWith(prefix) || !entry.VersionId)
- throw new Error('Invalid version scope')
- await client.send(
- new DeleteObjectCommand({
+ if (result.Errors?.length)
+ throw new Error('Object batch removal incomplete')
+ }
+ for (let page = 0; page < 4 && Date.now() < deadline; page++) {
+ // Read the first remaining page after deletion, so retries cannot skip shifted keys.
+ if (versioned) {
+ const versions = await client.send(
+ new ListObjectVersionsCommand({
Bucket,
- Key: entry.Key,
- VersionId: entry.VersionId,
+ Prefix: prefix,
+ MaxKeys: 1000,
}),
)
+ const entries = [
+ ...(versions.Versions ?? []),
+ ...(versions.DeleteMarkers ?? []),
+ ].map((v) => {
+ if (!v.Key?.startsWith(prefix) || !v.VersionId)
+ throw new Error('Invalid version scope')
+ return { Key: v.Key, VersionId: v.VersionId }
+ })
+ await removeBatch(entries)
+ if (entries.length || versions.IsTruncated) continue
}
- if (entries.length || versions.IsTruncated) return false
- }
- const uploads = await client.send(
- new ListMultipartUploadsCommand({
- Bucket,
- Prefix: prefix,
- MaxUploads: 10,
- }),
- )
- for (const upload of uploads.Uploads ?? []) {
- if (!upload.Key?.startsWith(prefix) || !upload.UploadId)
- throw new Error('Invalid multipart scope')
- await client.send(
- new AbortMultipartUploadCommand({
+ const uploads = await client.send(
+ new ListMultipartUploadsCommand({
Bucket,
- Key: upload.Key,
- UploadId: upload.UploadId,
+ Prefix: prefix,
+ MaxUploads: 10,
}),
)
+ for (const upload of uploads.Uploads ?? []) {
+ if (!upload.Key?.startsWith(prefix) || !upload.UploadId)
+ throw new Error('Invalid multipart scope')
+ await client.send(
+ new AbortMultipartUploadCommand({
+ Bucket,
+ Key: upload.Key,
+ UploadId: upload.UploadId,
+ }),
+ )
+ }
+ const objects = await client.send(
+ new ListObjectsV2Command({ Bucket, Prefix: prefix, MaxKeys: 1000 }),
+ )
+ const entries = (objects.Contents ?? []).map((o) => {
+ if (!o.Key?.startsWith(prefix))
+ throw new Error('Invalid object scope')
+ return { Key: o.Key }
+ })
+ await removeBatch(entries)
+ if (
+ !entries.length &&
+ !objects.IsTruncated &&
+ !uploads.Uploads?.length &&
+ !uploads.IsTruncated
+ )
+ return true
}
- const objects = await client.send(
- new ListObjectsV2Command({ Bucket, Prefix: prefix, MaxKeys: 20 }),
- )
- for (const object of objects.Contents ?? []) {
- if (!object.Key?.startsWith(prefix))
- throw new Error('Invalid object scope')
- await client.send(new DeleteObjectCommand({ Bucket, Key: object.Key }))
- }
- return !(
- uploads.Uploads?.length ||
- uploads.IsTruncated ||
- objects.Contents?.length ||
- objects.IsTruncated
- )
+ return false
},
async remove(key: string) {
await client.send(new DeleteObjectCommand({ Bucket, Key: safeKey(key) }))
diff --git a/tests/account-lifecycle-mail.test.ts b/tests/account-lifecycle-mail.test.ts
index 9a4cb603..54c35bed 100644
--- a/tests/account-lifecycle-mail.test.ts
+++ b/tests/account-lifecycle-mail.test.ts
@@ -299,6 +299,17 @@ test('Lifecycle notices, current-login race, restore, retained deadline and retr
).suspended,
1,
)
+ await db
+ .prepare(
+ "INSERT INTO platform_verifications(identifier,value,expires_at,created_at,updated_at) VALUES('[email protected]','own',?,?,?),('[email protected]','other',?,?,?)",
+ )
+ .bind(now + DAY, now, now, now + DAY, now, now)
+ .run()
+ await db
+ .prepare(
+ "INSERT INTO platform_verifications(identifier,value,expires_at,created_at,updated_at) VALUES('reset-password:own','3',9999999999999,0,0),('reset-password:other','13',9999999999999,0,0),('2fa-own','3',9999999999999,0,0),('2fa-attempts-2fa-own','0',9999999999999,0,0),('2fa-otp-2fa-own','hash',9999999999999,0,0),('trust-device-own','3',9999999999999,0,0)",
+ )
+ .run()
let domainReady = false,
storageReady = false,
storageCalls = 0
@@ -348,6 +359,39 @@ test('Lifecycle notices, current-login race, restore, retained deadline and retr
assert.ok(
await db.prepare('SELECT id FROM cloud_photos WHERE site_id=2').first(),
)
+ assert.ok(
+ await db
+ .prepare(
+ "SELECT id FROM platform_verifications WHERE identifier='[email protected]'",
+ )
+ .first(),
+ )
+ assert.equal(
+ await db
+ .prepare(
+ "SELECT id FROM platform_verifications WHERE identifier='[email protected]'",
+ )
+ .first(),
+ null,
+ )
+
+ assert.equal(
+ (
+ await db
+ .prepare(
+ "SELECT COUNT(*) AS n FROM platform_verifications WHERE identifier IN('reset-password:own','2fa-own','2fa-attempts-2fa-own','2fa-otp-2fa-own','trust-device-own')",
+ )
+ .first()
+ ).n,
+ 0,
+ )
+ assert.ok(
+ await db
+ .prepare(
+ "SELECT id FROM platform_verifications WHERE identifier='reset-password:other'",
+ )
+ .first(),
+ )
assert.equal(
(
await db
@@ -510,6 +554,19 @@ test('S3 cleanup lists and deletes only the exact tenant prefix, including unfin
{ headers: { 'content-type': 'application/xml' } },
)
}
+ if (url.searchParams.has('delete')) {
+ assert.equal(method, 'POST')
+ const body =
+ input instanceof Request
+ ? await input.clone().text()
+ : await new Response(init?.body as BodyInit).text()
+ assert.ok(body.includes('<Key>chronoframe/7/original.jpg</Key>'))
+ removed.push('chronoframe/7/original.jpg')
+ objects = false
+ return new Response('<DeleteResult/>', {
+ headers: { 'content-type': 'application/xml' },
+ })
+ }
assert.equal(method, 'DELETE')
assert.ok(url.pathname.startsWith('/photos/chronoframe/7/'))
if (url.searchParams.has('uploadId')) {
@@ -523,7 +580,7 @@ test('S3 cleanup lists and deletes only the exact tenant prefix, including unfin
}) as typeof fetch,
},
)
- assert.equal(await s3.purgeTenant(7), false)
+ assert.equal(await s3.purgeTenant(7), true)
assert.equal(await s3.purgeTenant(7), true)
assert.equal(removed.length, 1)
assert.equal(aborted.length, 1)
@@ -751,3 +808,324 @@ test('Subscription exemptions match live/expired history, sources, tiers and com
await mf.dispose()
}
})
+test('Migration retains known sign-in timestamps and safely enrolls existing accounts with unknown history', async () => {
+ const mf = new Miniflare(
+ convertV4MiniflareOptions({
+ modules: true,
+ compatibilityDate: '2026-10-26',
+ script: 'export default {fetch(){return new Response("ok")}}',
+ d1Databases: ['DB'],
+ }),
+ )
+ try {
+ const db = await mf.getD1Database('DB'),
+ now = Date.now()
+ for (const name of (
+ await readdir(
+ new URL('../packages/platform/migrations/', import.meta.url),
+ )
+ )
+ .filter((f) => f.endsWith('.sql'))
+ .sort()) {
+ if (name.startsWith('0020')) {
+ await db
+ .prepare(
+ "INSERT INTO platform_users(id,email,created_at) VALUES(1,'[email protected]',?),(2,'[email protected]',?)",
+ )
+ .bind(now - 200 * DAY, now - 200 * DAY)
+ .run()
+ await db
+ .prepare(
+ "INSERT INTO platform_auth_sessions(token,user_id,created_at,updated_at,expires_at) VALUES('known',2,?,?,?)",
+ )
+ .bind(new Date(now - 20 * DAY).toISOString(), now, now + DAY)
+ .run()
+ }
+ for (const sql of (
+ await readFile(
+ new URL('../packages/platform/migrations/' + name, import.meta.url),
+ 'utf8',
+ )
+ )
+ .replace(/^--.*$/gm, '')
+ .split(';')
+ .filter((s) => s.trim()))
+ await db.prepare(sql).run()
+ }
+ const known = await db
+ .prepare('SELECT last_login_at FROM platform_users WHERE id=2')
+ .first(),
+ unknown = await db
+ .prepare('SELECT last_login_at FROM platform_users WHERE id=1')
+ .first()
+ assert.ok(Math.abs(known.last_login_at - (now - 20 * DAY)) < 1000)
+ assert.ok(
+ unknown.last_login_at >= now - 1000 &&
+ unknown.last_login_at <= Date.now(),
+ )
+ } finally {
+ await mf.dispose()
+ }
+})
+test('Malformed subscription history cannot starve scanning or independent lifecycle stages', async () => {
+ const { lifecycleStages } =
+ await import('../packages/platform/src/account-lifecycle.ts')
+ const { mf, db } = await fixture()
+ try {
+ const now = Date.now(),
+ exemptions = {
+ enabled: true,
+ match: 'any',
+ rules: [{ kind: 'ever-subscribed', sources: ['manual'], planIds: [] }],
+ }
+ await db
+ .prepare(
+ 'UPDATE platform_lifecycle_settings SET activated_at=?,exemptions_json=?',
+ )
+ .bind(now - 200 * DAY, JSON.stringify(exemptions))
+ .run()
+ await db
+ .prepare(
+ "INSERT INTO platform_users(id,email,email_verified,created_at,last_login_at) VALUES(1,'[email protected]',1,?,?),(2,'[email protected]',1,?,?)",
+ )
+ .bind(now - 100 * DAY, now - 100 * DAY, now - 100 * DAY, now - 100 * DAY)
+ .run()
+ await db
+ .prepare(
+ "INSERT INTO platform_workspaces(id,name,owner_user_id) VALUES(1,'Bad history',1)",
+ )
+ .run()
+ await db
+ .prepare(
+ "INSERT INTO platform_subscription_events(id,workspace_id,kind,before_json,after_json,created_at) VALUES('invalid',1,'assigned','not-json','null',?)",
+ )
+ .bind(now)
+ .run()
+ await scanAccounts(db, now, 1)
+ assert.equal(
+ (await db.prepare('SELECT value FROM platform_lifecycle_cursor').first())
+ .value,
+ 1,
+ )
+ assert.ok(
+ await db
+ .prepare(
+ "SELECT scope FROM platform_lifecycle_errors WHERE scope='scan-user:1'",
+ )
+ .first(),
+ )
+ await scanAccounts(db, now, 1)
+ assert.ok(
+ await db
+ .prepare(
+ 'SELECT user_id FROM platform_account_deletions WHERE user_id=2',
+ )
+ .first(),
+ )
+ const ran: string[] = []
+ await lifecycleStages(
+ db,
+ {
+ scan: async () => {
+ throw new Error('sensitive failure must not be logged')
+ },
+ mail: async () => {
+ ran.push('mail')
+ },
+ purge: async () => {
+ ran.push('purge')
+ },
+ outbox: async () => {
+ ran.push('outbox')
+ },
+ },
+ now,
+ )
+ assert.deepEqual(ran, ['mail', 'purge', 'outbox'])
+ assert.equal(
+ (
+ await db
+ .prepare(
+ "SELECT error FROM platform_lifecycle_errors WHERE scope='scan'",
+ )
+ .first()
+ ).error,
+ 'lifecycle_stage_failed',
+ )
+ } finally {
+ await mf.dispose()
+ }
+})
+test('Actual rolling session refresh records activity and prevents an active account from aging out', async () => {
+ const { mf, db } = await fixture()
+ try {
+ const now = Date.now(),
+ messages: string[] = [],
+ portal = 'https://app.chronoframe.art'
+ const auth = createAccountAuth(
+ db,
+ {
+ portalOrigin: portal,
+ secret:
+ '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_',
+ emailFrom: '[email protected]',
+ },
+ {
+ send: async (m: any) => {
+ messages.push(m.text)
+ return { messageId: 'test' }
+ },
+ },
+ )
+ const call = (path: string, body?: any, cookie?: string) =>
+ auth.handler(
+ new Request(portal + '/api/auth' + path, {
+ method: body ? 'POST' : 'GET',
+ headers: {
+ origin: portal,
+ 'content-type': 'application/json',
+ 'cf-connecting-ip': '192.0.2.51',
+ ...(cookie ? { cookie } : {}),
+ },
+ ...(body ? { body: JSON.stringify(body) } : {}),
+ }),
+ )
+ assert.equal(
+ (
+ await call('/sign-up/email', {
+ email: '[email protected]',
+ name: 'Active',
+ password: 'Strong-example-password-123',
+ })
+ ).status,
+ 200,
+ )
+ const verified = await call('/email-otp/verify-email', {
+ email: '[email protected]',
+ otp: messages.at(-1)!.match(/\b\d{6}\b/)![0],
+ })
+ assert.equal(verified.status, 200)
+ const cookie = verified.headers
+ .getSetCookie()
+ .find((c) => c.startsWith('__Host-chronoframe-auth='))!
+ .split(';')[0]!
+ const user = await db
+ .prepare("SELECT id FROM platform_users WHERE email='[email protected]'")
+ .first()
+ await db
+ .prepare('UPDATE platform_users SET last_login_at=? WHERE id=?')
+ .bind(now - 100 * DAY, user.id)
+ .run()
+ await db
+ .prepare(
+ 'UPDATE platform_auth_sessions SET updated_at=?,expires_at=? WHERE user_id=?',
+ )
+ .bind(
+ new Date(now - 2 * DAY).toISOString(),
+ new Date(now + 5 * DAY).toISOString(),
+ user.id,
+ )
+ .run()
+ const response = await call('/get-session', undefined, cookie)
+ assert.equal(response.status, 200, await response.clone().text())
+ assert.ok(
+ (
+ await db
+ .prepare('SELECT last_login_at FROM platform_users WHERE id=?')
+ .bind(user.id)
+ .first()
+ ).last_login_at >= now,
+ )
+ await db
+ .prepare('UPDATE platform_lifecycle_settings SET activated_at=?')
+ .bind(now - 200 * DAY)
+ .run()
+ await scanAccounts(db, now + 1000)
+ assert.equal(
+ await db
+ .prepare(
+ 'SELECT user_id FROM platform_account_deletions WHERE user_id=?',
+ )
+ .bind(user.id)
+ .first(),
+ null,
+ )
+ } finally {
+ await mf.dispose()
+ }
+})
+test('Versioned S3 cleanup batches 1000 scoped versions and surfaces partial deletion errors', async () => {
+ const { createCloudS3 } = await import('../packages/storage/src/s3-edge.ts')
+ let remaining = true,
+ batches = 0,
+ fail = false
+ const keys = Array.from({ length: 1000 }, (_, i) => `chronoframe/8/p${i}`)
+ const s3 = createCloudS3(
+ {
+ endpoint: 'https://s3.us-east-1.amazonaws.com',
+ region: 'us-east-1',
+ bucket: 'photos',
+ accessKeyId: 'fake',
+ secretAccessKey: 'fake',
+ },
+ {
+ fetch: (async (input, init) => {
+ const url = new URL(
+ input instanceof Request ? input.url : String(input),
+ )
+ if (url.searchParams.has('versioning'))
+ return new Response(
+ '<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>',
+ { headers: { 'content-type': 'application/xml' } },
+ )
+ if (url.searchParams.has('versions')) {
+ assert.equal(url.searchParams.get('max-keys'), '1000')
+ return new Response(
+ '<ListVersionsResult>' +
+ (remaining
+ ? keys
+ .map(
+ (k, i) =>
+ `<Version><Key>${k}</Key><VersionId>v${i}</VersionId></Version>`,
+ )
+ .join('')
+ : '') +
+ '<IsTruncated>false</IsTruncated></ListVersionsResult>',
+ { headers: { 'content-type': 'application/xml' } },
+ )
+ }
+ if (url.searchParams.has('delete')) {
+ const body =
+ input instanceof Request
+ ? await input.clone().text()
+ : await new Response(init?.body as BodyInit).text()
+ assert.equal((body.match(/<Object>/g) ?? []).length, 1000)
+ assert.equal((body.match(/<VersionId>/g) ?? []).length, 1000)
+ assert.ok(body.includes('<Key>chronoframe/8/p999</Key>'))
+ batches++
+ remaining = false
+ return new Response(
+ fail
+ ? '<DeleteResult><Error><Key>chronoframe/8/p0</Key><Code>AccessDenied</Code><Message>blocked</Message></Error></DeleteResult>'
+ : '<DeleteResult/>',
+ { headers: { 'content-type': 'application/xml' } },
+ )
+ }
+ if (url.searchParams.has('uploads'))
+ return new Response(
+ '<ListMultipartUploadsResult><IsTruncated>false</IsTruncated></ListMultipartUploadsResult>',
+ { headers: { 'content-type': 'application/xml' } },
+ )
+ return new Response(
+ '<ListBucketResult><IsTruncated>false</IsTruncated></ListBucketResult>',
+ { headers: { 'content-type': 'application/xml' } },
+ )
+ }) as typeof fetch,
+ },
+ )
+ assert.equal(await s3.purgeTenant(8), true)
+ assert.equal(batches, 1)
+ remaining = true
+ fail = true
+ await assert.rejects(s3.purgeTenant(8), /batch removal incomplete/)
+})