Secure Controller

Last updated: 2026-10-22

SecureController extends BaseController and pins the medium security-header tier on every controller method, regardless of whatever the application-wide default is configured to. Controllers should extend SecureController when handling sensitive operations or user data, so the headers they get are guaranteed rather than dependent on app-wide configuration.

Using SecureController

Extend SecureController in your controller classes to automatically apply MediumSecurityHeaderPolicy security headers to all responses:

<?php
namespace App\Controllers;

use Quellabs\Canvas\Controllers\SecureController;
use Quellabs\Canvas\Annotations\Route;
use Symfony\Component\HttpFoundation\Response;

class AdminController extends SecureController {

    /**
     * @Route("/admin/dashboard")
     */
    public function dashboard(): Response {
        // All security headers are automatically applied
        return $this->render('admin/dashboard.tpl');
    }
}

Automatic Security Headers

The following headers are automatically added to all responses from a controller extending SecureController:

HeaderValueProtection
X-Frame-OptionsSAMEORIGINPrevents clickjacking by disallowing framing from external sites
X-Content-Type-OptionsnosniffPrevents MIME-type sniffing attacks
X-XSS-Protection1; mode=blockEnables browser XSS filtering and blocks detected attacks
Strict-Transport-Securitymax-age=63072000; includeSubDomains
(HTTPS only)
Forces HTTPS connections for 2 years, including all subdomains
Referrer-Policystrict-originControls referrer information sent with requests
Content-Security-Policydefault-src 'self'Restricts resource loading to the same origin

Policy Tiers

MediumSecurityHeaderPolicy is one of four built-in classes in Quellabs\Canvas\Security\Policies that implement SecurityHeaderPolicyInterface. For the full list of tiers, how the app-wide default is resolved from config/app.php, and how that default interacts with per-route overrides, see Security Overview.

Customizing Security Headers

Pass a different policyClass to @InterceptWith to use a different tier for one controller or method:

<?php
namespace App\Controllers;

use Quellabs\Canvas\Controllers\BaseController;
use Quellabs\Canvas\Annotations\InterceptWith;
use Quellabs\Canvas\Security\Policies\StrictSecurityHeaderPolicy;
use Quellabs\Canvas\Security\SecurityHeadersAspect;
use Quellabs\Canvas\Annotations\Route;
use Symfony\Component\HttpFoundation\Response;

/**
 * @InterceptWith(SecurityHeadersAspect::class, policyClass=StrictSecurityHeaderPolicy::class)
 */
class AdminController extends BaseController {

    /**
     * @Route("/admin/dashboard")
     */
    public function dashboard(): Response {
        // Uses StrictSecurityHeaderPolicy instead of this app's default
        return $this->render('admin/dashboard.tpl');
    }
}

policyClass also accepts an ordered array of classes — this is how a route adds CORS on top of a header tier. See Security Overview for how ordering and precedence work across an array:

<?php
namespace App\Controllers;

use Quellabs\Canvas\Controllers\BaseController;
use Quellabs\Canvas\Annotations\InterceptWith;
use Quellabs\Canvas\Security\Policies\MediumSecurityHeaderPolicy;
use Quellabs\Canvas\Security\Policies\CorsSecurityHeaderPolicy;
use Quellabs\Canvas\Security\SecurityHeadersAspect;
use Quellabs\Canvas\Annotations\Route;
use Symfony\Component\HttpFoundation\Response;

/**
 * @InterceptWith(SecurityHeadersAspect::class, policyClass={MediumSecurityHeaderPolicy::class, CorsSecurityHeaderPolicy::class})
 */
class ApiController extends BaseController {

    /**
     * @Route("/api/data")
     */
    public function getData(): Response {
        // Gets MediumSecurityHeaderPolicy's headers plus CORS approval headers
        // for whatever config/cors.php allows
        return $this->json(['data' => 'sensitive']);
    }
}

An array override replaces the entire policy list, so a class or method that extends SecureController but also declares its own policyClass array no longer gets MediumSecurityHeaderPolicy automatically — list it explicitly if the route still needs it. At that point there's nothing left for extending SecureController to add, so a controller that specifies its own full list, as above, extends BaseController directly instead.

For per-method customization, apply the annotation to individual methods:

<?php
namespace App\Controllers;

use Quellabs\Canvas\Controllers\SecureController;
use Quellabs\Canvas\Annotations\InterceptWith;
use Quellabs\Canvas\Security\Policies\StrictSecurityHeaderPolicy;
use Quellabs\Canvas\Security\SecurityHeadersAspect;
use Quellabs\Canvas\Annotations\Route;
use Symfony\Component\HttpFoundation\Response;

class ApiController extends SecureController {

    /**
     * @Route("/api/data")
     * @InterceptWith(SecurityHeadersAspect::class, policyClass=StrictSecurityHeaderPolicy::class)
     */
    public function getData(): Response {
        // This method uses the strict tier instead of SecureController's medium default
        return $this->json(['data' => 'sensitive']);
    }
}

For headers none of the three built-in tiers cover, implement SecurityHeaderPolicyInterface yourself and point policyClass at it:

<?php
namespace App\Security;

use Quellabs\Canvas\Security\SecurityHeaderPolicyInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class CompanyHeaderPolicy implements SecurityHeaderPolicyInterface {
    public function apply(Request $request, Response $response): void {
        if (!$response->headers->has('X-Frame-Options')) {
            $response->headers->set('X-Frame-Options', 'DENY');
        }

        if (!$response->headers->has('Content-Security-Policy')) {
            $response->headers->set('Content-Security-Policy', "default-src 'self'; script-src 'self' 'unsafe-inline'");
        }
    }
}

Inherited Helper Methods

SecureController extends BaseController, providing access to all convenience methods:

MethodDescriptionExample
render($template, $data, $status)Render a template and return Response$this->render('admin/users.tpl', ['users' => $users])
json($data, $status)Return JSON response$this->json(['success' => true])
text($text, $status)Return plain text response$this->text('Operation complete')
redirect($url, $status)Redirect to another URL$this->redirect('/admin/login')
notFound($message, $status)Return 404 response$this->notFound('Resource not found')
forbidden($message, $status)Return 403 response$this->forbidden('Insufficient permissions')
em()Get EntityManager instance$this->em()->findAll(User::class)
view()Get template engine instance$this->view()->render('partial.tpl')
service($className)Get any service from container$this->service(CacheInterface::class)