Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Count each redirect against a document's lookups
Claude4b1e5ae052f6
diff --git a/src/lib/services/pdf-ltv.test.ts b/src/lib/services/pdf-ltv.test.tsindex 9832404..b1f9a84 100644--- a/src/lib/services/pdf-ltv.test.ts+++ b/src/lib/services/pdf-ltv.test.ts@@ -434,6 +434,42 @@ test("a certificate authority that does not answer is given up on in time", asyn});});+test("each redirect counts against the document's lookups", async () => {+ let requests = 0;+ const server = createServer((request, response) => {+ requests += 1;+ // Always somewhere else.+ response.writeHead(302, { location: `/next-${String(requests)}` }).end();+ });++ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));++ try {+ const address = server.address();+ const port = typeof address === "object" && address ? address.port : 0;+ const fetchFrom = createValidationFetch({ allowPrivateAddresses: true });+ let redirects = 0;++ await assert.rejects(+ fetchFrom(`http://127.0.0.1:${String(port)}/start`, undefined, {+ onRedirect: () => {+ redirects += 1;++ if (redirects > 2) {+ throw new Error("Too many lookups for one document.");+ }+ },+ }),+ /Too many lookups/,+ );+ // The first request and two redirects, then the third was refused.+ assert.equal(requests, 3);+ } finally {+ server.closeAllConnections();+ await new Promise<void>((resolve) => server.close(() => resolve()));+ }+});+test("a lookup told to stop closes its connection", async () => {let closed = false;const server = createServer((request) => {diff --git a/src/lib/services/pdf-ltv.ts b/src/lib/services/pdf-ltv.tsindex c22474a..d6626bc 100644--- a/src/lib/services/pdf-ltv.ts+++ b/src/lib/services/pdf-ltv.ts@@ -333,8 +333,12 @@ function cmsCertificates(cms: Uint8Array, depth = 0): Buffer[] {export type ValidationFetch = (url: string,request?: { body: Buffer; contentType: string },- /** Aborted when the document's lookup time runs out. */- options?: { signal?: AbortSignal },+ options?: {+ /** Called before following each redirect; may throw to stop. */+ onRedirect?: () => void;+ /** Aborted when the document's lookup time runs out. */+ signal?: AbortSignal;+ },) => Promise<Buffer>;function isPrivateAddress(host: string): boolean {@@ -551,7 +555,7 @@ export function createValidationFetch({} = {}): ValidationFetch {const lookup = guardedLookup(resolve, allowPrivateAddresses);- return async (url, request, { signal } = {}) => {+ return async (url, request, { onRedirect, signal } = {}) => {let target = new URL(url);for (let hop = 0; hop < 4; hop += 1) {@@ -559,6 +563,10 @@ export function createValidationFetch({throw new LongTermValidationError("Ran out of time for lookups.");}+ if (hop > 0) {+ onRedirect?.();+ }+if (target.protocol !== "http:" && target.protocol !== "https:") {throw new LongTermValidationError(`Refused to fetch ${target.protocol} URL.`,@@ -1142,15 +1150,19 @@ export async function collectValidationData(revoked: [],};// A slow certificate authority must not hold up a signing for long.- const limitedFetch: ValidationFetch = (url, request) => {+ // Every request counts, each redirect too.+ const countRequest = () => {budget.fetches += 1;if (budget.fetches > MAX_FETCHES) {throw new LongTermValidationError("Too many lookups for one document.");}+ };+ const limitedFetch: ValidationFetch = (url, request) => {+ countRequest();return withinBudget(budget, (signal) =>- fetchFrom(url, request, { signal }),+ fetchFrom(url, request, { onRedirect: countRequest, signal }),);};const addToPool = (certificate: Certificate) => {