Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Stop logging Veriff response bodies and harden session errors

Kiran Kd971b94d5b90
diff --git a/apps/web/lib/http/base-client.ts b/apps/web/lib/http/base-client.ts
index 292e6a0724..79b807433c 100644
--- a/apps/web/lib/http/base-client.ts
+++ b/apps/web/lib/http/base-client.ts
@@ -1,4 +1,5 @@
import { logger } from "@/lib/axiom/server";
+import { waitUntil } from "@vercel/functions";
import * as z from "zod/v4";
const DEFAULT_TIMEOUT_MS = 30_000;
@@ -74,6 +75,10 @@ export abstract class HttpBaseClient {
// message and error. Request headers are never logged.
protected readonly redactedQueryParams: string[] = [];
+ // When false, response bodies are omitted from Axiom/console logs and
+ // HttpClientError (for vendors that return PII).
+ protected readonly logResponseBodies: boolean = true;
+
// Override to translate HTTP failures into vendor-specific errors
// (e.g. a DubApiError with a caller-facing message).
protected mapError({
@@ -157,7 +162,32 @@ export abstract class HttpBaseClient {
let input: unknown = options.input;
if (options.inputSchema && options.input !== undefined) {
- input = options.inputSchema.parse(options.input);
+ const parsedInput = options.inputSchema.safeParse(options.input);
+
+ if (!parsedInput.success) {
+ const safeUrl = this.redactUrl(url);
+ const message = `[${this.vendor}] ${method} ${safeUrl} request validation failed`;
+
+ waitUntil(
+ this.logError(message, {
+ method,
+ url: safeUrl,
+ status: null,
+ issues: z.prettifyError(parsedInput.error),
+ }),
+ );
+
+ throw new HttpClientError({
+ vendor: this.vendor,
+ method,
+ url: safeUrl,
+ status: null,
+ responseBody: null,
+ message,
+ });
+ }
+
+ input = parsedInput.data;
}
// GET/DELETE requests carry no body — their input becomes query params
@@ -203,12 +233,14 @@ export abstract class HttpBaseClient {
const reason = error instanceof Error ? error.message : String(error);
const message = `[${this.vendor}] ${method} ${safeUrl} network error: ${reason}`;
- await this.logError(message, {
- method,
- url: safeUrl,
- status: null,
- error: reason,
- });
+ waitUntil(
+ this.logError(message, {
+ method,
+ url: safeUrl,
+ status: null,
+ error: reason,
+ }),
+ );
throw new HttpClientError({
vendor: this.vendor,
@@ -239,16 +271,18 @@ export abstract class HttpBaseClient {
}
if (!response.ok) {
- const responseBody = this.truncate(text);
-
- await this.logError(
- `[${this.vendor}] ${method} ${safeUrl} failed with status ${response.status}`,
- {
- method,
- url: safeUrl,
- status: response.status,
- responseBody,
- },
+ const responseBody = this.responseBodyForLog(text);
+
+ waitUntil(
+ this.logError(
+ `[${this.vendor}] ${method} ${safeUrl} failed with status ${response.status}`,
+ {
+ method,
+ url: safeUrl,
+ status: response.status,
+ responseBody,
+ },
+ ),
);
throw this.mapError({
@@ -264,21 +298,24 @@ export abstract class HttpBaseClient {
if (!parsed.success) {
const message = `[${this.vendor}] ${method} ${safeUrl} response validation failed`;
+ const responseBody = this.responseBodyForLog(text);
- await this.logError(message, {
- method,
- url: safeUrl,
- status: response.status,
- issues: z.prettifyError(parsed.error),
- responseBody: this.truncate(text),
- });
+ waitUntil(
+ this.logError(message, {
+ method,
+ url: safeUrl,
+ status: response.status,
+ issues: z.prettifyError(parsed.error),
+ responseBody,
+ }),
+ );
throw new HttpClientError({
vendor: this.vendor,
method,
url: safeUrl,
status: response.status,
- responseBody: this.truncate(text),
+ responseBody,
message,
});
}
@@ -326,6 +363,14 @@ export abstract class HttpBaseClient {
return safe.toString();
}
+ private responseBodyForLog(text: string): string | null {
+ if (!this.logResponseBodies) {
+ return null;
+ }
+
+ return this.truncate(text);
+ }
+
private truncate(text: string): string | null {
if (!text) {
return null;
diff --git a/apps/web/lib/veriff/client.ts b/apps/web/lib/veriff/client.ts
index 978c584e4a..5ee288ae48 100644
--- a/apps/web/lib/veriff/client.ts
+++ b/apps/web/lib/veriff/client.ts
@@ -11,6 +11,7 @@ import {
class VeriffClient extends HttpBaseClient {
protected readonly vendor = "Veriff";
protected readonly baseUrl = "https://stationapi.veriff.com/v1";
+ protected readonly logResponseBodies = false;
protected buildAuthHeaders() {
return {
@@ -43,6 +44,4 @@ class VeriffClient extends HttpBaseClient {
}
}
-export const veriffClient = new VeriffClient({
- debug: true,
-});
+export const veriffClient = new VeriffClient();
diff --git a/apps/web/lib/veriff/create-veriff-session.ts b/apps/web/lib/veriff/create-veriff-session.ts
index 9cf8017ae2..f56fa4071d 100644
--- a/apps/web/lib/veriff/create-veriff-session.ts
+++ b/apps/web/lib/veriff/create-veriff-session.ts
@@ -10,13 +10,19 @@ export async function createVeriffSession({
const firstName = nameParts[0] || partner.name;
const lastName = nameParts.slice(1).join(" ") || partner.name;
- return await veriffClient.createSession({
- verification: {
- vendorData: partner.id,
- person: {
- firstName,
- lastName,
+ try {
+ return await veriffClient.createSession({
+ verification: {
+ vendorData: partner.id,
+ person: {
+ firstName,
+ lastName,
+ },
},
- },
- });
+ });
+ } catch (error) {
+ throw new Error(
+ "Failed to create Veriff session. Please try again later or contact support.",
+ );
+ }
}