API

Varnish WAF works on the backend side of Varnish. Anytime that Varnish makes a request to a backend (misses or passes), it is scanned by Varnish WAF. The API for Varnish WAF is to be used in the vcl that includes waf.vcl. The API is as follows:

Functions

All functions are prefixed with varnish_waf, except for version() which is prefixed just by waf.

VOID .add_files(STRING path)

  • Description

    Add a configuration file containing the list of rule sets. This is called in vcl_init.

  • Arguments

    • path: Path to the configuration file. This can be a glob.
  • Return Value

    None.

VOID .add_file_remote(STRING url, STRING key)

  • Description

    Add a configuration file from a remote server. This is called in vcl_init.

  • Arguments

    • url: URL to the rule set.
    • key: The key passed to the server stored in the header ModSec-key. Defaults to an empty string.
  • Return Value

    None.

VOID .skip_rule_by_id(STRING id)

  • Description

    Skip a rule by its ID. If called in vcl_init, it is for the entire VCL lifetime. To skip rules for a single request, use the waf-skip-id header.

  • Arguments

    • id: ID of the rule to be skipped, or multiple comma-separated IDs.
  • Return Value

    None.

VOID .skip_rule_by_tag(STRING tag)

  • Description

    Skip a rule by its tag. If called in vcl_init, it is for the entire VCL lifetime. To skip rules for a single request, use the waf-skip-tag header.

  • Arguments

    • tag: Tag of the rule to be skipped, or multiple comma-separated tags.
  • Return Value

    None.

STRING .detection_mode()

  • Description

    Return the detection mode of the WAF engine for the current transaction, using the same spelling as the SecRuleEngine directive. A ctl:ruleEngine action that changed the mode during the transaction is taken into account. This can be used to annotate logs depending on whether the WAF is blocking or only observing. It is called on the backend side, for example in vcl_backend_response.

  • Return Value

    One of:

    • On: the engine is enabled and disruptive actions block.
    • DetectionOnly: rules are evaluated and logged, but never disrupt the transaction. A rule set that does not set SecRuleEngine also reports DetectionOnly.
    • Off: the engine is disabled and no rules are evaluated. This is also returned when there is no WAF transaction for the request, for example when waf-skip is true, so it is safe to call unconditionally.
  • Example

    sub vcl_backend_response {
    	std.log("waf-mode: " + varnish_waf.detection_mode());
    }
    

VOID .log_matches(BOOL enable)

  • Description

    Enable or disable logging of non-disruptive rule matches for the current transaction. waf.vcl calls it when the waf-log-matches header is set, which is the way to enable it per request.

  • Arguments

    • enable: Whether to log matches. Defaults to true.
  • Return Value

    None.

STRING version()

  • Description

    Return the version of the Varnish WAF VMOD and ModSecurity.

  • Return Value

    A string in the format VMOD-VERSION_MODSECURITY-VERSION, for example 6.0.0_3.0.17.100. The VMOD version is not the same as the version of the Varnish WAF package.

Headers

Varnish WAF makes use of headers to store certain information to allow the user configuration options. All of the headers should be set in vcl_recv.

The waf-skip, waf-skip-id, waf-skip-tag and waf-log-matches headers are controlled by your VCL, not by clients. waf.vcl removes any values a client sends for them in its vcl_recv, which runs before yours, so set them in your own vcl_recv. Values for waf-skip-id, waf-skip-tag and waf-log-matches set before a restart are kept.

waf-skip

  • Description

    Should ModSecurity be skipped for this request?

  • Possible Values

    If set to true, the entire request not be parsed by ModSecurity. If set to request, the request body and headers will not be parsed while the response body and headers will be parsed. If set to response, the request body and headers will be parsed while the response body and headers will not be parsed.

  • Default Value

    Undefined.

waf-skip-id

  • Description

    Rules to skip for this request, by ID.

  • Possible Values

    A rule ID, or multiple comma-separated IDs.

  • Default Value

    Undefined.

waf-skip-tag

  • Description

    Rules to skip for this request, by tag.

  • Possible Values

    A rule tag, or multiple comma-separated tags.

  • Default Value

    Undefined.

waf-log-matches

  • Description

    Log non-disruptive rule matches for this request. Each rule that matches and logs, but does not disrupt the transaction, is written to the VSL under the WAF tag with a MATCH: prefix. This is the way to see what the WAF detects while running with SecRuleEngine DetectionOnly, and it also shows detection and scoring rules, like the CRS anomaly scoring rules, with SecRuleEngine On. It is off by default because a single request can match many rules, and setting it per request lets you enable it for a sample of the traffic.

  • Possible Values

    If set to any value, matches are logged.

  • Default Value

    Undefined, matches are not logged.

  • Example

    sub vcl_recv {
    	set req.http.waf-log-matches = "1";
    }
    

waf-client-ip

  • Description

    Store the IP address of the client.

  • Possible Values

    The IP address of the client.

  • Default Value

    If the X-Forward-For header is present, the first IP address is used. Otherwise, the IP is set to client.ip.

waf-client-port

  • Description

    Hold the port number of the client.

  • Possible Values

    The port number of the client.

  • Default Value

    If the X-Forward-For header is present, the header is set to 0. Otherwise, the port is set to std.port(client.ip).

Options

Options are set using the waf_opts object.

cache_req_body_bytes

  • Description

    A string of the maximum size of the request body to cache. This is set in vcl_init.

  • Possible Values

    The format is a number proceed by a unit like B, MB, KB, …, etc. (i.e 5MB).

  • Default Value

    1MB.

  • Example

    	sub vcl_init {
    		waf_opts.set("cache_req_body_bytes", "5MB");
    	}
    

®Varnish Software, Wallingatan 12, 111 60 Stockholm, Organization nr. 556805-6203