inkphp GitHub 中文

Security

What the kernel does

What pages do

ConcernHow
Output escapingevery variable <?= e($x) ?>
SQL injectiononly q/one/all/val with placeholders
CSRFcsrf_field() in forms, csrf_check() before writes
MethodsPOST pages declare allow('GET', 'POST'); GET never changes data
Loginrequire_login(); check ownership in SQL (AND owner_id = ?)
Brute forcerate_limit('login:' . ip_key(), 5, 300), plus a per-account limit
Redirectsredirect() accepts local paths only
Uploadsstore_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);