@webdecoy/fastify 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 +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +21 -0
- package/dist/index.mjs +21 -0
- package/package.json +7 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig } from '@webdecoy/node';
|
|
3
3
|
export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -51,11 +51,12 @@ declare module 'fastify' {
|
|
|
51
51
|
* ```typescript
|
|
52
52
|
* import Fastify from 'fastify';
|
|
53
53
|
* import webdecoy from '@webdecoy/fastify';
|
|
54
|
+
* import { rateLimit } from '@webdecoy/node';
|
|
54
55
|
*
|
|
55
56
|
* const fastify = Fastify();
|
|
56
57
|
*
|
|
57
58
|
* fastify.register(webdecoy, {
|
|
58
|
-
*
|
|
59
|
+
* rules: [rateLimit({ max: 100, window: 60 })],
|
|
59
60
|
* skipPaths: ['/health'],
|
|
60
61
|
* });
|
|
61
62
|
* ```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig } from '@webdecoy/node';
|
|
3
3
|
export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -51,11 +51,12 @@ declare module 'fastify' {
|
|
|
51
51
|
* ```typescript
|
|
52
52
|
* import Fastify from 'fastify';
|
|
53
53
|
* import webdecoy from '@webdecoy/fastify';
|
|
54
|
+
* import { rateLimit } from '@webdecoy/node';
|
|
54
55
|
*
|
|
55
56
|
* const fastify = Fastify();
|
|
56
57
|
*
|
|
57
58
|
* fastify.register(webdecoy, {
|
|
58
|
-
*
|
|
59
|
+
* rules: [rateLimit({ max: 100, window: 60 })],
|
|
59
60
|
* skipPaths: ['/health'],
|
|
60
61
|
* });
|
|
61
62
|
* ```
|
package/dist/index.js
CHANGED
|
@@ -95,6 +95,27 @@ async function webdecoyPluginImpl(fastify, options) {
|
|
|
95
95
|
skipLocalAnalysis: options.skipLocalAnalysis,
|
|
96
96
|
metadata: options.metadata
|
|
97
97
|
});
|
|
98
|
+
if (!result.allowed && result.ruleResult) {
|
|
99
|
+
const rr = result.ruleResult;
|
|
100
|
+
if (rr.action === "THROTTLE") {
|
|
101
|
+
const retryAfter = rr.metadata?.retryAfter ?? 60;
|
|
102
|
+
reply.header("Retry-After", String(retryAfter));
|
|
103
|
+
reply.status(429).send({
|
|
104
|
+
error: "Too Many Requests",
|
|
105
|
+
message: rr.reason || "Rate limit exceeded",
|
|
106
|
+
retry_after: retryAfter
|
|
107
|
+
});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (rr.action === "DENY") {
|
|
111
|
+
reply.status(403).send({
|
|
112
|
+
error: "Forbidden",
|
|
113
|
+
message: rr.reason || "Access denied by rule",
|
|
114
|
+
rule: rr.rule
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
98
119
|
if (result.allowed) {
|
|
99
120
|
req.webdecoy = result.detection;
|
|
100
121
|
} else {
|
package/dist/index.mjs
CHANGED
|
@@ -58,6 +58,27 @@ async function webdecoyPluginImpl(fastify, options) {
|
|
|
58
58
|
skipLocalAnalysis: options.skipLocalAnalysis,
|
|
59
59
|
metadata: options.metadata
|
|
60
60
|
});
|
|
61
|
+
if (!result.allowed && result.ruleResult) {
|
|
62
|
+
const rr = result.ruleResult;
|
|
63
|
+
if (rr.action === "THROTTLE") {
|
|
64
|
+
const retryAfter = rr.metadata?.retryAfter ?? 60;
|
|
65
|
+
reply.header("Retry-After", String(retryAfter));
|
|
66
|
+
reply.status(429).send({
|
|
67
|
+
error: "Too Many Requests",
|
|
68
|
+
message: rr.reason || "Rate limit exceeded",
|
|
69
|
+
retry_after: retryAfter
|
|
70
|
+
});
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (rr.action === "DENY") {
|
|
74
|
+
reply.status(403).send({
|
|
75
|
+
error: "Forbidden",
|
|
76
|
+
message: rr.reason || "Access denied by rule",
|
|
77
|
+
rule: rr.rule
|
|
78
|
+
});
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
61
82
|
if (result.allowed) {
|
|
62
83
|
req.webdecoy = result.detection;
|
|
63
84
|
} else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/fastify",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Web Decoy plugin for Fastify",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -30,13 +30,17 @@
|
|
|
30
30
|
],
|
|
31
31
|
"author": "Web Decoy",
|
|
32
32
|
"license": "MIT",
|
|
33
|
+
"homepage": "https://github.com/WebDecoy/node-sdk#readme",
|
|
33
34
|
"repository": {
|
|
34
35
|
"type": "git",
|
|
35
|
-
"url": "https://github.com/
|
|
36
|
+
"url": "git+https://github.com/WebDecoy/node-sdk.git",
|
|
36
37
|
"directory": "packages/fastify"
|
|
37
38
|
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/WebDecoy/node-sdk/issues"
|
|
41
|
+
},
|
|
38
42
|
"dependencies": {
|
|
39
|
-
"@webdecoy/node": "
|
|
43
|
+
"@webdecoy/node": "^0.2.1",
|
|
40
44
|
"fastify-plugin": "^4.5.1"
|
|
41
45
|
},
|
|
42
46
|
"peerDependencies": {
|