@arcis/node 1.1.0 → 1.2.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 (50) hide show
  1. package/README.md +156 -211
  2. package/dist/core/index.d.mts +4 -4
  3. package/dist/core/index.d.ts +4 -4
  4. package/dist/core/index.js +13 -2
  5. package/dist/core/index.js.map +1 -1
  6. package/dist/core/index.mjs +13 -2
  7. package/dist/core/index.mjs.map +1 -1
  8. package/dist/{index-CslcoZUN.d.mts → index-A-m-pPeW.d.mts} +1 -1
  9. package/dist/{index-CCcPuTBo.d.mts → index-CgK94hY_.d.mts} +96 -2
  10. package/dist/{index-iCOw8Fcg.d.ts → index-Co5kPRZz.d.ts} +1 -1
  11. package/dist/{index-BvcFpoR3.d.ts → index-D_bdJcF0.d.ts} +96 -2
  12. package/dist/index.d.mts +4 -4
  13. package/dist/index.d.ts +4 -4
  14. package/dist/index.js +553 -5
  15. package/dist/index.js.map +1 -1
  16. package/dist/index.mjs +540 -7
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/logging/index.d.mts +1 -1
  19. package/dist/logging/index.d.ts +1 -1
  20. package/dist/logging/index.js +12 -1
  21. package/dist/logging/index.js.map +1 -1
  22. package/dist/logging/index.mjs +12 -1
  23. package/dist/logging/index.mjs.map +1 -1
  24. package/dist/middleware/index.d.mts +2 -2
  25. package/dist/middleware/index.d.ts +2 -2
  26. package/dist/middleware/index.js +146 -4
  27. package/dist/middleware/index.js.map +1 -1
  28. package/dist/middleware/index.mjs +143 -5
  29. package/dist/middleware/index.mjs.map +1 -1
  30. package/dist/{headers-DBQedhrb.d.mts → pii-CXcHMlnX.d.mts} +156 -2
  31. package/dist/{headers-BJq2OA0i.d.ts → pii-DhNpl7M3.d.ts} +156 -2
  32. package/dist/sanitizers/index.d.mts +2 -2
  33. package/dist/sanitizers/index.d.ts +2 -2
  34. package/dist/sanitizers/index.js +331 -3
  35. package/dist/sanitizers/index.js.map +1 -1
  36. package/dist/sanitizers/index.mjs +321 -4
  37. package/dist/sanitizers/index.mjs.map +1 -1
  38. package/dist/stores/index.d.mts +1 -1
  39. package/dist/stores/index.d.ts +1 -1
  40. package/dist/stores/index.js.map +1 -1
  41. package/dist/stores/index.mjs.map +1 -1
  42. package/dist/{types-BOdL3ZWo.d.mts → types-CsOFHoD9.d.mts} +6 -1
  43. package/dist/{types-BOdL3ZWo.d.ts → types-CsOFHoD9.d.ts} +6 -1
  44. package/dist/validation/index.d.mts +2 -2
  45. package/dist/validation/index.d.ts +2 -2
  46. package/dist/validation/index.js +105 -3
  47. package/dist/validation/index.js.map +1 -1
  48. package/dist/validation/index.mjs +105 -3
  49. package/dist/validation/index.mjs.map +1 -1
  50. package/package.json +114 -114
@@ -1,3 +1,5 @@
1
+ import { randomBytes } from 'crypto';
2
+
1
3
  // src/core/constants.ts
2
4
  var INPUT = {
3
5
  /** Default maximum input size (1MB) */
@@ -85,7 +87,11 @@ var SQL_PATTERNS = [
85
87
  /** Time-based blind: SLEEP() */
86
88
  /\bSLEEP\s*\(\s*\d+\s*\)/gi,
87
89
  /** Time-based blind: BENCHMARK() */
88
- /\bBENCHMARK\s*\(/gi
90
+ /\bBENCHMARK\s*\(/gi,
91
+ /** Time-based blind: PostgreSQL pg_sleep() */
92
+ /\bpg_sleep\s*\(/gi,
93
+ /** Time-based blind: MSSQL WAITFOR DELAY */
94
+ /\bWAITFOR\s+DELAY\b/gi
89
95
  ];
90
96
  var PATH_PATTERNS = [
91
97
  /** Unix path traversal */
@@ -103,6 +109,10 @@ var PATH_PATTERNS = [
103
109
  /\.%2e[\\/]/gi,
104
110
  /** Fully URL-encoded: %2e%2e%2f */
105
111
  /%2e%2e%2f/gi,
112
+ /** Double URL-encoded forward slash: %252f */
113
+ /%252f/gi,
114
+ /** Dotdotslash bypass: ....// or ....\\ */
115
+ /\.{2,}[/\\]{2,}/g,
106
116
  /** Null byte injection in paths */
107
117
  /\0/g
108
118
  ];
@@ -118,7 +128,9 @@ var COMMAND_PATTERNS = [
118
128
  */
119
129
  /[;&|`]/g,
120
130
  /** Command substitution: $( ... ) — matched as a pair to reduce false positives */
121
- /\$\(/g
131
+ /\$\(/g,
132
+ /** URL-encoded newline/carriage-return injection (%0a, %0d) */
133
+ /%0[ad]/gi
122
134
  ];
123
135
  var DANGEROUS_PROTO_KEYS = /* @__PURE__ */ new Set([
124
136
  "__proto__",
@@ -152,6 +164,7 @@ var NOSQL_DANGEROUS_KEYS = /* @__PURE__ */ new Set([
152
164
  "$expr",
153
165
  "$mod",
154
166
  "$text",
167
+ "$jsonSchema",
155
168
  // Array
156
169
  "$elemMatch",
157
170
  "$all",
@@ -705,7 +718,8 @@ function sanitizeObject(obj, options = {}) {
705
718
  if (typeof obj === "string") return sanitizeString(obj, options);
706
719
  if (typeof obj !== "object") return obj;
707
720
  if (Array.isArray(obj)) return obj.map((item) => sanitizeObject(item, options));
708
- return sanitizeObjectDepth(obj, options, 0);
721
+ const result = sanitizeObjectDepth(obj, options, 0);
722
+ return options.freeze ? Object.freeze(result) : result;
709
723
  }
710
724
  function sanitizeObjectDepth(obj, options, depth) {
711
725
  if (depth >= INPUT.MAX_RECURSION_DEPTH) return obj;
@@ -926,12 +940,21 @@ function validateField(field, value, rules) {
926
940
  "audio/mpeg": [Buffer.from([255, 251]), Buffer.from([255, 243]), Buffer.from([73, 68, 51])]});
927
941
 
928
942
  // src/logging/redactor.ts
943
+ var LOG_LEVELS = {
944
+ debug: 0,
945
+ info: 1,
946
+ warn: 2,
947
+ error: 3,
948
+ silent: 4
949
+ };
929
950
  function createSafeLogger(options = {}) {
930
951
  const {
931
952
  redactKeys = [],
932
953
  maxLength = REDACTION.DEFAULT_MAX_LENGTH,
933
- redactPatterns = []
954
+ redactPatterns = [],
955
+ level: minLevel = "debug"
934
956
  } = options;
957
+ const minLevelNum = LOG_LEVELS[minLevel] ?? 0;
935
958
  const allRedactKeys = /* @__PURE__ */ new Set([
936
959
  ...Array.from(REDACTION.SENSITIVE_KEYS),
937
960
  ...redactKeys.map((k) => k.toLowerCase())
@@ -957,6 +980,8 @@ function createSafeLogger(options = {}) {
957
980
  return result;
958
981
  }
959
982
  function log(level, message, data) {
983
+ const levelNum = LOG_LEVELS[level] ?? 0;
984
+ if (levelNum < minLevelNum) return;
960
985
  const entry = {
961
986
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
962
987
  level,
@@ -1524,7 +1549,120 @@ function botProtection(options = {}) {
1524
1549
  next();
1525
1550
  };
1526
1551
  }
1552
+ var DEFAULTS = {
1553
+ cookieName: "_csrf",
1554
+ headerName: "x-csrf-token",
1555
+ fieldName: "_csrf",
1556
+ tokenLength: 32,
1557
+ protectedMethods: ["POST", "PUT", "PATCH", "DELETE"]
1558
+ };
1559
+ function generateCsrfToken(length = 32) {
1560
+ return randomBytes(length).toString("hex");
1561
+ }
1562
+ function validateCsrfToken(cookieToken, requestToken) {
1563
+ if (!cookieToken || !requestToken) return false;
1564
+ if (cookieToken.length !== requestToken.length) return false;
1565
+ let result = 0;
1566
+ for (let i = 0; i < cookieToken.length; i++) {
1567
+ result |= cookieToken.charCodeAt(i) ^ requestToken.charCodeAt(i);
1568
+ }
1569
+ return result === 0;
1570
+ }
1571
+ function getRequestToken(req, headerName, fieldName) {
1572
+ const headerToken = req.headers[headerName.toLowerCase()];
1573
+ if (typeof headerToken === "string" && headerToken) return headerToken;
1574
+ if (req.body && typeof req.body === "object" && fieldName in req.body) {
1575
+ const bodyToken = req.body[fieldName];
1576
+ if (typeof bodyToken === "string" && bodyToken) return bodyToken;
1577
+ }
1578
+ if (req.query && fieldName in req.query) {
1579
+ const queryToken = req.query[fieldName];
1580
+ if (typeof queryToken === "string" && queryToken) return queryToken;
1581
+ }
1582
+ return void 0;
1583
+ }
1584
+ function csrfProtection(options = {}) {
1585
+ const cookieName = options.cookieName ?? DEFAULTS.cookieName;
1586
+ const headerName = options.headerName ?? DEFAULTS.headerName;
1587
+ const fieldName = options.fieldName ?? DEFAULTS.fieldName;
1588
+ const tokenLength = options.tokenLength ?? DEFAULTS.tokenLength;
1589
+ const protectedMethods = options.protectedMethods ?? [...DEFAULTS.protectedMethods];
1590
+ const excludePaths = options.excludePaths ?? [];
1591
+ const isProduction = process.env.NODE_ENV === "production";
1592
+ const cookieOpts = {
1593
+ path: options.cookie?.path ?? "/",
1594
+ httpOnly: options.cookie?.httpOnly ?? false,
1595
+ // Must be readable by client JS
1596
+ secure: options.cookie?.secure ?? isProduction,
1597
+ sameSite: options.cookie?.sameSite ?? "Lax",
1598
+ domain: options.cookie?.domain
1599
+ };
1600
+ const defaultOnError = (_req, res, _next) => {
1601
+ res.status(403).json({
1602
+ error: "CSRF token validation failed",
1603
+ message: "Invalid or missing CSRF token. Include the token from the cookie in the X-CSRF-Token header."
1604
+ });
1605
+ };
1606
+ const onError = options.onError ?? defaultOnError;
1607
+ const protectedSet = new Set(protectedMethods.map((m) => m.toUpperCase()));
1608
+ return (req, res, next) => {
1609
+ const method = req.method.toUpperCase();
1610
+ const requestPath = req.path || req.url;
1611
+ if (excludePaths.some((p) => requestPath === p || requestPath.startsWith(p + "/"))) {
1612
+ return next();
1613
+ }
1614
+ req.csrfToken = () => {
1615
+ const existing = getCookieValue(req, cookieName);
1616
+ if (existing) return existing;
1617
+ const token = generateCsrfToken(tokenLength);
1618
+ setCsrfCookie(res, cookieName, token, cookieOpts);
1619
+ return token;
1620
+ };
1621
+ if (!protectedSet.has(method)) {
1622
+ const existing = getCookieValue(req, cookieName);
1623
+ if (!existing) {
1624
+ const token = generateCsrfToken(tokenLength);
1625
+ setCsrfCookie(res, cookieName, token, cookieOpts);
1626
+ }
1627
+ return next();
1628
+ }
1629
+ const cookieToken = getCookieValue(req, cookieName);
1630
+ if (!cookieToken) {
1631
+ return onError(req, res, next);
1632
+ }
1633
+ const requestToken = getRequestToken(req, headerName, fieldName);
1634
+ if (!requestToken) {
1635
+ return onError(req, res, next);
1636
+ }
1637
+ if (!validateCsrfToken(cookieToken, requestToken)) {
1638
+ return onError(req, res, next);
1639
+ }
1640
+ next();
1641
+ };
1642
+ }
1643
+ function getCookieValue(req, name) {
1644
+ if (req.cookies && typeof req.cookies === "object" && name in req.cookies) {
1645
+ return req.cookies[name];
1646
+ }
1647
+ const cookieHeader = req.headers.cookie;
1648
+ if (!cookieHeader) return void 0;
1649
+ const match = cookieHeader.match(new RegExp(`(?:^|;\\s*)${escapeRegex(name)}=([^;]*)`));
1650
+ return match ? decodeURIComponent(match[1]) : void 0;
1651
+ }
1652
+ function setCsrfCookie(res, name, token, opts) {
1653
+ const parts = [`${name}=${token}`];
1654
+ parts.push(`Path=${opts.path}`);
1655
+ if (opts.httpOnly) parts.push("HttpOnly");
1656
+ if (opts.secure) parts.push("Secure");
1657
+ parts.push(`SameSite=${opts.sameSite}`);
1658
+ if (opts.domain) parts.push(`Domain=${opts.domain}`);
1659
+ res.setHeader("Set-Cookie", parts.join("; "));
1660
+ }
1661
+ function escapeRegex(str) {
1662
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1663
+ }
1664
+ var createCsrf = csrfProtection;
1527
1665
 
1528
- export { arcis, arcisWithMethods as arcisFunction, botProtection, createCors, createErrorHandler, createHeaders, createRateLimiter, createSecureCookies, createSlidingWindowLimiter, createTokenBucketLimiter, main_default as default, detectBot, enforceSecureCookie, errorHandler, rateLimit, safeCors, secureCookieDefaults, securityHeaders };
1666
+ export { arcis, arcisWithMethods as arcisFunction, botProtection, createCors, createCsrf, createErrorHandler, createHeaders, createRateLimiter, createSecureCookies, createSlidingWindowLimiter, createTokenBucketLimiter, csrfProtection, main_default as default, detectBot, enforceSecureCookie, errorHandler, generateCsrfToken, rateLimit, safeCors, secureCookieDefaults, securityHeaders, validateCsrfToken };
1529
1667
  //# sourceMappingURL=index.mjs.map
1530
1668
  //# sourceMappingURL=index.mjs.map