Skip to content

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

TypeScript

Make download-link renewal safe when two requests arrive at once

Claudeb6ce1d4c8586
diff --git a/src/lib/download-renewal.dbtest.ts b/src/lib/download-renewal.dbtest.ts
new file mode 100644
index 0000000..fcc989c
--- /dev/null
+++ b/src/lib/download-renewal.dbtest.ts
@@ -0,0 +1,251 @@
+import assert from "node:assert/strict";
+import { randomUUID } from "node:crypto";
+import { after, before, test } from "node:test";
+
+import type pg from "pg";
+
+import { openTestDatabase } from "./db/test-database.ts";
+import {
+ createRecipientDownloadLink,
+ DownloadAccessError,
+ renewRecipientDownloadLinks,
+} from "./downloads.ts";
+import { hashOpaqueToken } from "./services/opaque-tokens.ts";
+
+// Renewing a signer's download links, against a real Postgres: the point is
+// what two requests at once, a failed write and a failed email leave in the
+// table. Run with `npm run test:db`.
+
+process.env.APP_URL ??= "https://sign.example.test";
+
+let database: pg.Pool;
+const createdUsers: string[] = [];
+
+before(async () => {
+ database = await openTestDatabase();
+});
+
+after(async () => {
+ await database.query(`delete from public."user" where id = any($1)`, [
+ createdUsers,
+ ]);
+ await database.end();
+});
+
+/** A finished envelope whose one signer holds a link to each file. */
+async function createFinishedEnvelope() {
+ const userId = randomUUID();
+ const envelopeId = randomUUID();
+ const signerId = randomUUID();
+
+ createdUsers.push(userId);
+ await database.query(
+ `insert into public."user" (id, name, email, role) values ($1, 'Sender', $2, 'user')`,
+ [userId, `${userId}@example.test`],
+ );
+ await database.query(
+ `
+ insert into envelopes (
+ id, user_id, title, status, source_pdf_key, source_pdf_filename,
+ source_pdf_size_bytes, page_count, source_pdf_hash, completed_at
+ ) values ($1, $2, 'Test', 'completed', $3, 'test.pdf', 10, 1, $4, $5)
+ `,
+ [
+ envelopeId,
+ userId,
+ `test/${envelopeId}.pdf`,
+ "a".repeat(64),
+ Math.floor(Date.now() / 1000),
+ ],
+ );
+ await database.query(
+ `
+ insert into signers (id, envelope_id, name, email, sign_order, status)
+ values ($1, $2, 'Signer', $3, 1, 'signed')
+ `,
+ [signerId, envelopeId, `${signerId}@example.test`],
+ );
+
+ const signed = await createRecipientDownloadLink(
+ { envelopeId, fileType: "signed", signerId },
+ { database },
+ );
+ const evidence = await createRecipientDownloadLink(
+ { envelopeId, fileType: "evidence", signerId },
+ { database },
+ );
+
+ return { envelopeId, evidence, signed, signerId };
+}
+
+async function storedHashes(envelopeId: string, signerId: string) {
+ const result = await database.query<{
+ file_type: string;
+ token_hash: string;
+ }>(
+ `
+ select file_type, token_hash from recipient_download_tokens
+ where envelope_id = $1 and signer_id = $2
+ `,
+ [envelopeId, signerId],
+ );
+
+ return Object.fromEntries(
+ result.rows.map((row) => [row.file_type, row.token_hash]),
+ );
+}
+
+function hashOfLink(url: string | null): string | null {
+ return url
+ ? hashOpaqueToken(new URL(url).pathname.split("/").at(-1) ?? "")
+ : null;
+}
+
+type QueryFunction = (text: unknown, values?: unknown[]) => unknown;
+
+/**
+ * The same database, except that one write of a download link fails: the
+ * first, the second, and so on, counted across the pool and its clients.
+ */
+function failOnLinkWrite(pool: pg.Pool, failAt: number): pg.Pool {
+ let writes = 0;
+
+ function intercept<T extends object>(target: T): T {
+ return new Proxy(target, {
+ get(object, property) {
+ const value: unknown = Reflect.get(object, property);
+
+ if (property === "query") {
+ const query: QueryFunction = (text, values) => {
+ if (
+ typeof text === "string" &&
+ text.includes("INSERT INTO recipient_download_tokens")
+ ) {
+ writes += 1;
+
+ if (writes === failAt) {
+ // Not at once: a write that was started alongside this one
+ // has time to land before the failure is reported.
+ return new Promise((_, reject) => {
+ setTimeout(() => {
+ reject(new Error("the link could not be stored"));
+ }, 25);
+ });
+ }
+ }
+
+ return (value as QueryFunction).call(object, text, values);
+ };
+
+ return query;
+ }
+
+ if (property === "connect") {
+ return async () =>
+ intercept(
+ await (value as () => Promise<pg.PoolClient>).call(object),
+ );
+ }
+
+ return typeof value === "function"
+ ? (value.bind(object) as unknown)
+ : value;
+ },
+ });
+ }
+
+ return intercept(pool);
+}
+
+test("two renewals at once send one email, and its links are the ones that work", async () => {
+ const { envelopeId, evidence, signed, signerId } =
+ await createFinishedEnvelope();
+ const sent: { evidenceUrl: string; signedUrl: string | null }[] = [];
+ const sendLinks = async (links: {
+ evidenceUrl: string;
+ signedUrl: string | null;
+ }) => {
+ // Long enough for the other request to overlap.
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ sent.push({ evidenceUrl: links.evidenceUrl, signedUrl: links.signedUrl });
+ };
+
+ // One request from each of the signer's two old links.
+ const results = await Promise.allSettled([
+ renewRecipientDownloadLinks(signed.token, { database, sendLinks }),
+ renewRecipientDownloadLinks(evidence.token, { database, sendLinks }),
+ ]);
+ const refused = results.filter((result) => result.status === "rejected");
+
+ assert.equal(
+ results.filter((result) => result.status === "fulfilled").length,
+ 1,
+ );
+ assert.equal(refused.length, 1);
+ assert.ok(refused[0]?.reason instanceof DownloadAccessError);
+ assert.equal(refused[0].reason.statusCode, 410);
+
+ // One email, and both of its links are the stored ones.
+ assert.equal(sent.length, 1);
+ assert.deepEqual(await storedHashes(envelopeId, signerId), {
+ evidence: hashOfLink(sent[0]?.evidenceUrl ?? null),
+ signed: hashOfLink(sent[0]?.signedUrl ?? null),
+ });
+});
+
+test("a new link that cannot be stored leaves both old links as they were", async () => {
+ const { envelopeId, evidence, signed, signerId } =
+ await createFinishedEnvelope();
+ let emails = 0;
+
+ // The signed link is written first, so failing the second write fails
+ // the certificate's link after the signed one has been replaced.
+ await assert.rejects(
+ renewRecipientDownloadLinks(signed.token, {
+ database: failOnLinkWrite(database, 2),
+ sendLinks: async () => {
+ emails += 1;
+ },
+ }),
+ /could not be stored/,
+ );
+
+ assert.equal(emails, 0);
+ assert.deepEqual(await storedHashes(envelopeId, signerId), {
+ evidence: evidence.tokenHash,
+ signed: signed.tokenHash,
+ });
+});
+
+test("when the email does not go out the old links are put back, and the signer can ask again", async () => {
+ const { envelopeId, evidence, signed, signerId } =
+ await createFinishedEnvelope();
+
+ await assert.rejects(
+ renewRecipientDownloadLinks(evidence.token, {
+ database,
+ sendLinks: async () => {
+ throw new Error("the mail provider is down");
+ },
+ }),
+ /mail provider is down/,
+ );
+ assert.deepEqual(await storedHashes(envelopeId, signerId), {
+ evidence: evidence.tokenHash,
+ signed: signed.tokenHash,
+ });
+
+ const sent: (string | null)[] = [];
+ const renewed = await renewRecipientDownloadLinks(evidence.token, {
+ database,
+ sendLinks: async (links) => {
+ sent.push(links.signedUrl, links.evidenceUrl);
+ },
+ });
+
+ assert.match(renewed.maskedEmail, /^.\*+@example\.test$/);
+ assert.deepEqual(await storedHashes(envelopeId, signerId), {
+ evidence: hashOfLink(sent[1] ?? null),
+ signed: hashOfLink(sent[0] ?? null),
+ });
+});
diff --git a/src/lib/downloads.ts b/src/lib/downloads.ts
index c5278f4..ebaf97f 100644
--- a/src/lib/downloads.ts
+++ b/src/lib/downloads.ts
@@ -3,6 +3,7 @@ import {
getDb,
queryMany,
queryOne,
+ withTransaction,
} from "./db/connection.ts";
import {
downloadFileTypeSchema,
@@ -785,6 +786,91 @@ function maskEmail(email: string): string {
return `${local.slice(0, 1)}${"*".repeat(Math.max(1, local.length - 1))}@${domain}`;
}
+function isPool(database: DbExecutor): database is ReturnType<typeof getDb> {
+ return "connect" in database && "totalCount" in database;
+}
+
+type StoredDownloadToken = {
+ createdAt: number;
+ expiresAt: number;
+ fileType: string;
+ tokenHash: string;
+};
+
+/**
+ * Puts a new pair of links in place of a signer's stored ones and returns
+ * the old rows. Call it inside a transaction. The signer's rows are locked
+ * first, so two renewals for the same signer take turns, and the links are
+ * stored together or not at all.
+ */
+async function replaceRecipientDownloadLinks(
+ {
+ envelope,
+ presentedTokenHash,
+ signerId,
+ }: {
+ envelope: EnvelopeRecord;
+ presentedTokenHash: string;
+ signerId: string;
+ },
+ tx: DbExecutor,
+): Promise<{
+ evidenceLink: IssuedDownloadLink;
+ previousTokens: StoredDownloadToken[];
+ signedLink: IssuedDownloadLink | null;
+}> {
+ const previousTokens = await queryMany<StoredDownloadToken>(
+ tx,
+ `
+ SELECT
+ token_hash AS "tokenHash",
+ file_type AS "fileType",
+ expires_at AS "expiresAt",
+ created_at AS "createdAt"
+ FROM recipient_download_tokens
+ WHERE envelope_id = $1 AND signer_id = $2
+ ORDER BY file_type
+ FOR UPDATE
+ `,
+ [envelope.id, signerId],
+ );
+
+ // A renewal that got the lock first has already replaced the link this
+ // request came with, and its email carries the pair that works. Sending
+ // a second pair would leave that email with dead links.
+ if (!previousTokens.some((row) => row.tokenHash === presentedTokenHash)) {
+ throw new DownloadAccessError(
+ "This download link is invalid or has expired.",
+ 410,
+ );
+ }
+
+ const issuedAt = Math.floor(Date.now() / 1000);
+ const signedLink =
+ envelope.status === "completed"
+ ? await createRecipientDownloadLink(
+ {
+ envelopeId: envelope.id,
+ fileType: "signed",
+ issuedAt,
+ signerId,
+ },
+ { database: tx },
+ )
+ : null;
+ const evidenceLink = await createRecipientDownloadLink(
+ {
+ envelopeId: envelope.id,
+ fileType: "evidence",
+ issuedAt,
+ signerId,
+ },
+ { database: tx },
+ );
+
+ return { evidenceLink, previousTokens, signedLink };
+}
+
/**
* Someone holding an expired download link asks for a new one. The new links
* go only to the signer's own address on record, never anywhere the request
@@ -805,8 +891,9 @@ export async function renewRecipientDownloadLinks(
}) => Promise<void>;
},
): Promise<RenewedDownloadLinks> {
+ const presentedTokenHash = hashOpaqueToken(token);
const tokenRow = await getRecipientDownloadTokenByHash(
- hashOpaqueToken(token),
+ presentedTokenHash,
database,
);
const envelope = tokenRow
@@ -831,49 +918,18 @@ export async function renewRecipientDownloadLinks(
);
}
- // The new links replace the old ones, so keep the old rows to put back
- // if the email does not go out: the signer can then try again.
- const previousTokens = await queryMany<{
- createdAt: number;
- expiresAt: number;
- fileType: string;
- tokenHash: string;
- }>(
- database,
- `
- SELECT
- token_hash AS "tokenHash",
- file_type AS "fileType",
- expires_at AS "expiresAt",
- created_at AS "createdAt"
- FROM recipient_download_tokens
- WHERE envelope_id = $1 AND signer_id = $2
- `,
- [envelope.id, signer.id],
- );
- const issuedAt = Math.floor(Date.now() / 1000);
- const [signedLink, evidenceLink] = await Promise.all([
- envelope.status === "completed"
- ? createRecipientDownloadLink(
- {
- envelopeId: envelope.id,
- fileType: "signed",
- issuedAt,
- signerId: signer.id,
- },
- { database },
- )
- : Promise.resolve(null),
- createRecipientDownloadLink(
- {
- envelopeId: envelope.id,
- fileType: "evidence",
- issuedAt,
- signerId: signer.id,
- },
- { database },
- ),
- ]);
+ const signerId = signer.id;
+ const replaceLinks = (tx: DbExecutor) =>
+ replaceRecipientDownloadLinks(
+ { envelope, presentedTokenHash, signerId },
+ tx,
+ );
+ // The new links are stored in a transaction of their own, which ends
+ // before the email is sent: a database connection is not held while the
+ // mail provider answers.
+ const { evidenceLink, previousTokens, signedLink } = isPool(database)
+ ? await withTransaction(replaceLinks, database)
+ : await replaceLinks(database);
try {
await sendLinks({
@@ -889,27 +945,35 @@ export async function renewRecipientDownloadLinks(
? [["signed", signedLink.tokenHash] as [string, string]]
: []),
]);
+ // The email did not go out, so the old links go back, both or neither,
+ // and the signer can ask again.
+ const restoreLinks = async (tx: DbExecutor) => {
+ for (const previous of previousTokens) {
+ // Only where this request's link is still the stored one.
+ await tx.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,
+ previous.expiresAt,
+ previous.createdAt,
+ envelope.id,
+ signerId,
+ previous.fileType,
+ issuedHashes.get(previous.fileType) ?? "",
+ ],
+ );
+ }
+ };
- 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,
- previous.expiresAt,
- previous.createdAt,
- envelope.id,
- signer.id,
- previous.fileType,
- issuedHashes.get(previous.fileType) ?? "",
- ],
- );
+ if (isPool(database)) {
+ await withTransaction(restoreLinks, database);
+ } else {
+ await restoreLinks(database);
}
throw error;