Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/bio/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { parseBioUrl, setVisitorCookies } from '@barely/lib/middleware/request-parsing';
import { isSpamRequest, spamResponse } from '@barely/lib/middleware/spam-filter';
import { log } from '@barely/lib/utils/log';

export async function middleware(req: NextRequest) {
if (isSpamRequest(req.nextUrl.pathname)) return spamResponse();

const { handle, key } = parseBioUrl(req.url);

console.log('bio middleware', handle, key);
Expand Down
3 changes: 3 additions & 0 deletions apps/cart/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import {
parseReqForVisitorInfo,
setVisitorCookies,
} from '@barely/lib/middleware/request-parsing';
import { isSpamRequest, spamResponse } from '@barely/lib/middleware/spam-filter';
import { log } from '@barely/lib/utils/log';
import { getAbsoluteUrl, isDevelopment, newId } from '@barely/utils';

import type { CreateCartBody } from '~/app/api/cart/create/route';
import { cartEnv } from '~/env';

export async function middleware(req: NextRequest, ev: NextFetchEvent) {
if (isSpamRequest(req.nextUrl.pathname)) return spamResponse();

const domain = req.headers.get('host');
const pathname = req.nextUrl.pathname;

Expand Down
3 changes: 3 additions & 0 deletions apps/link/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { sqlAnd } from '@barely/db/utils';
import { recordLinkClick } from '@barely/lib/functions/event.fns';
import { parseLink } from '@barely/lib/middleware';
import { parseReqForVisitorInfo } from '@barely/lib/middleware/request-parsing';
import { isSpamRequest, spamResponse } from '@barely/lib/middleware/spam-filter';
import { getAbsoluteUrl } from '@barely/utils';
import { eq } from 'drizzle-orm';

Expand All @@ -18,6 +19,8 @@ export const config = {
};

export async function middleware(req: NextRequest, ev: NextFetchEvent) {
if (isSpamRequest(req.nextUrl.pathname)) return spamResponse();

const url = req.nextUrl;
const linkProps = parseLink(req);

Expand Down
3 changes: 3 additions & 0 deletions apps/nyc/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { setVisitorCookies } from '@barely/lib/middleware/request-parsing';
import { isSpamRequest, spamResponse } from '@barely/lib/middleware/spam-filter';

export async function middleware(req: NextRequest) {
if (isSpamRequest(req.nextUrl.pathname)) return spamResponse();

const res = NextResponse.next();

// Set visitor cookies to persist tracking parameters (fbclid, sessionId, etc.)
Expand Down
3 changes: 3 additions & 0 deletions apps/vip/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { setVisitorCookies } from '@barely/lib/middleware/request-parsing';
import { isSpamRequest, spamResponse } from '@barely/lib/middleware/spam-filter';
import { log } from '@barely/lib/utils/log';

export async function middleware(req: NextRequest) {
if (isSpamRequest(req.nextUrl.pathname)) return spamResponse();

const pathname = req.nextUrl.pathname;

// Parse the VIP URL to extract handle and key
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
"types": "./dist/src/middleware/request-parsing.d.ts",
"default": "./src/middleware/request-parsing.ts"
},
"./middleware/spam-filter": {
"types": "./dist/src/middleware/spam-filter.d.ts",
"default": "./src/middleware/spam-filter.ts"
},
"./trigger/*": {
"types": "./dist/src/integrations/trigger/*.d.ts",
"default": "./src/integrations/trigger/*.ts"
Expand Down
26 changes: 26 additions & 0 deletions packages/lib/src/middleware/spam-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextResponse } from 'next/server';

const SPAM_PATTERNS = [
'.php',
'license.txt',
'wp-content',
'wp-admin',
'wp-includes',
'wp-login',
'xmlrpc',
'.trash',
'.env',
];

export function isSpamRequest(pathname: string): boolean {
const lower = pathname.toLowerCase();
const matched = SPAM_PATTERNS.find(pattern => lower.includes(pattern));
if (matched) console.log(`[spam-filter] blocked "${pathname}" (matched: ${matched})`);
return !!matched;
}
Comment on lines +15 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 No logging for blocked requests

When a spam request is blocked, nothing is logged. Adding a log line here would let you monitor attack frequency and patterns over time, and verify the filter is working as expected in production. The log utility is already imported in the consuming middlewares, or it could be imported directly here:

Suggested change
export function isSpamRequest(pathname: string): boolean {
const lower = pathname.toLowerCase();
return SPAM_PATTERNS.some(pattern => lower.includes(pattern));
}
export function isSpamRequest(pathname: string): boolean {
const lower = pathname.toLowerCase();
const matched = SPAM_PATTERNS.find(pattern => lower.includes(pattern));
if (matched) console.log(`[spam-filter] blocked "${pathname}" (matched: ${matched})`);
return !!matched;
}


export function spamResponse() {
return new NextResponse('Nice try. Pls go away.', {
status: 418,
});
}
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

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

Loading