Skip to content

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

TypeScript

Let a site already on a full list be added again without an error

Claude5c5098142dde
diff --git a/src/lib/embedding.dbtest.ts b/src/lib/embedding.dbtest.ts
index ac4e589..fa28f14 100644
--- a/src/lib/embedding.dbtest.ts
+++ b/src/lib/embedding.dbtest.ts
@@ -79,6 +79,8 @@ before(async () => {
document.addPage([612, 792]);
pdf = await document.save();
+ // The end-to-end run shares this database and leaves its site behind.
+ await database.query(`delete from embed_allowed_origins`);
});
afterEach(async () => {
@@ -666,3 +668,23 @@ test("a link opened while its site is being removed stays expired", async () =>
assert.equal(after.openedAt, null);
assert.ok(after.expiresAt < Date.now() / 1000);
});
+
+test("a site already on a full list can be added again without an error", async () => {
+ const admin = await createAdmin();
+ const sites = Array.from(
+ { length: 10 },
+ (_, index) => `https://app${String(index)}.example.com`,
+ );
+
+ for (const site of sites) {
+ await addEmbedOrigin(admin, site, { database });
+ }
+
+ assert.equal(await addEmbedOrigin(admin, sites[3]!, { database }), sites[3]!);
+ await assert.rejects(
+ addEmbedOrigin(admin, "https://eleventh.example.com", { database }),
+ (error: unknown) =>
+ error instanceof EmbeddingError && error.statusCode === 409,
+ );
+ assert.equal((await listEmbedOrigins(database)).length, 10);
+});
diff --git a/src/lib/embedding.ts b/src/lib/embedding.ts
index 253ff9e..f57f475 100644
--- a/src/lib/embedding.ts
+++ b/src/lib/embedding.ts
@@ -126,9 +126,12 @@ export async function addEmbedOrigin(
`LOCK TABLE embed_allowed_origins IN SHARE ROW EXCLUSIVE MODE`,
);
+ // A site already on the list takes no new place, so adding it again
+ // succeeds and changes nothing, even when the list is full.
const count = await queryOne<{ count: number }>(
tx,
- `SELECT count(*)::int AS count FROM embed_allowed_origins`,
+ `SELECT count(*)::int AS count FROM embed_allowed_origins WHERE origin <> $1`,
+ [origin],
);
if ((count?.count ?? 0) >= MAX_EMBED_ORIGINS) {