File tree Expand file tree Collapse file tree 5 files changed +77
-3
lines changed
sample-apps/express-mysql Expand file tree Collapse file tree 5 files changed +77
-3
lines changed Original file line number Diff line number Diff line change @@ -546,4 +546,8 @@ export class Agent {
546546 onMiddlewareExecuted ( ) {
547547 this . middlewareInstalled = true ;
548548 }
549+
550+ getToken ( ) {
551+ return this . token ;
552+ }
549553}
Original file line number Diff line number Diff line change @@ -3,7 +3,10 @@ import isFirewallSupported from "./helpers/isFirewallSupported";
33import shouldEnableFirewall from "./helpers/shouldEnableFirewall" ;
44import { setUser } from "./agent/context/user" ;
55import { shouldBlockRequest } from "./middleware/shouldBlockRequest" ;
6- import { addExpressMiddleware } from "./middleware/express" ;
6+ import {
7+ addExpressMiddleware ,
8+ addExpressMiddlewareAsync ,
9+ } from "./middleware/express" ;
710import { addHonoMiddleware } from "./middleware/hono" ;
811import { addHapiMiddleware } from "./middleware/hapi" ;
912import { addFastifyHook } from "./middleware/fastify" ;
@@ -20,6 +23,7 @@ export {
2023 setUser ,
2124 shouldBlockRequest ,
2225 addExpressMiddleware ,
26+ addExpressMiddlewareAsync ,
2327 addHonoMiddleware ,
2428 addHapiMiddleware ,
2529 addFastifyHook ,
@@ -32,6 +36,7 @@ export default {
3236 setUser,
3337 shouldBlockRequest,
3438 addExpressMiddleware,
39+ addExpressMiddlewareAsync,
3540 addHonoMiddleware,
3641 addHapiMiddleware,
3742 addFastifyHook,
Original file line number Diff line number Diff line change 11/** TS_EXPECT_TYPES_ERROR_OPTIONAL_DEPENDENCY **/
22import type { Express } from "express" ;
3- import { shouldBlockRequest } from "./shouldBlockRequest" ;
3+ import {
4+ shouldBlockRequest ,
5+ shouldBlockRequestAsync ,
6+ } from "./shouldBlockRequest" ;
47import { escapeHTML } from "../helpers/escapeHTML" ;
58
69/**
@@ -30,3 +33,31 @@ export function addExpressMiddleware(app: Express) {
3033 next ( ) ;
3134 } ) ;
3235}
36+
37+ export function addExpressMiddlewareAsync ( app : Express ) {
38+ app . use ( ( req , res , next ) => {
39+ shouldBlockRequestAsync ( )
40+ . then ( ( result ) => {
41+ if ( result . block ) {
42+ if ( result . type === "ratelimited" ) {
43+ let message = "You are rate limited by Zen." ;
44+ if ( result . trigger === "ip" && result . ip ) {
45+ message += ` (Your IP: ${ escapeHTML ( result . ip ) } )` ;
46+ }
47+
48+ return res . status ( 429 ) . type ( "text" ) . send ( message ) ;
49+ }
50+
51+ if ( result . type === "blocked" ) {
52+ return res . status ( 403 ) . type ( "text" ) . send ( "You are blocked by Zen." ) ;
53+ }
54+ }
55+
56+ next ( ) ;
57+ } )
58+ . catch ( ( error ) => {
59+ console . error ( error ) ;
60+ next ( ) ;
61+ } ) ;
62+ } ) ;
63+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,38 @@ type Result = {
99 ip ?: string ;
1010} ;
1111
12+ const SERVERLESS_URL = "http://localhost:5132" ;
13+
14+ export async function shouldBlockRequestAsync ( ) : Promise < Result > {
15+ const context = getContext ( ) ;
16+ if ( ! context ) {
17+ return { block : false } ;
18+ }
19+
20+ const agent = getInstance ( ) ;
21+ if ( ! agent ) {
22+ return { block : false } ;
23+ }
24+
25+ const response = await fetch ( `${ SERVERLESS_URL } /check-request` , {
26+ method : "POST" ,
27+ headers : {
28+ // TODO: Remove "!", token is not guaranteed to be present
29+ Authorization : agent . getToken ( ) ! . asString ( ) ,
30+ "Content-Type" : "application/json" ,
31+ } ,
32+ body : JSON . stringify ( {
33+ method : context . method ,
34+ headers : context . headers ,
35+ url : context . url ,
36+ route : context . route ,
37+ clientIp : context . remoteAddress ,
38+ } ) ,
39+ } ) ;
40+
41+ return await response . json ( ) ;
42+ }
43+
1244export function shouldBlockRequest ( ) : Result {
1345 const context = getContext ( ) ;
1446 if ( ! context ) {
Original file line number Diff line number Diff line change 11require ( "dotenv" ) . config ( ) ;
2- require ( "@aikidosec/firewall" ) ;
2+ const Zen = require ( "@aikidosec/firewall" ) ;
33const Sentry = require ( "@sentry/node" ) ;
44
55Sentry . init ( {
@@ -59,6 +59,8 @@ async function main(port) {
5959
6060 const app = express ( ) ;
6161
62+ Zen . addExpressMiddlewareAsync ( app ) ;
63+
6264 app . use ( Sentry . Handlers . requestHandler ( ) ) ;
6365 app . use ( morgan ( "tiny" ) ) ;
6466
You can’t perform that action at this time.
0 commit comments