Repository navigation
Architecture
This page is a map of KeyAnyWhere's code for contributors: the layers and which may depend on which, the path of a request through the middleware, and the cross-cutting pieces (encryption, validation, audit trail, logs) you will meet in almost every change. Read it before your first non-trivial pull request.
KeyAnyWhere is a CakePHP 5 application on PHP 8.4, with PostgreSQL, server-rendered templates, Bootstrap 5.3 and small, framework-free JavaScript files in webroot/js/. There is no build or minification step: assets are served as they are, and the Bootstrap files are copied from Composer packages into webroot/ on composer install. Code and identifiers are in Portuguese, the project's working language.
Deptrac (deptrac.yaml) enforces which namespaces may use which; composer arquitetura checks it and CI fails on a new violation.
| Layer | Namespace | May use |
|---|---|---|
| Controller | App\Controller |
Middleware, Service, Table, Entity, View, Nucleo |
| Console |
App\Command, App\Console
|
Table, Entity, Nucleo |
| Middleware | App\Middleware |
Table, Entity, Nucleo |
| Service | App\Service |
Table, Entity, Nucleo |
| View / Cell | App\View |
Entity, Nucleo (cells also Table) |
| Table |
App\Model\Table, Behavior, Custom
|
Entity, Auditoria, Nucleo |
| Auditoria | App\Auditoria |
Table, Entity, Nucleo |
| Entity | App\Model\Entity |
Nucleo |
| LogBanco | App\Log\Engine |
Table, Entity, Nucleo |
| Nucleo |
App\Configuracao, Criptografia, I18n, Log
|
nothing in the app (only external packages) |
| Plugins | plugins/ |
everything in the core; the core never depends on a plugin |
A new top-level namespace under src/ needs its own layer in deptrac.yaml.
The middleware queue (Application::middleware()) runs in this order, and the order matters:
- ErrorHandler, Asset, Routing, BodyParser.
-
EmailLoginNormalizado: trims and lowercases the e-mail of the login form, like the
Userentity does on save. -
Authentication: session login; API routes added by plugins (
/api/...) authenticate by token and are never redirected to the login page. -
CSRF (
CsrfProtectionKawMiddleware): validates every form post (except API routes); on aGET, drops a CSRF cookie signed with an older salt so a new one is issued. -
HttpsEnforcer: HTTPS only and HSTS, when
debugis off. -
SecurityHeadersKaw: security headers, CSP in production,
X-Robots-Tag: noindex, nofollowalways. - SessionsKawMiddleware: stamps user id and user agent on the session row (it must run after Authentication), and purges old unauthenticated sessions.
Environment detection (Application::isTheExecutionEnvironment()) is described in Development Environment.
-
Encryption at rest (
App\Criptografia\Criptografia): libsodiumsecretboxwithKEY_CRIPTOGRAFIC, through entity mutators and*Descrip()accessors (Entrada,Categoria,User::tfa_secret). A fresh nonce per call means the same text never encrypts to the same value, soEntrada::isModified()andCategoria::isModified()decrypt and compare before the normal dirty check. -
Validation (
App\Model\Custom\ValidatorKaw): every Table'svalidationDefault()usescheckXSS(), which runsvoku/anti-xssand records anxss_detectadosecurity event (table, field and action; never the content). -
Audit trail (
App\Auditoria): audited Tablesuse AuditavelTrait;and call$this->adicionarAuditoria('<context>', mascararAlteracoes: true).AuditoriaBehaviorcomputes the changed fields onafterSave/afterDelete. An audit failure becomes a log error and never rolls back the business save. Internal saves that are not a user action pass['auditoria_acao' => false]. -
Logs (
App\Log): one JSON file per level plus thesegurancascope; security events go throughSeguranca::registrar(), andApp\Log\Engine\EventosSegurancaLogalso stores them ineventos_seguranca. A new event needs a label inEventoSeguranca::titulo(). -
Administrator-only screens: checked in
beforeFilter()with$user->root; a refused request is redirected with$event->setResult(...)and$event->stopPropagation(). Neverreturna response frombeforeFilter(). -
Flash messages: controllers pass the message untranslated; the elements in
templates/element/flash/translate and escape it, so a flash message can't carry HTML. -
Time zone and language: stored in the
configuracoestable and applied on every request byconfig/bootstrap.php(App\I18n\Regional::aplicar()).
This repository is the open-source community edition and the single source of the core. A hosted edition, maintained separately, receives this core by git merge and adds only what depends on its hosting (licensing, billing, multi-tenancy). That is why the core depends only on public packages, why composer.json and composer.lock are kept identical in both, and why extension happens through plugins rather than by editing the core.
Source code · Issues · Releases · Packagist · Online demo · Hosted edition
1. Getting started
2. Installing in production
- Overview
- Preparing the Server
- Installing KeyAnyWhere
- File Permissions
- Generating Security Keys
- Environment Variables
- Configuration Files
- Database Setup
- Insecure Password Catalog
- Web Server and HTTPS
- First-Run Setup
3. Operating your installation
4. Using KeyAnyWhere
5. Security
6. Development
7. Extending