brackt/server/app.ts
Chris Parsons 68193e31c5
Block bot probe requests before routing (#444)
Fixes #344

* Add bot probe filter middleware and Sentry noise suppression

Block common bot scanning paths (wp-admin, .env, .php, etc.) in Express
before React Router handles them, preventing spurious Sentry errors.
Also filter Sentry events for React Flight protocol probes ($1:aa:aa
multipart body pattern) and any remaining bot-path 404 noise.

https://claude.ai/code/session_01Y5Ca6oxd5zyyM89acmogf7

* Tighten bot probe regex and React Flight Sentry filter

- Remove .xml, .log, .git from bot probe blocklist to avoid false positives
  on legitimate routes (e.g. /sitemap.xml)
- Tighten React Flight error filter from broad string match to precise
  wire-format pattern (/\$\d+:[a-z]/) to avoid swallowing real errors
- Remove redundant URL check from beforeSend (ignoreErrors already covers it)
- Fix missing newline at end of instrument.server.mjs

https://claude.ai/code/session_01Y5Ca6oxd5zyyM89acmogf7

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-18 11:17:18 -07:00

36 lines
1.3 KiB
TypeScript

import { createRequestHandler } from "@react-router/express";
import express from "express";
import type { ServerBuild } from "react-router";
import { RouterContextProvider } from "react-router";
import { DatabaseContext } from "~/database/context";
import { db } from "./db";
import { expressValueContext } from "~/contexts/express";
export const app = express();
app.use((_, __, next) => DatabaseContext.run(db, next));
// Block common bot probe paths before React Router (and Sentry) see them
const BOT_PROBE_RE =
/\.(php|env|htaccess|aspx|asp|jsp|config|bak|sql|ini|swp|DS_Store)$|^\/(wp-admin|wp-login|phpmyadmin|xmlrpc|server-status|cgi-bin|shell|cmd|console|actuator)(\/|$)/i;
app.use((req, res, next) => {
if (BOT_PROBE_RE.test(req.path)) {
res.status(404).end();
return;
}
next();
});
app.use(
createRequestHandler({
build: () => import("virtual:react-router/server-build") as unknown as Promise<ServerBuild>,
// @ts-ignore -- RouterContextProvider is the correct runtime type but tsconfig.server.json can't resolve the conditional type statically
getLoadContext() {
const provider = new RouterContextProvider();
provider.set(expressValueContext, "Hello from Express");
return provider as unknown as RouterContextProvider;
},
}),
);