@zudojs/security 0.0.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/LICENSE +21 -0
- package/README.md +37 -0
- package/dist/body/body.core.d.ts +51 -0
- package/dist/body/body.core.d.ts.map +1 -0
- package/dist/body/body.core.js +120 -0
- package/dist/body/body.core.js.map +1 -0
- package/dist/body/index.d.ts +5 -0
- package/dist/body/index.d.ts.map +1 -0
- package/dist/body/index.js +5 -0
- package/dist/body/index.js.map +1 -0
- package/dist/cookie/cookie.core.d.ts +58 -0
- package/dist/cookie/cookie.core.d.ts.map +1 -0
- package/dist/cookie/cookie.core.js +163 -0
- package/dist/cookie/cookie.core.js.map +1 -0
- package/dist/cookie/index.d.ts +5 -0
- package/dist/cookie/index.d.ts.map +1 -0
- package/dist/cookie/index.js +5 -0
- package/dist/cookie/index.js.map +1 -0
- package/dist/cors/cors.core.d.ts +58 -0
- package/dist/cors/cors.core.d.ts.map +1 -0
- package/dist/cors/cors.core.js +131 -0
- package/dist/cors/cors.core.js.map +1 -0
- package/dist/cors/cors.namespace.d.ts +15 -0
- package/dist/cors/cors.namespace.d.ts.map +1 -0
- package/dist/cors/cors.namespace.js +14 -0
- package/dist/cors/cors.namespace.js.map +1 -0
- package/dist/cors/index.d.ts +6 -0
- package/dist/cors/index.d.ts.map +1 -0
- package/dist/cors/index.js +5 -0
- package/dist/cors/index.js.map +1 -0
- package/dist/csrf/csrf.core.d.ts +56 -0
- package/dist/csrf/csrf.core.d.ts.map +1 -0
- package/dist/csrf/csrf.core.js +149 -0
- package/dist/csrf/csrf.core.js.map +1 -0
- package/dist/csrf/index.d.ts +5 -0
- package/dist/csrf/index.d.ts.map +1 -0
- package/dist/csrf/index.js +5 -0
- package/dist/csrf/index.js.map +1 -0
- package/dist/header/header.core.d.ts +47 -0
- package/dist/header/header.core.d.ts.map +1 -0
- package/dist/header/header.core.js +158 -0
- package/dist/header/header.core.js.map +1 -0
- package/dist/header/index.d.ts +5 -0
- package/dist/header/index.d.ts.map +1 -0
- package/dist/header/index.js +5 -0
- package/dist/header/index.js.map +1 -0
- package/dist/headers/headers.core.d.ts +48 -0
- package/dist/headers/headers.core.d.ts.map +1 -0
- package/dist/headers/headers.core.js +124 -0
- package/dist/headers/headers.core.js.map +1 -0
- package/dist/headers/index.d.ts +6 -0
- package/dist/headers/index.d.ts.map +1 -0
- package/dist/headers/index.js +6 -0
- package/dist/headers/index.js.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.d.ts.map +1 -0
- package/dist/input/index.js +5 -0
- package/dist/input/index.js.map +1 -0
- package/dist/input/input.core.d.ts +73 -0
- package/dist/input/input.core.d.ts.map +1 -0
- package/dist/input/input.core.js +182 -0
- package/dist/input/input.core.js.map +1 -0
- package/dist/rateLimit/index.d.ts +5 -0
- package/dist/rateLimit/index.d.ts.map +1 -0
- package/dist/rateLimit/index.js +5 -0
- package/dist/rateLimit/index.js.map +1 -0
- package/dist/rateLimit/rateLimit.core.d.ts +51 -0
- package/dist/rateLimit/rateLimit.core.d.ts.map +1 -0
- package/dist/rateLimit/rateLimit.core.js +175 -0
- package/dist/rateLimit/rateLimit.core.js.map +1 -0
- package/dist/rateLimit/rateLimit.namespace.d.ts +13 -0
- package/dist/rateLimit/rateLimit.namespace.d.ts.map +1 -0
- package/dist/rateLimit/rateLimit.namespace.js +13 -0
- package/dist/rateLimit/rateLimit.namespace.js.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +7 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/security.type.d.ts +203 -0
- package/dist/types/security.type.d.ts.map +1 -0
- package/dist/types/security.type.js +31 -0
- package/dist/types/security.type.js.map +1 -0
- package/dist/url/index.d.ts +5 -0
- package/dist/url/index.d.ts.map +1 -0
- package/dist/url/index.js +5 -0
- package/dist/url/index.js.map +1 -0
- package/dist/url/url.core.d.ts +38 -0
- package/dist/url/url.core.d.ts.map +1 -0
- package/dist/url/url.core.js +187 -0
- package/dist/url/url.core.js.map +1 -0
- package/package.json +53 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/header/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/header/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security — Security Headers
|
|
3
|
+
*
|
|
4
|
+
* Generates security-related HTTP response headers with secure defaults.
|
|
5
|
+
*/
|
|
6
|
+
import type { SecurityHeadersConfig } from "../types/security.type.js";
|
|
7
|
+
/** Security header names. */
|
|
8
|
+
export declare const SECURITY_HEADER_NAMES: {
|
|
9
|
+
readonly CONTENT_SECURITY_POLICY: "Content-Security-Policy";
|
|
10
|
+
readonly CONTENT_TYPE_OPTIONS: "X-Content-Type-Options";
|
|
11
|
+
readonly FRAME_OPTIONS: "X-Frame-Options";
|
|
12
|
+
readonly XSS_PROTECTION: "X-XSS-Protection";
|
|
13
|
+
readonly HSTS: "Strict-Transport-Security";
|
|
14
|
+
readonly REFERRER_POLICY: "Referrer-Policy";
|
|
15
|
+
readonly PERMISSIONS_POLICY: "Permissions-Policy";
|
|
16
|
+
readonly DNS_PREFETCH_CONTROL: "X-DNS-Prefetch-Control";
|
|
17
|
+
readonly OPENER_POLICY: "Cross-Origin-Opener-Policy";
|
|
18
|
+
readonly RESOURCE_POLICY: "Cross-Origin-Resource-Policy";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Generates security headers with secure defaults.
|
|
22
|
+
*
|
|
23
|
+
* @param config - Optional security headers configuration.
|
|
24
|
+
* @returns Record of security headers to set on the response.
|
|
25
|
+
*/
|
|
26
|
+
export declare function generateSecurityHeaders(config?: SecurityHeadersConfig): Record<string, string>;
|
|
27
|
+
/**
|
|
28
|
+
* Checks if a response already has security headers set.
|
|
29
|
+
*
|
|
30
|
+
* @param headers - Response headers.
|
|
31
|
+
* @returns An array of missing security header names.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getMissingSecurityHeaders(headers: Record<string, string | string[] | undefined>): string[];
|
|
34
|
+
/**
|
|
35
|
+
* Creates a CSP nonce for inline scripts.
|
|
36
|
+
*
|
|
37
|
+
* @param nonceLength - Length of the nonce (default: 16).
|
|
38
|
+
* @returns The base64-encoded nonce.
|
|
39
|
+
*/
|
|
40
|
+
export declare function generateCspNonce(nonceLength?: number): string;
|
|
41
|
+
/**
|
|
42
|
+
* Validates a Content-Security-Policy directive.
|
|
43
|
+
*
|
|
44
|
+
* @param directive - The CSP directive string.
|
|
45
|
+
* @returns An error message if invalid, or undefined.
|
|
46
|
+
*/
|
|
47
|
+
export declare function validateCspDirective(directive: string): string | undefined;
|
|
48
|
+
//# sourceMappingURL=headers.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"headers.core.d.ts","sourceRoot":"","sources":["../../src/headers/headers.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAEvE,6BAA6B;AAC7B,eAAO,MAAM,qBAAqB;aAChC,uBAAuB,EAAE,yBAAyB;aAClD,oBAAoB,EAAE,wBAAwB;aAC9C,aAAa,EAAE,iBAAiB;aAChC,cAAc,EAAE,kBAAkB;aAClC,IAAI,EAAE,2BAA2B;aACjC,eAAe,EAAE,iBAAiB;aAClC,kBAAkB,EAAE,oBAAoB;aACxC,oBAAoB,EAAE,wBAAwB;aAC9C,aAAa,EAAE,4BAA4B;aAC3C,eAAe,EAAE,8BAA8B;CACvC,CAAC;AAaX;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,CAAC,EAAE,qBAAqB,GAC7B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAgDxB;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,GACrD,MAAM,EAAE,CAaV;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAK7D;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAsB1E"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security — Security Headers
|
|
3
|
+
*
|
|
4
|
+
* Generates security-related HTTP response headers with secure defaults.
|
|
5
|
+
*/
|
|
6
|
+
/** Security header names. */
|
|
7
|
+
export const SECURITY_HEADER_NAMES = {
|
|
8
|
+
CONTENT_SECURITY_POLICY: "Content-Security-Policy",
|
|
9
|
+
CONTENT_TYPE_OPTIONS: "X-Content-Type-Options",
|
|
10
|
+
FRAME_OPTIONS: "X-Frame-Options",
|
|
11
|
+
XSS_PROTECTION: "X-XSS-Protection",
|
|
12
|
+
HSTS: "Strict-Transport-Security",
|
|
13
|
+
REFERRER_POLICY: "Referrer-Policy",
|
|
14
|
+
PERMISSIONS_POLICY: "Permissions-Policy",
|
|
15
|
+
DNS_PREFETCH_CONTROL: "X-DNS-Prefetch-Control",
|
|
16
|
+
OPENER_POLICY: "Cross-Origin-Opener-Policy",
|
|
17
|
+
RESOURCE_POLICY: "Cross-Origin-Resource-Policy",
|
|
18
|
+
};
|
|
19
|
+
/** Default security headers. */
|
|
20
|
+
const DEFAULT_HEADERS = {
|
|
21
|
+
[SECURITY_HEADER_NAMES.CONTENT_TYPE_OPTIONS]: "nosniff",
|
|
22
|
+
[SECURITY_HEADER_NAMES.FRAME_OPTIONS]: "DENY",
|
|
23
|
+
[SECURITY_HEADER_NAMES.XSS_PROTECTION]: "1; mode=block",
|
|
24
|
+
[SECURITY_HEADER_NAMES.REFERRER_POLICY]: "strict-origin-when-cross-origin",
|
|
25
|
+
[SECURITY_HEADER_NAMES.DNS_PREFETCH_CONTROL]: "off",
|
|
26
|
+
[SECURITY_HEADER_NAMES.OPENER_POLICY]: "same-origin",
|
|
27
|
+
[SECURITY_HEADER_NAMES.RESOURCE_POLICY]: "same-origin",
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Generates security headers with secure defaults.
|
|
31
|
+
*
|
|
32
|
+
* @param config - Optional security headers configuration.
|
|
33
|
+
* @returns Record of security headers to set on the response.
|
|
34
|
+
*/
|
|
35
|
+
export function generateSecurityHeaders(config) {
|
|
36
|
+
const headers = { ...DEFAULT_HEADERS };
|
|
37
|
+
if (config?.contentSecurityPolicy) {
|
|
38
|
+
headers[SECURITY_HEADER_NAMES.CONTENT_SECURITY_POLICY] =
|
|
39
|
+
config.contentSecurityPolicy;
|
|
40
|
+
}
|
|
41
|
+
if (config?.contentTypeOptions) {
|
|
42
|
+
headers[SECURITY_HEADER_NAMES.CONTENT_TYPE_OPTIONS] =
|
|
43
|
+
config.contentTypeOptions;
|
|
44
|
+
}
|
|
45
|
+
if (config?.frameOptions) {
|
|
46
|
+
headers[SECURITY_HEADER_NAMES.FRAME_OPTIONS] = config.frameOptions;
|
|
47
|
+
}
|
|
48
|
+
if (config?.xssProtection) {
|
|
49
|
+
headers[SECURITY_HEADER_NAMES.XSS_PROTECTION] = config.xssProtection;
|
|
50
|
+
}
|
|
51
|
+
if (config?.hsts) {
|
|
52
|
+
headers[SECURITY_HEADER_NAMES.HSTS] = config.hsts;
|
|
53
|
+
}
|
|
54
|
+
if (config?.referrerPolicy) {
|
|
55
|
+
headers[SECURITY_HEADER_NAMES.REFERRER_POLICY] = config.referrerPolicy;
|
|
56
|
+
}
|
|
57
|
+
if (config?.permissionsPolicy) {
|
|
58
|
+
headers[SECURITY_HEADER_NAMES.PERMISSIONS_POLICY] =
|
|
59
|
+
config.permissionsPolicy;
|
|
60
|
+
}
|
|
61
|
+
if (config?.dnsPrefetchControl) {
|
|
62
|
+
headers[SECURITY_HEADER_NAMES.DNS_PREFETCH_CONTROL] =
|
|
63
|
+
config.dnsPrefetchControl;
|
|
64
|
+
}
|
|
65
|
+
if (config?.openerPolicy) {
|
|
66
|
+
headers[SECURITY_HEADER_NAMES.OPENER_POLICY] = config.openerPolicy;
|
|
67
|
+
}
|
|
68
|
+
if (config?.resourcePolicy) {
|
|
69
|
+
headers[SECURITY_HEADER_NAMES.RESOURCE_POLICY] = config.resourcePolicy;
|
|
70
|
+
}
|
|
71
|
+
return headers;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Checks if a response already has security headers set.
|
|
75
|
+
*
|
|
76
|
+
* @param headers - Response headers.
|
|
77
|
+
* @returns An array of missing security header names.
|
|
78
|
+
*/
|
|
79
|
+
export function getMissingSecurityHeaders(headers) {
|
|
80
|
+
const missing = [];
|
|
81
|
+
const lowerHeaders = new Map(Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v]));
|
|
82
|
+
for (const name of Object.values(SECURITY_HEADER_NAMES)) {
|
|
83
|
+
if (!lowerHeaders.has(name.toLowerCase())) {
|
|
84
|
+
missing.push(name);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return missing;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Creates a CSP nonce for inline scripts.
|
|
91
|
+
*
|
|
92
|
+
* @param nonceLength - Length of the nonce (default: 16).
|
|
93
|
+
* @returns The base64-encoded nonce.
|
|
94
|
+
*/
|
|
95
|
+
export function generateCspNonce(nonceLength) {
|
|
96
|
+
const { randomBytes } = require("node:crypto");
|
|
97
|
+
const length = nonceLength ?? 16;
|
|
98
|
+
return randomBytes(length).toString("base64");
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Validates a Content-Security-Policy directive.
|
|
102
|
+
*
|
|
103
|
+
* @param directive - The CSP directive string.
|
|
104
|
+
* @returns An error message if invalid, or undefined.
|
|
105
|
+
*/
|
|
106
|
+
export function validateCspDirective(directive) {
|
|
107
|
+
if (!directive || directive.trim().length === 0) {
|
|
108
|
+
return "CSP directive cannot be empty";
|
|
109
|
+
}
|
|
110
|
+
// Check for unsafe-inline, unsafe-eval (warnings)
|
|
111
|
+
const warnings = [];
|
|
112
|
+
if (directive.includes("'unsafe-inline'")) {
|
|
113
|
+
warnings.push("unsafe-inline weakens CSP");
|
|
114
|
+
}
|
|
115
|
+
if (directive.includes("'unsafe-eval'")) {
|
|
116
|
+
warnings.push("unsafe-eval weakens CSP");
|
|
117
|
+
}
|
|
118
|
+
if (directive.includes("'unsafe-hashes'")) {
|
|
119
|
+
warnings.push("unsafe-hashes weakens CSP");
|
|
120
|
+
}
|
|
121
|
+
// Return first warning as info (not error)
|
|
122
|
+
return warnings.length > 0 ? warnings[0] : undefined;
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=headers.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"headers.core.js","sourceRoot":"","sources":["../../src/headers/headers.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,6BAA6B;AAC7B,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,uBAAuB,EAAE,yBAAyB;IAClD,oBAAoB,EAAE,wBAAwB;IAC9C,aAAa,EAAE,iBAAiB;IAChC,cAAc,EAAE,kBAAkB;IAClC,IAAI,EAAE,2BAA2B;IACjC,eAAe,EAAE,iBAAiB;IAClC,k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|
|
4
|
+
export { SECURITY_HEADER_NAMES } from "./headers.core.js";
|
|
5
|
+
export { generateSecurityHeaders, getMissingSecurityHeaders, generateCspNonce, validateCspDirective, } from "./headers.core.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/headers/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EACzB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security — Security Headers Barrel
|
|
3
|
+
*/
|
|
4
|
+
export { SECURITY_HEADER_NAMES } from "./headers.core.js";
|
|
5
|
+
export { generateSecurityHeaders, getMissingSecurityHeaders, generateCspNonce, validateCspDirective, } from "./headers.core.js";
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/headers/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EACzB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,mBAAmB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security
|
|
3
|
+
*
|
|
4
|
+
* Security primitives for the Zudojs framework.
|
|
5
|
+
*
|
|
6
|
+
* Provides input validation, header security, URL normalization,
|
|
7
|
+
* body limits, cookie security, CORS, CSRF, rate limiting, and
|
|
8
|
+
* security headers with secure defaults.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { validateHeaders, validateUrl, generateSecurityHeaders } from '@zudojs/security';
|
|
13
|
+
*
|
|
14
|
+
* // Validate request headers
|
|
15
|
+
* const result = validateHeaders(request.headers);
|
|
16
|
+
* if (!result.valid) {
|
|
17
|
+
* throw new Error(result.errors.join(', '));
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Validate URL
|
|
21
|
+
* const urlResult = validateUrl(request.url);
|
|
22
|
+
*
|
|
23
|
+
* // Add security headers
|
|
24
|
+
* const headers = generateSecurityHeaders();
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @packageDocumentation
|
|
28
|
+
*/
|
|
29
|
+
export type { HeaderSecurityConfig, HeaderValidationResult, BodyLimitConfig, BodyLimitPresets, UrlValidationConfig, UrlValidationResult, CookieSecurityConfig, ParsedCookie, CorsConfig, CsrfConfig, RateLimitConfig, RateLimitRequest, RateLimitResponse, RateLimitResult, SecurityHeadersConfig, RequestValidationConfig, InputSanitizationConfig, } from "./types/security.type.js";
|
|
30
|
+
export { PROTOTYPE_POLLUTION_KEYS, SQL_INJECTION_PATTERNS, XSS_PATTERNS, } from "./types/security.type.js";
|
|
31
|
+
export { validateHeaderName, validateHeaderValue, validateHeaders, sanitizeHeaderValue, isHopByHopHeader, } from "./header/index.js";
|
|
32
|
+
export { validateUrl, normalizePath, validateRequestTarget, isSafeUrl, } from "./url/index.js";
|
|
33
|
+
export { DEFAULT_BODY_LIMITS, validateContentLength, validateBodySize, getBodyLimitForContentType, validateBodyLimitConfig, createBodySizeChecker, } from "./body/index.js";
|
|
34
|
+
export { parseCookieHeader, serializeCookie, createSecureCookie, validateCookieName, validateCookieValue, stripSensitiveCookies, } from "./cookie/index.js";
|
|
35
|
+
export type { CorsHeaders } from "./cors/index.js";
|
|
36
|
+
export { isOriginAllowed, generatePreflightHeaders, generateSimpleHeaders, isMethodAllowed, getDisallowedHeaders, } from "./cors/index.js";
|
|
37
|
+
export { cors } from "./cors/cors.namespace.js";
|
|
38
|
+
export { generateCsrfToken, validateCsrfToken, requiresCsrfProtection, extractCsrfTokenFromHeaders, extractCsrfTokenFromCookies, generateCsrfCookie, } from "./csrf/index.js";
|
|
39
|
+
export { defaultKeyGenerator, defaultHandler, createRateLimiter, extractClientIp, } from "./rateLimit/index.js";
|
|
40
|
+
export { rateLimit } from "./rateLimit/rateLimit.namespace.js";
|
|
41
|
+
export { SECURITY_HEADER_NAMES } from "./headers/index.js";
|
|
42
|
+
export { generateSecurityHeaders, getMissingSecurityHeaders, generateCspNonce, validateCspDirective, } from "./headers/index.js";
|
|
43
|
+
export { containsSqlInjection, containsXss, containsPrototypePollution, sanitizeString, sanitizeObject, isSafeString, detectThreats, escapeHtml, stripHtml, } from "./input/index.js";
|
|
44
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,YAAY,EACV,oBAAoB,EACpB,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,YAAY,EACZ,UAAU,EACV,UAAU,EACV,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,qBAAqB,EACrB,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACtB,YAAY,GACb,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,WAAW,EACX,aAAa,EACb,qBAAqB,EACrB,SAAS,GACV,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,gBAAgB,EAChB,0BAA0B,EAC1B,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,mBAAmB,CAAC;AAG3B,YAAY,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,qBAAqB,EACrB,eAAe,EACf,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAGhD,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,sBAAsB,EACtB,2BAA2B,EAC3B,2BAA2B,EAC3B,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,iBAAiB,EACjB,eAAe,GAChB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,SAAS,EAAE,MAAM,oCAAoC,CAAC;AAG/D,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EACzB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,0BAA0B,EAC1B,cAAc,EACd,cAAc,EACd,YAAY,EACZ,aAAa,EACb,UAAU,EACV,SAAS,GACV,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security
|
|
3
|
+
*
|
|
4
|
+
* Security primitives for the Zudojs framework.
|
|
5
|
+
*
|
|
6
|
+
* Provides input validation, header security, URL normalization,
|
|
7
|
+
* body limits, cookie security, CORS, CSRF, rate limiting, and
|
|
8
|
+
* security headers with secure defaults.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { validateHeaders, validateUrl, generateSecurityHeaders } from '@zudojs/security';
|
|
13
|
+
*
|
|
14
|
+
* // Validate request headers
|
|
15
|
+
* const result = validateHeaders(request.headers);
|
|
16
|
+
* if (!result.valid) {
|
|
17
|
+
* throw new Error(result.errors.join(', '));
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Validate URL
|
|
21
|
+
* const urlResult = validateUrl(request.url);
|
|
22
|
+
*
|
|
23
|
+
* // Add security headers
|
|
24
|
+
* const headers = generateSecurityHeaders();
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @packageDocumentation
|
|
28
|
+
*/
|
|
29
|
+
export { PROTOTYPE_POLLUTION_KEYS, SQL_INJECTION_PATTERNS, XSS_PATTERNS, } from "./types/security.type.js";
|
|
30
|
+
/* ─── Header Security ────────────────────────────────────────────────────── */
|
|
31
|
+
export { validateHeaderName, validateHeaderValue, validateHeaders, sanitizeHeaderValue, isHopByHopHeader, } from "./header/index.js";
|
|
32
|
+
/* ─── URL Validation ─────────────────────────────────────────────────────── */
|
|
33
|
+
export { validateUrl, normalizePath, validateRequestTarget, isSafeUrl, } from "./url/index.js";
|
|
34
|
+
/* ─── Body Validation ────────────────────────────────────────────────────── */
|
|
35
|
+
export { DEFAULT_BODY_LIMITS, validateContentLength, validateBodySize, getBodyLimitForContentType, validateBodyLimitConfig, createBodySizeChecker, } from "./body/index.js";
|
|
36
|
+
/* ─── Cookie Security ────────────────────────────────────────────────────── */
|
|
37
|
+
export { parseCookieHeader, serializeCookie, createSecureCookie, validateCookieName, validateCookieValue, stripSensitiveCookies, } from "./cookie/index.js";
|
|
38
|
+
export { isOriginAllowed, generatePreflightHeaders, generateSimpleHeaders, isMethodAllowed, getDisallowedHeaders, } from "./cors/index.js";
|
|
39
|
+
export { cors } from "./cors/cors.namespace.js";
|
|
40
|
+
/* ─── CSRF ───────────────────────────────────────────────────────────────── */
|
|
41
|
+
export { generateCsrfToken, validateCsrfToken, requiresCsrfProtection, extractCsrfTokenFromHeaders, extractCsrfTokenFromCookies, generateCsrfCookie, } from "./csrf/index.js";
|
|
42
|
+
/* ─── Rate Limiting ──────────────────────────────────────────────────────── */
|
|
43
|
+
export { defaultKeyGenerator, defaultHandler, createRateLimiter, extractClientIp, } from "./rateLimit/index.js";
|
|
44
|
+
export { rateLimit } from "./rateLimit/rateLimit.namespace.js";
|
|
45
|
+
/* ─── Security Headers ───────────────────────────────────────────────────── */
|
|
46
|
+
export { SECURITY_HEADER_NAMES } from "./headers/index.js";
|
|
47
|
+
export { generateSecurityHeaders, getMissingSecurityHeaders, generateCspNonce, validateCspDirective, } from "./headers/index.js";
|
|
48
|
+
/* ─── Input Sanitization ─────────────────────────────────────────────────── */
|
|
49
|
+
export { containsSqlInjection, containsXss, containsPrototypePollution, sanitizeString, sanitizeObject, isSafeString, detectThreats, escapeHtml, stripHtml, } from "./input/index.js";
|
|
50
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAuBH,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACtB,YAAY,GACb,MAAM,0BAA0B,CAAC;AAElC,gFAAgF;AAChF,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAE3B,gFAAgF;AAChF,OAAO,EACL,WAAW,EACX,aAAa,EACb,qBAAqB,EACrB,SAAS,GACV,MAAM,gBAAgB,CAAC;AAExB,gFAAgF;AAChF,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,gBAAgB,EAChB,0BAA0B,EAC1B,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC;AAEzB,gFAAgF;AAChF,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,qBAAqB,EACrB,eAAe,EACf,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAEhD,gFAAgF;AAChF,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,sBAAsB,EACtB,2BAA2B,EAC3B,2BAA2B,EAC3B,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AAEzB,gFAAgF;AAChF,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,iBAAiB,EACjB,eAAe,GAChB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,SAAS,EAAE,MAAM,oCAAoC,CAAC;AAE/D,gFAAgF;AAChF,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EACzB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,oBAAoB,CAAC;AAE5B,gFAAgF;AAChF,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,0BAA0B,EAC1B,cAAc,EACd,cAAc,EACd,YAAY,EACZ,aAAa,EACb,UAAU,EACV,SAAS,GACV,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security — Input Sanitization Barrel
|
|
3
|
+
*/
|
|
4
|
+
export { containsSqlInjection, containsXss, containsPrototypePollution, sanitizeString, sanitizeObject, isSafeString, detectThreats, escapeHtml, stripHtml, } from "./input.core.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/input/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,0BAA0B,EAC1B,cAAc,EACd,cAAc,EACd,YAAY,EACZ,aAAa,EACb,UAAU,EACV,SAAS,GACV,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security — Input Sanitization Barrel
|
|
3
|
+
*/
|
|
4
|
+
export { containsSqlInjection, containsXss, containsPrototypePollution, sanitizeString, sanitizeObject, isSafeString, detectThreats, escapeHtml, stripHtml, } from "./input.core.js";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/input/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,0BAA0B,EAC1B,cAAc,EACd,cAAc,EACd,YAAY,EACZ,aAAa,EACb,UAAU,EACV,SAAS,GACV,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security — Input Sanitization
|
|
3
|
+
*
|
|
4
|
+
* Sanitizes user input against common attack patterns.
|
|
5
|
+
*/
|
|
6
|
+
import type { InputSanitizationConfig } from "../types/security.type.js";
|
|
7
|
+
/**
|
|
8
|
+
* Checks if a string contains SQL injection patterns.
|
|
9
|
+
*
|
|
10
|
+
* @param input - The string to check.
|
|
11
|
+
* @returns True if SQL injection patterns are detected.
|
|
12
|
+
*/
|
|
13
|
+
export declare function containsSqlInjection(input: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Checks if a string contains XSS patterns.
|
|
16
|
+
*
|
|
17
|
+
* @param input - The string to check.
|
|
18
|
+
* @returns True if XSS patterns are detected.
|
|
19
|
+
*/
|
|
20
|
+
export declare function containsXss(input: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Checks if a string contains prototype pollution keys.
|
|
23
|
+
*
|
|
24
|
+
* @param input - The string to check.
|
|
25
|
+
* @returns True if prototype pollution keys are detected.
|
|
26
|
+
*/
|
|
27
|
+
export declare function containsPrototypePollution(input: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Sanitizes a string by removing dangerous characters.
|
|
30
|
+
*
|
|
31
|
+
* @param input - The string to sanitize.
|
|
32
|
+
* @param config - Optional sanitization configuration.
|
|
33
|
+
* @returns The sanitized string.
|
|
34
|
+
*/
|
|
35
|
+
export declare function sanitizeString(input: string, config?: InputSanitizationConfig): string;
|
|
36
|
+
/**
|
|
37
|
+
* Sanitizes an object by recursively cleaning its values.
|
|
38
|
+
*
|
|
39
|
+
* @param obj - The object to sanitize.
|
|
40
|
+
* @param config - Optional sanitization configuration.
|
|
41
|
+
* @returns The sanitized object.
|
|
42
|
+
*/
|
|
43
|
+
export declare function sanitizeObject<T extends Record<string, unknown>>(obj: T, config?: InputSanitizationConfig): T;
|
|
44
|
+
/**
|
|
45
|
+
* Validates that a string contains only safe characters.
|
|
46
|
+
*
|
|
47
|
+
* @param input - The string to validate.
|
|
48
|
+
* @param allowedPattern - Optional regex pattern for allowed characters.
|
|
49
|
+
* @returns True if the string is safe.
|
|
50
|
+
*/
|
|
51
|
+
export declare function isSafeString(input: string, allowedPattern?: RegExp): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Checks for common attack patterns in a string.
|
|
54
|
+
*
|
|
55
|
+
* @param input - The string to check.
|
|
56
|
+
* @returns An array of detected threats.
|
|
57
|
+
*/
|
|
58
|
+
export declare function detectThreats(input: string): string[];
|
|
59
|
+
/**
|
|
60
|
+
* HTML-escapes a string to prevent XSS.
|
|
61
|
+
*
|
|
62
|
+
* @param input - The string to escape.
|
|
63
|
+
* @returns The escaped string.
|
|
64
|
+
*/
|
|
65
|
+
export declare function escapeHtml(input: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Strips HTML tags from a string.
|
|
68
|
+
*
|
|
69
|
+
* @param input - The string to strip.
|
|
70
|
+
* @returns The string with HTML tags removed.
|
|
71
|
+
*/
|
|
72
|
+
export declare function stripHtml(input: string): string;
|
|
73
|
+
//# sourceMappingURL=input.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input.core.d.ts","sourceRoot":"","sources":["../../src/input/input.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAazE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE3D;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAElD;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAIjE;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,uBAAuB,GAC/B,MAAM,CAsBR;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9D,GAAG,EAAE,CAAC,EACN,MAAM,CAAC,EAAE,uBAAuB,GAC/B,CAAC,CAmCH;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,OAAO,CAiB5E;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAoBrD;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAUhD;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAe/C"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zudojs/security — Input Sanitization
|
|
3
|
+
*
|
|
4
|
+
* Sanitizes user input against common attack patterns.
|
|
5
|
+
*/
|
|
6
|
+
import { PROTOTYPE_POLLUTION_KEYS, SQL_INJECTION_PATTERNS, XSS_PATTERNS, } from "../types/security.type.js";
|
|
7
|
+
/** Null byte pattern. */
|
|
8
|
+
const NULL_BYTE_PATTERN = /\x00/g;
|
|
9
|
+
/** Control character pattern (except tab, newline, carriage return). */
|
|
10
|
+
const CONTROL_CHAR_PATTERN = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g;
|
|
11
|
+
/**
|
|
12
|
+
* Checks if a string contains SQL injection patterns.
|
|
13
|
+
*
|
|
14
|
+
* @param input - The string to check.
|
|
15
|
+
* @returns True if SQL injection patterns are detected.
|
|
16
|
+
*/
|
|
17
|
+
export function containsSqlInjection(input) {
|
|
18
|
+
return SQL_INJECTION_PATTERNS.some((pattern) => pattern.test(input));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Checks if a string contains XSS patterns.
|
|
22
|
+
*
|
|
23
|
+
* @param input - The string to check.
|
|
24
|
+
* @returns True if XSS patterns are detected.
|
|
25
|
+
*/
|
|
26
|
+
export function containsXss(input) {
|
|
27
|
+
return XSS_PATTERNS.some((pattern) => pattern.test(input));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a string contains prototype pollution keys.
|
|
31
|
+
*
|
|
32
|
+
* @param input - The string to check.
|
|
33
|
+
* @returns True if prototype pollution keys are detected.
|
|
34
|
+
*/
|
|
35
|
+
export function containsPrototypePollution(input) {
|
|
36
|
+
return PROTOTYPE_POLLUTION_KEYS.includes(input);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sanitizes a string by removing dangerous characters.
|
|
40
|
+
*
|
|
41
|
+
* @param input - The string to sanitize.
|
|
42
|
+
* @param config - Optional sanitization configuration.
|
|
43
|
+
* @returns The sanitized string.
|
|
44
|
+
*/
|
|
45
|
+
export function sanitizeString(input, config) {
|
|
46
|
+
let sanitized = input;
|
|
47
|
+
// Strip null bytes
|
|
48
|
+
if (config?.stripNullBytes !== false) {
|
|
49
|
+
sanitized = sanitized.replace(NULL_BYTE_PATTERN, "");
|
|
50
|
+
}
|
|
51
|
+
// Strip control characters
|
|
52
|
+
sanitized = sanitized.replace(CONTROL_CHAR_PATTERN, "");
|
|
53
|
+
// Truncate if max length configured
|
|
54
|
+
if (config?.maxStringLength && sanitized.length > config.maxStringLength) {
|
|
55
|
+
sanitized = sanitized.slice(0, config.maxStringLength);
|
|
56
|
+
}
|
|
57
|
+
// Custom sanitizer
|
|
58
|
+
if (config?.customSanitizer) {
|
|
59
|
+
sanitized = config.customSanitizer(sanitized);
|
|
60
|
+
}
|
|
61
|
+
return sanitized;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Sanitizes an object by recursively cleaning its values.
|
|
65
|
+
*
|
|
66
|
+
* @param obj - The object to sanitize.
|
|
67
|
+
* @param config - Optional sanitization configuration.
|
|
68
|
+
* @returns The sanitized object.
|
|
69
|
+
*/
|
|
70
|
+
export function sanitizeObject(obj, config) {
|
|
71
|
+
const sanitized = { ...obj };
|
|
72
|
+
for (const [key, value] of Object.entries(sanitized)) {
|
|
73
|
+
// Check for prototype pollution keys
|
|
74
|
+
if (config?.preventPrototypePollution !== false) {
|
|
75
|
+
if (containsPrototypePollution(key)) {
|
|
76
|
+
delete sanitized[key];
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Sanitize string values
|
|
81
|
+
if (typeof value === "string") {
|
|
82
|
+
sanitized[key] = sanitizeString(value, config);
|
|
83
|
+
}
|
|
84
|
+
else if (typeof value === "object" && value !== null) {
|
|
85
|
+
// Recursively sanitize nested objects
|
|
86
|
+
if (Array.isArray(value)) {
|
|
87
|
+
sanitized[key] = value.map((item) => typeof item === "object" && item !== null
|
|
88
|
+
? sanitizeObject(item, config)
|
|
89
|
+
: typeof item === "string"
|
|
90
|
+
? sanitizeString(item, config)
|
|
91
|
+
: item);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
sanitized[key] = sanitizeObject(value, config);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return sanitized;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Validates that a string contains only safe characters.
|
|
102
|
+
*
|
|
103
|
+
* @param input - The string to validate.
|
|
104
|
+
* @param allowedPattern - Optional regex pattern for allowed characters.
|
|
105
|
+
* @returns True if the string is safe.
|
|
106
|
+
*/
|
|
107
|
+
export function isSafeString(input, allowedPattern) {
|
|
108
|
+
// Check for null bytes
|
|
109
|
+
if (NULL_BYTE_PATTERN.test(input)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
// Check for control characters
|
|
113
|
+
if (CONTROL_CHAR_PATTERN.test(input)) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
// Check custom pattern
|
|
117
|
+
if (allowedPattern && !allowedPattern.test(input)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Checks for common attack patterns in a string.
|
|
124
|
+
*
|
|
125
|
+
* @param input - The string to check.
|
|
126
|
+
* @returns An array of detected threats.
|
|
127
|
+
*/
|
|
128
|
+
export function detectThreats(input) {
|
|
129
|
+
const threats = [];
|
|
130
|
+
if (containsSqlInjection(input)) {
|
|
131
|
+
threats.push("SQL_INJECTION");
|
|
132
|
+
}
|
|
133
|
+
if (containsXss(input)) {
|
|
134
|
+
threats.push("XSS");
|
|
135
|
+
}
|
|
136
|
+
if (NULL_BYTE_PATTERN.test(input)) {
|
|
137
|
+
threats.push("NULL_BYTE");
|
|
138
|
+
}
|
|
139
|
+
if (CONTROL_CHAR_PATTERN.test(input)) {
|
|
140
|
+
threats.push("CONTROL_CHARACTERS");
|
|
141
|
+
}
|
|
142
|
+
return threats;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* HTML-escapes a string to prevent XSS.
|
|
146
|
+
*
|
|
147
|
+
* @param input - The string to escape.
|
|
148
|
+
* @returns The escaped string.
|
|
149
|
+
*/
|
|
150
|
+
export function escapeHtml(input) {
|
|
151
|
+
const map = {
|
|
152
|
+
"&": "&",
|
|
153
|
+
"<": "<",
|
|
154
|
+
">": ">",
|
|
155
|
+
'"': """,
|
|
156
|
+
"'": "'",
|
|
157
|
+
};
|
|
158
|
+
return input.replace(/[&<>"']/g, (char) => map[char] ?? char);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Strips HTML tags from a string.
|
|
162
|
+
*
|
|
163
|
+
* @param input - The string to strip.
|
|
164
|
+
* @returns The string with HTML tags removed.
|
|
165
|
+
*/
|
|
166
|
+
export function stripHtml(input) {
|
|
167
|
+
let result = "";
|
|
168
|
+
let inTag = false;
|
|
169
|
+
for (const char of input) {
|
|
170
|
+
if (char === "<") {
|
|
171
|
+
inTag = true;
|
|
172
|
+
}
|
|
173
|
+
else if (char === ">") {
|
|
174
|
+
inTag = false;
|
|
175
|
+
}
|
|
176
|
+
else if (!inTag) {
|
|
177
|
+
result += char;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=input.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input.core.js","sourceRoot":"","sources":["../../src/input/input.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACtB,YAAY,GACb,MAAM,2BAA2B,CAAC;AAEnC,yBAAyB;AACzB,MAAM,iBAAiB,GAAG,OAAO,CAAC;AAElC,wEAAwE;AACxE,MAAM,oBAAoB,GAAG,mCAAmC,CAAC;AAEjE;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAa;IAChD,OAAO,sBAAsB,CAAC,IAAI,CAAC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AC,CAAC;AAChE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,KAAa;IACrC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,KAAK,GAAG,KAAK,CAAC;IAElB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;YACjB,KAAK,GAAG,IAAI,CAAC;QACf,CAAC;aAAM,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;YACxB,KAAK,GAAG,KAAK,CAAC;QAChB,CAAC;aAAM,IAAI,CAAC,KAAK,EAAE,CAAC;YAClB,MAAM,IAAI,IAAI,CAAC;QACjB,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rateLimit/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,iBAAiB,EACjB,eAAe,GAChB,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rateLimit/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,iBAAiB,EACjB,eAAe,GAChB,MAAM,qBAAqB,CAAC"}
|