Zap
Documentation / Protection

Security

Protect every state-changing controller action with request-token validation. Zap provides CSRF token helpers through Security; your application remains responsible for validating input and authorizing the requested operation.

Generate and send a token

The shared app layout already emits a csrf-token meta tag using set_meta_token(). For a regular HTML form, render the hidden field generated by set_token_field(). For JavaScript requests, send the meta token in the X-CSRF-TOKEN header.

<form method="post" action="/profile">
 <?= service('\Zap\Core\Utils\Security')
 ->get_instance()
 ->set_token_field() ?>
 <button type="submit">Save</button>
</form>
const csrfToken = document.querySelector('meta[name="csrf-token"]').content;

await fetch('/profile', {
 method: 'PATCH',
 headers: {
 'Content-Type': 'application/json',
 'X-CSRF-TOKEN': csrfToken,
 },
 body: JSON.stringify({ display_name: 'Zap User' }),
});
Keep tokens out of URLs. The security helper accepts the _csrf_token POST field or the X-CSRF-TOKEN request header. For PUT, PATCH, and DELETE requests, send the token in the header because PHP does not populate $_POST from those request bodies.

Validate in the controller action

Call validate_request() before changing data in each state-changing action. Passing true consumes a valid token so it cannot be reused.

use Zap\Core\Http\Request;
use Zap\Core\Http\Response;
use Zap\Core\Utils\Security;

public function update(Request $request): Response
{
 $security = Security::get_instance();

 if (!$security->validate_request(true)) {
 return $this->text('Invalid CSRF token.', 403);
 }

 $displayName = $request->input('display_name');
 // Validate and authorize the submitted value before saving it.

 return $this->redirect(route('profile.edit'));
}

The layout produces tokens through the same security helper. You can also access it through the framework service helper when following the app's existing service pattern.

Protect POST, PUT, PATCH, and DELETE

Add validation to every action that creates, changes, or removes state. This applies to browser forms, JSON endpoints, and method-overridden forms alike.

Route::post('/profile', 'ProfileController@store');
Route::put('/profile', 'ProfileController@update');
Route::patch('/profile', 'ProfileController@update');
Route::delete('/profile', 'ProfileController@destroy');

public function destroy(Request $request): Response
{
 if (!Security::get_instance()->validate_request(true)) {
 return $this->text('Invalid CSRF token.', 403);
 }

 // Authorize the current user, then perform the deletion.
 return $this->redirect(route('profile.index'));
}
CSRF validation does not replace authorization or validation. Verify that the current user may perform the operation and validate all submitted values before persistence. Tokens expire after the helper's default one-hour lifetime.

Keep request handling safe

  • Validate the CSRF token before any mutation or irreversible side effect.
  • Use validate_request(true) for a successful single-use token check.
  • Authorize the current user separately; possession of a CSRF token is not permission.
  • Validate and normalize input before using it in queries, commands, or output.
  • Use HTTPS in production and do not expose tokens in logs or URLs.
Next: manage user sessions

Store authenticated state, regenerate the session ID, and log out safely.

Session guide