Skip to content

Commit bdaf0d8

Browse files
committed
fix: bot check;
1 parent e5bf324 commit bdaf0d8

File tree

1 file changed

+136
-10
lines changed

1 file changed

+136
-10
lines changed

apps/basket/src/routes/basket.ts

Lines changed: 136 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ async function validateRequest(body: any, query: any, request: Request) {
129129
request.headers.get('user-agent'),
130130
VALIDATION_LIMITS.STRING_MAX_LENGTH
131131
) || '';
132+
133+
const ip = extractIpFromRequest(request);
134+
135+
return {
136+
success: true,
137+
clientId,
138+
userAgent,
139+
ip,
140+
ownerId: website.ownerId,
141+
};
142+
}
143+
144+
async function checkForBot(
145+
request: Request,
146+
body: any,
147+
query: any,
148+
clientId: string,
149+
userAgent: string
150+
): Promise<{ error?: { status: string } } | null> {
132151
const botCheck = detectBot(userAgent, request);
133152
if (botCheck.isBot) {
134153
await logBlockedTraffic(
@@ -142,16 +161,7 @@ async function validateRequest(body: any, query: any, request: Request) {
142161
);
143162
return { error: { status: 'ignored' } };
144163
}
145-
146-
const ip = extractIpFromRequest(request);
147-
148-
return {
149-
success: true,
150-
clientId,
151-
userAgent,
152-
ip,
153-
ownerId: website.ownerId,
154-
};
164+
return null;
155165
}
156166

157167
async function insertError(
@@ -688,6 +698,18 @@ const app = new Elysia()
688698
const eventType = body.type || 'track';
689699

690700
if (eventType === 'track') {
701+
// Check for bots before processing track events
702+
const botError = await checkForBot(
703+
request,
704+
body,
705+
query,
706+
clientId,
707+
userAgent
708+
);
709+
if (botError) {
710+
return botError.error;
711+
}
712+
691713
const parseResult = analyticsEventSchema.safeParse(body);
692714
if (!parseResult.success) {
693715
console.error(
@@ -716,6 +738,18 @@ const app = new Elysia()
716738
}
717739

718740
if (eventType === 'error') {
741+
// Check for bots before processing error events
742+
const botError = await checkForBot(
743+
request,
744+
body,
745+
query,
746+
clientId,
747+
userAgent
748+
);
749+
if (botError) {
750+
return botError.error;
751+
}
752+
719753
const parseResult = errorEventSchema.safeParse(body);
720754
if (!parseResult.success) {
721755
console.error(
@@ -744,6 +778,18 @@ const app = new Elysia()
744778
}
745779

746780
if (eventType === 'web_vitals') {
781+
// Check for bots before processing web vitals events
782+
const botError = await checkForBot(
783+
request,
784+
body,
785+
query,
786+
clientId,
787+
userAgent
788+
);
789+
if (botError) {
790+
return botError.error;
791+
}
792+
747793
const parseResult = webVitalsEventSchema.safeParse(body);
748794
if (!parseResult.success) {
749795
console.error(
@@ -800,6 +846,18 @@ const app = new Elysia()
800846
}
801847

802848
if (eventType === 'outgoing_link') {
849+
// Check for bots before processing outgoing link events
850+
const botError = await checkForBot(
851+
request,
852+
body,
853+
query,
854+
clientId,
855+
userAgent
856+
);
857+
if (botError) {
858+
return botError.error;
859+
}
860+
803861
const parseResult = outgoingLinkSchema.safeParse(body);
804862
if (!parseResult.success) {
805863
console.error(
@@ -871,6 +929,23 @@ const app = new Elysia()
871929
const eventType = event.type || 'track';
872930

873931
if (eventType === 'track') {
932+
// Check for bots before processing track events
933+
const botError = await checkForBot(
934+
request,
935+
event,
936+
query,
937+
clientId,
938+
userAgent
939+
);
940+
if (botError) {
941+
return {
942+
status: 'error',
943+
message: 'Bot detected',
944+
eventType,
945+
error: 'ignored',
946+
};
947+
}
948+
874949
const parseResult = analyticsEventSchema.safeParse(event);
875950
if (!parseResult.success) {
876951
console.error(
@@ -913,6 +988,23 @@ const app = new Elysia()
913988
}
914989
}
915990
if (eventType === 'error') {
991+
// Check for bots before processing error events
992+
const botError = await checkForBot(
993+
request,
994+
event,
995+
query,
996+
clientId,
997+
userAgent
998+
);
999+
if (botError) {
1000+
return {
1001+
status: 'error',
1002+
message: 'Bot detected',
1003+
eventType,
1004+
error: 'ignored',
1005+
};
1006+
}
1007+
9161008
const parseResult = errorEventSchema.safeParse(event);
9171009
if (!parseResult.success) {
9181010
console.error(
@@ -955,6 +1047,23 @@ const app = new Elysia()
9551047
}
9561048
}
9571049
if (eventType === 'web_vitals') {
1050+
// Check for bots before processing web vitals events
1051+
const botError = await checkForBot(
1052+
request,
1053+
event,
1054+
query,
1055+
clientId,
1056+
userAgent
1057+
);
1058+
if (botError) {
1059+
return {
1060+
status: 'error',
1061+
message: 'Bot detected',
1062+
eventType,
1063+
error: 'ignored',
1064+
};
1065+
}
1066+
9581067
const parseResult = webVitalsEventSchema.safeParse(event);
9591068
if (!parseResult.success) {
9601069
console.error(
@@ -1039,6 +1148,23 @@ const app = new Elysia()
10391148
}
10401149
}
10411150
if (eventType === 'outgoing_link') {
1151+
// Check for bots before processing outgoing link events
1152+
const botError = await checkForBot(
1153+
request,
1154+
event,
1155+
query,
1156+
clientId,
1157+
userAgent
1158+
);
1159+
if (botError) {
1160+
return {
1161+
status: 'error',
1162+
message: 'Bot detected',
1163+
eventType,
1164+
error: 'ignored',
1165+
};
1166+
}
1167+
10421168
const parseResult = outgoingLinkSchema.safeParse(event);
10431169
if (!parseResult.success) {
10441170
console.error(

0 commit comments

Comments
 (0)