Security
What the kernel does
- Pages live outside the docroot;
public/holds onlyindex.php, so no other.phpfile can be requested directly. - Every response has
X-Content-Type-Options,Referrer-PolicyandX-Frame-Options; noX-Powered-By. - Sessions start lazily; the cookie is HttpOnly, SameSite=Lax and Secure over HTTPS; login rotates the session id.
- In production error pages show no details; details go to
storage/logs/.
What pages do
| Concern | How |
|---|---|
| Output escaping | every variable <?= e($x) ?> |
| SQL injection | only q/one/all/val with placeholders |
| CSRF | csrf_field() in forms, csrf_check() before writes |
| Methods | POST pages declare allow('GET', 'POST'); GET never changes data |
| Login | require_login(); check ownership in SQL (AND owner_id = ?) |
| Brute force | rate_limit('login:' . ip_key(), 5, 300), plus a per-account limit |
| Redirects | redirect() accepts local paths only |
| Uploads | store_upload(): type from the content, random names, whitelisted extensions |
Login
// pages/login.php (excerpt)
$hash = (string) env('ADMIN_PASSWORD_HASH'); // made with password_hash(), kept in .env.php
if (hash_equals((string) env('ADMIN_USER'), $in['user']) && password_verify($in['password'], $hash)) {
login('admin'); // rotates the session id
redirect('/admin', 303);
}
A hidden back office
require_login(hidden: true): a visitor who is not logged in gets exactly the same 404 as for an unknown URL, with no redirect to a login page, so outsiders cannot find the admin area. Combine it with an entrance only you know (secret in .env.php):
pages/gate.php<?php // pages/gate.php → /gate/<secret>: right secret → pass cookie; wrong → an ordinary 404
[$token] = params(1);
if (!hash_equals((string) env('ADMIN_GATE'), $token)) abort(404);
setcookie('gate', hash_hmac('sha256', 'gate', (string) env('ADMIN_GATE')),
['path' => '/', 'httponly' => true, 'secure' => is_https(), 'samesite' => 'Strict', 'expires' => time() + 30 * 86400]);
redirect('/login');
// top of pages/login.php: no pass cookie → 404
if (!hash_equals(hash_hmac('sha256', 'gate', (string) env('ADMIN_GATE')), (string) ($_COOKIE['gate'] ?? ''))) abort(404);
// pages/admin*.php
$me = require_login(hidden: true);