Skip to main content
Version: Fegg 5.x

Security

What Fegg does for you, and what to set per project

Most protections are on by default. This page lists them and the settings that change them.

Secrets and environments​

  • Passwords, API keys and anything else that differs per server are not committed: they go in site/config/env.php (in .gitignore) or in environment variables, read with feggEnv('NAME', $default). See Settings.
  • A site without env.php runs as production. Test servers matched by site/config/env/paths.php use the committed site/config/env/staging.php, which holds only test values. Environments are chosen by the server's path, never by the request's host name, which the client controls.
  • Error details are shown only in developer mode: a developer_ip address and FEGG_ENV other than production.

Security headers​

Every response (pages, redirects, 404, error pages, the CMS) gets these headers. The bootstrap sends them before the controller runs.

HeaderDefaultSetting in site/config/settings.php
X-Content-Type-Optionsnosniff$settings['security_headers']
Referrer-Policystrict-origin-when-cross-origin$settings['security_headers']
X-Frame-OptionsSAMEORIGIN (the CMS image and file pickers are iframes)$settings['security_headers']
Strict-Transport-Securitynot sent$settings['hsts'] (sent on HTTPS responses only)
Content-Security-Policynot sent$settings['csp']; $settings['csp_report_only'] = true sends it as Content-Security-Policy-Report-Only
htdocs/_fegg/site/config/settings.php
// Change or remove a default ('' removes it), or add another header
$settings['security_headers'] = array(
'Referrer-Policy' => 'same-origin',
'Permissions-Policy' => 'camera=(), microphone=()',
);

// Only once the whole site redirects to HTTPS (and every subdomain, with includeSubDomains)
$settings['hsts'] = 'max-age=31536000';

// Try a policy in report-only mode first
$settings['csp'] = "default-src 'self'; img-src 'self' data:; script-src 'self' https://www.google.com https://www.gstatic.com";
$settings['csp_report_only'] = true;
  • Turn HSTS on only when every page is served over HTTPS: browsers then refuse HTTP for that domain for max-age seconds.
  • A CSP must list inline scripts and every external service the site uses (analytics, reCAPTCHA, Turnstile, YouTube ...). Watch the browser console in report-only mode before you enforce it.
  • A page that other sites may show in a frame (an embeddable widget) calls $this->allowFraming() before displayPage(). It removes X-Frame-Options for that response only.

Sessions and cookies​

HTTPS​

feggIsHttps() decides whether a request came over HTTPS (the constant FEGG_CURRENT_PROTOCOL, used by {{ base_url }} and the Secure cookies). It looks at:

  • HTTPS set to anything but off, REQUEST_SCHEME, port 443 (HTTPS on the server itself);
  • the headers of a proxy, load balancer or CDN that terminates TLS: X-Forwarded-Proto, CloudFront-Forwarded-Proto, X-Forwarded-Ssl, X-SSL-Request, Front-End-Https, Forwarded: proto=https.

Behind a proxy that sends none of these, configure it to send one (usually X-Forwarded-Proto). redirect() within the site does not depend on it: it sends a path, and the browser keeps the scheme of the page.

Forms​

  • Output escaping: {{ $x }} escapes HTML. In URLs and JavaScript use feggUrl and feggJs; ./fegg-cli template:lint reports outputs without them. See Template security.
  • Double submission: public forms use tickets (setTicket() / useTicket()). The skeleton's contact form revokes the ticket when a confirmation fails, and sends the auto-reply to exactly one address.
  • CSRF in the CMS: every POST must carry the session's token, and every action that changes data calls requirePost(). See Controller.
  • Actions: the framework's methods (setSession(), mail() ...) are never reachable from a URL. See Which methods are actions.
  • Database: values always go through ? placeholders. Table, column and order names are checked by Fegg\Model and must never come from user input.
  • Spam: the contact form supports reCAPTCHA and Turnstile. When the service cannot be reached the check fails, it is never skipped (see Captcha).
  • Repeated sending: the skeleton's contact form sends at most 5 mails an hour from one address (rate_limit_per_hour). Limit other forms the same way with Fegg\Tool\RateLimit.

CMS login​

  • Every login attempt is recorded before the password is checked. After 5 failed logins for one login ID from one address, or 20 from one address, logins from that address are refused for 15 minutes. The attempts are counted by Fegg\Tool\RateLimit in the core table rate_limit: run fegg-cli migration after updating the core. Without the table the login works unthrottled and a warning is logged.
  • /cms/login/auth is POST only. Deleted users (valid = 0) cannot log in. Every CMS request checks that the session's user still exists in cms_user, so deleting a user ends their sessions at once. Old password hashes are upgraded at login. An unknown login ID takes as long to check as a wrong password.
  • Logout is a POST with the CSRF token, so another site cannot log a user out (see Controller).
  • Users are created and their passwords changed with fegg-cli user:create / user:password, which never take the password on the command line. /cms/register/ needs a CMS login and developer mode.
  • Basic authentication compares the user and password with hash_equals(). With basic_auth_scope = 1 it is checked before the controller's __init() and action, so redirects, downloads and direct output are protected too.

Uploads​

Uploads are checked by extension and content and saved under generated names in htdocs/_fegg/site/up_dir/, whose .htaccess serves only the file types that can be uploaded and runs no script (fegg-cli doctor reports a missing one; on nginx, see Directory structure). See Upload.

Protecting _fegg/​

htdocs/_fegg/ is always inside the document root. It is protected by .htaccess files (only the CSS, JS, images and fonts of _fegg/assets/ and of the site folders, and the uploads of _fegg/site/up_dir/, are served), and every PHP file starts with defined('FEGG_CORE_DIR') || exit;. Check a live site with ./fegg-cli doctor https://example.com after every FTP upload: it also warns about outdated .htaccess rules. See Directory structure and fegg-cli.