Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix: use payload type
Matheus Nogueira21023713d6f1
diff --git a/backend/src/ee/routes/v1/honey-token-routers/honey-token-endpoints.ts b/backend/src/ee/routes/v1/honey-token-routers/honey-token-endpoints.tsindex 5dece07280..55da7a5519 100644--- a/backend/src/ee/routes/v1/honey-token-routers/honey-token-endpoints.ts+++ b/backend/src/ee/routes/v1/honey-token-routers/honey-token-endpoints.ts@@ -6,6 +6,7 @@ import {THoneyTokenConfigByType,THoneyTokenTestConnectionResponseByType} from "@app/ee/services/honey-token/honey-token-provider-types";+import { HoneyTokenWebhookPayloadSchema } from "@app/ee/services/honey-token/honey-token-types";import { BadRequestError } from "@app/lib/errors";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";@@ -129,6 +130,9 @@ export const registerHoneyTokenEndpoints = <TType extends HoneyTokenType>({const TRIGGER_BODY_LIMIT = 256 * 1024;void server.register(async (triggerScope) => {+ // We remove the default parser because we need to:+ // 1. Limit the size of the request to prevent DDoS attacks+ // 2. Access the raw JSON body to run the signature validationtriggerScope.removeContentTypeParser("application/json");triggerScope.addContentTypeParser("application/json",@@ -154,13 +158,19 @@ export const registerHoneyTokenEndpoints = <TType extends HoneyTokenType>({handler: async (req) => {const rawBody = Buffer.isBuffer(req.body) ? req.body.toString("utf-8") : String(req.body);- let payload: unknown;+ let parsed;try {- payload = JSON.parse(rawBody);+ parsed = JSON.parse(rawBody) as unknown;} catch {throw new BadRequestError({ message: "Invalid JSON body" });}+ const result = HoneyTokenWebhookPayloadSchema.safeParse(parsed);+ if (!result.success) {+ throw new BadRequestError({ message: "Invalid webhook payload" });+ }+ const payload = result.data;+const { acknowledged } = await server.services.honeyToken.handleTrigger({type,signature: req.headers["x-infisical-signature"] as string | undefined,diff --git a/backend/src/ee/services/honey-token/honey-token-service.ts b/backend/src/ee/services/honey-token/honey-token-service.tsindex e89d92d34f..f2f67616dd 100644--- a/backend/src/ee/services/honey-token/honey-token-service.ts+++ b/backend/src/ee/services/honey-token/honey-token-service.ts@@ -52,11 +52,7 @@ import {THoneyTokenListInput,THoneyTokenUpdateInput} from "./honey-token-provider-types";-import {- AwsHoneyTokenConfigSchema,- AwsHoneyTokenEventMetadataSchema,- THoneyTokenEventsInput-} from "./honey-token-types";+import { AwsHoneyTokenConfigSchema, THoneyTokenEventsInput, THoneyTokenWebhookPayload } from "./honey-token-types";const TRIGGER_NOTIFICATION_COOLDOWN_MS = 24 * 60 * 60 * 1000;const SIGNATURE_TOLERANCE_MS = 5 * 60 * 1000;@@ -128,7 +124,7 @@ type THandleTriggerInput = {type: HoneyTokenType;signature: string | undefined;rawBody: string;- payload: unknown;+ payload: THoneyTokenWebhookPayload;};export const honeyTokenServiceFactory = ({@@ -959,19 +955,12 @@ export const honeyTokenServiceFactory = ({throw new UnauthorizedError({ message: "Request timestamp is too old or invalid" });}- const rawEvents = Array.isArray(payload) ? (payload as unknown[]) : [payload];- const firstAccessKeyId = rawEvents- .map((rawEvent) => {- const wrapped = rawEvent as { event?: unknown };- const parsed = AwsHoneyTokenEventMetadataSchema.safeParse(wrapped.event ?? rawEvent);- return parsed.success ? parsed.data.accessKeyId : null;- })- .find((accessKeyId): accessKeyId is string => Boolean(accessKeyId));- if (!firstAccessKeyId) {+ const eventMetadata = payload.event;+ if (!eventMetadata) {throw new UnauthorizedError({ message: "Invalid webhook request" });}- const honeyTokenWithOrg = await honeyTokenDAL.findOneByTokenIdentifier(firstAccessKeyId);+ const honeyTokenWithOrg = await honeyTokenDAL.findOneByTokenIdentifier(eventMetadata.accessKeyId);if (!honeyTokenWithOrg) {throw new UnauthorizedError({ message: "Invalid webhook request" });}@@ -1005,77 +994,60 @@ export const honeyTokenServiceFactory = ({throw new UnauthorizedError({ message: "Invalid webhook request" });}- /* eslint-disable no-continue */- for await (const rawEvent of rawEvents) {- const wrapped = rawEvent as { event?: unknown };- const parsed = AwsHoneyTokenEventMetadataSchema.safeParse(wrapped.event ?? rawEvent);- if (!parsed.success) {- logger.warn(- { orgId: honeyTokenWithOrg.orgId, event: rawEvent, error: parsed.error },- `Failed to parse honey token event [orgId=${honeyTokenWithOrg.orgId}]`- );- continue;- }- const honeyToken = await honeyTokenDAL.findOneByTokenIdentifierAndOrgId(- parsed.data.accessKeyId,- honeyTokenWithOrg.orgId- );- if (!honeyToken) continue;- if (honeyToken.status === HoneyTokenStatus.Revoked) continue;-- await honeyTokenEventDAL.create({- honeyTokenId: honeyToken.id,- eventType: HoneyTokenEventType.AWS,- metadata: parsed.data- });+ const honeyToken = await honeyTokenDAL.findOneByTokenIdentifierAndOrgId(+ eventMetadata.accessKeyId,+ honeyTokenWithOrg.orgId+ );+ if (!honeyToken || honeyToken.status === HoneyTokenStatus.Revoked) {+ return { acknowledged: true };+ }- const updatedToken = await honeyTokenDAL.tryMarkTriggered(- parsed.data.accessKeyId,- TRIGGER_NOTIFICATION_COOLDOWN_MS- );+ await honeyTokenEventDAL.create({+ honeyTokenId: honeyToken.id,+ eventType: HoneyTokenEventType.AWS,+ metadata: eventMetadata+ });+ const updatedToken = await honeyTokenDAL.tryMarkTriggered(+ eventMetadata.accessKeyId,+ TRIGGER_NOTIFICATION_COOLDOWN_MS+ );+ void telemetryService+ .sendPostHogEvents({+ event: PostHogEventTypes.HoneyTokenTriggered,+ distinctId: "anonymous-honey-token-trigger",+ anonymous: true,+ properties: {+ type: honeyToken.type+ }+ })+ .catch(() => {});+ if (updatedToken) {+ void $sendTriggerNotification({ orgId: honeyTokenWithOrg.orgId, honeyToken, eventMetadata });- void telemetryService- .sendPostHogEvents({- event: PostHogEventTypes.HoneyTokenTriggered,- distinctId: "anonymous-honey-token-trigger",- anonymous: true,- properties: {- type: honeyToken.type+ void auditLogService+ .createAuditLog({+ actor: {+ type: ActorType.UNKNOWN_USER,+ metadata: {}+ },+ orgId: honeyTokenWithOrg.orgId,+ projectId: honeyToken.projectId,+ event: {+ type: EventType.TRIGGER_HONEY_TOKEN,+ metadata: {+ honeyTokenId: honeyToken.id,+ name: honeyToken.name,+ type: honeyToken.type as HoneyTokenType,+ projectId: honeyToken.projectId,+ eventName: eventMetadata.eventName,+ eventTime: eventMetadata.eventTime,+ sourceIp: eventMetadata.sourceIp ?? "Unknown",+ awsRegion: eventMetadata.awsRegion+ }}}).catch(() => {});-- // This block only is executed once per token trigger. So, even if we get 100 events- // this will run only once.- if (updatedToken) {- void $sendTriggerNotification({ orgId: honeyTokenWithOrg.orgId, honeyToken, eventMetadata: parsed.data });-- void auditLogService- .createAuditLog({- actor: {- type: ActorType.UNKNOWN_USER,- metadata: {}- },- orgId: honeyTokenWithOrg.orgId,- projectId: honeyToken.projectId,- event: {- type: EventType.TRIGGER_HONEY_TOKEN,- metadata: {- honeyTokenId: honeyToken.id,- name: honeyToken.name,- type: honeyToken.type as HoneyTokenType,- projectId: honeyToken.projectId,- eventName: parsed.data.eventName,- eventTime: parsed.data.eventTime,- sourceIp: parsed.data.sourceIp ?? "Unknown",- awsRegion: parsed.data.awsRegion- }- }- })- .catch(() => {});- }}- /* eslint-enable no-continue */return { acknowledged: true };};diff --git a/backend/src/ee/services/honey-token/honey-token-types.ts b/backend/src/ee/services/honey-token/honey-token-types.tsindex 89e0be83d2..99a33e41a8 100644--- a/backend/src/ee/services/honey-token/honey-token-types.ts+++ b/backend/src/ee/services/honey-token/honey-token-types.ts@@ -3,7 +3,7 @@ import { z } from "zod";import { isValidAwsRegion } from "@app/lib/aws/region";import { slugSchema } from "@app/server/lib/schemas";-import { HoneyTokenEventType, HoneyTokenType } from "./honey-token-enums";+import { HoneyTokenType } from "./honey-token-enums";export const AwsHoneyTokenEventMetadataSchema = z.object({accessKeyId: z.string(),@@ -21,16 +21,14 @@ export const AwsHoneyTokenEventMetadataSchema = z.object({requestParameters: z.unknown().nullable().optional()});-export type TAwsHoneyTokenEventMetadata = z.infer<typeof AwsHoneyTokenEventMetadataSchema>;--export const HoneyTokenEventMetadataSchema = z.discriminatedUnion("eventType", [+export const HoneyTokenWebhookPayloadSchema = z.discriminatedUnion("type", [z.object({- eventType: z.literal(HoneyTokenEventType.AWS),- metadata: AwsHoneyTokenEventMetadataSchema+ type: z.literal(HoneyTokenType.AWS),+ event: AwsHoneyTokenEventMetadataSchema.optional()})]);-export type THoneyTokenEventMetadata = z.infer<typeof HoneyTokenEventMetadataSchema>;+export type THoneyTokenWebhookPayload = z.infer<typeof HoneyTokenWebhookPayloadSchema>;// --- Config schemas (typed shape for the encrypted config blob per provider) ---