Files
calendar/packages/features/bot-detection/BotDetectionService.ts
T
Benny JooandGitHub ff38d6c7db refactor: Remove circular deps between @calcom/lib and @calcom/features [1] (#24399)
* add eslint config

* migrate autoLock to features

* migrate teamService to features

* migrate userCreationService

* migrate insights services to features

* migrate ProfileRepository

* update imports

* migrate filter segmen tests

* migrate filter segment repository

* migrate getBusyTimes

* migrate getLocaleFromRequest

* refactor csvUtils

* make filename clearer

* migrate getLuckyUser integration test

* migrate autoLock test to features

* wip

* refactors

* migrate useBookerUrl

* migrate more

* wip

* Migrate eventTypeRepository

* membership repository

* update imports

* update imports

* migrate

* move organization repository

* update imports

* update imports

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix tests

* fix type checks

* fix

* fix

* migrate

* update imports

* fix tests

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix
2025-10-13 12:01:02 -03:00

82 lines
2.4 KiB
TypeScript

import { checkBotId } from "botid/server";
import type { IncomingHttpHeaders } from "http";
import type { FeaturesRepository } from "@calcom/features/flags/features.repository";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
import type { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
interface BotDetectionConfig {
eventTypeId?: number;
headers: IncomingHttpHeaders;
}
const log = logger.getSubLogger({ prefix: ["[BotDetectionService]"] });
export class BotDetectionService {
constructor(
private featuresRepository: FeaturesRepository,
private eventTypeRepository: EventTypeRepository
) {}
private instanceHasBotIdEnabled() {
return process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER === "1";
}
async checkBotDetection(config: BotDetectionConfig): Promise<void> {
if (!this.instanceHasBotIdEnabled()) return;
const { eventTypeId, headers } = config;
// If no eventTypeId provided, skip bot detection
if (!eventTypeId) {
return;
}
// Fetch only the teamId from the event type
const eventType = await this.eventTypeRepository.getTeamIdByEventTypeId({
id: eventTypeId,
});
// Only check for team events
if (!eventType?.teamId) {
return;
}
// Check if BotID feature is enabled for this team (also checks global scope - enabling on all teams)
const isBotIDEnabled = await this.featuresRepository.checkIfTeamHasFeature(
eventType.teamId,
"booker-botid"
);
if (!isBotIDEnabled) {
return;
}
// Perform bot detection
const verification = await checkBotId({
advancedOptions: {
headers,
},
});
// Log verification results with detailed information
const verificationDetails = {
isBot: verification.isBot,
isHuman: verification.isHuman,
isVerifiedBot: verification.isVerifiedBot,
verifiedBotName: verification.verifiedBotName,
verifiedBotCategory: verification.verifiedBotCategory,
bypassed: verification.bypassed,
classificationReason: verification.classificationReason,
teamId: eventType.teamId,
eventTypeId,
};
if (verification.isBot) {
log.warn("Bot detected - blocking request", verificationDetails);
throw new HttpError({ statusCode: 403, message: "Access denied" });
}
}
}