Skip to content

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

TypeScript

Hold the site row while issuing an embed link and record the key that asked

Claude9e0cfb79cf08
diff --git a/db/migrations/20261118120500_embed_link_api_key.sql b/db/migrations/20261118120500_embed_link_api_key.sql
new file mode 100644
index 0000000..d1f2885
--- /dev/null
+++ b/db/migrations/20261118120500_embed_link_api_key.sql
@@ -0,0 +1,9 @@
+-- signers.embed_api_key_id: the API key that asked for the signer's
+-- current embedded link. The signature's evidence names this key, which
+-- can differ from the key that created or sent the envelope (a key
+-- rotated since, or a draft made in the dashboard and sent through the
+-- API).
+
+alter table public.signers
+ add column if not exists embed_api_key_id text
+ references public.api_keys(id) on delete set null;
diff --git a/src/app/api/v1/envelopes/[id]/signers/[signerId]/embed-link/route.ts b/src/app/api/v1/envelopes/[id]/signers/[signerId]/embed-link/route.ts
index da9d769..d23691d 100644
--- a/src/app/api/v1/envelopes/[id]/signers/[signerId]/embed-link/route.ts
+++ b/src/app/api/v1/envelopes/[id]/signers/[signerId]/embed-link/route.ts
@@ -26,6 +26,7 @@ export async function POST(request: Request, context: RouteContext) {
} | null;
const link = await issueEmbedLink({
+ apiKeyId: api.actor.apiKeyId,
envelopeId: id,
origin: body?.origin,
signerId,
diff --git a/src/lib/embedding.dbtest.ts b/src/lib/embedding.dbtest.ts
index c18fa87..926754e 100644
--- a/src/lib/embedding.dbtest.ts
+++ b/src/lib/embedding.dbtest.ts
@@ -21,6 +21,7 @@ import {
issueEmbedLink,
listEmbedOrigins,
openEmbedLink,
+ readEmbedEvidence,
removeEmbedOrigin,
} from "./embedding.ts";
import {
@@ -451,9 +452,14 @@ test("an emailed envelope gets no embed links, and an embedded one no reminders
);
});
-async function linkFor(admin: ApiActor, envelopeId: string, signerId: string) {
+async function linkFor(
+ admin: ApiActor,
+ envelopeId: string,
+ signerId: string,
+ apiKeyId: string = admin.apiKeyId,
+) {
const link = await issueEmbedLink(
- { envelopeId, origin: ORIGIN, signerId, userId: admin.userId },
+ { apiKeyId, envelopeId, origin: ORIGIN, signerId, userId: admin.userId },
{ database },
);
@@ -603,3 +609,23 @@ test("a signer who declines in the app is recorded as declining there", async ()
origin: ORIGIN,
});
});
+
+test("the evidence names the key that asked for the link, not the one that made the envelope", async () => {
+ const admin = await createAdmin();
+
+ await addEmbedOrigin(admin, ORIGIN, { database });
+
+ const envelope = await create(admin);
+ const signerId = envelope.signers[0]!.id;
+ const { summary: other } = await createApiKey(
+ { email: admin.email, userId: admin.userId },
+ { expiresInDays: null, name: "Kiosk", scope: "write" },
+ database,
+ );
+
+ await linkFor(admin, envelope.id, signerId, other.id);
+ assert.deepEqual(await readEmbedEvidence(envelope.id, signerId, database), {
+ apiKeyName: "Kiosk",
+ origin: ORIGIN,
+ });
+});
diff --git a/src/lib/embedding.ts b/src/lib/embedding.ts
index 7c14091..ea19cb5 100644
--- a/src/lib/embedding.ts
+++ b/src/lib/embedding.ts
@@ -16,7 +16,8 @@ export { httpEmbedOriginsAllowed, parseEmbedOrigin } from "./embed-origin.ts";
// Embedded signing: an app shows SignuDoc's signing page inside its own
// site, in a frame, instead of SignuDoc emailing the signer a link. See
-// db/migrations/20261118120000_embedded_signing.sql.
+// db/migrations/20261118120000_embedded_signing.sql and
+// 20261118120500_embed_link_api_key.sql.
//
// - An administrator lists the sites allowed to frame signing pages.
// - An envelope sent through the API with delivery "embedded" emails no
@@ -224,6 +225,8 @@ export function embedSigningPath(token: string, origin: string): string {
*/
export async function issueEmbedLink(
input: {
+ /** The key asking for the link; the signature's evidence names it. */
+ apiKeyId?: string | null;
envelopeId: string;
origin: unknown;
signerId: string;
@@ -251,6 +254,23 @@ export async function issueEmbedLink(
const now = nowInSeconds();
return withTransaction(async (tx) => {
+ // Holds the site on the list until this link is saved. Removing the
+ // site deletes this row and then expires its links, so it waits for
+ // this link and expires it too, or goes first and this is refused.
+ // Taken before any other lock here, so the two cannot deadlock.
+ const stillAllowed = await queryOne<{ origin: string }>(
+ tx,
+ `SELECT origin FROM embed_allowed_origins WHERE origin = $1 FOR SHARE`,
+ [origin],
+ );
+
+ if (!stillAllowed) {
+ throw new EmbeddingError(
+ "origin: this site is not allowed to frame signing pages. An administrator can add it under Settings, Organization, Embedded signing.",
+ 403,
+ );
+ }
+
const envelope = await queryOne<{
delivery: string;
expiresAt: number | null;
@@ -322,10 +342,18 @@ export async function issueEmbedLink(
`
UPDATE signers
SET signing_token_hash = $1, signing_token_expires_at = $2,
- embed_origin = $3, embed_link_opened_at = NULL, updated_at = $4
+ embed_origin = $3, embed_link_opened_at = NULL, updated_at = $4,
+ embed_api_key_id = $6
WHERE id = $5
`,
- [link.tokenHash, link.exp, origin, now, input.signerId],
+ [
+ link.tokenHash,
+ link.exp,
+ origin,
+ now,
+ input.signerId,
+ input.apiKeyId ?? null,
+ ],
);
return {
@@ -434,7 +462,9 @@ export async function readEmbedEvidence(
SELECT e.delivery, s.embed_origin AS origin, k.name AS "apiKeyName"
FROM envelopes e
JOIN signers s ON s.envelope_id = e.id AND s.id = $2
- LEFT JOIN api_keys k ON k.id = e.api_key_id
+ -- The key that asked for this signer's link, not the one that made
+ -- the envelope.
+ LEFT JOIN api_keys k ON k.id = s.embed_api_key_id
WHERE e.id = $1
`,
[envelopeId, signerId],