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 / plugin-system

Plugin system

The plugin system lets BunkerWeb add security features, configuration, background work, and UI features as separate extensions. A plugin declares its identity and settings, enters the generated runtime configuration, and then runs in the request or UI lifecycle that matches its role. See the extension ecosystem page for the wider extension model. The runtime security engine page explains how security behavior uses this shared runtime.

Plugin model

Plugin roles

Plugins share one identity model but serve different runtime paths. Core plugins provide built-in product features. External and Pro plugins extend the same runtime from separate plugin sources. UI plugins can add pages and request-time UI behavior without needing to change the built-in UI.

The runtime treats plugin source as part of the trust and replacement order:

SourceMain roleUI precedence
CoreBuilt-in runtime featuresLowest
ExternalInstalled customer or community extensionsAbove core
ProLicensed extensionsAbove external
UI pluginA plugin with UI contentUses its source priority

References: src/…/Configurator.py• L101-115 src/…/main.py• L258-271 src/…/main.py• L553-568

Plugin metadata

The metadata file is the contract that makes a plugin discoverable and configurable. Each plugin identifies itself with an id, display name, description, and version. Its stream value states whether it supports stream traffic. Its settings map declares the values that the configuration system exposes to operators.

Each setting describes its scope, default, user-facing text, input shape, and validation rule. A global setting applies to the installation. A multisite setting can have a value for each configured site. A setting can also declare repeated values through the multiple convention.

References: src/…/Configurator.py• L36-49 src/…/Configurator.py• L219-244 src/…/plugin.json• L1-17 src/…/plugin.json• L1-25

Configuration contract

Configuration sources

The configuration system combines the main settings with settings declared by core, external, and Pro plugins. It accepts only known setting names and maps site-prefixed values to the site scope when multisite mode is active. This makes a plugin setting part of the same operator-facing configuration model as a built-in setting.

The plugin code reads the resulting values through the shared runtime context. A missing value means that the generated configuration and the plugin metadata are out of sync. The plugin base reports that condition instead of treating it as a normal request value.

References: src/…/Configurator.py• L141-143 src/…/helpers.lua• L357-434 src/…/plugin.lua• L69-101

The convention also covers shared template inputs. A plugin or core feature can consume one value in several location-based templates, so common template helpers keep equivalent configuration forms consistent across reverse proxy, redirect, and gRPC behavior. The location operand helper removes one matching pair of outer quotes before the template escapes the value for the generated proxy configuration.

References: src/…/_bunkerweb_location_operand.jinja• L1-7 src/…/reverse-proxy.conf• L99-106 src/…/redirect.conf• L16-27 src/…/grpc.conf• L65-72

Configuration output

The generator turns the combined settings into the configuration used by the proxy. It renders the common templates together with plugin templates and installs the result only after a complete render. A plugin therefore affects the runtime through both its declared values and any configuration templates that consume those values.

References: src/…/main.py• L115-124 src/…/main.py• L158-186 src/…/Templator.py• L551-561

Runtime lifecycle

Plugin loading

At startup or after a configuration change, the runtime discovers plugin metadata and identifies the phases each plugin supports. It loads the matching Lua module when one exists, creates a plugin object, and calls the method for the current phase. The shared helper checks the result so each phase returns a consistent success or failure shape.

The phase order is normally defined by the built-in order and can be changed by global or site-specific plugin-order settings. An explicit order applies only when the plugin supports that phase. Unlisted plugins are added in a stable fallback order.

This diagram shows how a plugin moves from its declaration to request-time behavior.

flowchart LR
    A[plugin.json] --> B[settings and plugin discovery]
    B --> C[generated proxy configuration]
    C --> D[phase order]
    D --> E[plugin instance]
    E --> F[request or timer phase]
    F --> G[response, decision, metric, or stored state]

References: src/…/helpers.lua• L92-141 src/…/helpers.lua• L165-231 src/…/helpers.lua• L234-285

Request context

The shared plugin base gives each plugin a common identity, request context, logger, and access to the internal store, data store, cache stores, and cluster store. The same base works for HTTP and stream traffic. Request phases receive request-local state. Startup and timer work receive shared service objects without request state.

Plugins use this shared context to exchange state without creating a separate storage contract for each feature. They can also publish counters or tables under their own plugin identity for the metrics system.

References: src/…/plugin.lua• L26-68 src/…/plugin.lua• L113-184

Request effects

A plugin phase returns a structured result with a success flag and message. The surrounding runtime uses that result to continue processing, reject a request, redirect it, or expose data to the next stage. A failure to load a plugin or a missing phase method is reported as a plugin error rather than silently producing an invalid result.

The headers plugin shows the model: initialization stores site-aware data, then the header phase changes the response according to the declared settings. Other plugins use the same lifecycle to enforce access rules, create challenge responses, or add response content.

References: src/…/helpers.lua• L260-285 src/…/headers.lua• L14-77 src/…/headers.lua• L84-149

Scheduled work

Plugins can declare background jobs as well as request phases. The job declaration supplies a name, worker file, run frequency, and reload behavior. The job receives the same plugin settings contract, so its setting resolution must preserve the declared scope and defaults before it produces service or runtime state.

The CrowdSec plugin illustrates this extension path. Its metadata exposes per-site connection, decision, and AppSec controls plus global shared cache limits, while its one-time configuration job turns the per-site values into the form expected by the bouncer. This keeps operator settings in the common plugin model even when the work runs outside a request.

References: src/…/plugin.json• L7-208 src/…/crowdsec_conf_utils.py• L1-51

UI extension points

UI hooks

UI plugins extend the Flask application through functions in ui/hooks.py. Supported hooks run before a request, after a response, during request teardown, or while building template context. Two UI-specific hooks provide extra JavaScript and CSS. Their returned assets are collected into the page environment for the current request.

The UI reloads these hooks when plugin state changes. It clears old hook lists, loads active plugin hooks, removes stale paths, and removes duplicate registrations. A failing before-request hook blocks the request because it may enforce access control. Other hook failures are logged and do not replace the main response flow.

References: src/…/main.py• L131-164 src/…/main.py• L426-551 src/…/main.py• L905-974 src/…/main.py• L1474-1482 src/…/main.py• L1560-1593

UI pages

UI plugins can provide an embedded page or a Flask blueprint with its own routes and templates. The UI selects the page from the installed plugin tree when possible and falls back to the stored plugin data when needed. A blueprint-only plugin sends the user to its dedicated route instead of rendering an empty embedded page.

When two plugins expose the same blueprint name, source priority selects the active one. Higher-priority UI content and templates can replace lower-priority content. This allows an external or Pro extension to extend a core UI surface without changing the core page.

References: src/…/main.py• L280-318 src/…/main.py• L553-665 src/…/plugins.py• L547-639

UI plugin lifecycle

The API accepts UI plugin archives, validates their metadata, extracts safe paths, and stores a normalized archive with a checksum. The UI later synchronizes stored plugin data to the external or Pro plugin directory. It skips unchanged content and removes plugin directories that are no longer registered.

This lifecycle separates installation from loading. An upload can succeed without changing the running UI until the reload path applies the stored plugin and refreshes hooks and blueprints.

References: src/…/plugins.py• L111-137 src/…/plugins.py• L140-227 src/…/dependencies.py• L82-179

Extension examples

Security integration settings

The CrowdSec plugin shows how a shared plugin contract can cover both simple feature switches and service-wide integration policy. Its settings separate per-site API and behavior choices from global memory limits, and its optional management credentials support investigation actions without treating the bouncer API key as an administrative credential.

References: src/…/plugin.json• L7-107 src/…/plugin.json• L136-199

Response transformation settings

The Zstandard plugin shows the smaller end of the same contract. It exposes a per-site enable switch, a MIME type list, a minimum response size, and a bounded compression level. The metadata alone makes the feature available to the shared configuration and operator UI model.

References: src/…/plugin.json• L1-66

Change checklist

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

  • Keep the metadata schema, configuration parsing, and runtime variable lookup aligned when adding or renaming a setting.
  • Keep a plugin's declared phase support aligned with its order entries and available runtime methods.
  • Keep UI hook return keys aligned with the template context keys for scripts and styles.
  • Keep plugin source priority aligned with blueprint name and template ownership when two extensions provide the same UI surface.
  • Preserve archive checks, plugin IDs, and checksums across API upload, database storage, filesystem sync, and UI reload.
  • Keep shared template helpers aligned across every feature that parses location operands so quoted and regex-based routes generate the same match.
  • Keep job setting defaults, scope resolution, and value conversion aligned with the plugin metadata when a background job consumes the setting.

Important failure modes

TriggerConsequenceGuard
Invalid or incomplete plugin.jsonThe plugin is ignored during configuration loading or retained as an invalid manual pluginMetadata validation in Configurator and invalid_plugin_json
A plugin setting is declared but absent from generated variablesThe plugin treats the feature as unavailable and logs a throttled warningRegenerate configuration after metadata or setting changes
A UI hook raises during before_requestThe request returns an internal error instead of reaching the routeKeep authorization checks small and make hook imports safe
Stored plugin data cannot be read during reloadThe old plugin tree stays in place and the reload remains pendingRetry the reload after the stored data is complete
Two UI plugins claim the same blueprint nameThe lower-priority blueprint is not registeredUse unique blueprint names or confirm the intended source priority
A location operand keeps outer quotes after template processingReverse proxy, redirect, or gRPC matching can target the wrong pathUse the shared operand helper before escaping generated configuration
A job setting uses the wrong scope or unitBackground plugin behavior differs from the operator's configured valueKeep job resolution and value conversion aligned with metadata

Key files

FileWhy to read it
src/bw/lua/bunkerweb/plugin.luaShared runtime context, stores, metrics, and plugin identity
src/bw/lua/bunkerweb/helpers.luaPlugin ordering, construction, phase calls, and setting scope mapping
src/…/Configurator.pyPlugin metadata and setting validation
src/…/main.pyConfiguration generation and safe installation boundary
src/…/main.pyUI hook loading, blueprint selection, and request integration
src/…/dependencies.pyStored plugin synchronization and reload behavior
src/…/plugins.pyUI plugin upload, listing, deletion, and archive validation
src/common/core/reverseproxy/confs/_bunkerweb_location_operand.jinjaShared normalization for location operands in generated proxy rules
src/…/plugin.jsonExample of a security integration with scoped settings and scheduled work
src/…/crowdsec_conf_utils.pyExample of background-job setting resolution and value conversion
src/…/plugin.jsonExample of a focused response transformation contract
src/…/plugin.jsonExample of a setting-rich plugin contract
src/common/core/headers/headers.luaExample of initialization, stored state, and response effects