Skip to content

Commit 1c1057a

Browse files
test(*): Mount a volume for crowdsec docker as it is required since 1.7.0
1 parent baf5f61 commit 1c1057a

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

examples/express-server/docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ services:
88
- crowdsec/.env
99
volumes:
1010
- ./crowdsec/custom-config.yaml:/etc/crowdsec/config.yaml.local
11+
- crowdsec_data:/var/lib/crowdsec/data
12+
13+
volumes:
14+
crowdsec_data:

examples/nextjs/README.md

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,39 @@ It aims to help developers to understand how to integrate CrowdSec remediation i
1111
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1212

1313
- [NextJS basic implementation](#nextjs-basic-implementation)
14-
- [Technical overview](#technical-overview)
15-
- [Middleware (`src/middleware.ts`)](#middleware-srcmiddlewarets)
16-
- [API Routes](#api-routes)
17-
- [Remediation Check (`src/app/api/crowdsec/remediation/route.ts`)](#remediation-check-srcappapicrowdsecremediationroutets)
18-
- [Captcha Handler (`src/app/api/crowdsec/captcha/route.ts`)](#captcha-handler-srcappapicrowdseccaptcharoutets)
19-
- [Test the bouncer](#test-the-bouncer)
20-
- [Pre-requisites](#pre-requisites)
21-
- [Prepare the tests](#prepare-the-tests)
22-
- [Test a "bypass" remediation](#test-a-bypass-remediation)
23-
- [Test a "ban" remediation](#test-a-ban-remediation)
24-
- [Test a "captcha" remediation](#test-a-captcha-remediation)
14+
- [Technical overview](#technical-overview)
15+
- [Middleware (`src/middleware.ts`)](#middleware-srcmiddlewarets)
16+
- [API Routes](#api-routes)
17+
- [Remediation Check (
18+
`src/app/api/crowdsec/remediation/route.ts`)](#remediation-check-srcappapicrowdsecremediationroutets)
19+
- [Captcha Handler (
20+
`src/app/api/crowdsec/captcha/route.ts`)](#captcha-handler-srcappapicrowdseccaptcharoutets)
21+
- [Test the bouncer](#test-the-bouncer)
22+
- [Pre-requisites](#pre-requisites)
23+
- [Prepare the tests](#prepare-the-tests)
24+
- [Test a "bypass" remediation](#test-a-bypass-remediation)
25+
- [Test a "ban" remediation](#test-a-ban-remediation)
26+
- [Test a "captcha" remediation](#test-a-captcha-remediation)
2527

2628
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2729

2830
## Technical overview
2931

3032
The implementation uses Next.js App Router with middleware and API routes.
3133

32-
**Important Note**: Starting from Next.js 15.5, the middleware now supports the Node.js runtime, which is required for the CrowdSec bouncer to function properly. You will need Next.js version 15.5 or higher to use this implementation. The middleware configuration includes `runtime: 'nodejs'` to enable this feature. For compatibility reasons, we still use custom API routes (`/api/crowdsec/remediation` and `/api/crowdsec/captcha`) to handle the bouncer logic separately from the middleware.
34+
**Important Note**: Starting from Next.js 15.5, the middleware now supports the Node.js runtime, which is required for
35+
the CrowdSec bouncer to function properly. You will need Next.js version 15.5 or higher to use this implementation. The
36+
middleware configuration includes `runtime: 'nodejs'` to enable this feature. For compatibility reasons, we still use
37+
custom API routes (`/api/crowdsec/remediation` and `/api/crowdsec/captcha`) to handle the bouncer logic separately from
38+
the middleware.
3339

3440
**Additional Notes**:
35-
- The Next.js configuration (`next.config.ts`) includes a custom Webpack plugin to copy font files from the `svg-captcha-fixed` library, making them available at runtime for captcha generation.
41+
42+
- The Next.js configuration (`next.config.ts`) includes a custom Webpack plugin to copy font files from the
43+
`svg-captcha-fixed` library, making them available at runtime for captcha generation.
3644
- The project now includes Tailwind CSS v4 for styling the captcha page and other UI components.
37-
- Environment variables are loaded from `.env` files in the `nextjs` directory using `dotenv` and `dotenv-safe` for validation.
45+
- Environment variables are loaded from `.env` files in the `nextjs` directory using `dotenv` and `dotenv-safe` for
46+
validation.
3847

3948
### Middleware (`src/middleware.ts`)
4049

@@ -44,12 +53,12 @@ The middleware intercepts all requests and calls the CrowdSec API:
4453
export async function middleware(req: NextRequest) {
4554
// Check CrowdSec remediation using helper function
4655
const res = await checkRequestRemediation(req);
47-
56+
4857
if (res) {
4958
// Return ban/captcha wall if remediation is required
5059
return res;
5160
}
52-
61+
5362
return NextResponse.next();
5463
}
5564

@@ -95,7 +104,7 @@ export async function POST(req: Request) {
95104
const phrase = form.get('phrase')?.toString() || '';
96105
const refresh = form.get('crowdsec_captcha_refresh')?.toString() || '0';
97106
const ip = getIpFromRequest(req);
98-
107+
99108
await bouncer.handleCaptchaSubmission({ ip, userPhrase: phrase, refresh, origin });
100109
return NextResponse.redirect(new URL('/', req.url));
101110
}
@@ -111,8 +120,6 @@ export async function POST(req: Request) {
111120
112121
- Copy the `.env.example` file to `.env` in the `nextjs` folder and fill in the required values
113122
114-
- Copy the `crowdsec/.env.example` file to `crowdsec/.env` and fill in the required values
115-
116123
- Install all dependencies.
117124
118125
Run the following command from the `nextjs` folder:
@@ -121,7 +128,8 @@ export async function POST(req: Request) {
121128
npm install
122129
```
123130
124-
**Note**: The `npm run dev` and `npm run start` commands will automatically build and pack the bouncer library before starting the server.
131+
**Note**: The `npm run dev` and `npm run start` commands will automatically build and pack the bouncer library before
132+
starting the server.
125133
126134
### Prepare the tests
127135
@@ -141,7 +149,7 @@ In another terminal, create a bouncer if you haven't already:
141149
docker exec -ti nodejs-cs-nextjs-crowdsec sh -c 'cscli bouncers add NodeBouncer --key $BOUNCER_KEY'
142150
```
143151
144-
We are using here the `BOUNCER_KEY` variable defined in `crowdsec/.env` file.
152+
We are using here the `BOUNCER_KEY` variable defined in `.env` file.
145153
146154
3. Launch the Next.js Server
147155
@@ -161,7 +169,8 @@ You should see different log messages in your terminal when you access the home
161169
162170
### Test a "bypass" remediation
163171
164-
As you don't have yet any decisions, you can access the `http://localhost:3000` page and just see the normal Next.js content.
172+
As you don't have yet any decisions, you can access the `http://localhost:3000` page and just see the normal Next.js
173+
content.
165174

166175
![](./docs/bypass.png)
167176

@@ -175,7 +184,7 @@ First, add a ban remediation for the IP that will be tested:
175184
docker exec -ti nodejs-cs-nextjs-crowdsec sh -c 'cscli decisions add --ip $BOUNCED_IP --duration 12m --type ban'
176185
```
177186

178-
We are using here the `BOUNCED_IP` variable defined in `crowdsec/.env` file.
187+
We are using here the `BOUNCED_IP` variable defined in `.env` file.
179188

180189
You should see the success message `Decision successfully added`.
181190

@@ -191,13 +200,13 @@ You should see `Final remediation for IP <BOUNCED_IP> is ban` in terminal.
191200
First, remove your last decision:
192201

193202
```shell
194-
docker exec -ti nodejs-cs-crowdsec sh -c 'cscli decisions delete --ip $BOUNCED_IP'
203+
docker exec -ti nodejs-cs-nextjs-crowdsec sh -c 'cscli decisions delete --ip $BOUNCED_IP'
195204
```
196205

197206
Then, add a captcha decision:
198207

199208
```shell
200-
docker exec -ti nodejs-cs-crowdsec sh -c 'cscli decisions add --ip $BOUNCED_IP --duration 12m --type captcha'
209+
docker exec -ti nodejs-cs-nextjs-crowdsec sh -c 'cscli decisions add --ip $BOUNCED_IP --duration 12m --type captcha'
201210
```
202211

203212
If you try to access the home page (after two minutes as it is the default ttl for malicious IP), you should the "Access

examples/nextjs/docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ services:
88
- ../express-server/crowdsec/.env
99
volumes:
1010
- ./crowdsec/custom-config.yaml:/etc/crowdsec/config.yaml.local
11+
- crowdsec_data:/var/lib/crowdsec/data
12+
13+
volumes:
14+
crowdsec_data:

0 commit comments

Comments
 (0)