Skip to content

Architecture

Arthu Santiago edited this page Nov 30, 2026 · 1 revision

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.

Stack

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.

Layers

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.

Life of a request

The middleware queue (Application::middleware()) runs in this order, and the order matters:

  1. ErrorHandler, Asset, Routing, BodyParser.
  2. EmailLoginNormalizado: trims and lowercases the e-mail of the login form, like the User entity does on save.
  3. Authentication: session login; API routes added by plugins (/api/...) authenticate by token and are never redirected to the login page.
  4. CSRF (CsrfProtectionKawMiddleware): validates every form post (except API routes); on a GET, drops a CSRF cookie signed with an older salt so a new one is issued.
  5. HttpsEnforcer: HTTPS only and HSTS, when debug is off.
  6. SecurityHeadersKaw: security headers, CSP in production, X-Robots-Tag: noindex, nofollow always.
  7. 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.

Cross-cutting pieces

  • Encryption at rest (App\Criptografia\Criptografia): libsodium secretbox with KEY_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, so Entrada::isModified() and Categoria::isModified() decrypt and compare before the normal dirty check.
  • Validation (App\Model\Custom\ValidatorKaw): every Table's validationDefault() uses checkXSS(), which runs voku/anti-xss and records an xss_detectado security event (table, field and action; never the content).
  • Audit trail (App\Auditoria): audited Tables use AuditavelTrait; and call $this->adicionarAuditoria('<context>', mascararAlteracoes: true). AuditoriaBehavior computes the changed fields on afterSave/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 the seguranca scope; security events go through Seguranca::registrar(), and App\Log\Engine\EventosSegurancaLog also stores them in eventos_seguranca. A new event needs a label in EventoSeguranca::titulo().
  • Administrator-only screens: checked in beforeFilter() with $user->root; a refused request is redirected with $event->setResult(...) and $event->stopPropagation(). Never return a response from beforeFilter().
  • 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 configuracoes table and applied on every request by config/bootstrap.php (App\I18n\Regional::aplicar()).

Editions

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.

Next step

Translations

Clone this wiki locally