Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Fix the second round of review findings

Claudef1c2860b8d5b
diff --git a/src/lib/downloads.ts b/src/lib/downloads.ts
index 0666997..c5278f4 100644
--- a/src/lib/downloads.ts
+++ b/src/lib/downloads.ts
@@ -883,12 +883,22 @@ export async function renewRecipientDownloadLinks(
signer,
});
} catch (error) {
+ const issuedHashes = new Map<string, string>([
+ ["evidence", evidenceLink.tokenHash],
+ ...(signedLink
+ ? [["signed", signedLink.tokenHash] as [string, string]]
+ : []),
+ ]);
+
for (const previous of previousTokens) {
+ // Only where this request's link is still the stored one: a renewal
+ // that ran meanwhile and did get its email out keeps its links.
await database.query(
`
UPDATE recipient_download_tokens
SET token_hash = $1, expires_at = $2, created_at = $3
WHERE envelope_id = $4 AND signer_id = $5 AND file_type = $6
+ AND token_hash = $7
`,
[
previous.tokenHash,
@@ -897,6 +907,7 @@ export async function renewRecipientDownloadLinks(
envelope.id,
signer.id,
previous.fileType,
+ issuedHashes.get(previous.fileType) ?? "",
],
);
}
diff --git a/src/lib/envelopes.ts b/src/lib/envelopes.ts
index b679bc5..1a0f464 100644
--- a/src/lib/envelopes.ts
+++ b/src/lib/envelopes.ts
@@ -1610,6 +1610,12 @@ export async function sendDraftEnvelope(
AND event = 'sent'
AND created_at = $2
AND actor_email = $3
+ -- Only while it is still the newest link of the audit chain: an
+ -- event recorded after it (say, the invitation was opened before
+ -- the send failed) chains to it, so it stays as history.
+ AND sequence = (
+ SELECT max(sequence) FROM audit_events WHERE envelope_id = $1
+ )
`,
[envelopeId, sentAuditAt, input.senderEmail],
);
diff --git a/src/lib/reminders.ts b/src/lib/reminders.ts
index db6559b..04a640f 100644
--- a/src/lib/reminders.ts
+++ b/src/lib/reminders.ts
@@ -438,6 +438,12 @@ export async function sendManualEnvelopeNudge(
AND event = 'reminded_manual'
AND created_at = $2
AND actor_email = $3
+ -- Only while it is still the newest link of the audit chain: an
+ -- event recorded after it (say, the invitation was opened before
+ -- the send failed) chains to it, so it stays as history.
+ AND sequence = (
+ SELECT max(sequence) FROM audit_events WHERE envelope_id = $1
+ )
`,
[persisted.envelopeId, nowUnixTimestamp, input.senderEmail],
);
diff --git a/src/lib/retention.ts b/src/lib/retention.ts
index 5aab1aa..249f4c9 100644
--- a/src/lib/retention.ts
+++ b/src/lib/retention.ts
@@ -308,8 +308,10 @@ const FINISHED_ENVELOPE_STATUSES = ["completed", "declined", "voided"];
/**
* On a self-hosted install the organization owns its signed records, so a
* person leaving must not take them along. Finished envelopes move to an
- * administrator (or, failing that, any other active user) before the
- * account is deleted. Returns false when there is nobody to keep them.
+ * administrator before the
+ * account is deleted. Only an administrator may receive them: anyone else
+ * would gain documents they were never party to. Returns false when there
+ * is no administrator to keep them.
*/
async function handOverFinishedEnvelopes(
userId: string,
@@ -335,10 +337,11 @@ async function handOverFinishedEnvelopes(
SELECT u.id
FROM "user" u
WHERE u.id <> $1
+ AND u.role = 'admin'
AND NOT EXISTS (
SELECT 1 FROM account_closure_requests c WHERE c.user_id = u.id
)
- ORDER BY (u.role = 'admin') DESC, u."createdAt" ASC, u.id ASC
+ ORDER BY u."createdAt" ASC, u.id ASC
LIMIT 1
`,
[userId],
@@ -366,7 +369,7 @@ async function handOverFinishedEnvelopes(
export class AccountPurgeDeferredError extends Error {
constructor(userId: string) {
super(
- `Account ${userId} holds finished envelopes and no other user can keep them. Its purge is deferred until an administrator exists or the records are exported and deleted by hand.`,
+ `Account ${userId} holds finished envelopes and no other user can keep them. Its purge is deferred until another administrator exists or the records are exported and deleted by hand.`,
);
this.name = "AccountPurgeDeferredError";
}
diff --git a/src/lib/services/pdf-finalize.ts b/src/lib/services/pdf-finalize.ts
index e0974c3..151c9b7 100644
--- a/src/lib/services/pdf-finalize.ts
+++ b/src/lib/services/pdf-finalize.ts
@@ -90,16 +90,28 @@ export function computeAuditFingerprint(
return sha256Hex(
new TextEncoder().encode(
canonicalJson({
- events: auditEvents.map((event) => [
- event.id,
- event.createdAt,
- event.event,
- event.actorType,
- event.actorEmail ?? null,
- event.ipAddress ?? null,
- event.userAgent ?? null,
- event.metadata ?? null,
- ]),
+ // One fixed order (time, then id by code unit), whatever order the
+ // list was built or read back in.
+ events: [...auditEvents]
+ .sort(
+ (left, right) =>
+ (left.createdAt ?? 0) - (right.createdAt ?? 0) ||
+ (String(left.id) < String(right.id)
+ ? -1
+ : String(left.id) > String(right.id)
+ ? 1
+ : 0),
+ )
+ .map((event) => [
+ event.id,
+ event.createdAt,
+ event.event,
+ event.actorType,
+ event.actorEmail ?? null,
+ event.ipAddress ?? null,
+ event.userAgent ?? null,
+ event.metadata ?? null,
+ ]),
version: CERTIFICATE_FINGERPRINT_VERSION,
}),
),
diff --git a/src/lib/signing.ts b/src/lib/signing.ts
index 9b3db7f..9848f9d 100644
--- a/src/lib/signing.ts
+++ b/src/lib/signing.ts
@@ -1102,8 +1102,9 @@ export async function sendSignerEmailCode(
const issued = issueEmailCode();
const branding = await getBranding();
-
- await database.query(
+ // Claim the resend window and save the code in one statement, so two
+ // tabs cannot both send, leaving one emailed code already replaced.
+ const claimed = await database.query(
`
UPDATE signers
SET
@@ -1112,10 +1113,30 @@ export async function sendSignerEmailCode(
email_code_attempts = 0,
email_code_sent_at = $3
WHERE id = $4
+ AND (
+ email_code_hash IS NULL
+ OR email_code_sent_at IS NULL
+ OR email_code_sent_at <= $5
+ )
`,
- [issued.hash, now + EMAIL_CODE_TTL_SECONDS, now, context.signer.id],
+ [
+ issued.hash,
+ now + EMAIL_CODE_TTL_SECONDS,
+ now,
+ context.signer.id,
+ now - EMAIL_CODE_RESEND_SECONDS,
+ ],
);
+ if (claimed.rowCount !== 1) {
+ const current = await getSignerCheckState(context.signer.id!, database);
+
+ return {
+ emailCodeSentAt: current.emailCodeSentAt ?? now,
+ maskedEmail: maskEmailAddress(context.signer.email),
+ };
+ }
+
const minutes = Math.round(EMAIL_CODE_TTL_SECONDS / 60);
const textBody = [
`Hi ${context.signer.name},`,