DEV Community

Parimal Makes
Parimal Makes

Posted on

Stopping 404 Bot Scans at the Edge Before Your App Bundle Loads

If you host a full-stack app on serverless or edge workers, your logs are probably full of requests for /wp-login.php, .env, and random PHP paths.

On a traditional VPS, Nginx drops those fast and you barely notice. On a worker runtime like Cloudflare Workers running Next.js, those probes can wake up your app handler just to render a 404 page.

The hidden cost of a 404

Every time an unknown route hits your main framework handler, you pay for routing, middleware, and sometimes SSR rendering just to tell a bot the page doesn't exist.

The same thing happens with webhook endpoints. Automated scanners love poking /api/webhooks/stripe or /api/webhooks/paypal with empty POST requests. If your handler parses the body, initializes a database client, and only then checks the signature, you're burning CPU on junk traffic.

Two cheap checks at the boundary

You can cut most of this noise out in a few milliseconds before your framework even boots.

1. Generate a route allowlist at build time

Hand-maintaining regexes for valid routes always drifts out of sync with your app/ directory. Instead, you can scan your route files during the build step (for example with TypeScript's compiler API or a small build script) and emit a static manifest of exact paths and dynamic route patterns.

At the worker entry point, check incoming GET requests against that manifest first:

import { staticRoutes, dynamicPatterns } from "./edge-routes.generated.mjs";

function isKnownRoute(pathname) {
  if (staticRoutes.has(pathname)) return true;
  return dynamicPatterns.some((pattern) => pattern.test(pathname));
}
Enter fullscreen mode Exit fullscreen mode

If isKnownRoute(pathname) is false, return a plain 404 response right away. No SSR, no React tree, no wasted CPU.

2. Check webhook headers before loading the handler

For POST endpoints like payment webhooks, check for the required signature headers at the edge before importing or running the route handler:

if (pathname === "/api/webhooks/paypal") {
  const hasSig = request.headers.has("paypal-transmission-sig");
  if (!hasSig) {
    return new Response("Bad Request", { status: 400 });
  }
}
Enter fullscreen mode Exit fullscreen mode

You still verify the cryptographic signature inside the real handler, of course. The edge check just drops unsigned probes in about 1ms instead of 400ms.

If you're hitting worker CPU limits or seeing spiky cold starts, check how much of your traffic is actually 404 scans and empty POSTs. Filtering them at the boundary is one of the highest-leverage fixes you can ship.

Top comments (0)