Shield
A web application firewall that blocks the most common attacks before they reach your code.
The Shield inspects the path, URL parameters, body and headers of every request, including in encoded form, and blocks known attack signatures:
- SQL and NoSQL injection;
- XSS (script injection);
- directory traversal and file inclusion;
- command injection;
- SSRF (the server forced to call an internal address);
- Log4Shell;
- server-side template injection (SSTI);
- XXE, LDAP injection, header injection (CRLF), JavaScript prototype pollution;
- injection into mail protocols, server-side code sent in a request;
- callback addresses used to confirm a vulnerability (OAST) and attack tools recognizable by their User-Agent.
multipart/form-data forms are inspected field by field.
The problem
As soon as an application is online, it receives automated requests looking for known flaws: a badly filtered URL parameter that exposes the database (SQL injection), a field that echoes JavaScript back (XSS), a path that gives access to the server's files… A single flaw, in your code or in a dependency, is enough for one of these requests to succeed.
What the Shield stops
- automated scanners, which test thousands of sites in a row;
- targeted intrusion attempts on your forms and APIs;
- the exploitation of known flaws in your dependencies, such as Log4Shell.
When to use it
Everywhere: it is the baseline rule. The Shield does not replace secure code (parameterized SQL queries, output escaping); it adds a layer of defense in front of it.
Configuration
import { shield } from "@zarex/express"; // or your framework's package
rules: [shield()],With no option, the Shield is LIVE, every detected attack is denied, and a URL or header too long to be inspected is denied.
Example
import express from "express";
import { zarex, shield } from "@zarex/express";
const app = express();
app.use(express.json());
app.use(
zarex({
key: process.env.ZAREX_KEY!,
rules: [shield()],
})
);The body and cookies
Since 0.2.0, request bodies are inspected by default (inspectBody: false to turn it off; with Express, place the middleware after express.json()). Cookies are only sent if you enable inspectCookies, since they hold your visitors' sessions.
Options
| Option | Values | Default | Role |
|---|---|---|---|
mode | "LIVE" · "DRY_RUN" · "OFF" | "LIVE" | Deny, only observe, or disable. |
threshold | integer from 1 to 100 | none: any detection denies | Score from which a request is denied. |
oversize | "deny" · "inspect" · "allow" | path and headers: "deny"; body: "inspect" | What to do with an input too large to be inspected in full. |
mode: observe before blocking
rules: [shield({ mode: "DRY_RUN" })],LIVE: a detected attack is denied.DRY_RUN: the Shield inspects and records what it would have denied, without blocking anything. The result appears in the decision log and indecision.results(dryRun: true).OFF: the Shield is not evaluated.
On an application that is already live, start with a few days in DRY_RUN, look in the decision log at what would have been denied, then switch to LIVE.
threshold: tolerate weak signals
Each signature has a weight that reflects its certainty. A certain signature, such as UNION SELECT or a ../../etc/passwd path, weighs 100. A less certain signal weighs less: the SQL comment signature weighs 45, an always-true condition such as ' OR 1=1 weighs 85.
When several signatures fire on the same request, their weights combine: two weak signals reinforce each other, without going above 100. Two signatures weighing 45 give a score of 70. ?q=1' OR 1=1 triggers two (85 and 45) and scores 92.
rules: [shield({ threshold: 80 })],- Without
threshold: any signature that fires denies the request, even a weak one. - With
threshold: 80: a single weak signal goes through; a clear attack or several combined signals are denied. A request that goes through is still flagged inreason(below_threshold: true).
The higher the threshold, the fewer false positives, but the more attacks get through.
The threshold, as a last resort
Only raise threshold if the decision log shows wrongful denials on your own traffic (a free-text field that contains legitimate SQL, for example). Observe in DRY_RUN first.
oversize: inputs that are too large
Zarex inspects at most 8 KB of the path and of each header, and 48 KB of the body (beyond that, the beginning and the end).
| Value | Effect |
|---|---|
"deny" | Deny the request. This is the default for the path and headers: an 8 KB URL is almost always an attack. |
"inspect" | Inspect what was received and flag it. This is the default for the body: a file upload often exceeds 48 KB. |
"allow" | Let it through without a full inspection. |
rules: [shield({ oversize: "inspect" })],In every case, a warning (path_truncated, body_truncated…) is added to the decision.
What the decision contains
The Shield's result, in decision.results (ruleType: "SHIELD"), contains in reason:
| Field | Meaning |
|---|---|
clean | true: no signature found. |
pattern | The main signature, for example sqli-union or path-traversal. |
description | Its plain-language description. |
score | The combined score, from 0 to 100. |
patterns | Every signature that fired (8 at most), when there are several. |
below_threshold, threshold | Present when the score is below your threshold: the request went through. |
body_truncated | true: the body received was incomplete; "nothing found" then does not mean "everything checked". |
Example, for ?q=1 UNION SELECT password FROM users:
{ "pattern": "sqli-union", "patterns": ["sqli-union", "sqli-query"], "score": 100,
"description": "Injection SQL : UNION SELECT (y compris avec commentaires, parenthèses ou espaces exotiques)" }The description is written in French by the engine. These fields are for you, in your logs and in the decision log. To the visitor, return a generic message.