@arcis/node 1.0.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.
Files changed (52) hide show
  1. package/README.md +222 -0
  2. package/dist/core/index.d.mts +170 -0
  3. package/dist/core/index.d.ts +170 -0
  4. package/dist/core/index.js +327 -0
  5. package/dist/core/index.js.map +1 -0
  6. package/dist/core/index.mjs +307 -0
  7. package/dist/core/index.mjs.map +1 -0
  8. package/dist/headers-BJq2OA0i.d.ts +284 -0
  9. package/dist/headers-DBQedhrb.d.mts +284 -0
  10. package/dist/index-BgHPM7LC.d.ts +129 -0
  11. package/dist/index-BpT7flAQ.d.ts +255 -0
  12. package/dist/index-JaFOUKyK.d.mts +255 -0
  13. package/dist/index-nAgXexwD.d.mts +129 -0
  14. package/dist/index.d.mts +139 -0
  15. package/dist/index.d.ts +139 -0
  16. package/dist/index.js +1860 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/index.mjs +1797 -0
  19. package/dist/index.mjs.map +1 -0
  20. package/dist/logging/index.d.mts +38 -0
  21. package/dist/logging/index.d.ts +38 -0
  22. package/dist/logging/index.js +140 -0
  23. package/dist/logging/index.js.map +1 -0
  24. package/dist/logging/index.mjs +136 -0
  25. package/dist/logging/index.mjs.map +1 -0
  26. package/dist/middleware/index.d.mts +3 -0
  27. package/dist/middleware/index.d.ts +3 -0
  28. package/dist/middleware/index.js +1173 -0
  29. package/dist/middleware/index.js.map +1 -0
  30. package/dist/middleware/index.mjs +1156 -0
  31. package/dist/middleware/index.mjs.map +1 -0
  32. package/dist/sanitizers/index.d.mts +24 -0
  33. package/dist/sanitizers/index.d.ts +24 -0
  34. package/dist/sanitizers/index.js +610 -0
  35. package/dist/sanitizers/index.js.map +1 -0
  36. package/dist/sanitizers/index.mjs +587 -0
  37. package/dist/sanitizers/index.mjs.map +1 -0
  38. package/dist/stores/index.d.mts +106 -0
  39. package/dist/stores/index.d.ts +106 -0
  40. package/dist/stores/index.js +149 -0
  41. package/dist/stores/index.js.map +1 -0
  42. package/dist/stores/index.mjs +145 -0
  43. package/dist/stores/index.mjs.map +1 -0
  44. package/dist/types-BOdL3ZWo.d.mts +264 -0
  45. package/dist/types-BOdL3ZWo.d.ts +264 -0
  46. package/dist/validation/index.d.mts +3 -0
  47. package/dist/validation/index.d.ts +3 -0
  48. package/dist/validation/index.js +705 -0
  49. package/dist/validation/index.js.map +1 -0
  50. package/dist/validation/index.mjs +699 -0
  51. package/dist/validation/index.mjs.map +1 -0
  52. package/package.json +109 -0
@@ -0,0 +1,145 @@
1
+ // src/core/constants.ts
2
+ var RATE_LIMIT = {
3
+ /** Default window size (1 minute) */
4
+ DEFAULT_WINDOW_MS: 6e4,
5
+ /** Minimum window size (1 second) */
6
+ MIN_WINDOW_MS: 1e3};
7
+
8
+ // src/stores/memory.ts
9
+ var MemoryStore = class {
10
+ constructor(windowMs = RATE_LIMIT.DEFAULT_WINDOW_MS) {
11
+ this.store = /* @__PURE__ */ new Map();
12
+ this.cleanupInterval = null;
13
+ if (!Number.isFinite(windowMs) || windowMs < RATE_LIMIT.MIN_WINDOW_MS) {
14
+ throw new RangeError(
15
+ `MemoryStore: windowMs must be a finite number >= ${RATE_LIMIT.MIN_WINDOW_MS} (got ${windowMs})`
16
+ );
17
+ }
18
+ this.windowMs = windowMs;
19
+ this.startCleanup();
20
+ }
21
+ /**
22
+ * Start the cleanup interval to remove expired entries.
23
+ */
24
+ startCleanup() {
25
+ const CLEANUP_MIN_MS = 3e4;
26
+ const CLEANUP_MAX_MS = 3e5;
27
+ const cleanupMs = Math.min(Math.max(this.windowMs, CLEANUP_MIN_MS), CLEANUP_MAX_MS);
28
+ this.cleanupInterval = setInterval(() => {
29
+ const now = Date.now();
30
+ for (const [key, entry] of this.store.entries()) {
31
+ if (entry.resetTime < now) {
32
+ this.store.delete(key);
33
+ }
34
+ }
35
+ }, cleanupMs);
36
+ if (typeof this.cleanupInterval.unref === "function") {
37
+ this.cleanupInterval.unref();
38
+ }
39
+ }
40
+ async get(key) {
41
+ const entry = this.store.get(key);
42
+ if (!entry) return null;
43
+ if (entry.resetTime < Date.now()) {
44
+ this.store.delete(key);
45
+ return null;
46
+ }
47
+ return entry;
48
+ }
49
+ async set(key, entry) {
50
+ this.store.set(key, entry);
51
+ }
52
+ async increment(key) {
53
+ const now = Date.now();
54
+ const entry = this.store.get(key);
55
+ if (!entry || entry.resetTime < now) {
56
+ this.store.set(key, { count: 1, resetTime: now + this.windowMs });
57
+ return 1;
58
+ }
59
+ entry.count++;
60
+ return entry.count;
61
+ }
62
+ async decrement(key) {
63
+ const entry = this.store.get(key);
64
+ if (entry && entry.count > 0) {
65
+ entry.count--;
66
+ }
67
+ }
68
+ async reset(key) {
69
+ this.store.delete(key);
70
+ }
71
+ async close() {
72
+ if (this.cleanupInterval) {
73
+ clearInterval(this.cleanupInterval);
74
+ this.cleanupInterval = null;
75
+ }
76
+ this.store.clear();
77
+ }
78
+ /**
79
+ * Get current store size (for monitoring).
80
+ */
81
+ get size() {
82
+ return this.store.size;
83
+ }
84
+ };
85
+
86
+ // src/stores/redis.ts
87
+ var RedisStore = class {
88
+ constructor(options) {
89
+ this.client = options.client;
90
+ this.prefix = options.prefix ?? "arcis:rl:";
91
+ this.windowMs = options.windowMs ?? RATE_LIMIT.DEFAULT_WINDOW_MS;
92
+ this.windowSec = Math.ceil(this.windowMs / 1e3);
93
+ }
94
+ getKey(key) {
95
+ return `${this.prefix}${key}`;
96
+ }
97
+ async get(key) {
98
+ const redisKey = this.getKey(key);
99
+ const [countStr, ttl] = await Promise.all([
100
+ this.client.get(redisKey),
101
+ this.client.ttl(redisKey)
102
+ ]);
103
+ if (!countStr || ttl < 0) {
104
+ return null;
105
+ }
106
+ const count = parseInt(countStr, 10);
107
+ if (isNaN(count)) {
108
+ return null;
109
+ }
110
+ return {
111
+ count,
112
+ resetTime: Date.now() + ttl * 1e3
113
+ };
114
+ }
115
+ async set(key, entry) {
116
+ const redisKey = this.getKey(key);
117
+ const ttlSec = Math.max(1, Math.ceil((entry.resetTime - Date.now()) / 1e3));
118
+ await this.client.setex(redisKey, ttlSec, entry.count.toString());
119
+ }
120
+ async increment(key) {
121
+ const redisKey = this.getKey(key);
122
+ const count = await this.client.incr(redisKey);
123
+ if (count === 1) {
124
+ await this.client.expire(redisKey, this.windowSec);
125
+ }
126
+ return count;
127
+ }
128
+ async decrement(key) {
129
+ const redisKey = this.getKey(key);
130
+ await this.client.decr(redisKey);
131
+ }
132
+ async reset(key) {
133
+ const redisKey = this.getKey(key);
134
+ await this.client.del(redisKey);
135
+ }
136
+ async close() {
137
+ }
138
+ };
139
+ function createRedisStore(options) {
140
+ return new RedisStore(options);
141
+ }
142
+
143
+ export { MemoryStore, RedisStore, createRedisStore };
144
+ //# sourceMappingURL=index.mjs.map
145
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/constants.ts","../../src/stores/memory.ts","../../src/stores/redis.ts"],"names":[],"mappings":";AAkBO,IAAM,UAAA,GAAa;AAAA;AAAA,EAExB,iBAAA,EAAmB,GAAA;AAAA,EAMF;AAAA,EAEjB,aAAA,EAAe,GAGjB,CAAA;;;ACdO,IAAM,cAAN,MAA4C;AAAA,EAKjD,WAAA,CAAY,QAAA,GAAmB,UAAA,CAAW,iBAAA,EAAmB;AAJ7D,IAAA,IAAA,CAAQ,KAAA,uBAAyC,GAAA,EAAI;AACrD,IAAA,IAAA,CAAQ,eAAA,GAAyD,IAAA;AAI/D,IAAA,IAAI,CAAC,MAAA,CAAO,QAAA,CAAS,QAAQ,CAAA,IAAK,QAAA,GAAW,WAAW,aAAA,EAAe;AACrE,MAAA,MAAM,IAAI,UAAA;AAAA,QACR,CAAA,iDAAA,EAAoD,UAAA,CAAW,aAAa,CAAA,MAAA,EAAS,QAAQ,CAAA,CAAA;AAAA,OAC/F;AAAA,IACF;AACA,IAAA,IAAA,CAAK,QAAA,GAAW,QAAA;AAChB,IAAA,IAAA,CAAK,YAAA,EAAa;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A;AAGhC,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,IAAA,CAAK,IAAA,CAAA,CAAM,KAAA,CAAM,SAAA,GAAY,IAAA,CAAK,GAAA,EAAI,IAAK,GAAI,CAAC,CAAA;AAC3E,IAAA,MAAM,IAAA,CAAK,OAAO,KAAA,CAAM,QAAA,EAAU,QAAQ,KAAA,CAAM,KAAA,CAAM,UAAU,CAAA;AAAA,EAClE;AAAA,EAEA,MAAM,UAAU,GAAA,EAA8B;AAC5C,IAAA,MAAM,QAAA,GAAW,IAAA,CAAK,MAAA,CAAO,GAAG,CAAA;AAGhC,IAAA,MAAM,KAAA,GAAQ,MAAM,IAAA,CAAK,MAAA,CAAO,KAAK,QAAQ,CAAA;AAK7C,IAAA,IAAI,UAAU,CAAA,EAAG;AACf,MAAA,MAAM,IAAA,CAAK,MAAA,CAAO,MAAA,CAAO,QAAA,EAAU,KAAK,SAAS,CAAA;AAAA,IACnD;AAEA,IAAA,OAAO,KAAA;AAAA,EACT;AAAA,EAEA,MAAM,UAAU,GAAA,EAA4B;AAC1C,IAAA,MAAM,QAAA,GAAW,IAAA,CAAK,MAAA,CAAO,GAAG,CAAA;AAChC,IAAA,MAAM,IAAA,CAAK,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA;AAAA,EACjC;AAAA,EAEA,MAAM,MAAM,GAAA,EAA4B;AACtC,IAAA,MAAM,QAAA,GAAW,IAAA,CAAK,MAAA,CAAO,GAAG,CAAA;AAChC,IAAA,MAAM,IAAA,CAAK,MAAA,CAAO,GAAA,CAAI,QAAQ,CAAA;AAAA,EAChC;AAAA,EAEA,MAAM,KAAA,GAAuB;AAAA,EAG7B;AACF;AASO,SAAS,iBAAiB,OAAA,EAAwC;AACvE,EAAA,OAAO,IAAI,WAAW,OAAO,CAAA;AAC/B","file":"index.mjs","sourcesContent":["/**\r\n * @module @arcis/node/core/constants\r\n * Named constants for Arcis - no magic numbers\r\n */\r\n\r\n// =============================================================================\r\n// INPUT LIMITS\r\n// =============================================================================\r\nexport const INPUT = {\r\n /** Default maximum input size (1MB) */\r\n DEFAULT_MAX_SIZE: 1_000_000,\r\n /** Maximum recursion depth for nested objects */\r\n MAX_RECURSION_DEPTH: 10,\r\n} as const;\r\n\r\n// =============================================================================\r\n// RATE LIMITING\r\n// =============================================================================\r\nexport const RATE_LIMIT = {\r\n /** Default window size (1 minute) */\r\n DEFAULT_WINDOW_MS: 60_000,\r\n /** Default max requests per window */\r\n DEFAULT_MAX_REQUESTS: 100,\r\n /** Default HTTP status code for rate limited responses */\r\n DEFAULT_STATUS_CODE: 429,\r\n /** Default error message */\r\n DEFAULT_MESSAGE: 'Too many requests, please try again later.',\r\n /** Minimum window size (1 second) */\r\n MIN_WINDOW_MS: 1_000,\r\n /** Maximum window size (24 hours) */\r\n MAX_WINDOW_MS: 86_400_000,\r\n} as const;\r\n\r\n// =============================================================================\r\n// SECURITY HEADERS\r\n// =============================================================================\r\nexport const HEADERS = {\r\n /** Default Content Security Policy */\r\n DEFAULT_CSP: [\r\n \"default-src 'self'\",\r\n \"script-src 'self'\",\r\n \"style-src 'self' 'unsafe-inline'\",\r\n \"img-src 'self' data: https:\",\r\n \"font-src 'self'\",\r\n \"object-src 'none'\",\r\n \"frame-ancestors 'none'\",\r\n ].join('; '),\r\n /** Default HSTS max age (1 year in seconds) */\r\n HSTS_MAX_AGE: 31_536_000,\r\n /** Default X-Frame-Options value */\r\n FRAME_OPTIONS: 'DENY' as const,\r\n /** Default X-Content-Type-Options value */\r\n CONTENT_TYPE_OPTIONS: 'nosniff',\r\n /** Default Referrer-Policy value */\r\n REFERRER_POLICY: 'strict-origin-when-cross-origin',\r\n /** Default Permissions-Policy value */\r\n PERMISSIONS_POLICY: 'geolocation=(), microphone=(), camera=()',\r\n /** Default Cache-Control value for security */\r\n CACHE_CONTROL: 'no-store, no-cache, must-revalidate, proxy-revalidate',\r\n} as const;\r\n\r\n// =============================================================================\r\n// XSS PATTERNS (ReDoS-safe)\r\n// =============================================================================\r\n\r\n/**\r\n * Detection patterns — used to flag whether a string contains XSS payloads.\r\n * Must stay in sync with XSS_REMOVE_PATTERNS below.\r\n */\r\nexport const XSS_PATTERNS = [\r\n /** Script tags (ReDoS-safe version) */\r\n /<script[^>]*>[\\s\\S]*?<\\/script>/gi,\r\n /** javascript: protocol (allow optional spaces before colon) */\r\n /javascript\\s*:/gi,\r\n /** vbscript: protocol */\r\n /vbscript\\s*:/gi,\r\n /** Event handlers (onclick, onerror, etc.) — any separator before attribute */\r\n /(?:[\\s/])on\\w+\\s*=/gi,\r\n /** iframe tags */\r\n /<iframe/gi,\r\n /** object tags */\r\n /<object/gi,\r\n /** embed tags */\r\n /<embed/gi,\r\n /** data: URIs (only dangerous ones, avoid false positives) */\r\n /(?:^|[\\s\"'=])data:/gi,\r\n /** URL-encoded script tags */\r\n /%3Cscript/gi,\r\n /** SVG with onload */\r\n /<svg[^>]*onload/gi,\r\n] as const;\r\n\r\n/**\r\n * Removal patterns — used by sanitizeXss() to strip dangerous content.\r\n * More targeted than XSS_PATTERNS: each pattern captures the full dangerous\r\n * substring (tag, attribute + value, protocol) so it can be replaced safely.\r\n * Must stay in sync with XSS_PATTERNS above.\r\n */\r\nexport const XSS_REMOVE_PATTERNS = [\r\n /** Full script blocks (content + tags) */\r\n /<script[^>]*>[\\s\\S]*?<\\/script>/gi,\r\n /** Standalone/unclosed script tags */\r\n /<script[^>]*>/gi,\r\n /** iframe — full block and partial/unclosed */\r\n /<iframe[^>]*>[\\s\\S]*?<\\/iframe>/gi,\r\n /<iframe[^>]*/gi,\r\n /** object — full block and partial/unclosed */\r\n /<object[^>]*>[\\s\\S]*?<\\/object>/gi,\r\n /<object[^>]*/gi,\r\n /** embed tags */\r\n /<embed[^>]*/gi,\r\n /** SVG with inline event handlers */\r\n /<svg[^>]*onload[^>]*>/gi,\r\n /** URL-encoded script tags */\r\n /%3Cscript/gi,\r\n /** Event handlers with quoted values: onclick=\"...\", onerror='...' */\r\n /(?:[\\s/])on\\w+\\s*=\\s*[\"'][^\"']*[\"']/gi,\r\n /** Event handlers with unquoted values: onload=value */\r\n /(?:[\\s/])on\\w+\\s*=\\s*[^\\s>]*/gi,\r\n /** javascript: and vbscript: protocols (allow optional spaces before colon) */\r\n /javascript\\s*:/gi,\r\n /vbscript\\s*:/gi,\r\n /** data: URIs with HTML/script content */\r\n /data\\s*:\\s*text\\/html[^>\\s]*/gi,\r\n] as const;\r\n\r\n// =============================================================================\r\n// SQL INJECTION PATTERNS\r\n// =============================================================================\r\nexport const SQL_PATTERNS = [\r\n /** SQL keywords */\r\n /(\\b(SELECT|INSERT|UPDATE|DELETE|DROP|UNION|ALTER|CREATE|TRUNCATE|EXEC|EXECUTE)\\b)/gi,\r\n /** SQL comments: ANSI (--), C-style (slash-star ... star-slash), MySQL (#) */\r\n /(--|\\/\\*|\\*\\/|#)/g,\r\n /** SQL statement separators */\r\n /(;|\\|\\||&&)/g,\r\n /** Boolean injection: OR 1=1 */\r\n /\\bOR\\s+\\d+\\s*=\\s*\\d+/gi,\r\n /** Boolean injection: OR 'a'='a' or OR \"a\"=\"a\" (including mixed quotes) */\r\n /\\bOR\\s+(['\"])[^'\"]*\\1\\s*=\\s*(['\"])[^'\"]*\\2/gi,\r\n /\\bOR\\s+('[^']*'|\"[^\"]*\")\\s*=\\s*('[^']*'|\"[^\"]*\")/gi,\r\n /** Boolean injection: AND 1=1 */\r\n /\\bAND\\s+\\d+\\s*=\\s*\\d+/gi,\r\n /** Boolean injection: AND 'a'='a' or AND \"a\"=\"a\" (including mixed quotes) */\r\n /\\bAND\\s+(['\"])[^'\"]*\\1\\s*=\\s*(['\"])[^'\"]*\\2/gi,\r\n /\\bAND\\s+('[^']*'|\"[^\"]*\")\\s*=\\s*('[^']*'|\"[^\"]*\")/gi,\r\n /** Time-based blind: SLEEP() */\r\n /\\bSLEEP\\s*\\(\\s*\\d+\\s*\\)/gi,\r\n /** Time-based blind: BENCHMARK() */\r\n /\\bBENCHMARK\\s*\\(/gi,\r\n] as const;\r\n\r\n// =============================================================================\r\n// PATH TRAVERSAL PATTERNS\r\n// =============================================================================\r\nexport const PATH_PATTERNS = [\r\n /** Unix path traversal */\r\n /\\.\\.\\//g,\r\n /** Windows path traversal */\r\n /\\.\\.\\\\/g,\r\n /** URL-encoded traversal (%2e%2e) */\r\n /%2e%2e/gi,\r\n /** Double URL-encoded traversal (%252e) */\r\n /%252e/gi,\r\n /** Mixed encoding: ..%2F */\r\n /\\.\\.%2F/gi,\r\n /** Mixed encoding: %2e./ and .%2e/ */\r\n /%2e\\.[\\\\/]/gi,\r\n /\\.%2e[\\\\/]/gi,\r\n /** Fully URL-encoded: %2e%2e%2f */\r\n /%2e%2e%2f/gi,\r\n /** Null byte injection in paths */\r\n /\\0/g,\r\n] as const;\r\n\r\n// =============================================================================\r\n// COMMAND INJECTION PATTERNS\r\n// =============================================================================\r\nexport const COMMAND_PATTERNS = [\r\n /**\r\n * Shell metacharacters that enable command chaining/substitution.\r\n * Bare ( and ) are excluded — they appear in common legitimate values\r\n * (function calls in code fields, math expressions, etc.).\r\n * Command substitution is caught by the $( combined pattern below.\r\n * NOTE: ';', '&', '|' may appear in legitimate URL query strings\r\n * and Markdown; consider disabling command checking (command: false)\r\n * for fields that intentionally allow those characters.\r\n */\r\n /[;&|`]/g,\r\n /** Command substitution: $( ... ) — matched as a pair to reduce false positives */\r\n /\\$\\(/g,\r\n] as const;\r\n\r\n// =============================================================================\r\n// DANGEROUS KEYS\r\n// =============================================================================\r\n\r\n/**\r\n * Prototype pollution keys to block.\r\n * Stored lowercase — always compare with key.toLowerCase().\r\n *\r\n * Includes:\r\n * - __proto__: direct prototype assignment\r\n * - constructor: access to constructor.prototype chain\r\n * - prototype: direct prototype property\r\n * - __defineGetter__/__defineSetter__: legacy property definition (can override getters/setters)\r\n * - __lookupGetter__/__lookupSetter__: legacy property introspection\r\n */\r\nexport const DANGEROUS_PROTO_KEYS = new Set([\r\n '__proto__',\r\n 'constructor',\r\n 'prototype',\r\n '__definegetter__',\r\n '__definesetter__',\r\n '__lookupgetter__',\r\n '__lookupsetter__',\r\n]);\r\n\r\n/** MongoDB operators to block */\r\nexport const NOSQL_DANGEROUS_KEYS = new Set([\r\n // Comparison\r\n '$gt', '$gte', '$lt', '$lte', '$ne', '$eq', '$in', '$nin',\r\n // Logical\r\n '$and', '$or', '$not', '$nor',\r\n // Element / evaluation\r\n '$exists', '$type', '$regex', '$where', '$expr', '$mod', '$text',\r\n // Array\r\n '$elemMatch', '$all', '$size',\r\n // JavaScript execution (critical)\r\n '$function', '$accumulator',\r\n // Aggregation pipeline operators (injectable via $lookup etc.)\r\n '$lookup', '$match', '$project', '$group', '$sort', '$limit', '$skip',\r\n '$unwind', '$addFields', '$replaceRoot',\r\n]);\r\n\r\n// =============================================================================\r\n// REDACTION\r\n// =============================================================================\r\nexport const REDACTION = {\r\n /** Replacement text for redacted values */\r\n REPLACEMENT: '[REDACTED]',\r\n /** Truncation indicator */\r\n TRUNCATED: '[TRUNCATED]',\r\n /** Max depth indicator */\r\n MAX_DEPTH: '[MAX_DEPTH]',\r\n /** Default max message length */\r\n DEFAULT_MAX_LENGTH: 10_000,\r\n /** Default sensitive keys to redact */\r\n SENSITIVE_KEYS: new Set([\r\n 'password', 'passwd', 'pwd', 'secret', 'token', 'apikey',\r\n 'api_key', 'apiKey', 'auth', 'authorization', 'credit_card',\r\n 'creditcard', 'cc', 'ssn', 'social_security', 'private_key',\r\n 'privateKey', 'access_token', 'accessToken', 'refresh_token',\r\n 'refreshToken', 'bearer', 'jwt', 'session', 'cookie',\r\n 'credentials', 'x-api-key', 'x-auth-token',\r\n ]),\r\n} as const;\r\n\r\n// =============================================================================\r\n// VALIDATION PATTERNS\r\n// =============================================================================\r\nexport const VALIDATION = {\r\n /**\r\n * Email regex pattern.\r\n * Rejects consecutive dots in local part (e.g. test..foo@example.com),\r\n * leading/trailing dots, and other common invalid forms.\r\n */\r\n EMAIL: /^[^\\s@.][^\\s@]*(?:\\.[^\\s@.][^\\s@]*)*@[^\\s@]+\\.[^\\s@]+$/,\r\n /**\r\n * URL regex pattern.\r\n * Only allows http:// and https:// — explicitly rejects javascript:,\r\n * data:, vbscript:, and other dangerous URI schemes.\r\n */\r\n URL: /^https?:\\/\\/[^\\s/$.?#][^\\s]*$/,\r\n /** UUID regex pattern (v4) */\r\n UUID: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,\r\n} as const;\r\n\r\n// =============================================================================\r\n// ERROR MESSAGES\r\n// =============================================================================\r\nexport const ERRORS = {\r\n /** Generic error message (production) */\r\n INTERNAL_SERVER_ERROR: 'Internal Server Error',\r\n /** Input too large error */\r\n INPUT_TOO_LARGE: (maxSize: number) => `Input exceeds maximum size of ${maxSize} bytes`,\r\n /** Validation error messages */\r\n VALIDATION: {\r\n REQUIRED: (field: string) => `${field} is required`,\r\n INVALID_TYPE: (field: string, type: string) => `${field} must be a ${type}`,\r\n MIN_LENGTH: (field: string, min: number) => `${field} must be at least ${min} characters`,\r\n MAX_LENGTH: (field: string, max: number) => `${field} must be at most ${max} characters`,\r\n MIN_VALUE: (field: string, min: number) => `${field} must be at least ${min}`,\r\n MAX_VALUE: (field: string, max: number) => `${field} must be at most ${max}`,\r\n INVALID_FORMAT: (field: string) => `${field} format is invalid`,\r\n INVALID_EMAIL: (field: string) => `${field} must be a valid email`,\r\n INVALID_URL: (field: string) => `${field} must be a valid URL`,\r\n INVALID_UUID: (field: string) => `${field} must be a valid UUID`,\r\n INVALID_ENUM: (field: string, values: unknown[]) => `${field} must be one of: ${values.join(', ')}`,\r\n MIN_ITEMS: (field: string, min: number) => `${field} must have at least ${min} items`,\r\n MAX_ITEMS: (field: string, max: number) => `${field} must have at most ${max} items`,\r\n },\r\n} as const;\r\n\r\n// =============================================================================\r\n// BLOCKED TEXT (for sanitizer replacements)\r\n// =============================================================================\r\nexport const BLOCKED = '[BLOCKED]' as const;\r\n","/**\r\n * @module @arcis/node/stores/memory\r\n * In-memory rate limit store\r\n */\r\n\r\nimport type { RateLimitStore, RateLimitEntry } from '../core/types';\r\nimport { RATE_LIMIT } from '../core/constants';\r\n\r\n/**\r\n * In-memory rate limit store.\r\n * Suitable for single-instance deployments.\r\n * For distributed systems, use RedisStore or a custom store.\r\n * \r\n * @example\r\n * const store = new MemoryStore(60000); // 1 minute window\r\n * const limiter = createRateLimiter({ store });\r\n */\r\nexport class MemoryStore implements RateLimitStore {\r\n private store: Map<string, RateLimitEntry> = new Map();\r\n private cleanupInterval: ReturnType<typeof setInterval> | null = null;\r\n private windowMs: number;\r\n\r\n constructor(windowMs: number = RATE_LIMIT.DEFAULT_WINDOW_MS) {\r\n if (!Number.isFinite(windowMs) || windowMs < RATE_LIMIT.MIN_WINDOW_MS) {\r\n throw new RangeError(\r\n `MemoryStore: windowMs must be a finite number >= ${RATE_LIMIT.MIN_WINDOW_MS} (got ${windowMs})`\r\n );\r\n }\r\n this.windowMs = windowMs;\r\n this.startCleanup();\r\n }\r\n\r\n /**\r\n * Start the cleanup interval to remove expired entries.\r\n */\r\n private startCleanup(): void {\r\n // Clamp the cleanup interval between 30 s and 5 min regardless of windowMs.\r\n // Running it every windowMs is fine for typical windows but would fire every\r\n // second for short windows (e.g. windowMs: 1000), causing O(n) GC pressure.\r\n const CLEANUP_MIN_MS = 30_000;\r\n const CLEANUP_MAX_MS = 300_000;\r\n const cleanupMs = Math.min(Math.max(this.windowMs, CLEANUP_MIN_MS), CLEANUP_MAX_MS);\r\n\r\n this.cleanupInterval = setInterval(() => {\r\n const now = Date.now();\r\n for (const [key, entry] of this.store.entries()) {\r\n if (entry.resetTime < now) {\r\n this.store.delete(key);\r\n }\r\n }\r\n }, cleanupMs);\r\n\r\n // Prevent interval from keeping the process alive\r\n if (typeof this.cleanupInterval.unref === 'function') {\r\n this.cleanupInterval.unref();\r\n }\r\n }\r\n\r\n async get(key: string): Promise<RateLimitEntry | null> {\r\n const entry = this.store.get(key);\r\n if (!entry) return null;\r\n \r\n // Check if expired\r\n if (entry.resetTime < Date.now()) {\r\n this.store.delete(key);\r\n return null;\r\n }\r\n \r\n return entry;\r\n }\r\n\r\n async set(key: string, entry: RateLimitEntry): Promise<void> {\r\n this.store.set(key, entry);\r\n }\r\n\r\n async increment(key: string): Promise<number> {\r\n const now = Date.now();\r\n const entry = this.store.get(key);\r\n \r\n if (!entry || entry.resetTime < now) {\r\n // Start new window\r\n this.store.set(key, { count: 1, resetTime: now + this.windowMs });\r\n return 1;\r\n }\r\n \r\n entry.count++;\r\n return entry.count;\r\n }\r\n\r\n async decrement(key: string): Promise<void> {\r\n const entry = this.store.get(key);\r\n if (entry && entry.count > 0) {\r\n entry.count--;\r\n }\r\n }\r\n\r\n async reset(key: string): Promise<void> {\r\n this.store.delete(key);\r\n }\r\n\r\n async close(): Promise<void> {\r\n if (this.cleanupInterval) {\r\n clearInterval(this.cleanupInterval);\r\n this.cleanupInterval = null;\r\n }\r\n this.store.clear();\r\n }\r\n\r\n /**\r\n * Get current store size (for monitoring).\r\n */\r\n get size(): number {\r\n return this.store.size;\r\n }\r\n}\r\n","/**\r\n * @module @arcis/node/stores/redis\r\n * Redis rate limit store\r\n * \r\n * Note: This is a reference implementation. You'll need to install\r\n * the 'ioredis' or 'redis' package and pass your client instance.\r\n */\r\n\r\nimport type { RateLimitStore, RateLimitEntry } from '../core/types';\r\nimport { RATE_LIMIT } from '../core/constants';\r\n\r\n/** Generic Redis client interface (works with ioredis, redis, etc.) */\r\nexport interface RedisClientLike {\r\n get(key: string): Promise<string | null>;\r\n set(key: string, value: string, mode?: string, duration?: number): Promise<unknown>;\r\n setex(key: string, seconds: number, value: string): Promise<unknown>;\r\n expire(key: string, seconds: number): Promise<unknown>;\r\n incr(key: string): Promise<number>;\r\n decr(key: string): Promise<number>;\r\n del(key: string): Promise<number>;\r\n ttl(key: string): Promise<number>;\r\n quit?(): Promise<unknown>;\r\n disconnect?(): Promise<unknown>;\r\n}\r\n\r\nexport interface RedisStoreOptions {\r\n /** Redis client instance */\r\n client: RedisClientLike;\r\n /** Key prefix. Default: 'arcis:rl:' */\r\n prefix?: string;\r\n /** Window size in milliseconds. Default: 60000 */\r\n windowMs?: number;\r\n}\r\n\r\n/**\r\n * Redis rate limit store for distributed deployments.\r\n * \r\n * @example\r\n * import Redis from 'ioredis';\r\n * \r\n * const redis = new Redis();\r\n * const store = new RedisStore({ client: redis });\r\n * const limiter = createRateLimiter({ store });\r\n * \r\n * // Cleanup on shutdown\r\n * process.on('SIGTERM', async () => {\r\n * await store.close();\r\n * });\r\n */\r\nexport class RedisStore implements RateLimitStore {\r\n private client: RedisClientLike;\r\n private prefix: string;\r\n private windowMs: number;\r\n private windowSec: number;\r\n\r\n constructor(options: RedisStoreOptions) {\r\n this.client = options.client;\r\n this.prefix = options.prefix ?? 'arcis:rl:';\r\n this.windowMs = options.windowMs ?? RATE_LIMIT.DEFAULT_WINDOW_MS;\r\n this.windowSec = Math.ceil(this.windowMs / 1000);\r\n }\r\n\r\n private getKey(key: string): string {\r\n return `${this.prefix}${key}`;\r\n }\r\n\r\n async get(key: string): Promise<RateLimitEntry | null> {\r\n const redisKey = this.getKey(key);\r\n \r\n const [countStr, ttl] = await Promise.all([\r\n this.client.get(redisKey),\r\n this.client.ttl(redisKey),\r\n ]);\r\n \r\n if (!countStr || ttl < 0) {\r\n return null;\r\n }\r\n \r\n const count = parseInt(countStr, 10);\r\n if (isNaN(count)) {\r\n // Corrupt value in Redis — treat as if key doesn't exist\r\n return null;\r\n }\r\n\r\n return {\r\n count,\r\n resetTime: Date.now() + (ttl * 1000),\r\n };\r\n }\r\n\r\n async set(key: string, entry: RateLimitEntry): Promise<void> {\r\n const redisKey = this.getKey(key);\r\n // Clamp to at least 1 second — Math.ceil can produce 0 or negative values\r\n // when entry.resetTime is in the past due to Redis latency or clock skew.\r\n const ttlSec = Math.max(1, Math.ceil((entry.resetTime - Date.now()) / 1000));\r\n await this.client.setex(redisKey, ttlSec, entry.count.toString());\r\n }\r\n\r\n async increment(key: string): Promise<number> {\r\n const redisKey = this.getKey(key);\r\n \r\n // INCR creates key with value 1 if it doesn't exist\r\n const count = await this.client.incr(redisKey);\r\n\r\n // Set expiry only on first increment using EXPIRE, which sets the TTL\r\n // without overwriting the value (unlike SET ... EX which would reset the\r\n // counter if two requests increment concurrently before expiry is set).\r\n if (count === 1) {\r\n await this.client.expire(redisKey, this.windowSec);\r\n }\r\n\r\n return count;\r\n }\r\n\r\n async decrement(key: string): Promise<void> {\r\n const redisKey = this.getKey(key);\r\n await this.client.decr(redisKey);\r\n }\r\n\r\n async reset(key: string): Promise<void> {\r\n const redisKey = this.getKey(key);\r\n await this.client.del(redisKey);\r\n }\r\n\r\n async close(): Promise<void> {\r\n // Don't close the client - it may be shared\r\n // The caller should manage the client lifecycle\r\n }\r\n}\r\n\r\n/**\r\n * Create a Redis store with the given options.\r\n * Convenience function for functional programming style.\r\n * \r\n * @example\r\n * const store = createRedisStore({ client: redisClient });\r\n */\r\nexport function createRedisStore(options: RedisStoreOptions): RedisStore {\r\n return new RedisStore(options);\r\n}\r\n"]}
@@ -0,0 +1,264 @@
1
+ import { Request, RequestHandler, Response, NextFunction } from 'express';
2
+
3
+ /**
4
+ * @module @arcis/node/core/types
5
+ * All TypeScript interfaces and types for Arcis
6
+ */
7
+
8
+ /** Main Arcis configuration options */
9
+ interface ArcisOptions {
10
+ /** Enable/configure input sanitization. Default: true */
11
+ sanitize?: boolean | SanitizeOptions;
12
+ /** Enable/configure rate limiting. Default: true */
13
+ rateLimit?: boolean | RateLimitOptions;
14
+ /** Enable/configure security headers. Default: true */
15
+ headers?: boolean | HeaderOptions;
16
+ /** Enable/configure safe logging. Default: true */
17
+ logging?: boolean | LogOptions;
18
+ }
19
+ /** Sanitization configuration */
20
+ interface SanitizeOptions {
21
+ /** Sanitize XSS attempts. Default: true */
22
+ xss?: boolean;
23
+ /** Sanitize SQL injection attempts. Default: true */
24
+ sql?: boolean;
25
+ /** Sanitize NoSQL injection attempts. Default: true */
26
+ nosql?: boolean;
27
+ /** Sanitize path traversal attempts. Default: true */
28
+ path?: boolean;
29
+ /** Protect against prototype pollution. Default: true */
30
+ proto?: boolean;
31
+ /** Sanitize command injection attempts. Default: true */
32
+ command?: boolean;
33
+ /** Maximum input size in bytes. Default: 1000000 (1MB) */
34
+ maxSize?: number;
35
+ /**
36
+ * How to handle detected SQL and command injection threats.
37
+ * - 'reject': Throw SecurityThreatError (returns 400). Recommended for APIs. Default.
38
+ * - 'sanitize': Strip/replace threats in-place. Use only when rejection is not feasible.
39
+ */
40
+ mode?: 'sanitize' | 'reject';
41
+ /**
42
+ * HTML-encode output after XSS stripping.
43
+ * Enable for SSR/template rendering. Do NOT enable for JSON REST APIs
44
+ * — it corrupts stored data with HTML entities. Default: false.
45
+ */
46
+ htmlEncode?: boolean;
47
+ }
48
+ /** Result of sanitizing a string */
49
+ interface SanitizeResult {
50
+ /** The sanitized value */
51
+ value: string;
52
+ /** Whether any sanitization was applied */
53
+ wasSanitized: boolean;
54
+ /** Details about detected threats */
55
+ threats: ThreatInfo[];
56
+ }
57
+ /** Information about a detected threat */
58
+ interface ThreatInfo {
59
+ /** Type of threat detected */
60
+ type: ThreatType;
61
+ /** Pattern that matched */
62
+ pattern: string;
63
+ /** Original matched content */
64
+ original: string;
65
+ /** Location in the input (if applicable) */
66
+ location?: string;
67
+ }
68
+ /** Types of security threats */
69
+ type ThreatType = 'xss' | 'sql_injection' | 'nosql_injection' | 'path_traversal' | 'command_injection' | 'prototype_pollution' | 'header_injection';
70
+ /** Rate limiting configuration */
71
+ interface RateLimitOptions {
72
+ /** Maximum requests per window. Default: 100 */
73
+ max?: number;
74
+ /** Window size in milliseconds. Default: 60000 (1 minute) */
75
+ windowMs?: number;
76
+ /** Error message when limit exceeded */
77
+ message?: string;
78
+ /** HTTP status code for rate limited responses. Default: 429 */
79
+ statusCode?: number;
80
+ /** Function to generate rate limit key from request */
81
+ keyGenerator?: (req: Request) => string;
82
+ /** Function to skip rate limiting for certain requests */
83
+ skip?: (req: Request) => boolean;
84
+ /** Optional external store for distributed rate limiting */
85
+ store?: RateLimitStore;
86
+ }
87
+ /** External store interface for distributed rate limiting */
88
+ interface RateLimitStore {
89
+ /** Get current count for a key */
90
+ get(key: string): Promise<RateLimitEntry | null>;
91
+ /** Set entry for a key */
92
+ set(key: string, entry: RateLimitEntry): Promise<void>;
93
+ /** Increment count for a key */
94
+ increment(key: string): Promise<number>;
95
+ /** Decrement count for a key (for sliding window) */
96
+ decrement?(key: string): Promise<void>;
97
+ /** Reset count for a key */
98
+ reset?(key: string): Promise<void>;
99
+ /** Close the store (cleanup connections) */
100
+ close?(): Promise<void>;
101
+ }
102
+ /** Rate limit entry stored in a store */
103
+ interface RateLimitEntry {
104
+ /** Number of requests in the current window */
105
+ count: number;
106
+ /** Timestamp when the window resets */
107
+ resetTime: number;
108
+ }
109
+ /** Result from incrementing a rate limit counter */
110
+ interface RateLimitResult {
111
+ /** Current request count */
112
+ count: number;
113
+ /** When the window resets */
114
+ resetTime: Date;
115
+ }
116
+ /** Rate limiter middleware with cleanup support */
117
+ interface RateLimiterMiddleware extends RequestHandler {
118
+ /** Clean up the rate limiter (clear intervals, close stores) */
119
+ close: () => void;
120
+ }
121
+ /** Security headers configuration */
122
+ interface HeaderOptions {
123
+ /** Content Security Policy. true = default, string = custom, false = disabled */
124
+ contentSecurityPolicy?: boolean | string;
125
+ /** Enable X-XSS-Protection header. Default: true */
126
+ xssFilter?: boolean;
127
+ /** Enable X-Content-Type-Options: nosniff. Default: true */
128
+ noSniff?: boolean;
129
+ /** X-Frame-Options value. Default: 'DENY' */
130
+ frameOptions?: 'DENY' | 'SAMEORIGIN' | false;
131
+ /** HSTS configuration. Default: true */
132
+ hsts?: boolean | HstsOptions;
133
+ /** Referrer-Policy value. Default: 'strict-origin-when-cross-origin' */
134
+ referrerPolicy?: string | false;
135
+ /** Permissions-Policy value */
136
+ permissionsPolicy?: string | false;
137
+ /** Cache-Control configuration. Default: true (no-cache) */
138
+ cacheControl?: boolean | string;
139
+ }
140
+ /** HSTS (HTTP Strict Transport Security) options */
141
+ interface HstsOptions {
142
+ /** Max age in seconds. Default: 31536000 (1 year) */
143
+ maxAge?: number;
144
+ /** Include subdomains. Default: true */
145
+ includeSubDomains?: boolean;
146
+ /** Enable HSTS preload. Default: false */
147
+ preload?: boolean;
148
+ }
149
+ /** Validation configuration */
150
+ interface ValidationConfig {
151
+ /** Strip fields not in schema. Default: true (prevents mass assignment) */
152
+ stripUnknown?: boolean;
153
+ /** Stop on first error. Default: false */
154
+ abortEarly?: boolean;
155
+ }
156
+ /** Validation schema for request data */
157
+ interface ValidationSchema {
158
+ [key: string]: FieldValidator;
159
+ }
160
+ /** Field validation rules */
161
+ interface FieldValidator {
162
+ /** Expected data type */
163
+ type: 'string' | 'number' | 'boolean' | 'email' | 'url' | 'uuid' | 'array' | 'object';
164
+ /** Whether field is required. Default: false */
165
+ required?: boolean;
166
+ /** Minimum value (number) or length (string/array) */
167
+ min?: number;
168
+ /** Maximum value (number) or length (string/array) */
169
+ max?: number;
170
+ /** Regex pattern for string validation */
171
+ pattern?: RegExp;
172
+ /** Allowed values */
173
+ enum?: unknown[];
174
+ /** Whether to sanitize the value. Default: true */
175
+ sanitize?: boolean;
176
+ /**
177
+ * Custom validation function.
178
+ * Return `true` to pass, `false` to fail with a default message,
179
+ * or a non-empty string to fail with that message.
180
+ * Returning `undefined` (i.e. forgetting to return) throws at runtime.
181
+ */
182
+ custom?: (value: unknown) => true | false | string;
183
+ }
184
+ /** Validation result */
185
+ interface ValidationResult {
186
+ /** Whether validation passed */
187
+ valid: boolean;
188
+ /** Validation errors */
189
+ errors: ValidationError[];
190
+ /** Validated and sanitized data */
191
+ data: Record<string, unknown>;
192
+ }
193
+ /** Single validation error */
194
+ interface ValidationError {
195
+ /** Field that failed validation */
196
+ field: string;
197
+ /** Human-readable error message */
198
+ message: string;
199
+ /** Error code for programmatic handling */
200
+ code: string;
201
+ }
202
+ /** Safe logging configuration */
203
+ interface LogOptions {
204
+ /** Additional keys to redact beyond defaults */
205
+ redactKeys?: string[];
206
+ /** Maximum message length before truncation. Default: 10000 */
207
+ maxLength?: number;
208
+ /** Additional patterns to redact (e.g., custom tokens) */
209
+ redactPatterns?: RegExp[];
210
+ }
211
+ /** Safe logger interface */
212
+ interface SafeLogger {
213
+ /** Log at specified level */
214
+ log: (level: string, message: string, data?: unknown) => void;
215
+ /** Log info message */
216
+ info: (message: string, data?: unknown) => void;
217
+ /** Log warning message */
218
+ warn: (message: string, data?: unknown) => void;
219
+ /** Log error message */
220
+ error: (message: string, data?: unknown) => void;
221
+ /** Log debug message */
222
+ debug: (message: string, data?: unknown) => void;
223
+ }
224
+ /** Error handler configuration */
225
+ interface ErrorHandlerOptions {
226
+ /** Show stack traces and detailed errors. Default: false */
227
+ isDev?: boolean;
228
+ /** Log errors. Default: true */
229
+ logErrors?: boolean;
230
+ /** Custom error logger */
231
+ logger?: SafeLogger;
232
+ /** Custom error handler */
233
+ customHandler?: (err: Error, req: Request, res: Response) => void;
234
+ }
235
+ /** Extended Error with optional status code */
236
+ interface HttpError extends Error {
237
+ statusCode?: number;
238
+ status?: number;
239
+ /**
240
+ * Whether the error message is safe to expose to API clients.
241
+ * Set to true for known client-facing errors (4xx with controlled messages).
242
+ * Defaults to false — message is hidden in production unless explicitly exposed.
243
+ */
244
+ expose?: boolean;
245
+ }
246
+ /** Generic Arcis middleware type */
247
+ type ArcisMiddleware = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;
248
+ /** Array of middlewares returned by arcis() with an attached cleanup method */
249
+ type ArcisMiddlewareStack = RequestHandler[] & {
250
+ /** Clean up resources created by arcis() (rate limiter intervals, etc.) */
251
+ close: () => void;
252
+ };
253
+ /** Arcis function with attached utilities */
254
+ interface ArcisFunction {
255
+ (options?: ArcisOptions): ArcisMiddlewareStack;
256
+ sanitize: (options?: SanitizeOptions) => RequestHandler;
257
+ rateLimit: (options?: RateLimitOptions) => RateLimiterMiddleware;
258
+ headers: (options?: HeaderOptions) => RequestHandler;
259
+ validate: (schema: ValidationSchema, source?: 'body' | 'query' | 'params') => RequestHandler;
260
+ logger: (options?: LogOptions) => SafeLogger;
261
+ errorHandler: (options?: ErrorHandlerOptions | boolean) => (err: Error, req: Request, res: Response, next: NextFunction) => void;
262
+ }
263
+
264
+ export type { ArcisFunction as A, ErrorHandlerOptions as E, FieldValidator as F, HeaderOptions as H, LogOptions as L, RateLimitEntry as R, SafeLogger as S, ThreatInfo as T, ValidationConfig as V, ArcisMiddleware as a, ArcisOptions as b, HstsOptions as c, HttpError as d, RateLimitOptions as e, RateLimitResult as f, RateLimitStore as g, RateLimiterMiddleware as h, SanitizeOptions as i, SanitizeResult as j, ThreatType as k, ValidationError as l, ValidationResult as m, ValidationSchema as n, ArcisMiddlewareStack as o };