Introducing a new way to review your PRs.

Read the announcement

bunkerity / bunkerweb

🛡️ Open-source and cloud-native Web Application Firewall (WAF)

View on GitHub

19 documents · Updated Nov 23, 2026

docs / runtime-security-engine

Runtime security engine

The BunkerWeb runtime is an NGINX gateway that receives HTTP and stream traffic, applies shared security and traffic rules, and forwards allowed requests to upstream services. Generated NGINX configuration defines the traffic shape, while Lua code and core plugins make request-time decisions and share state across workers.

Runtime boundary

The runtime has two related paths: HTTP for web requests and stream for connection-level proxying. Both paths load the same core and extension model, but each has its own NGINX context and shared state; this lets security policy cover both web and non-HTTP traffic without treating them as the same protocol. src/common/confs/nginx.conf src/common/confs/http.conf src/common/confs/stream.conf

The gateway loads NGINX modules for Lua, ModSecurity, headers, compression, and stream handling. Its HTTP and stream blocks then include base settings, core and plugin settings, and custom settings, so generated policy becomes part of one running NGINX process. src/common/confs/nginx.conf src/common/confs/nginx.conf

Runtime lifecycle

The runtime turns a configuration generation into an active worker set. The entrypoint prepares persistent data and temporary startup values, asks the generator to install configuration when needed, starts NGINX, and coordinates later reloads so an incomplete generation cannot replace a working one. src/…/entrypoint.sh src/…/entrypoint.sh src/…/entrypoint.sh

This diagram shows the main path from configuration input to protected proxy traffic.

flowchart LR
    A[Service settings] --> B[Generated NGINX files]
    B --> C[NGINX master and workers]
    C --> D[Lua and core policy]
    D --> E{Request allowed?}
    E -->|yes| F[Proxy to upstream]
    E -->|no| G[Security response]

Reloads use a marker and shared lock with the configuration installer. NGINX validates the new generation before it replaces workers; if the generation or reload fails, the current configuration stays in service. src/…/entrypoint.sh src/…/entrypoint.sh src/common/confs/init-lua.conf

References: src/…/entrypoint.sh• L42-58 src/…/entrypoint.sh• L106-193 src/…/init-lua.conf• L17-47

Lua policy layer

Lua is the runtime policy layer inside NGINX. At startup it discovers core and installed plugins, loads the generated variables, orders plugin actions by phase, and stores that shared model for workers and requests. src/common/confs/init-lua.conf

The shared plugin base gives each policy module one view of its settings, request context, local state, cluster state, and cache state. A plugin can therefore use the same contract during startup, worker work, timers, and request phases while keeping request data in the current NGINX context. src/bw/lua/bunkerweb/plugin.lua

Workers run the ordered worker hooks after startup. They also run recurring plugin work and publish readiness under a lock, which makes startup tasks run once for the active worker generation and lets the runtime release the reload marker only after the new generation is ready. src/common/confs/init-worker-lua.conf src/common/confs/init-worker-lua.conf

References: src/…/init-lua.conf• L51-116 src/…/plugin.lua• L26-101 src/…/init-worker-lua.conf• L25-71

Request protection

Core plugins attach security behavior to NGINX phases and can return a decision before proxying. The request context carries shared facts across those phases, so checks such as HTTPS redirects, allow lists, sessions, bot controls, and other policy modules can cooperate instead of making separate decisions. src/common/core/ssl/ssl.lua src/common/core/whitelist/confs/default-server-http/whitelist.conf src/bw/lua/bunkerweb/ctx.lua

ModSecurity adds a WAF layer to the HTTP path. Its service settings choose whether to inspect and block traffic, whether to use the OWASP Core Rule Set and its plugins, and how to record audit data. The settings also bound request-body inspection, so WAF coverage and large-upload behavior remain part of the generated service policy. src/…/plugin.json src/…/plugin.json src/…/plugin.json

The request path is intentionally split between generated NGINX directives and Lua decisions. NGINX handles routing, protocol settings, proxy limits, headers, caching, and upstream transport; Lua supplies dynamic checks and state. See Configuration generation for how the directives are produced and Request security processing for the request phase flow.

References: src/…/ssl.lua• L10-46 src/…/whitelist.conf• L1-77 src/…/ctx.lua• L44-97 src/…/plugin.json• L2-24

Proxy boundary

The reverse proxy plugin maps configured locations to upstream targets. For HTTP it creates locations that pass requests after policy checks and preserves the headers needed to describe the original client and request; for stream traffic it maps a connection to an upstream and can use the PROXY protocol. src/common/core/reverseproxy/confs/server-http/reverse-proxy.conf src/common/core/reverseproxy/confs/server-stream/reverse-proxy.conf

The proxy boundary also carries upstream safety and reliability choices. It can control buffering, timeouts, WebSocket transport, upstream TLS verification, client certificates, authentication subrequests, error handling, and response caching. These choices are part of the generated service policy rather than separate request code. src/…/plugin.json src/…/plugin.json

References: src/…/reverse-proxy.conf• L128-211 src/…/reverse-proxy.conf• L42-49 src/…/plugin.json• L7-25

Shared runtime state

The runtime uses NGINX shared dictionaries and optional Redis-backed stores to coordinate data between workers and, when enabled, between instances. The stores support plugin metadata, request caches, bans, sessions, metrics, locks, and other state needed by security decisions. src/common/confs/http.conf src/common/confs/stream.conf src/bw/lua/bunkerweb/plugin.lua

Redis can run as a single service, through Sentinel, or as a cluster. The cluster store selects the mode from generated settings, keeps HTTP and stream lock state separate, and treats short-lived slot movement errors as retryable. This gives shared security state a common contract while allowing the runtime to handle cluster changes without failing every operation at once. src/bw/lua/bunkerweb/clusterstore.lua src/bw/lua/bunkerweb/clusterstore.lua src/bw/lua/bunkerweb/clusterstore.lua

Sessions show how this state model affects a security feature. The runtime can keep session data in signed cookies or Redis, switch to cookie storage when Redis is unavailable, and use shared revocation state so a destroyed cookie session does not remain valid. src/common/core/sessions/sessions.lua src/common/core/sessions/sessions.lua

References: src/…/http.conf• L61-69 src/…/stream.conf• L30-37 src/…/plugin.lua• L47-68 src/…/clusterstore.lua• L37-107 src/…/clusterstore.lua• L364-399 src/…/sessions.lua• L103-202

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the HTTP and stream Lua shared-dictionary names aligned with the stores selected by the plugin base.
  • Keep plugin discovery, variable loading, and plugin ordering compatible with the generated variables.env contract.
  • Keep reload markers and configuration-install locks coordinated so workers never serve a mixed configuration generation.
  • Keep reverse proxy header rules consistent with the request context fields used by security plugins.
  • Keep upstream TLS file paths and scheduler-delivered certificate data aligned with the generated proxy directives.
  • Keep ModSecurity settings, generated WAF directives, CRS assets, and audit paths aligned when changing WAF controls.
  • Keep Redis cluster routing and retry behavior aligned with the shared lock dictionaries used by each NGINX subsystem.

Important failure modes

TriggerConsequenceGuard
A plugin or its settings cannot load during NGINX initThe new worker generation is rejected and the old one remains activeInit aborts before shared state is swapped.
Configuration installation overlaps with a reloadNGINX could read files from different generationsThe installer and reload path use lock and marker coordination.
Redis becomes unavailable during session useSessions fall back to signed cookie storage and may change behaviorThe session timer checks Redis and records the active storage mode.
A Redis Cluster slot moves during a multi-key commandThe command can fail with a transient TRYAGAIN responseCluster calls retry short-lived slot-movement errors when the runtime can yield.
WAF request-body limits are lower than the traffic needsModSecurity rejects a request or inspects only part of its bodyAlign the WAF body limit and action with the NGINX client-size limit.
Upstream verification is enabled without a trusted certificateUpstream TLS verification is disabled for that generated proxy pathThe proxy template checks for the cached trust file before enabling verification.

Key files

FileWhy to read it
src/common/confs/nginx.confShows the top-level NGINX runtime boundary and module loading.
src/…/entrypoint.shShows startup, configuration installation, reload safety, and process lifetime.
src/common/confs/http.confShows the HTTP policy context, shared state, and generated server inclusion.
src/common/confs/stream.confShows the stream policy context and generated stream server inclusion.
src/common/confs/init-lua.confShows plugin discovery and configuration acceptance during reload.
src/common/confs/init-worker-lua.confShows worker readiness, recurring policy work, and reload completion.
src/bw/lua/bunkerweb/plugin.luaShows the common contract between plugins and runtime state.
src/bw/lua/bunkerweb/clusterstore.luaShows shared Redis modes, cluster locks, and transient error handling.
src/…/plugin.jsonShows the WAF and CRS controls exposed to generated service policy.
src/common/core/reverseproxy/confs/server-http/reverse-proxy.confShows the HTTP proxy boundary between protected traffic and upstreams.
src/common/core/reverseproxy/confs/server-stream/reverse-proxy.confShows the stream proxy boundary.
src/common/core/sessions/sessions.luaIllustrates shared state and failure handling in a security feature.