Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Read the seal's key and the install's name before the final transaction
Claudeb578c7d1c1c4
diff --git a/src/lib/provider-signing.dbtest.ts b/src/lib/provider-signing.dbtest.tsindex ed4af36..fddd234 100644--- a/src/lib/provider-signing.dbtest.ts+++ b/src/lib/provider-signing.dbtest.ts@@ -8,7 +8,7 @@ import { after, before, test } from "node:test";import { promisify } from "node:util";import { PDFDocument, StandardFonts } from "pdf-lib";-import type pg from "pg";+import pg from "pg";import { openTestDatabase } from "./db/test-database.ts";import { ELECTRONIC_RECORDS_DISCLOSURE_VERSION } from "./electronic-records-disclosure.ts";@@ -26,9 +26,11 @@ import {startProviderSigning,} from "./provider-signing.ts";import { runRetentionSweep } from "./retention.ts";+import { invalidateSettingsCache } from "./settings.ts";import { verifyAuditChain } from "./services/audit.ts";import { findSignatures } from "./services/pdf-incremental.ts";import { resetValidationCache } from "./services/pdf-ltv.ts";+import { resetSealIdentityCache } from "./services/seal-identity.ts";import { startTestPki, type TestPki } from "./services/pki-test-support.ts";import { checkSignerSignature } from "./services/signer-cms.ts";import {@@ -841,6 +843,50 @@ test("two submissions by the last signer at once finish the envelope once, and k}});+test("the last signature needs no second connection while it holds the envelope", async () => {+ // A pool of one connection: anything that needed another one while the+ // envelope is locked would wait for it, and give up after 3 seconds.+ const single = new pg.Pool({+ connectionString: (database as unknown as { options: pg.PoolConfig })+ .options.connectionString,+ connectionTimeoutMillis: 3000,+ max: 1,+ });+ const draft = await createDraft();++ globalThis.__signudocPgPool = single;++ try {+ await send(draft, "simple");+ await submitSigningForToken(+ await linkFor(draft.riley.id),+ submissionFor(draft.riley),+ { database: single, storageService },+ );++ const caseyToken = await linkFor(draft.casey.id);++ // Nothing the seal reads from the database is cached.+ invalidateSettingsCache();+ resetSealIdentityCache();++ await submitSigningForToken(caseyToken, submissionFor(draft.casey), {+ database: single,+ storageService,+ });++ const finished = await database.query<{ status: string }>(+ `select status from envelopes where id = $1`,+ [draft.envelopeId],+ );++ assert.equal(finished.rows[0]!.status, "completed");+ } finally {+ globalThis.__signudocPgPool = database;+ await single.end();+ }+});+test("a revision the cleanup cannot delete is tried again, and the current one is kept", async () => {const draft = await createDraft();diff --git a/src/lib/services/pdf-finalize.ts b/src/lib/services/pdf-finalize.tsindex 8f3e2f2..275b7d6 100644--- a/src/lib/services/pdf-finalize.ts+++ b/src/lib/services/pdf-finalize.ts@@ -14,8 +14,10 @@ import { canonicalJson } from "../utils/canonical-json.ts";import { normalizedRectToPdfRect } from "../utils/pdf-coords.ts";import { embedTextFaces, type TextFace } from "./pdf-fonts.ts";import { sha256Hex } from "./pdf-seal.ts";+import { getBranding } from "../settings.ts";import {finishSignerSignedPdf,+ getSealIdentity,saveInstallSealedPdf,type SealPurpose,} from "./seal-identity.ts";@@ -55,6 +57,20 @@ export type PdfSealer = (const defaultSealer: PdfSealer = (pdfDocument, purpose) =>saveInstallSealedPdf(pdfDocument, purpose);+/**+ * The install's sealer, with what it reads from the database (the seal key+ * and the install's name) read now. Sealing with it then needs no database+ * connection, so a caller can seal while it holds one in a transaction+ * without waiting on a pool that its own concurrent requests have filled.+ */+export async function loadInstallSealer(): Promise<PdfSealer> {+ const identity = await getSealIdentity();+ const branding = await getBranding();++ return (pdfDocument, purpose) =>+ saveInstallSealedPdf(pdfDocument, purpose, { branding, identity });+}+const CERTIFICATE_FINGERPRINT_VERSION = 1;/**diff --git a/src/lib/services/seal-identity.ts b/src/lib/services/seal-identity.tsindex c455672..f9d7c43 100644--- a/src/lib/services/seal-identity.ts+++ b/src/lib/services/seal-identity.ts@@ -11,6 +11,7 @@ import {import { PDFDocument } from "pdf-lib";import { getDb, withTransaction, type DbPool } from "../db/connection.ts";+import type { Branding } from "../branding.ts";import { getBranding } from "../settings.ts";import {isProduction,@@ -249,13 +250,19 @@ export async function saveInstallSealedPdf(pdfDocument: PDFDocument,purpose: SealPurpose,{+ branding: givenBranding,database,env = process.env,identity,- }: { database?: DbPool; env?: EnvSource; identity?: SealIdentity } = {},+ }: {+ branding?: Branding;+ database?: DbPool;+ env?: EnvSource;+ identity?: SealIdentity;+ } = {},): Promise<Uint8Array> {const sealIdentity = identity ?? (await getSealIdentity({ database, env }));- const branding = await getBranding(env);+ const branding = givenBranding ?? (await getBranding(env));const timestampUrl = readEnv(env, "SEAL_TIMESTAMP_URL") ?? null;const options: SealOptions = {contactInfo: branding.supportAddress ?? "",diff --git a/src/lib/signing.ts b/src/lib/signing.tsindex 86d4f64..f2f2f54 100644--- a/src/lib/signing.ts+++ b/src/lib/signing.ts@@ -65,6 +65,7 @@ import { withUserEmailLegalFooter } from "./services/email-legal-footer.ts";import { hashOpaqueToken } from "./services/opaque-tokens.ts";import {finalizeEnvelope,+ loadInstallSealer,storeEnvelopeCertificatePdf,storeRecipientEnvelopeCertificatePdf,} from "./services/pdf-finalize.ts";@@ -2575,6 +2576,9 @@ export async function submitSigningForToken(}),];+ // The seal's key and the install's name, read before the transaction:+ // sealing inside it then needs no second connection from the pool.+ const seal = await loadInstallSealer();// Set once this submission may have stored the final files.let finalArtifactsStored = false;let finalArtifacts: Awaited<ReturnType<typeof finalizeEnvelope>>;@@ -2601,6 +2605,7 @@ export async function submitSigningForToken(},},{+ seal,signedRevision: providerSignature?.revision.bytes,storageService,},