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.

Parse and bound query params before storing API request logs.

Kiran K901cc1beb75c
diff --git a/apps/web/lib/api-logs/capture-request-log.ts b/apps/web/lib/api-logs/capture-request-log.ts
index 3be4fbdb28..0b1c7cd713 100644
--- a/apps/web/lib/api-logs/capture-request-log.ts
+++ b/apps/web/lib/api-logs/capture-request-log.ts
@@ -6,6 +6,7 @@ import {
maskSensitiveFields,
SENSITIVE_RESPONSE_FIELDS_BY_ROUTE,
} from "./mask-sensitive-fields";
+import { parseQueryParams } from "./query-params";
import { recordApiLog } from "./record-api-log";
// Precompile route patterns into regexes at module load
@@ -83,8 +84,7 @@ export async function captureRequestLog({
responseBody = await responseClone.json();
} catch {}
- const queryParams =
- url.searchParams.size > 0 ? Object.fromEntries(url.searchParams) : null;
+ const queryParams = parseQueryParams(url.searchParams);
// Mask sensitive fields in the response body
if (responseBody) {
diff --git a/apps/web/lib/api-logs/query-params.ts b/apps/web/lib/api-logs/query-params.ts
new file mode 100644
index 0000000000..8740788ced
--- /dev/null
+++ b/apps/web/lib/api-logs/query-params.ts
@@ -0,0 +1,72 @@
+import { maskSensitiveValue } from "./mask-sensitive-fields";
+
+export const MAX_QUERY_STRING_LENGTH = 4096;
+export const MAX_QUERY_PARAM_ENTRIES = 100;
+export const MAX_QUERY_PARAM_KEY_LENGTH = 100;
+export const MAX_QUERY_PARAM_VALUE_LENGTH = 1000;
+
+const SENSITIVE_QUERY_PARAM_KEYS = new Set([
+ "token",
+ "api_key",
+ "apikey",
+ "api-key",
+ "password",
+ "secret",
+ "authorization",
+ "auth",
+ "access_token",
+ "refresh_token",
+ "client_secret",
+ "client_id",
+ "key",
+]);
+
+function truncate(value: string, maxLength: number): string {
+ return value.length > maxLength ? value.slice(0, maxLength) : value;
+}
+
+export function parseQueryParams(
+ searchParams: URLSearchParams,
+): Record<string, string | string[]> | null {
+ const queryString = searchParams.toString();
+
+ if (queryString.length === 0) {
+ return null;
+ }
+
+ // Skip parsing if the query string exceeds the max length (safety check)
+ if (queryString.length > MAX_QUERY_STRING_LENGTH) {
+ return null;
+ }
+
+ const params: Record<string, string | string[]> = {};
+ let entryCount = 0;
+
+ for (const [rawKey, rawValue] of searchParams.entries()) {
+ if (entryCount >= MAX_QUERY_PARAM_ENTRIES) {
+ break;
+ }
+
+ const key = truncate(rawKey, MAX_QUERY_PARAM_KEY_LENGTH);
+ let value = truncate(rawValue, MAX_QUERY_PARAM_VALUE_LENGTH);
+
+ const isSensitiveKey = SENSITIVE_QUERY_PARAM_KEYS.has(key.toLowerCase());
+
+ if (isSensitiveKey) {
+ value = maskSensitiveValue(value);
+ }
+
+ if (key in params) {
+ const existing = params[key];
+ Array.isArray(existing)
+ ? existing.push(value)
+ : (params[key] = [existing, value]);
+ } else {
+ params[key] = value;
+ }
+
+ entryCount++;
+ }
+
+ return params;
+}