Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Check a decline's recipients under the lock, not the audit trail
Claudec0d4ac354ac0
diff --git a/src/lib/signer-correction.dbtest.ts b/src/lib/signer-correction.dbtest.tsindex 804cc84..f4c016f 100644--- a/src/lib/signer-correction.dbtest.ts+++ b/src/lib/signer-correction.dbtest.ts@@ -1494,7 +1494,9 @@ test("a failed reminder does not bring back the link a correction replaced", asyassert.equal((await verifyAuditChain(envelopeId, database)).ok, true);});-test("a decline whose certificate missed another signer's correction is refused", async () => {+async function declineRacingAnotherCorrection(+ isTheStatement: (text: string) => boolean,+) {const { userId } = await createOwner();const envelopeId = await createEnvelope({ userId });const declining = await createSigner({ envelopeId });@@ -1538,19 +1540,11 @@ test("a decline whose certificate missed another signer's correction is refused"Object.assign(process.env, settings);- // The correction lands after the decline has read the audit trail for- // its certificate, and before it takes the envelope lock.- const pool = afterStatement(- database,- (text) =>- text.includes("FROM audit_events") &&- text.includes("ORDER BY sequence ASC NULLS FIRST"),- async () => {- await correct(userId, envelopeId, waiting.signerId, {- email: "[email protected]",- });- },- );+ const pool = afterStatement(database, isTheStatement, async () => {+ await correct(userId, envelopeId, waiting.signerId, {+ email: "[email protected]",+ });+ });try {await assert.rejects(@@ -1590,6 +1584,24 @@ test("a decline whose certificate missed another signer's correction is refused"await rm(storageDir, { force: true, recursive: true });}+}++test("a decline whose certificate missed another signer's correction is refused", async () => {+ // The correction lands after the decline has read the audit trail for+ // its certificate, and before it takes the envelope lock.+ await declineRacingAnotherCorrection(+ (text) =>+ text.includes("FROM audit_events") &&+ text.includes("ORDER BY sequence ASC NULLS FIRST"),+ );+});++test("a decline whose recipients were read before another signer's correction is refused", async () => {+ // The correction lands after the decline has read the recipients, so the+ // audit trail it reads afterwards already holds the correction.+ await declineRacingAnotherCorrection((text) =>+ text.includes('access_code_hash AS "accessCodeHash"'),+ );});test("the corrected recipient's first view is recorded", async () => {diff --git a/src/lib/signing.ts b/src/lib/signing.tsindex 8a1e505..5a28d43 100644--- a/src/lib/signing.ts+++ b/src/lib/signing.ts@@ -3080,11 +3080,10 @@ export async function declineSigningForToken(declinedAt,declineReason,);- const auditEventsBefore = await listAuditEventsForEnvelope(- context.envelope.id,- database,- );- const auditEvents = [...auditEventsBefore, declinedAuditEvent];+ const auditEvents = [+ ...(await listAuditEventsForEnvelope(context.envelope.id, database)),+ declinedAuditEvent,+ ];const storedCertificateKeys: string[] = [];let certificate: { key: string; sha256: string };let recipientCertificateSha256: string;@@ -3140,21 +3139,31 @@ export async function declineSigningForToken(tokenHash: presentedTokenHash,});- // The certificates above were made before the envelope lock. If the- // sender corrected another recipient in that time, they name the old- // person and leave the correction out, so they must not be kept.- const correctedSince = await tx.query(+ // The certificates above were made before the envelope lock, from the+ // recipients read when the link was opened. If the sender corrected one+ // of them since, they name the old person and leave the correction+ // out, so they must not be kept. A correction cannot commit while this+ // lock is held, so comparing here covers the whole window.+ const recipientsNow = await tx.query<{+ email: string;+ id: string;+ name: string;+ }>(`- SELECT 1 FROM audit_events- WHERE envelope_id = $1- AND event = 'signer_corrected'- AND NOT (id = ANY($2::text[]))- LIMIT 1+ SELECT id, name, email FROM signers+ WHERE envelope_id = $1 AND id = ANY($2::text[])`,- [context.envelope.id, auditEventsBefore.map((event) => event.id)],+ [context.envelope.id, context.signers.map((signer) => signer.id)],);+ const recipientsChanged =+ recipientsNow.rows.length !== context.signers.length ||+ recipientsNow.rows.some((row) => {+ const seen = context.signers.find((signer) => signer.id === row.id);++ return !seen || seen.name !== row.name || seen.email !== row.email;+ });- if ((correctedSince.rowCount ?? 0) > 0) {+ if (recipientsChanged) {throw new SigningMutationError("This envelope changed while you were declining. Open your signing link again.",409,