diff --git a/keyserver/src/cron/cron.js b/keyserver/src/cron/cron.js --- a/keyserver/src/cron/cron.js +++ b/keyserver/src/cron/cron.js @@ -16,6 +16,7 @@ deleteOrphanedSessions, deleteOldWebSessions, } from '../deleters/session-deleters'; +import { deleteStaleSIWENonceEntries } from '../deleters/siwe-nonce-deleters.js'; import { deleteInaccessibleThreads } from '../deleters/thread-deleters'; import { deleteExpiredUpdates } from '../deleters/update-deleters'; import { deleteUnassignedUploads } from '../deleters/upload-deleters'; @@ -43,6 +44,7 @@ await deleteOldWebSessions(); await deleteExpiredUpdates(); await deleteUnassignedUploads(); + await deleteStaleSIWENonceEntries(); } catch (e) { console.warn('encountered error while trying to clean database', e); } diff --git a/keyserver/src/deleters/siwe-nonce-deleters.js b/keyserver/src/deleters/siwe-nonce-deleters.js new file mode 100644 --- /dev/null +++ b/keyserver/src/deleters/siwe-nonce-deleters.js @@ -0,0 +1,17 @@ +// @flow + +import { dbQuery, SQL } from '../database/database'; + +// 30 minutes = 30min * 60sec * 1000ms +export const nonceLifetime = 30 * 60 * 1000; + +async function deleteStaleSIWENonceEntries(): Promise { + const earliestValidCreationTime = Date.now() - nonceLifetime; + const query = SQL` + DELETE FROM siwe_nonces + WHERE creation_time < ${earliestValidCreationTime} + `; + await dbQuery(query); +} + +export { deleteStaleSIWENonceEntries };