stave.dev

Security & Logging

What core does on every request before a module runs — ban probes, resolve the real client IP, look after the session — and where it writes down what happened.

Before the module runs

session started
→
ban check
→
module check
→
disabled account logged out
→
attributes

The first is Application::__construct(); the rest are the opening lines of Application::run(), in that order (core/Application.php). Per-endpoint rules — login, permissions, allowed methods, IP restriction — are attributes and permissions, and have their own pages.

Banned requests

run() starts with banNefariousRequest(). A request whose URI contains any of three strings is one no legitimate client of a Zero site makes:

The URI contains Reason recorded
.phptrying to call some .php file.
.envtrying to call some .env file.
wp-trying to call some wordpress file.

The client's IP is blocked at Cloudflare with CloudFlare::block(), the reason and the URI go into the rule's note, and the request ends there. No module is loaded and no database is touched.

It is a substring match on the whole URI

Application::nefariousReason() uses str_contains() on REQUEST_URI, path and query string together. /blog/wp-migration-notes and /search?q=.env are banned like any probe. Keep the three strings out of your own URLs, and put your own address in allowed-ips.lst before testing this.

The block is a Cloudflare access rule, so it needs app/config/cloudflare.ini (Libraries). Without it nothing is blocked: the failure is written to the ban log and the request still ends.

allowed-ips.lst

app/config/allowed-ips.lst lists the clients that are never banned. A listed client's request carries on and is answered like any other — usually with a 404.

# one entry per line; `#` starts a comment
203.0.113.5          # a plain IP
198.51.100.0/24      # a CIDR range
2001:db8::/32        # IPv6 works the same way

The client IP

Request::clientIp() in core/Request.php is the one resolver. The ban check, the log line, #[RestrictIP] and the event log all ask it.

The peer (REMOTE_ADDR) is clientIp() returns
Not a Cloudflare edge REMOTE_ADDR. Forwarding headers are ignored.
A Cloudflare edge CF-Connecting-IP; else the first entry of X-Forwarded-For; else REMOTE_ADDR.

CF-Connecting-IP and X-Forwarded-For are ordinary request headers: anyone who can reach the origin directly can set them to anything. They are believed only when the peer that sent them is a Cloudflare edge, which Zero\Lib\Net\TrustedProxy::isCloudflare() decides against ranges compiled into the class. A site that is not behind Cloudflare simply gets REMOTE_ADDR.

use Zero\Core\Request;

$ip = Request::clientIp();            // attribute a rate limit, a log line, a ban

if (Request::clientIpIsVerified()) {
    // safe to make an access decision on $ip
}

clientIpIsVerified() is false in one case: the peer is a Cloudflare edge but only X-Forwarded-For is present. That header is a client-supplied chain, good enough to attribute a rate limit and not good enough to authorise on.

Keeping the ranges current

The ranges are constants, not a data file, so changing who is believed is a reviewed commit. core/bin/refresh-cloudflare-ips.php fetches Cloudflare's published IPv4 and IPv6 lists and reports the difference. It writes nothing.

php core/bin/refresh-cloudflare-ips.php
# refresh-cloudflare-ips: up to date (22 ranges)
Exit code Meaning
0The compiled list matches Cloudflare's.
1Drift. Each range is printed with + (Cloudflare added it) or - (Cloudflare dropped it); update core/lib/Net/TrustedProxy.php.
2A list could not be fetched.

Run from cron, a non-zero exit is what tells you.

Sessions

Application::__construct() starts the PHP session for every request, right after it turns output buffering on. A new session gets $_SESSION['created_at'].

The session id is replaced Where
At sign-in Session::establish() calls session_regenerate_id(true) before it writes the login, so an id issued before the login does not survive it.
On a timer Application::regenerateAgedSession(), at the end of defineConstants(), replaces the id of a session older than SESSION_REGENERATE_INTERVAL seconds and resets created_at.

The timer, in core/Application.php:

Disabled accounts

run() calls Session::endIfDisabled() before any module is built. When the session carries a login whose account has been disabled, the session is ended: an account an admin disables is logged out at its next request, not when its session expires.

A public method is an endpoint

The router calls whatever public method the URL names, inherited ones included, so declare a method public only when a URL may call it and make every helper protected or private. Routing has the full rule.

Logging

Zero\Core\Console (core/Console.php) appends one line per call to a log file. The level is the method name:

Index Level Call
0DEBUGConsole::debug(), Console::log()
1INFOConsole::info()
2NOTICEConsole::notice()
3WARNConsole::warn()
4ERRORConsole::error()
5CRITICALConsole::critical()
6ALERTConsole::alert()
7EMERGENCYConsole::emergency()
8BANConsole::ban()
use Zero\Core\Console;

Console::info("Import finished: $count rows");
Console::warn($payload);                              // arrays and objects go through print_r()
Console::error("Feed failed", '/var/log/myapp/feed.log'); // second argument: another file

Console::log("message", 'WARN');                      // level by name or index; DEBUG when omitted

Where it goes

Call File
Every level but ban()Console::$defaultLogFile, which is /var/log/php-fpm/zero.log
Console::ban()/var/log/php-fpm/zero-tolerance.log
Any call with a second argumentThe file that argument names

$defaultLogFile is a public static property; assign it to send the log elsewhere. The file must be writable by the PHP process. The ban log holds every Cloudflare block and unblock and every failed attempt at one.

The line

[2026-11-23 14:02:11] [203.0.113.5] [WARN]: [Application.php:517] Autoloader failed to place Zero\Module\Blgo\Model\Post

UTC time, the client IP as Request::clientIp() resolves it (cli when there is none), the level, the file and line that called Console, and the message.

Threshold

Two constants set the lowest level written to a file. With neither, everything is written.

Constant Value
ZERO_LOG_LEVEL_INTThe level's index. Checked first.
ZERO_LOG_LEVELThe level's name in capitals. A name that is not a level means DEBUG.
; WARN and above
ZERO_LOG_LEVEL=WARN

The threshold is resolved on the first call to Console and kept for the rest of the request. If anything logs before defineConstants() has read the config files, the constants are not defined yet and that request writes every level.

Messages of this request

Every message, below the threshold or not, is also kept in memory for the request. Console::getMessages() returns them and Console::clearMessages() empties the list:

foreach (Console::getMessages() as $m) {
    // $m['timestamp']  int, time()
    // $m['level']      'WARN'
    // $m['message']    string
    // $m['caller']     '/full/path/File.php:123'
}

This is what a debug toolbar plugin reads to show a page's log lines.