Himanshu SharmaSoftware engineer
Let’s talk

CodeIgniter 4 · Lesson 22 of 30

CodeIgniter security: separate escaping, validation and permission

Map bookshop risks to distinct controls for validation, authorization, CSRF, query construction, output escaping and private storage.

15 minAccess boundariesUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonMap bookshop risks to distinct controls for validation, authorization, CSRF, query construction, output escaping and private storage.

After this lesson you can identify which security boundary addresses a particular bookshop risk. Security mechanisms have different purposes. Treating every problem as “sanitize input” hides the distinction between accepting data, rendering it, constructing a query and authorizing a caller.

Start with the data journey

A title arrives in an HTTP request. Validation checks whether it satisfies the application's title policy. Authorization checks whether the caller may perform the operation. A model persists the intended field. A later view escapes the stored title for its output context. These decisions happen at different boundaries and remain necessary even when another boundary succeeds.

text
Input policy: required title, accepted length and expected type
Permission policy: caller may perform this operation on this record
Query boundary: request values stay separate from SQL structure
Output boundary: title rendered as escaped HTML text
Request-forgery boundary: browser write uses the chosen CSRF flow

This is a threat-to-control map, not program output. Use it to reason about a feature before choosing a helper. A CSRF token cannot make unsafe HTML safe. HTML escaping cannot authorize a delete. A model allowlist cannot establish the caller's identity.

XSS is an output-context problem

The course templates use esc for title text and the attr context for generated URLs in quoted attributes. That prevents title characters from becoming authored markup at those locations. It does not make arbitrary JavaScript fragments or attacker-chosen URLs acceptable. Avoid inserting untrusted values into executable script contexts in this beginner application.

A record read from your own database is still data. It may have arrived through a different form, import or earlier bug. Escape it when rendering rather than assuming persistence makes it trusted HTML. Do not store pre-escaped title entities as a universal solution; JSON and other outputs need the original value.

The PHP manual's htmlspecialchars documentation explains HTML character encoding, while CodeIgniter's Global Functions guide documents its context-aware escaping facility. The course's escaped-script feature test verifies the specific template boundary rather than claiming one string proves the whole application secure.

SQL construction needs its own boundary

The query builder accepts values separately from its structural choices. Keep user search terms in value arguments. Keep selected columns, tables and sort choices under application control or a deliberate allowlist. Do not disable escaping for a raw expression containing request data.

A model's allowedFields limits data keys for its write methods. It does not apply a universal database firewall, and direct builder writes do not inherit every model policy. This distinction becomes important when an application adds imports, background commands or a second API path.

CSRF and authorization remain separate

Browser clients can automatically send cookies with requests. CSRF protection addresses forged state-changing requests in that setting. Its configuration should match the application's session and authentication design. Using explicit verbs and disabling alternative auto-routed write paths makes the intended boundary easier to review.

An authorized editor still needs CSRF protection for a cookie-authenticated browser write. An unauthorized caller must still be denied even if they obtain a valid token for their own session. Neither mechanism replaces the other. The official Security guide documents the CSRF facilities; the application's permissions remain its own responsibility.

Protect files and diagnostics too

Keep database files, private uploads and logs outside public. Validate upload content and size, generate storage names and decide explicitly whether and how a file can later be downloaded. A valid image is not automatically safe to publish without access and content decisions.

Detailed error pages can reveal filesystem paths, queries or configuration. Use private diagnostics in development and restrained responses in production. Do not log passwords, tokens or complete request bodies just because logging is convenient. Information disclosure can happen through a troubleshooting feature even when the ordinary page is escaped correctly.

Verify boundaries with negative cases

For validation, submit a blank title and confirm no insert. For escaping, store markup-looking text and inspect the rendered body. For access control, deny a write and confirm the row is unchanged. For CSRF, inspect the initial rejection or redirect history and persistence effect rather than only the final page status.

The fetched official Security guide, Global Functions guide and Query Builder guide support the mechanisms discussed here. None promises that enabling a helper makes the entire application secure. Security review must follow the actual routes and data flows.

Common mistakes

Using one word such as sanitization for every boundary makes reviews vague. Trusting stored data as HTML creates persistent XSS risk. Treating CSRF as editor permission leaves unauthorized writes possible. Publishing writable storage exposes files outside the intended application flow. Finally, returning a generic error while logging secrets only moves the leak into another system.

Exercise

For each case choose the relevant boundary: a blank title, markup in a title, an unauthorized delete, a concatenated search term and a publicly downloadable database file. Explain why adding a CSRF token does not resolve all five.

Show an answer

Use validation for blank input, output escaping for title markup, authorization for the delete, safe query value handling for search and filesystem plus web-root boundaries for the database. CSRF addresses forged browser writes; it does not decide data validity, output interpretation, permission, query structure or file publication.

Previous lesson · Next lesson · Related: explicit routes

Key points

  • Each security boundary has a specific purpose.
  • Stored titles still need contextual escaping.
  • Negative tests should verify both responses and absence of unwanted side effects.

Check yourself

1. Which control addresses title markup in HTML text?
2. What protects an unauthorized deletion?
3. Why keep logs outside public?