Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Check a signing link's expiry once the signer's row is locked
Claude52523f5b3ea5
diff --git a/src/lib/embedding.dbtest.ts b/src/lib/embedding.dbtest.tsindex c7f6032..51af6d9 100644--- a/src/lib/embedding.dbtest.ts+++ b/src/lib/embedding.dbtest.ts@@ -832,6 +832,7 @@ test("a signing already under way when its site is removed does not finish", asy// expires the link (as removeEmbedOrigin does) once the submission waits.const remover = await database.connect();let submitted: Promise<unknown> | undefined;+ const startedAt = Math.floor(Date.now() / 1000);try {await remover.query("begin");@@ -858,6 +859,14 @@ test("a signing already under way when its site is removed does not finish", asysubmitted.catch(() => undefined);await waitUntilBlockedBy(database, remover);++ // The removal lands seconds into the wait, so the link's new expiry is+ // later than any time the submission read before it began to wait. A+ // check against that earlier time would still let it through.+ while (Math.floor(Date.now() / 1000) < startedAt + 3) {+ await new Promise((resolve) => setTimeout(resolve, 100));+ }+await remover.query(`update signers set signing_token_expires_at = $2 where id = $1`,[signerId, Math.floor(Date.now() / 1000) - 1],diff --git a/src/lib/signing.ts b/src/lib/signing.tsindex 20f5cd2..16a307d 100644--- a/src/lib/signing.ts+++ b/src/lib/signing.ts@@ -805,22 +805,29 @@ async function lockSignerForPresentedToken(tokenHash: string;},): Promise<void> {- const locked = await queryOne<{ id: string }>(+ const locked = await queryOne<{ expiresAt: number | null }>(tx,`- SELECT id+ SELECT signing_token_expires_at AS "expiresAt"FROM signersWHERE id = $1AND envelope_id = $2AND status = 'notified'AND signing_token_hash = $3- AND (signing_token_expires_at IS NULL OR signing_token_expires_at >= $4)FOR NO KEY UPDATE`,- [signerId, envelopeId, tokenHash, Math.floor(Date.now() / 1000)],+ [signerId, envelopeId, tokenHash],);- if (!locked) {+ // The expiry is checked once the lock is held, on the row as it is then+ // and against the time then. A time taken before the query would be as+ // old as the wait for the lock, and could let through a link that+ // expired during it, such as one whose site was removed meanwhile.+ if (+ !locked ||+ (locked.expiresAt !== null &&+ locked.expiresAt < Math.floor(Date.now() / 1000))+ ) {throw new SigningMutationError("This signing link is no longer active.",409,