xypriss 2.2.6 → 2.3.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.
Files changed (78) hide show
  1. package/dist/cjs/mods/security/src/algorithms/hash-algorithms.js +9 -5
  2. package/dist/cjs/mods/security/src/algorithms/hash-algorithms.js.map +1 -1
  3. package/dist/cjs/mods/security/src/components/cache/cacheSys.utils.js +9 -5
  4. package/dist/cjs/mods/security/src/components/cache/cacheSys.utils.js.map +1 -1
  5. package/dist/cjs/mods/security/src/components/cache/index.js +9 -5
  6. package/dist/cjs/mods/security/src/components/cache/index.js.map +1 -1
  7. package/dist/cjs/mods/security/src/components/fortified-function/index.js +8 -4
  8. package/dist/cjs/mods/security/src/components/fortified-function/index.js.map +1 -1
  9. package/dist/cjs/mods/security/src/components/fortified-function/security/security-handler.js +9 -5
  10. package/dist/cjs/mods/security/src/components/fortified-function/security/security-handler.js.map +1 -1
  11. package/dist/cjs/mods/security/src/index.js +9 -5
  12. package/dist/cjs/mods/security/src/index.js.map +1 -1
  13. package/dist/cjs/src/middleware/XyPrissMiddlewareAPI.js +87 -30
  14. package/dist/cjs/src/middleware/XyPrissMiddlewareAPI.js.map +1 -1
  15. package/dist/cjs/src/middleware/built-in/BuiltInMiddleware.js +320 -0
  16. package/dist/cjs/src/middleware/built-in/BuiltInMiddleware.js.map +1 -0
  17. package/dist/cjs/src/middleware/built-in/security/CommandInjectionDetector.js +215 -0
  18. package/dist/cjs/src/middleware/built-in/security/CommandInjectionDetector.js.map +1 -0
  19. package/dist/cjs/src/middleware/built-in/security/LDAPInjectionDetector.js +96 -0
  20. package/dist/cjs/src/middleware/built-in/security/LDAPInjectionDetector.js.map +1 -0
  21. package/dist/cjs/src/middleware/built-in/security/PathTraversalDetector.js +212 -0
  22. package/dist/cjs/src/middleware/built-in/security/PathTraversalDetector.js.map +1 -0
  23. package/dist/cjs/src/middleware/built-in/security/SQLInjectionDetector.js +335 -0
  24. package/dist/cjs/src/middleware/built-in/security/SQLInjectionDetector.js.map +1 -0
  25. package/dist/cjs/src/middleware/built-in/security/XXEProtector.js +175 -0
  26. package/dist/cjs/src/middleware/built-in/security/XXEProtector.js.map +1 -0
  27. package/dist/cjs/src/middleware/security-middleware.js +249 -132
  28. package/dist/cjs/src/middleware/security-middleware.js.map +1 -1
  29. package/dist/cjs/src/plugins/modules/index.js +8 -4
  30. package/dist/cjs/src/plugins/modules/index.js.map +1 -1
  31. package/dist/cjs/src/server/FastServer.js +49 -32
  32. package/dist/cjs/src/server/FastServer.js.map +1 -1
  33. package/dist/cjs/src/server/ServerFactory.js +34 -1
  34. package/dist/cjs/src/server/ServerFactory.js.map +1 -1
  35. package/dist/cjs/src/server/components/multi-server/MultiServerManager.js +11 -2
  36. package/dist/cjs/src/server/components/multi-server/MultiServerManager.js.map +1 -1
  37. package/dist/cjs/src/server/const/default.js +15 -1
  38. package/dist/cjs/src/server/const/default.js.map +1 -1
  39. package/dist/esm/mods/security/src/algorithms/hash-algorithms.js +9 -5
  40. package/dist/esm/mods/security/src/algorithms/hash-algorithms.js.map +1 -1
  41. package/dist/esm/mods/security/src/components/cache/cacheSys.utils.js +9 -5
  42. package/dist/esm/mods/security/src/components/cache/cacheSys.utils.js.map +1 -1
  43. package/dist/esm/mods/security/src/components/cache/index.js +9 -5
  44. package/dist/esm/mods/security/src/components/cache/index.js.map +1 -1
  45. package/dist/esm/mods/security/src/components/fortified-function/index.js +8 -4
  46. package/dist/esm/mods/security/src/components/fortified-function/index.js.map +1 -1
  47. package/dist/esm/mods/security/src/components/fortified-function/security/security-handler.js +9 -5
  48. package/dist/esm/mods/security/src/components/fortified-function/security/security-handler.js.map +1 -1
  49. package/dist/esm/mods/security/src/index.js +9 -5
  50. package/dist/esm/mods/security/src/index.js.map +1 -1
  51. package/dist/esm/src/middleware/XyPrissMiddlewareAPI.js +75 -18
  52. package/dist/esm/src/middleware/XyPrissMiddlewareAPI.js.map +1 -1
  53. package/dist/esm/src/middleware/built-in/BuiltInMiddleware.js +318 -0
  54. package/dist/esm/src/middleware/built-in/BuiltInMiddleware.js.map +1 -0
  55. package/dist/esm/src/middleware/built-in/security/CommandInjectionDetector.js +213 -0
  56. package/dist/esm/src/middleware/built-in/security/CommandInjectionDetector.js.map +1 -0
  57. package/dist/esm/src/middleware/built-in/security/LDAPInjectionDetector.js +94 -0
  58. package/dist/esm/src/middleware/built-in/security/LDAPInjectionDetector.js.map +1 -0
  59. package/dist/esm/src/middleware/built-in/security/PathTraversalDetector.js +210 -0
  60. package/dist/esm/src/middleware/built-in/security/PathTraversalDetector.js.map +1 -0
  61. package/dist/esm/src/middleware/built-in/security/SQLInjectionDetector.js +333 -0
  62. package/dist/esm/src/middleware/built-in/security/SQLInjectionDetector.js.map +1 -0
  63. package/dist/esm/src/middleware/built-in/security/XXEProtector.js +173 -0
  64. package/dist/esm/src/middleware/built-in/security/XXEProtector.js.map +1 -0
  65. package/dist/esm/src/middleware/security-middleware.js +248 -131
  66. package/dist/esm/src/middleware/security-middleware.js.map +1 -1
  67. package/dist/esm/src/plugins/modules/index.js +8 -4
  68. package/dist/esm/src/plugins/modules/index.js.map +1 -1
  69. package/dist/esm/src/server/FastServer.js +50 -33
  70. package/dist/esm/src/server/FastServer.js.map +1 -1
  71. package/dist/esm/src/server/ServerFactory.js +34 -1
  72. package/dist/esm/src/server/ServerFactory.js.map +1 -1
  73. package/dist/esm/src/server/components/multi-server/MultiServerManager.js +11 -2
  74. package/dist/esm/src/server/components/multi-server/MultiServerManager.js.map +1 -1
  75. package/dist/esm/src/server/const/default.js +15 -1
  76. package/dist/esm/src/server/const/default.js.map +1 -1
  77. package/dist/index.d.ts +895 -39
  78. package/package.json +2 -2
package/dist/index.d.ts CHANGED
@@ -1631,32 +1631,446 @@ interface CacheStrategy {
1631
1631
  */
1632
1632
  type SecurityLevel = "basic" | "enhanced" | "maximum";
1633
1633
  /**
1634
- * Main security configuration interface.
1634
+ * CSRF Protection Configuration
1635
1635
  *
1636
- * Comprehensive security configuration including authentication,
1637
- * encryption, and various security features.
1636
+ * Protects against Cross-Site Request Forgery attacks by requiring tokens.
1637
+ * Can be enabled/disabled or configured with custom options.
1638
1638
  *
1639
- * @interface SecurityConfig
1639
+ * @example Enable with defaults:
1640
+ * ```typescript
1641
+ * csrf: true
1642
+ * ```
1643
+ *
1644
+ * @example Disable:
1645
+ * ```typescript
1646
+ * csrf: false
1647
+ * ```
1648
+ *
1649
+ * @example Custom configuration:
1650
+ * ```typescript
1651
+ * csrf: {
1652
+ * cookieName: '__Host-csrf-token',
1653
+ * cookieOptions: {
1654
+ * httpOnly: true,
1655
+ * sameSite: 'strict',
1656
+ * secure: process.env.NODE_ENV === 'production'
1657
+ * }
1658
+ * }
1659
+ * ```
1660
+ */
1661
+ interface CSRFConfig {
1662
+ /** CSRF token cookie name */
1663
+ cookieName?: string;
1664
+ /** CSRF token cookie options */
1665
+ cookieOptions?: {
1666
+ httpOnly?: boolean;
1667
+ sameSite?: boolean | "lax" | "strict" | "none";
1668
+ secure?: boolean;
1669
+ };
1670
+ }
1671
+ /**
1672
+ * Helmet Security Headers Configuration
1673
+ *
1674
+ * Sets various HTTP headers to help protect against common attacks.
1675
+ * Can be enabled/disabled or configured with custom header options.
1676
+ *
1677
+ * @example Enable with defaults:
1678
+ * ```typescript
1679
+ * helmet: true
1680
+ * ```
1681
+ *
1682
+ * @example Custom CSP:
1683
+ * ```typescript
1684
+ * helmet: {
1685
+ * contentSecurityPolicy: {
1686
+ * directives: {
1687
+ * defaultSrc: ["'self'"],
1688
+ * scriptSrc: ["'self'", "'unsafe-inline'"]
1689
+ * }
1690
+ * },
1691
+ * hsts: { maxAge: 31536000 }
1692
+ * }
1693
+ * ```
1694
+ */
1695
+ interface HelmetConfig {
1696
+ /** Content Security Policy configuration */
1697
+ contentSecurityPolicy?: {
1698
+ directives: {
1699
+ defaultSrc?: string[];
1700
+ scriptSrc?: string[];
1701
+ };
1702
+ };
1703
+ /** HTTP Strict Transport Security configuration */
1704
+ hsts?: {
1705
+ maxAge: number;
1706
+ };
1707
+ }
1708
+ /**
1709
+ * XSS Protection Configuration
1710
+ *
1711
+ * Protects against Cross-Site Scripting attacks by sanitizing input.
1712
+ * Can be enabled/disabled or configured with custom sanitization rules.
1713
+ *
1714
+ * @example Enable with defaults:
1715
+ * ```typescript
1716
+ * xss: true
1717
+ * ```
1718
+ *
1719
+ * @example Custom configuration:
1720
+ * ```typescript
1721
+ * xss: {
1722
+ * blockOnDetection: true,
1723
+ * customPatterns: [/custom-pattern/g],
1724
+ * whitelist: { a: ['href', 'title'] }
1725
+ * }
1726
+ * ```
1727
+ */
1728
+ interface XSSConfig {
1729
+ /** Block requests on XSS detection */
1730
+ blockOnDetection?: boolean;
1731
+ /** Custom XSS patterns to detect */
1732
+ customPatterns?: RegExp[];
1733
+ /** Whitelist of allowed tags and attributes */
1734
+ whitelist?: {
1735
+ [tag: string]: string[];
1736
+ };
1737
+ }
1738
+ /**
1739
+ * SQL Injection Protection Configuration
1740
+ *
1741
+ * Detects and prevents SQL injection attacks in request data.
1742
+ * Can be enabled/disabled or configured with custom detection rules.
1743
+ *
1744
+ * @example Enable with defaults:
1745
+ * ```typescript
1746
+ * sqlInjection: true
1747
+ * ```
1748
+ *
1749
+ * @example Custom configuration:
1750
+ * ```typescript
1751
+ * sqlInjection: {
1752
+ * blockOnDetection: true,
1753
+ * riskThreshold: 'medium',
1754
+ * customPatterns: [/custom-sql-pattern/g]
1755
+ * }
1756
+ * ```
1757
+ */
1758
+ interface SQLInjectionConfig {
1759
+ /** Block requests on SQL injection detection */
1760
+ blockOnDetection?: boolean;
1761
+ /** Risk threshold for SQL injection detection */
1762
+ riskThreshold?: "low" | "medium" | "high";
1763
+ /** Custom SQL injection patterns to detect */
1764
+ customPatterns?: RegExp[];
1765
+ /** Enable contextual analysis to reduce false positives */
1766
+ contextualAnalysis?: boolean;
1767
+ /** Strict mode - more aggressive detection */
1768
+ strictMode?: boolean;
1769
+ /** Log detected attempts */
1770
+ logAttempts?: boolean;
1771
+ /** False positive threshold (0-1) */
1772
+ falsePositiveThreshold?: number;
1773
+ }
1774
+ /**
1775
+ * Path Traversal Protection Configuration
1776
+ *
1777
+ * Detects and prevents directory traversal attacks while allowing legitimate file paths.
1778
+ *
1779
+ * @example Enable with defaults:
1780
+ * ```typescript
1781
+ * pathTraversal: true
1782
+ * ```
1783
+ *
1784
+ * @example Custom configuration:
1785
+ * ```typescript
1786
+ * pathTraversal: {
1787
+ * blockOnDetection: true,
1788
+ * allowedPaths: ['/uploads/', '/public/'],
1789
+ * allowedExtensions: ['.jpg', '.png', '.pdf'],
1790
+ * maxDepth: 3
1791
+ * }
1792
+ * ```
1793
+ */
1794
+ interface PathTraversalConfig {
1795
+ /** Block requests on path traversal detection */
1796
+ blockOnDetection?: boolean;
1797
+ /** Allowed base paths */
1798
+ allowedPaths?: string[];
1799
+ /** Allowed file extensions */
1800
+ allowedExtensions?: string[];
1801
+ /** Maximum allowed path depth */
1802
+ maxDepth?: number;
1803
+ /** Strict mode */
1804
+ strictMode?: boolean;
1805
+ /** Log detected attempts */
1806
+ logAttempts?: boolean;
1807
+ /** False positive threshold (0-1) */
1808
+ falsePositiveThreshold?: number;
1809
+ }
1810
+ /**
1811
+ * Command Injection Protection Configuration
1812
+ *
1813
+ * Detects and prevents OS command injection attacks with context awareness.
1814
+ *
1815
+ * @example Enable with defaults:
1816
+ * ```typescript
1817
+ * commandInjection: true
1818
+ * ```
1819
+ *
1820
+ * @example Custom configuration:
1821
+ * ```typescript
1822
+ * commandInjection: {
1823
+ * blockOnDetection: true,
1824
+ * contextualAnalysis: true,
1825
+ * allowedCommands: ['git', 'npm']
1826
+ * }
1827
+ * ```
1828
+ */
1829
+ interface CommandInjectionConfig {
1830
+ /** Block requests on command injection detection */
1831
+ blockOnDetection?: boolean;
1832
+ /** Enable contextual analysis */
1833
+ contextualAnalysis?: boolean;
1834
+ /** Allowed commands (whitelist) */
1835
+ allowedCommands?: string[];
1836
+ /** Strict mode */
1837
+ strictMode?: boolean;
1838
+ /** Log detected attempts */
1839
+ logAttempts?: boolean;
1840
+ /** False positive threshold (0-1) */
1841
+ falsePositiveThreshold?: number;
1842
+ }
1843
+ /**
1844
+ * XXE (XML External Entity) Protection Configuration
1845
+ *
1846
+ * Prevents XXE attacks in XML parsing.
1847
+ *
1848
+ * @example Enable with defaults:
1849
+ * ```typescript
1850
+ * xxe: true
1851
+ * ```
1852
+ *
1853
+ * @example Custom configuration:
1854
+ * ```typescript
1855
+ * xxe: {
1856
+ * blockOnDetection: true,
1857
+ * allowDTD: false,
1858
+ * allowExternalEntities: false
1859
+ * }
1860
+ * ```
1861
+ */
1862
+ interface XXEConfig {
1863
+ /** Block requests on XXE detection */
1864
+ blockOnDetection?: boolean;
1865
+ /** Allow DTD declarations */
1866
+ allowDTD?: boolean;
1867
+ /** Allow external entities */
1868
+ allowExternalEntities?: boolean;
1869
+ /** Maximum entity expansions */
1870
+ maxEntityExpansions?: number;
1871
+ /** Strict mode */
1872
+ strictMode?: boolean;
1873
+ /** Log detected attempts */
1874
+ logAttempts?: boolean;
1875
+ }
1876
+ /**
1877
+ * LDAP Injection Protection Configuration
1878
+ *
1879
+ * Detects and prevents LDAP injection attacks.
1880
+ *
1881
+ * @example Enable with defaults:
1882
+ * ```typescript
1883
+ * ldapInjection: true
1884
+ * ```
1885
+ *
1886
+ * @example Custom configuration:
1887
+ * ```typescript
1888
+ * ldapInjection: {
1889
+ * blockOnDetection: true,
1890
+ * strictMode: true
1891
+ * }
1892
+ * ```
1893
+ */
1894
+ interface LDAPInjectionConfig {
1895
+ /** Block requests on LDAP injection detection */
1896
+ blockOnDetection?: boolean;
1897
+ /** Strict mode */
1898
+ strictMode?: boolean;
1899
+ /** Log detected attempts */
1900
+ logAttempts?: boolean;
1901
+ /** False positive threshold (0-1) */
1902
+ falsePositiveThreshold?: number;
1903
+ }
1904
+ /**
1905
+ * Compression Configuration
1906
+ *
1907
+ * Response compression to reduce bandwidth and improve performance.
1908
+ * Can be enabled/disabled or configured with custom compression settings.
1909
+ *
1910
+ * @example Enable with defaults:
1911
+ * ```typescript
1912
+ * compression: true
1913
+ * ```
1914
+ *
1915
+ * @example Custom compression:
1916
+ * ```typescript
1917
+ * compression: {
1918
+ * level: 6, // compression level (1-9)
1919
+ * threshold: 1024, // minimum response size to compress
1920
+ * filter: (req, res) => {
1921
+ * // custom filter logic
1922
+ * return /json|text|javascript|css/.test(res.get('Content-Type'));
1923
+ * }
1924
+ * }
1925
+ * ```
1926
+ */
1927
+ interface CompressionConfig$1 {
1928
+ /** Compression level (1-9) */
1929
+ level?: number;
1930
+ /** Minimum response size to compress (in bytes) */
1931
+ threshold?: number;
1932
+ /** Custom filter function for compression */
1933
+ filter?: (req: any, res: any) => boolean;
1934
+ }
1935
+ /**
1936
+ * HTTP Parameter Pollution Protection Configuration
1937
+ *
1938
+ * Prevents HTTP Parameter Pollution attacks by handling duplicate parameters.
1939
+ * Can be enabled/disabled or configured with custom parameter handling.
1940
+ *
1941
+ * @example Enable with defaults:
1942
+ * ```typescript
1943
+ * hpp: true
1944
+ * ```
1945
+ *
1946
+ * @example Custom configuration:
1947
+ * ```typescript
1948
+ * hpp: {
1949
+ * whitelist: ['tags', 'categories'], // allow arrays for these params
1950
+ * checkQuery: true,
1951
+ * checkBody: true
1952
+ * }
1953
+ * ```
1954
+ */
1955
+ interface HPPConfig {
1956
+ /** Whitelist of allowed parameters for arrays */
1957
+ whitelist?: string[];
1958
+ /** Check query parameters for duplicates */
1959
+ checkQuery?: boolean;
1960
+ /** Check body parameters for duplicates */
1961
+ checkBody?: boolean;
1962
+ }
1963
+ /**
1964
+ * MongoDB Injection Protection Configuration
1965
+ *
1966
+ * Sanitizes MongoDB queries to prevent NoSQL injection attacks.
1967
+ * Can be enabled/disabled or configured with custom sanitization rules.
1968
+ *
1969
+ * @example Enable with defaults:
1970
+ * ```typescript
1971
+ * mongoSanitize: true
1972
+ * ```
1973
+ *
1974
+ * @example Custom configuration:
1975
+ * ```typescript
1976
+ * mongoSanitize: {
1977
+ * replaceWith: '_',
1978
+ * onSanitize: ({ req, key }) => {
1979
+ * console.warn(`Sanitized MongoDB key: ${key} from ${req.ip}`);
1980
+ * }
1981
+ * }
1982
+ * ```
1983
+ */
1984
+ interface MongoSanitizeConfig {
1985
+ /** Replacement character for sanitized keys */
1986
+ replaceWith?: string;
1987
+ /** Custom callback function for sanitization */
1988
+ onSanitize?: (options: {
1989
+ req: any;
1990
+ key: string;
1991
+ }) => void;
1992
+ }
1993
+ /**
1994
+ * Request Logging Configuration
1995
+ *
1996
+ * HTTP request logging using Morgan middleware.
1997
+ * Can be enabled/disabled or configured with custom logging formats.
1998
+ *
1999
+ * @example Enable with defaults:
2000
+ * ```typescript
2001
+ * morgan: true
2002
+ * ```
2003
+ *
2004
+ * @example Custom logging format:
2005
+ * ```typescript
2006
+ * morgan: {
2007
+ * format: 'combined',
2008
+ * skip: (req, res) => res.statusCode < 400,
2009
+ * stream: customStream
2010
+ * }
2011
+ * ```
2012
+ */
2013
+ interface MorganConfig {
2014
+ /** Logging format for Morgan */
2015
+ format?: string;
2016
+ /** Custom skip function for logging */
2017
+ skip?: (req: any, res: any) => boolean;
2018
+ /** Custom stream for logging output */
2019
+ stream?: any;
2020
+ }
2021
+ /**
2022
+ * Slow Down Configuration
2023
+ *
2024
+ * Progressive delays for rate limiting to prevent abuse.
2025
+ * Can be enabled/disabled or configured with custom delay patterns.
2026
+ *
2027
+ * @example Enable with defaults:
2028
+ * ```typescript
2029
+ * slowDown: true
2030
+ * ```
2031
+ *
2032
+ * @example Custom slow down:
2033
+ * ```typescript
2034
+ * slowDown: {
2035
+ * windowMs: 15 * 60 * 1000, // 15 minutes
2036
+ * delayAfter: 100, // delay after 100 requests
2037
+ * delayMs: (used, req) => {
2038
+ * const delayAfter = req.slowDown?.limit || 100;
2039
+ * return (used - delayAfter) * 500; // 500ms per request over limit
2040
+ * }
2041
+ * }
2042
+ * ```
2043
+ */
2044
+ interface SlowDownConfig {
2045
+ /** Time window for slow down (in milliseconds) */
2046
+ windowMs?: number;
2047
+ /** Number of requests before delay starts */
2048
+ delayAfter?: number;
2049
+ /** Custom delay function */
2050
+ delayMs?: (used: number, req: any) => number;
2051
+ }
2052
+ /**
2053
+ * Security Configuration Interface
2054
+ *
2055
+ * Defines comprehensive security settings for XyPriss applications.
2056
+ * Each security feature can be enabled/disabled or configured with detailed options.
1640
2057
  *
1641
2058
  * @example
1642
2059
  * ```typescript
1643
2060
  * const securityConfig: SecurityConfig = {
1644
2061
  * level: 'enhanced',
1645
- * csrf: true,
1646
- * helmet: true,
1647
- * xss: true,
1648
- * sqlInjection: true,
1649
- * bruteForce: true,
1650
- * encryption: {
1651
- * algorithm: 'aes-256-gcm',
1652
- * keySize: 256
1653
- * },
1654
- * authentication: {
1655
- * jwt: {
1656
- * secret: 'your-secret-key',
1657
- * expiresIn: '24h',
1658
- * algorithm: 'HS256'
2062
+ * helmet: {
2063
+ * contentSecurityPolicy: {
2064
+ * directives: { defaultSrc: ["'self'"] }
1659
2065
  * }
2066
+ * },
2067
+ * cors: {
2068
+ * origin: 'https://myapp.com',
2069
+ * credentials: true
2070
+ * },
2071
+ * bruteForce: {
2072
+ * windowMs: 15 * 60 * 1000,
2073
+ * max: 100
1660
2074
  * }
1661
2075
  * };
1662
2076
  * ```
@@ -1664,16 +2078,366 @@ type SecurityLevel = "basic" | "enhanced" | "maximum";
1664
2078
  interface SecurityConfig {
1665
2079
  /** Security level preset */
1666
2080
  level?: SecurityLevel;
1667
- /** Enable CSRF protection */
1668
- csrf?: boolean;
1669
- /** Enable Helmet.js security headers */
1670
- helmet?: boolean;
1671
- /** Enable XSS protection */
1672
- xss?: boolean;
1673
- /** Enable SQL injection protection */
1674
- sqlInjection?: boolean;
1675
- /** Enable brute force protection */
1676
- bruteForce?: boolean;
2081
+ /**
2082
+ * CSRF Protection Configuration
2083
+ *
2084
+ * Protects against Cross-Site Request Forgery attacks by requiring tokens.
2085
+ * Can be enabled/disabled or configured with custom options.
2086
+ *
2087
+ * @example Enable with defaults:
2088
+ * ```typescript
2089
+ * csrf: true
2090
+ * ```
2091
+ *
2092
+ * @example Disable:
2093
+ * ```typescript
2094
+ * csrf: false
2095
+ * ```
2096
+ *
2097
+ * @example Custom configuration:
2098
+ * ```typescript
2099
+ * csrf: {
2100
+ * cookieName: '__Host-csrf-token',
2101
+ * cookieOptions: {
2102
+ * httpOnly: true,
2103
+ * sameSite: 'strict',
2104
+ * secure: process.env.NODE_ENV === 'production'
2105
+ * }
2106
+ * }
2107
+ * ```
2108
+ */
2109
+ csrf?: boolean | CSRFConfig;
2110
+ /**
2111
+ * Helmet Security Headers Configuration
2112
+ *
2113
+ * Sets various HTTP headers to help protect against common attacks.
2114
+ * Can be enabled/disabled or configured with custom header options.
2115
+ *
2116
+ * @example Enable with defaults:
2117
+ * ```typescript
2118
+ * helmet: true
2119
+ * ```
2120
+ *
2121
+ * @example Custom CSP:
2122
+ * ```typescript
2123
+ * helmet: {
2124
+ * contentSecurityPolicy: {
2125
+ * directives: {
2126
+ * defaultSrc: ["'self'"],
2127
+ * scriptSrc: ["'self'", "'unsafe-inline'"]
2128
+ * }
2129
+ * },
2130
+ * hsts: { maxAge: 31536000 }
2131
+ * }
2132
+ * ```
2133
+ */
2134
+ helmet?: boolean | HelmetConfig;
2135
+ /**
2136
+ * XSS Protection Configuration
2137
+ *
2138
+ * Protects against Cross-Site Scripting attacks by sanitizing input.
2139
+ * Can be enabled/disabled or configured with custom sanitization rules.
2140
+ *
2141
+ * @example Enable with defaults:
2142
+ * ```typescript
2143
+ * xss: true
2144
+ * ```
2145
+ *
2146
+ * @example Custom configuration:
2147
+ * ```typescript
2148
+ * xss: {
2149
+ * blockOnDetection: true,
2150
+ * customPatterns: [/custom-pattern/g],
2151
+ * whitelist: { a: ['href', 'title'] }
2152
+ * }
2153
+ * ```
2154
+ */
2155
+ xss?: boolean | XSSConfig;
2156
+ /**
2157
+ * SQL Injection Protection Configuration
2158
+ *
2159
+ * Detects and prevents SQL injection attacks in request data.
2160
+ * Can be enabled/disabled or configured with custom detection rules.
2161
+ *
2162
+ * @example Enable with defaults:
2163
+ * ```typescript
2164
+ * sqlInjection: true
2165
+ * ```
2166
+ *
2167
+ * @example Custom configuration:
2168
+ * ```typescript
2169
+ * sqlInjection: {
2170
+ * blockOnDetection: true,
2171
+ * riskThreshold: 'medium',
2172
+ * customPatterns: [/custom-sql-pattern/g]
2173
+ * }
2174
+ * ```
2175
+ */
2176
+ sqlInjection?: boolean | SQLInjectionConfig;
2177
+ /**
2178
+ * Path Traversal Protection Configuration
2179
+ *
2180
+ * Detects and prevents directory traversal attacks while allowing legitimate file paths.
2181
+ * Can be enabled/disabled or configured with custom detection rules.
2182
+ *
2183
+ * @example Enable with defaults:
2184
+ * ```typescript
2185
+ * pathTraversal: true
2186
+ * ```
2187
+ *
2188
+ * @example Custom configuration:
2189
+ * ```typescript
2190
+ * pathTraversal: {
2191
+ * blockOnDetection: true,
2192
+ * allowedPaths: ['/uploads/', '/public/'],
2193
+ * allowedExtensions: ['.jpg', '.png', '.pdf'],
2194
+ * maxDepth: 3
2195
+ * }
2196
+ * ```
2197
+ */
2198
+ pathTraversal?: boolean | PathTraversalConfig;
2199
+ /**
2200
+ * Command Injection Protection Configuration
2201
+ *
2202
+ * Detects and prevents OS command injection attacks with context awareness.
2203
+ * Can be enabled/disabled or configured with custom detection rules.
2204
+ *
2205
+ * @example Enable with defaults:
2206
+ * ```typescript
2207
+ * commandInjection: true
2208
+ * ```
2209
+ *
2210
+ * @example Custom configuration:
2211
+ * ```typescript
2212
+ * commandInjection: {
2213
+ * blockOnDetection: true,
2214
+ * contextualAnalysis: true,
2215
+ * allowedCommands: ['git', 'npm']
2216
+ * }
2217
+ * ```
2218
+ */
2219
+ commandInjection?: boolean | CommandInjectionConfig;
2220
+ /**
2221
+ * XXE (XML External Entity) Protection Configuration
2222
+ *
2223
+ * Prevents XXE attacks in XML parsing.
2224
+ * Can be enabled/disabled or configured with custom detection rules.
2225
+ *
2226
+ * @example Enable with defaults:
2227
+ * ```typescript
2228
+ * xxe: true
2229
+ * ```
2230
+ *
2231
+ * @example Custom configuration:
2232
+ * ```typescript
2233
+ * xxe: {
2234
+ * blockOnDetection: true,
2235
+ * allowDTD: false,
2236
+ * allowExternalEntities: false
2237
+ * }
2238
+ * ```
2239
+ */
2240
+ xxe?: boolean | XXEConfig;
2241
+ /**
2242
+ * LDAP Injection Protection Configuration
2243
+ *
2244
+ * Detects and prevents LDAP injection attacks.
2245
+ * Can be enabled/disabled or configured with custom detection rules.
2246
+ *
2247
+ * @example Enable with defaults:
2248
+ * ```typescript
2249
+ * ldapInjection: true
2250
+ * ```
2251
+ *
2252
+ * @example Custom configuration:
2253
+ * ```typescript
2254
+ * ldapInjection: {
2255
+ * blockOnDetection: true,
2256
+ * strictMode: true
2257
+ * }
2258
+ * ```
2259
+ */
2260
+ ldapInjection?: boolean | LDAPInjectionConfig;
2261
+ /**
2262
+ * Brute Force Protection Configuration
2263
+ *
2264
+ * Specialized protection against brute force attacks on authentication endpoints.
2265
+ * More aggressive than general rate limiting, designed for login/password attempts.
2266
+ * Can be enabled/disabled or configured with custom protection rules.
2267
+ *
2268
+ * @example Enable with defaults:
2269
+ * ```typescript
2270
+ * bruteForce: true
2271
+ * ```
2272
+ *
2273
+ * @example Custom brute force protection:
2274
+ * ```typescript
2275
+ * bruteForce: {
2276
+ * windowMs: 15 * 60 * 1000, // 15 minutes
2277
+ * max: 5, // only 5 attempts per window (stricter than rateLimit)
2278
+ * message: 'Too many login attempts, account temporarily locked.',
2279
+ * standardHeaders: true
2280
+ * }
2281
+ * ```
2282
+ */
2283
+ bruteForce?: boolean | RateLimitConfig$1;
2284
+ /**
2285
+ * Rate Limiting Configuration
2286
+ *
2287
+ * General rate limiting to prevent abuse and control request frequency.
2288
+ * Can be enabled/disabled or configured with custom rate limiting rules.
2289
+ *
2290
+ * @example Enable with defaults:
2291
+ * ```typescript
2292
+ * rateLimit: true
2293
+ * ```
2294
+ *
2295
+ * @example Custom rate limiting:
2296
+ * ```typescript
2297
+ * rateLimit: {
2298
+ * windowMs: 15 * 60 * 1000, // 15 minutes
2299
+ * max: 100, // limit each IP to 100 requests per windowMs
2300
+ * message: 'Too many requests, please try again later.',
2301
+ * standardHeaders: true
2302
+ * }
2303
+ * ```
2304
+ */
2305
+ rateLimit?: boolean | RateLimitConfig$1;
2306
+ /**
2307
+ * CORS Configuration
2308
+ *
2309
+ * Cross-Origin Resource Sharing settings for API access control.
2310
+ * Can be enabled/disabled or configured with custom CORS policies.
2311
+ *
2312
+ * @example Enable with defaults:
2313
+ * ```typescript
2314
+ * cors: true
2315
+ * ```
2316
+ *
2317
+ * @example Custom CORS policy:
2318
+ * ```typescript
2319
+ * cors: {
2320
+ * origin: ['https://myapp.com', 'https://admin.myapp.com'],
2321
+ * methods: ['GET', 'POST', 'PUT', 'DELETE'],
2322
+ * allowedHeaders: ['Content-Type', 'Authorization'],
2323
+ * credentials: true,
2324
+ * maxAge: 86400
2325
+ * }
2326
+ * ```
2327
+ */
2328
+ cors?: boolean | CORSConfig;
2329
+ /**
2330
+ * Compression Configuration
2331
+ *
2332
+ * Response compression to reduce bandwidth and improve performance.
2333
+ * Can be enabled/disabled or configured with custom compression settings.
2334
+ *
2335
+ * @example Enable with defaults:
2336
+ * ```typescript
2337
+ * compression: true
2338
+ * ```
2339
+ *
2340
+ * @example Custom compression:
2341
+ * ```typescript
2342
+ * compression: {
2343
+ * level: 6, // compression level (1-9)
2344
+ * threshold: 1024, // minimum response size to compress
2345
+ * filter: (req, res) => {
2346
+ * // custom filter logic
2347
+ * return /json|text|javascript|css/.test(res.get('Content-Type'));
2348
+ * }
2349
+ * }
2350
+ * ```
2351
+ */
2352
+ compression?: boolean | CompressionConfig$1;
2353
+ /**
2354
+ * HTTP Parameter Pollution Protection Configuration
2355
+ *
2356
+ * Prevents HTTP Parameter Pollution attacks by handling duplicate parameters.
2357
+ * Can be enabled/disabled or configured with custom parameter handling.
2358
+ *
2359
+ * @example Enable with defaults:
2360
+ * ```typescript
2361
+ * hpp: true
2362
+ * ```
2363
+ *
2364
+ * @example Custom configuration:
2365
+ * ```typescript
2366
+ * hpp: {
2367
+ * whitelist: ['tags', 'categories'], // allow arrays for these params
2368
+ * checkQuery: true,
2369
+ * checkBody: true
2370
+ * }
2371
+ * ```
2372
+ */
2373
+ hpp?: boolean | HPPConfig;
2374
+ /**
2375
+ * MongoDB Injection Protection Configuration
2376
+ *
2377
+ * Sanitizes MongoDB queries to prevent NoSQL injection attacks.
2378
+ * Can be enabled/disabled or configured with custom sanitization rules.
2379
+ *
2380
+ * @example Enable with defaults:
2381
+ * ```typescript
2382
+ * mongoSanitize: true
2383
+ * ```
2384
+ *
2385
+ * @example Custom configuration:
2386
+ * ```typescript
2387
+ * mongoSanitize: {
2388
+ * replaceWith: '_',
2389
+ * onSanitize: ({ req, key }) => {
2390
+ * console.warn(`Sanitized MongoDB key: ${key} from ${req.ip}`);
2391
+ * }
2392
+ * }
2393
+ * ```
2394
+ */
2395
+ mongoSanitize?: boolean | MongoSanitizeConfig;
2396
+ /**
2397
+ * Request Logging Configuration
2398
+ *
2399
+ * HTTP request logging using Morgan middleware.
2400
+ * Can be enabled/disabled or configured with custom logging formats.
2401
+ *
2402
+ * @example Enable with defaults:
2403
+ * ```typescript
2404
+ * morgan: true
2405
+ * ```
2406
+ *
2407
+ * @example Custom logging format:
2408
+ * ```typescript
2409
+ * morgan: {
2410
+ * format: 'combined',
2411
+ * skip: (req, res) => res.statusCode < 400,
2412
+ * stream: customStream
2413
+ * }
2414
+ * ```
2415
+ */
2416
+ morgan?: boolean | MorganConfig;
2417
+ /**
2418
+ * Slow Down Configuration
2419
+ *
2420
+ * Progressive delays for rate limiting to prevent abuse.
2421
+ * Can be enabled/disabled or configured with custom delay patterns.
2422
+ *
2423
+ * @example Enable with defaults:
2424
+ * ```typescript
2425
+ * slowDown: true
2426
+ * ```
2427
+ *
2428
+ * @example Custom slow down:
2429
+ * ```typescript
2430
+ * slowDown: {
2431
+ * windowMs: 15 * 60 * 1000, // 15 minutes
2432
+ * delayAfter: 100, // delay after 100 requests
2433
+ * delayMs: (used, req) => {
2434
+ * const delayAfter = req.slowDown?.limit || 100;
2435
+ * return (used - delayAfter) * 500; // 500ms per request over limit
2436
+ * }
2437
+ * }
2438
+ * ```
2439
+ */
2440
+ slowDown?: boolean | SlowDownConfig;
1677
2441
  /** Encryption configuration */
1678
2442
  encryption?: EncryptionConfig;
1679
2443
  /** Authentication configuration */
@@ -1830,6 +2594,79 @@ interface SessionCookieConfig {
1830
2594
  /** SameSite cookie attribute */
1831
2595
  sameSite?: boolean | "lax" | "strict" | "none";
1832
2596
  }
2597
+ /**
2598
+ * CORS (Cross-Origin Resource Sharing) configuration interface.
2599
+ *
2600
+ * Configuration for CORS policies including allowed origins,
2601
+ * methods, and headers.
2602
+ *
2603
+ * By default, all headers are allowed to be developer-friendly.
2604
+ * You can restrict headers by specifying the allowedHeaders array.
2605
+ *
2606
+ * @interface CORSConfig
2607
+ *
2608
+ * @example
2609
+ * ```typescript
2610
+ * // Allow all headers (default - developer-friendly)
2611
+ * const corsConfig: CORSConfig = {
2612
+ * origin: '*',
2613
+ * methods: ['GET', 'POST', 'PUT', 'DELETE'],
2614
+ * credentials: true
2615
+ * };
2616
+ *
2617
+ * // Restrict specific headers (production)
2618
+ * const restrictiveCorsConfig: CORSConfig = {
2619
+ * origin: ['https://example.com', 'https://app.example.com'],
2620
+ * methods: ['GET', 'POST', 'PUT', 'DELETE'],
2621
+ * allowedHeaders: ['Content-Type', 'Authorization'],
2622
+ * credentials: true
2623
+ * };
2624
+ * ```
2625
+ */
2626
+ interface CORSConfig {
2627
+ /** Allowed origins - can be string, array of strings, or boolean */
2628
+ origin?: string | string[] | boolean;
2629
+ /** Allowed HTTP methods */
2630
+ methods?: string[];
2631
+ /**
2632
+ * Allowed headers - if not specified, all headers are allowed by default.
2633
+ * Specify this array to restrict which headers are allowed.
2634
+ */
2635
+ allowedHeaders?: string[];
2636
+ /** Allow credentials in CORS requests */
2637
+ credentials?: boolean;
2638
+ }
2639
+ /**
2640
+ * Rate limiting configuration interface.
2641
+ *
2642
+ * Configuration for rate limiting including time windows,
2643
+ * request limits, and custom messages.
2644
+ *
2645
+ * @interface RateLimitConfig
2646
+ *
2647
+ * @example
2648
+ * ```typescript
2649
+ * const rateLimitConfig: RateLimitConfig = {
2650
+ * windowMs: 900000, // 15 minutes
2651
+ * max: 100, // 100 requests per window
2652
+ * message: 'Too many requests, please try again later',
2653
+ * standardHeaders: true,
2654
+ * legacyHeaders: false
2655
+ * };
2656
+ * ```
2657
+ */
2658
+ interface RateLimitConfig$1 {
2659
+ /** Time window in milliseconds */
2660
+ windowMs?: number;
2661
+ /** Maximum requests per window */
2662
+ max?: number;
2663
+ /** Message to send when limit is exceeded */
2664
+ message?: string;
2665
+ /** Include standard rate limit headers */
2666
+ standardHeaders?: boolean;
2667
+ /** Include legacy rate limit headers */
2668
+ legacyHeaders?: boolean;
2669
+ }
1833
2670
  /**
1834
2671
  * Route-specific security configuration interface.
1835
2672
  *
@@ -3417,7 +4254,7 @@ declare class ConsoleInterceptor {
3417
4254
 
3418
4255
  /**
3419
4256
  * XyPriss Security Middleware
3420
- * Comprehensive security middleware using proven external libraries
4257
+ * Comprehensive security middleware using BuiltInMiddleware as single source of truth
3421
4258
  */
3422
4259
 
3423
4260
  /**
@@ -3426,25 +4263,44 @@ declare class ConsoleInterceptor {
3426
4263
  */
3427
4264
  declare class SecurityMiddleware implements Required<SecurityConfig> {
3428
4265
  level: SecurityLevel;
3429
- csrf: boolean;
3430
- helmet: boolean;
3431
- xss: boolean;
3432
- sqlInjection: boolean;
3433
- bruteForce: boolean;
4266
+ csrf: boolean | CSRFConfig;
4267
+ helmet: boolean | HelmetConfig;
4268
+ xss: boolean | XSSConfig;
4269
+ sqlInjection: boolean | SQLInjectionConfig;
4270
+ pathTraversal: boolean | PathTraversalConfig;
4271
+ commandInjection: boolean | CommandInjectionConfig;
4272
+ xxe: boolean | XXEConfig;
4273
+ ldapInjection: boolean | LDAPInjectionConfig;
4274
+ bruteForce: boolean | RateLimitConfig$1;
4275
+ rateLimit: boolean | RateLimitConfig$1;
4276
+ cors: boolean | CORSConfig;
4277
+ compression: boolean | CompressionConfig$1;
4278
+ hpp: boolean | HPPConfig;
4279
+ mongoSanitize: boolean | MongoSanitizeConfig;
4280
+ morgan: boolean | MorganConfig;
4281
+ slowDown: boolean | SlowDownConfig;
3434
4282
  encryption: Required<SecurityConfig>["encryption"];
3435
4283
  authentication: Required<SecurityConfig>["authentication"];
3436
4284
  private helmetMiddleware;
3437
4285
  private corsMiddleware;
3438
4286
  private rateLimitMiddleware;
4287
+ private bruteForceMiddleware;
3439
4288
  private csrfMiddleware;
3440
4289
  private mongoSanitizeMiddleware;
3441
4290
  private hppMiddleware;
3442
4291
  private compressionMiddleware;
4292
+ private morganMiddleware;
4293
+ private slowDownMiddleware;
3443
4294
  private sqlInjectionDetector;
4295
+ private pathTraversalDetector;
4296
+ private commandInjectionDetector;
4297
+ private xxeProtector;
4298
+ private ldapInjectionDetector;
3444
4299
  private logger;
3445
4300
  constructor(config?: SecurityConfig, logger?: Logger);
3446
4301
  /**
3447
- * Initialize all security middleware instances using external libraries
4302
+ * Initialize all security middleware instances using BuiltInMiddleware
4303
+ * BuiltInMiddleware is the single source of truth for all middleware wrappers
3448
4304
  */
3449
4305
  private initializeMiddleware;
3450
4306
  /**
@@ -3464,10 +4320,6 @@ declare class SecurityMiddleware implements Required<SecurityConfig> {
3464
4320
  * Custom XSS protection middleware
3465
4321
  */
3466
4322
  private xssProtection;
3467
- /**
3468
- * Make request properties writable to avoid readonly property errors
3469
- */
3470
- private makeRequestPropertiesWritable;
3471
4323
  /**
3472
4324
  * Recursively sanitize object properties
3473
4325
  */
@@ -4111,6 +4963,10 @@ interface XyPrissMiddlewareAPI {
4111
4963
  priority?: MiddlewarePriority;
4112
4964
  routes?: string[];
4113
4965
  }): XyPrissMiddlewareAPI;
4966
+ /**
4967
+ * Initialize default middleware with security configuration
4968
+ */
4969
+ initializeWithConfig(securityConfig?: SecurityConfig): void;
4114
4970
  /**
4115
4971
  * Configure security middleware bundle
4116
4972
  */