Skip to content

Commit

Permalink
fix: block bots more aggressively
Browse files Browse the repository at this point in the history
  • Loading branch information
eligundry committed Aug 13, 2024
1 parent 559b260 commit fe0f881
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
8 changes: 2 additions & 6 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import type { AppLoadContext, EntryContext } from '@remix-run/node'
import { createReadableStreamFromReadable } from '@remix-run/node'
import { RemixServer } from '@remix-run/react'
import { detect } from 'detect-browser'
import { isbot } from 'isbot'
import { PassThrough } from 'node:stream'
import { renderToPipeableStream } from 'react-dom/server'

Expand All @@ -19,11 +19,7 @@ export default function handleRequest(
remixContext: EntryContext,
_loadContext: AppLoadContext,
) {
const isbot =
detect(request.headers.get('user-agent') ?? '')?.type?.startsWith('bot') ??
false

return isbot
return isbot(request.headers.get('user-agent'))
? handleBotRequest(
request,
responseStatusCode,
Expand Down
6 changes: 2 additions & 4 deletions app/lib/responses.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HeadersFunction, json } from '@remix-run/node'
import { detect } from 'detect-browser'
import { isbot } from 'isbot'
import omit from 'lodash/omit'
import type { Logger } from 'winston'
import type { ZodIssue } from 'zod'
Expand Down Expand Up @@ -118,9 +118,7 @@ export function blockBots(request: Request) {
})
}

const detected = detect(userAgent)

if (detected && detected.type.startsWith('bot')) {
if (isbot(userAgent)) {
throw forbidden({
error: 'Bots are not allowed to access this resource.',
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"date-fns": "^3.6.0",
"detect-browser": "^5.3.0",
"drizzle-orm": "^0.31.2",
"isbot": "^5.1.14",
"lodash": "^4.17.21",
"morgan": "^1.10.0",
"react": "^18.2.0",
Expand Down
12 changes: 7 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fe0f881

Please sign in to comment.