Skip to content

SecurityConfig

SecurityConfig is the central configuration object for all @guardcore adapters. It is validated at startup using a Zod schema. All fields have sensible defaults — you only need to specify what you want to change.

import { SecurityConfigSchema } from '@guardcore/core';
import type { SecurityConfig } from '@guardcore/core';
const config: SecurityConfig = {
rateLimit: 100,
rateLimitWindow: 60,
};
const resolved = SecurityConfigSchema.parse(config);
FieldTypeDefaultDescription
trustedProxiesstring[][]IP addresses or CIDR ranges of trusted reverse proxies
trustedProxyDepthnumber1How many X-Forwarded-For hops to trust
trustXForwardedProtobooleanfalseTrust X-Forwarded-Proto header for HTTPS detection
passiveModebooleanfalseLog violations without blocking requests
FieldTypeDefaultDescription
whiteliststring[] | nullnullIP/CIDR allowlist. When set, only these IPs are allowed
blackliststring[][]IP/CIDR blocklist. These IPs are always blocked
whitelistCountriesstring[][]Two-letter country codes to allow (requires geoIpHandler or geoResolver)
blockedCountriesstring[][]Two-letter country codes to block (requires geoIpHandler or geoResolver)
FieldTypeDefaultDescription
rateLimitnumber10Max requests per window per IP
rateLimitWindownumber60Window duration in seconds
enableRateLimitingbooleantrueEnable/disable rate limiting globally
endpointRateLimitsRecord<string, [number, number]>{}Per-endpoint overrides: { "/api/search": [5, 60] }
FieldTypeDefaultDescription
enablePenetrationDetectionbooleantrueEnable the 75-pattern detection engine
detectionCompilerTimeoutnumber2.0Regex compilation timeout in seconds (0.1-10)
detectionMaxContentLengthnumber10000Max content length for scanning (1000-100000)
detectionPreserveAttackPatternsbooleantruePreserve attack regions when truncating long content
detectionSemanticThresholdnumber0.7Semantic analysis threat score threshold (0-1)
detectionAnomalyThresholdnumber3.0Z-score threshold for performance anomaly detection (1-10)
detectionSlowPatternThresholdnumber0.1Seconds before a pattern is considered slow (0.01-1)
detectionMonitorHistorySizenumber1000Number of recent metrics to keep (100-10000)
detectionMaxTrackedPatternsnumber1000Max patterns to track in performance monitor (100-5000)
FieldTypeDefaultDescription
enableIpBanningbooleantrueEnable automatic IP banning
autoBanThresholdnumber10Suspicious requests before auto-ban
autoBanDurationnumber3600Ban duration in seconds
FieldTypeDefaultDescription
blockedUserAgentsstring[][]Substring patterns to match against User-Agent header
FieldTypeDefaultDescription
securityHeadersobject | nullSee Security HeadersSecurity headers configuration object
enforceHttpsbooleanfalseRedirect HTTP requests to HTTPS
FieldTypeDefaultDescription
enableCorsbooleanfalseEnable CORS handling
corsAllowOriginsstring[]['*']Allowed origins
corsAllowMethodsstring[]['GET','POST','PUT','PATCH','DELETE','OPTIONS']Allowed methods
corsAllowHeadersstring[]['*']Allowed headers
corsAllowCredentialsbooleanfalseAllow credentials
corsExposeHeadersstring[][]Headers exposed to the browser
corsMaxAgenumber600Preflight cache duration in seconds
FieldTypeDefaultDescription
blockCloudProviders('AWS' | 'GCP' | 'Azure')[][]Cloud providers to block
cloudIpRefreshIntervalnumber3600Seconds between cloud IP range refreshes (60-86400)
FieldTypeDefaultDescription
geoIpHandlerGeoIPHandlerundefinedGeoIP handler instance (e.g., IPInfoManager)
geoResolver(ip: string) => string | nullundefinedSimple function returning 2-letter country code
FieldTypeDefaultDescription
enableAgentbooleanfalseEnable telemetry agent
agentApiKeystring | nullnullAPI key (required when enableAgent is true)
agentEndpointstring'https://api.fastapi-guard.com'Agent endpoint URL
agentProjectIdstring | nullnullProject identifier
agentBufferSizenumber100Events buffered before flush
agentFlushIntervalnumber30Flush interval in seconds
agentEnableEventsbooleantrueSend security events
agentEnableMetricsbooleantrueSend performance metrics
agentTimeoutnumber30Request timeout in seconds
agentRetryAttemptsnumber3Retry count on failure
FieldTypeDefaultDescription
enableDynamicRulesbooleanfalseEnable agent-driven rule updates (requires enableAgent)
dynamicRuleIntervalnumber300Polling interval in seconds
FieldTypeDefaultDescription
enableRedisbooleantrueEnable Redis for distributed state
redisUrlstring'redis://localhost:6379'Redis connection URL
redisPrefixstring'guard_core:'Prefix for all Redis keys
FieldTypeDefaultDescription
loggerLoggerdefaultLoggerCustom logger instance
customLogFilestring | nullnullPath to a log file
logSuspiciousLevelstring | null'WARNING'Log level for suspicious requests
logRequestLevelstring | nullnullLog level for all requests (null = disabled)
logFormat'text' | 'json''text'Log output format
FieldTypeDefaultDescription
emergencyModebooleanfalseBlock all traffic except whitelisted IPs
emergencyWhiteliststring[][]IPs allowed during emergency mode
FieldTypeDefaultDescription
excludePathsstring[][]URL paths to skip all security checks
customErrorResponsesRecord<number, string>{}Custom error messages by status code
customRequestCheck(req: GuardRequest) => Promise<GuardResponse | null>undefinedCustom check function run at the end of the pipeline
customResponseModifier(res: GuardResponse) => Promise<GuardResponse>undefinedModify responses before sending

The schema enforces these constraints:

  • agentApiKey is required when enableAgent is true
  • enableAgent must be true when enableDynamicRules is true
  • geoIpHandler or geoResolver is required when using blockedCountries or whitelistCountries
import type { SecurityConfig } from '@guardcore/core';
const config: SecurityConfig = {
trustedProxies: ['10.0.0.0/8'],
trustedProxyDepth: 2,
trustXForwardedProto: true,
enableRedis: true,
redisUrl: process.env.REDIS_URL ?? 'redis://localhost:6379',
rateLimit: 100,
rateLimitWindow: 60,
endpointRateLimits: {
'/api/login': [5, 300],
'/api/search': [20, 60],
},
enablePenetrationDetection: true,
detectionSemanticThreshold: 0.7,
autoBanThreshold: 5,
autoBanDuration: 7200,
blockedUserAgents: ['sqlmap', 'nikto', 'nmap', 'masscan'],
enforceHttps: true,
enableCors: true,
corsAllowOrigins: ['https://app.example.com'],
corsAllowCredentials: true,
excludePaths: ['/health', '/metrics'],
customErrorResponses: {
403: 'Access denied',
429: 'Too many requests, slow down',
},
};