@webdecoy/fastify 0.1.0
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 +66 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +116 -0
- package/dist/index.mjs +79 -0
- package/package.json +60 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
|
2
|
+
import { WebDecoyConfig, ProtectOptions } from '@webdecoy/node';
|
|
3
|
+
export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Web Decoy Fastify Plugin
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
interface WebDecoyPluginOptions extends ProtectOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Custom function to extract IP address from request
|
|
12
|
+
* By default, uses request.ip or x-forwarded-for header
|
|
13
|
+
*/
|
|
14
|
+
getIP?: (req: FastifyRequest) => string;
|
|
15
|
+
/**
|
|
16
|
+
* Custom function to handle blocked requests
|
|
17
|
+
* By default, returns 403 Forbidden
|
|
18
|
+
*/
|
|
19
|
+
onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection: any) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Custom function to handle errors
|
|
22
|
+
* By default, logs error and allows request (fail open)
|
|
23
|
+
*/
|
|
24
|
+
onError?: (req: FastifyRequest, reply: FastifyReply, error: Error) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Paths to skip protection (e.g., health checks, static assets)
|
|
27
|
+
*/
|
|
28
|
+
skipPaths?: string[] | RegExp[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Web Decoy detection info attached to requests
|
|
32
|
+
*/
|
|
33
|
+
interface WebDecoyDetection {
|
|
34
|
+
decision: string;
|
|
35
|
+
confidence: number;
|
|
36
|
+
threat_level: string;
|
|
37
|
+
bot_detected: boolean;
|
|
38
|
+
bot_type?: string;
|
|
39
|
+
detection_id: string;
|
|
40
|
+
rule_enforced: boolean;
|
|
41
|
+
}
|
|
42
|
+
declare module 'fastify' {
|
|
43
|
+
interface FastifyRequest {
|
|
44
|
+
webdecoy?: WebDecoyDetection;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Fastify plugin for Web Decoy protection
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import Fastify from 'fastify';
|
|
53
|
+
* import webdecoy from '@webdecoy/fastify';
|
|
54
|
+
*
|
|
55
|
+
* const fastify = Fastify();
|
|
56
|
+
*
|
|
57
|
+
* fastify.register(webdecoy, {
|
|
58
|
+
* apiKey: process.env.WEBDECOY_API_KEY,
|
|
59
|
+
* skipPaths: ['/health'],
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare function webdecoyPluginImpl(fastify: FastifyInstance, options: WebDecoyConfig & WebDecoyPluginOptions): Promise<void>;
|
|
64
|
+
declare const webdecoyPlugin: typeof webdecoyPluginImpl;
|
|
65
|
+
|
|
66
|
+
export { type WebDecoyPluginOptions, webdecoyPlugin as default, webdecoyPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
|
2
|
+
import { WebDecoyConfig, ProtectOptions } from '@webdecoy/node';
|
|
3
|
+
export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Web Decoy Fastify Plugin
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
interface WebDecoyPluginOptions extends ProtectOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Custom function to extract IP address from request
|
|
12
|
+
* By default, uses request.ip or x-forwarded-for header
|
|
13
|
+
*/
|
|
14
|
+
getIP?: (req: FastifyRequest) => string;
|
|
15
|
+
/**
|
|
16
|
+
* Custom function to handle blocked requests
|
|
17
|
+
* By default, returns 403 Forbidden
|
|
18
|
+
*/
|
|
19
|
+
onBlocked?: (req: FastifyRequest, reply: FastifyReply, detection: any) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Custom function to handle errors
|
|
22
|
+
* By default, logs error and allows request (fail open)
|
|
23
|
+
*/
|
|
24
|
+
onError?: (req: FastifyRequest, reply: FastifyReply, error: Error) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Paths to skip protection (e.g., health checks, static assets)
|
|
27
|
+
*/
|
|
28
|
+
skipPaths?: string[] | RegExp[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Web Decoy detection info attached to requests
|
|
32
|
+
*/
|
|
33
|
+
interface WebDecoyDetection {
|
|
34
|
+
decision: string;
|
|
35
|
+
confidence: number;
|
|
36
|
+
threat_level: string;
|
|
37
|
+
bot_detected: boolean;
|
|
38
|
+
bot_type?: string;
|
|
39
|
+
detection_id: string;
|
|
40
|
+
rule_enforced: boolean;
|
|
41
|
+
}
|
|
42
|
+
declare module 'fastify' {
|
|
43
|
+
interface FastifyRequest {
|
|
44
|
+
webdecoy?: WebDecoyDetection;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Fastify plugin for Web Decoy protection
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import Fastify from 'fastify';
|
|
53
|
+
* import webdecoy from '@webdecoy/fastify';
|
|
54
|
+
*
|
|
55
|
+
* const fastify = Fastify();
|
|
56
|
+
*
|
|
57
|
+
* fastify.register(webdecoy, {
|
|
58
|
+
* apiKey: process.env.WEBDECOY_API_KEY,
|
|
59
|
+
* skipPaths: ['/health'],
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare function webdecoyPluginImpl(fastify: FastifyInstance, options: WebDecoyConfig & WebDecoyPluginOptions): Promise<void>;
|
|
64
|
+
declare const webdecoyPlugin: typeof webdecoyPluginImpl;
|
|
65
|
+
|
|
66
|
+
export { type WebDecoyPluginOptions, webdecoyPlugin as default, webdecoyPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
default: () => plugin_default,
|
|
34
|
+
webdecoyPlugin: () => webdecoyPlugin
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/plugin.ts
|
|
39
|
+
var import_fastify_plugin = __toESM(require("fastify-plugin"));
|
|
40
|
+
var import_node = require("@webdecoy/node");
|
|
41
|
+
function defaultGetIP(req) {
|
|
42
|
+
return req.ip || "127.0.0.1";
|
|
43
|
+
}
|
|
44
|
+
function defaultOnBlocked(req, reply, detection) {
|
|
45
|
+
reply.status(403).send({
|
|
46
|
+
error: "Forbidden",
|
|
47
|
+
message: "Access denied by Web Decoy protection",
|
|
48
|
+
detection_id: detection.detection_id
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function defaultOnError(req, reply, error) {
|
|
52
|
+
req.log.error({ err: error }, "[WebDecoy] Plugin error");
|
|
53
|
+
}
|
|
54
|
+
function shouldSkipPath(path, skipPaths) {
|
|
55
|
+
if (!skipPaths || skipPaths.length === 0) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return skipPaths.some((pattern) => {
|
|
59
|
+
if (typeof pattern === "string") {
|
|
60
|
+
return path === pattern || path.startsWith(pattern);
|
|
61
|
+
}
|
|
62
|
+
return pattern.test(path);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async function webdecoyPluginImpl(fastify, options) {
|
|
66
|
+
const sdk = new import_node.WebDecoy(options);
|
|
67
|
+
const getIP = options.getIP || defaultGetIP;
|
|
68
|
+
const onBlocked = options.onBlocked || defaultOnBlocked;
|
|
69
|
+
const onError = options.onError || defaultOnError;
|
|
70
|
+
const skipPaths = options.skipPaths;
|
|
71
|
+
fastify.decorateRequest("webdecoy", null);
|
|
72
|
+
fastify.addHook("preHandler", async (req, reply) => {
|
|
73
|
+
try {
|
|
74
|
+
if (shouldSkipPath(req.url, skipPaths)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const headers = {};
|
|
78
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
79
|
+
if (typeof value === "string") {
|
|
80
|
+
headers[key] = value;
|
|
81
|
+
} else if (Array.isArray(value)) {
|
|
82
|
+
headers[key] = value[0];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const metadata = {
|
|
86
|
+
method: req.method,
|
|
87
|
+
path: req.url,
|
|
88
|
+
ip: getIP(req),
|
|
89
|
+
user_agent: req.headers["user-agent"],
|
|
90
|
+
headers,
|
|
91
|
+
timestamp: Date.now()
|
|
92
|
+
};
|
|
93
|
+
const result = await sdk.protect(metadata, {
|
|
94
|
+
threshold: options.threshold,
|
|
95
|
+
skipLocalAnalysis: options.skipLocalAnalysis,
|
|
96
|
+
metadata: options.metadata
|
|
97
|
+
});
|
|
98
|
+
if (result.allowed) {
|
|
99
|
+
req.webdecoy = result.detection;
|
|
100
|
+
} else {
|
|
101
|
+
onBlocked(req, reply, result.detection);
|
|
102
|
+
}
|
|
103
|
+
} catch (error) {
|
|
104
|
+
onError(req, reply, error);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
var webdecoyPlugin = (0, import_fastify_plugin.default)(webdecoyPluginImpl, {
|
|
109
|
+
fastify: ">=4.0.0",
|
|
110
|
+
name: "@webdecoy/fastify"
|
|
111
|
+
});
|
|
112
|
+
var plugin_default = webdecoyPlugin;
|
|
113
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
114
|
+
0 && (module.exports = {
|
|
115
|
+
webdecoyPlugin
|
|
116
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
import fp from "fastify-plugin";
|
|
3
|
+
import { WebDecoy } from "@webdecoy/node";
|
|
4
|
+
function defaultGetIP(req) {
|
|
5
|
+
return req.ip || "127.0.0.1";
|
|
6
|
+
}
|
|
7
|
+
function defaultOnBlocked(req, reply, detection) {
|
|
8
|
+
reply.status(403).send({
|
|
9
|
+
error: "Forbidden",
|
|
10
|
+
message: "Access denied by Web Decoy protection",
|
|
11
|
+
detection_id: detection.detection_id
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function defaultOnError(req, reply, error) {
|
|
15
|
+
req.log.error({ err: error }, "[WebDecoy] Plugin error");
|
|
16
|
+
}
|
|
17
|
+
function shouldSkipPath(path, skipPaths) {
|
|
18
|
+
if (!skipPaths || skipPaths.length === 0) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return skipPaths.some((pattern) => {
|
|
22
|
+
if (typeof pattern === "string") {
|
|
23
|
+
return path === pattern || path.startsWith(pattern);
|
|
24
|
+
}
|
|
25
|
+
return pattern.test(path);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async function webdecoyPluginImpl(fastify, options) {
|
|
29
|
+
const sdk = new WebDecoy(options);
|
|
30
|
+
const getIP = options.getIP || defaultGetIP;
|
|
31
|
+
const onBlocked = options.onBlocked || defaultOnBlocked;
|
|
32
|
+
const onError = options.onError || defaultOnError;
|
|
33
|
+
const skipPaths = options.skipPaths;
|
|
34
|
+
fastify.decorateRequest("webdecoy", null);
|
|
35
|
+
fastify.addHook("preHandler", async (req, reply) => {
|
|
36
|
+
try {
|
|
37
|
+
if (shouldSkipPath(req.url, skipPaths)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const headers = {};
|
|
41
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
42
|
+
if (typeof value === "string") {
|
|
43
|
+
headers[key] = value;
|
|
44
|
+
} else if (Array.isArray(value)) {
|
|
45
|
+
headers[key] = value[0];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const metadata = {
|
|
49
|
+
method: req.method,
|
|
50
|
+
path: req.url,
|
|
51
|
+
ip: getIP(req),
|
|
52
|
+
user_agent: req.headers["user-agent"],
|
|
53
|
+
headers,
|
|
54
|
+
timestamp: Date.now()
|
|
55
|
+
};
|
|
56
|
+
const result = await sdk.protect(metadata, {
|
|
57
|
+
threshold: options.threshold,
|
|
58
|
+
skipLocalAnalysis: options.skipLocalAnalysis,
|
|
59
|
+
metadata: options.metadata
|
|
60
|
+
});
|
|
61
|
+
if (result.allowed) {
|
|
62
|
+
req.webdecoy = result.detection;
|
|
63
|
+
} else {
|
|
64
|
+
onBlocked(req, reply, result.detection);
|
|
65
|
+
}
|
|
66
|
+
} catch (error) {
|
|
67
|
+
onError(req, reply, error);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
var webdecoyPlugin = fp(webdecoyPluginImpl, {
|
|
72
|
+
fastify: ">=4.0.0",
|
|
73
|
+
name: "@webdecoy/fastify"
|
|
74
|
+
});
|
|
75
|
+
var plugin_default = webdecoyPlugin;
|
|
76
|
+
export {
|
|
77
|
+
plugin_default as default,
|
|
78
|
+
webdecoyPlugin
|
|
79
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webdecoy/fastify",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Web Decoy plugin for Fastify",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "./dist/index.js",
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
19
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
20
|
+
"test": "jest",
|
|
21
|
+
"lint": "eslint src/**/*.ts",
|
|
22
|
+
"clean": "rm -rf dist"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"web-decoy",
|
|
26
|
+
"fastify",
|
|
27
|
+
"plugin",
|
|
28
|
+
"bot-detection",
|
|
29
|
+
"security"
|
|
30
|
+
],
|
|
31
|
+
"author": "Web Decoy",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/webdecoy/webdecoy-node.git",
|
|
36
|
+
"directory": "packages/fastify"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@webdecoy/node": "*",
|
|
40
|
+
"fastify-plugin": "^4.5.1"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"fastify": "^4.0.0 || ^5.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/jest": "^29.5.11",
|
|
47
|
+
"@types/node": "^20.11.0",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^6.18.1",
|
|
49
|
+
"@typescript-eslint/parser": "^6.18.1",
|
|
50
|
+
"eslint": "^8.56.0",
|
|
51
|
+
"fastify": "^4.25.0",
|
|
52
|
+
"jest": "^29.7.0",
|
|
53
|
+
"ts-jest": "^29.1.1",
|
|
54
|
+
"tsup": "^8.0.1",
|
|
55
|
+
"typescript": "^5.3.3"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=18.0.0"
|
|
59
|
+
}
|
|
60
|
+
}
|