@webdecoy/nextjs 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +36 -0
- package/dist/index.mjs +36 -0
- package/package.json +7 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import {
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig } from '@webdecoy/node';
|
|
3
3
|
export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -39,9 +39,10 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
|
|
|
39
39
|
* ```typescript
|
|
40
40
|
* // middleware.ts
|
|
41
41
|
* import { withWebDecoy } from '@webdecoy/nextjs';
|
|
42
|
+
* import { rateLimit } from '@webdecoy/node';
|
|
42
43
|
*
|
|
43
44
|
* export default withWebDecoy({
|
|
44
|
-
*
|
|
45
|
+
* rules: [rateLimit({ max: 100, window: 60 })],
|
|
45
46
|
* skipPaths: ['/_next', '/favicon.ico'],
|
|
46
47
|
* });
|
|
47
48
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import {
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig } from '@webdecoy/node';
|
|
3
3
|
export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -39,9 +39,10 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
|
|
|
39
39
|
* ```typescript
|
|
40
40
|
* // middleware.ts
|
|
41
41
|
* import { withWebDecoy } from '@webdecoy/nextjs';
|
|
42
|
+
* import { rateLimit } from '@webdecoy/node';
|
|
42
43
|
*
|
|
43
44
|
* export default withWebDecoy({
|
|
44
|
-
*
|
|
45
|
+
* rules: [rateLimit({ max: 100, window: 60 })],
|
|
45
46
|
* skipPaths: ['/_next', '/favicon.ico'],
|
|
46
47
|
* });
|
|
47
48
|
*
|
package/dist/index.js
CHANGED
|
@@ -97,6 +97,33 @@ function withWebDecoy(config) {
|
|
|
97
97
|
skipLocalAnalysis: config.skipLocalAnalysis,
|
|
98
98
|
metadata: config.metadata
|
|
99
99
|
});
|
|
100
|
+
if (!result.allowed && result.ruleResult) {
|
|
101
|
+
const rr = result.ruleResult;
|
|
102
|
+
if (rr.action === "THROTTLE") {
|
|
103
|
+
const retryAfter = rr.metadata?.retryAfter ?? 60;
|
|
104
|
+
return import_server.NextResponse.json(
|
|
105
|
+
{
|
|
106
|
+
error: "Too Many Requests",
|
|
107
|
+
message: rr.reason || "Rate limit exceeded",
|
|
108
|
+
retry_after: retryAfter
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
status: 429,
|
|
112
|
+
headers: { "Retry-After": String(retryAfter) }
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
if (rr.action === "DENY") {
|
|
117
|
+
return import_server.NextResponse.json(
|
|
118
|
+
{
|
|
119
|
+
error: "Forbidden",
|
|
120
|
+
message: rr.reason || "Access denied by rule",
|
|
121
|
+
rule: rr.rule
|
|
122
|
+
},
|
|
123
|
+
{ status: 403 }
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
100
127
|
if (result.allowed) {
|
|
101
128
|
const response = import_server.NextResponse.next();
|
|
102
129
|
if (result.detection) {
|
|
@@ -138,6 +165,15 @@ function withBotProtection(handler, config) {
|
|
|
138
165
|
metadata: config.metadata
|
|
139
166
|
});
|
|
140
167
|
if (!result.allowed) {
|
|
168
|
+
if (result.ruleResult?.action === "THROTTLE") {
|
|
169
|
+
const retryAfter = result.ruleResult.metadata?.retryAfter ?? 60;
|
|
170
|
+
res.setHeader("Retry-After", String(retryAfter));
|
|
171
|
+
return res.status(429).json({
|
|
172
|
+
error: "Too Many Requests",
|
|
173
|
+
message: result.ruleResult.reason || "Rate limit exceeded",
|
|
174
|
+
retry_after: retryAfter
|
|
175
|
+
});
|
|
176
|
+
}
|
|
141
177
|
return res.status(403).json({
|
|
142
178
|
error: "Forbidden",
|
|
143
179
|
message: "Access denied by Web Decoy protection",
|
package/dist/index.mjs
CHANGED
|
@@ -70,6 +70,33 @@ function withWebDecoy(config) {
|
|
|
70
70
|
skipLocalAnalysis: config.skipLocalAnalysis,
|
|
71
71
|
metadata: config.metadata
|
|
72
72
|
});
|
|
73
|
+
if (!result.allowed && result.ruleResult) {
|
|
74
|
+
const rr = result.ruleResult;
|
|
75
|
+
if (rr.action === "THROTTLE") {
|
|
76
|
+
const retryAfter = rr.metadata?.retryAfter ?? 60;
|
|
77
|
+
return NextResponse.json(
|
|
78
|
+
{
|
|
79
|
+
error: "Too Many Requests",
|
|
80
|
+
message: rr.reason || "Rate limit exceeded",
|
|
81
|
+
retry_after: retryAfter
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
status: 429,
|
|
85
|
+
headers: { "Retry-After": String(retryAfter) }
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
if (rr.action === "DENY") {
|
|
90
|
+
return NextResponse.json(
|
|
91
|
+
{
|
|
92
|
+
error: "Forbidden",
|
|
93
|
+
message: rr.reason || "Access denied by rule",
|
|
94
|
+
rule: rr.rule
|
|
95
|
+
},
|
|
96
|
+
{ status: 403 }
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
73
100
|
if (result.allowed) {
|
|
74
101
|
const response = NextResponse.next();
|
|
75
102
|
if (result.detection) {
|
|
@@ -111,6 +138,15 @@ function withBotProtection(handler, config) {
|
|
|
111
138
|
metadata: config.metadata
|
|
112
139
|
});
|
|
113
140
|
if (!result.allowed) {
|
|
141
|
+
if (result.ruleResult?.action === "THROTTLE") {
|
|
142
|
+
const retryAfter = result.ruleResult.metadata?.retryAfter ?? 60;
|
|
143
|
+
res.setHeader("Retry-After", String(retryAfter));
|
|
144
|
+
return res.status(429).json({
|
|
145
|
+
error: "Too Many Requests",
|
|
146
|
+
message: result.ruleResult.reason || "Rate limit exceeded",
|
|
147
|
+
retry_after: retryAfter
|
|
148
|
+
});
|
|
149
|
+
}
|
|
114
150
|
return res.status(403).json({
|
|
115
151
|
error: "Forbidden",
|
|
116
152
|
message: "Access denied by Web Decoy protection",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/nextjs",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Web Decoy middleware for Next.js",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -31,13 +31,17 @@
|
|
|
31
31
|
],
|
|
32
32
|
"author": "Web Decoy",
|
|
33
33
|
"license": "MIT",
|
|
34
|
+
"homepage": "https://github.com/WebDecoy/node-sdk#readme",
|
|
34
35
|
"repository": {
|
|
35
36
|
"type": "git",
|
|
36
|
-
"url": "https://github.com/
|
|
37
|
+
"url": "git+https://github.com/WebDecoy/node-sdk.git",
|
|
37
38
|
"directory": "packages/nextjs"
|
|
38
39
|
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/WebDecoy/node-sdk/issues"
|
|
42
|
+
},
|
|
39
43
|
"dependencies": {
|
|
40
|
-
"@webdecoy/node": "
|
|
44
|
+
"@webdecoy/node": "^0.2.1"
|
|
41
45
|
},
|
|
42
46
|
"peerDependencies": {
|
|
43
47
|
"next": ">=13.0.0"
|