Download the PHP package rafalmasiarek/csrf-token without Composer

On this page you can find all versions of the php package rafalmasiarek/csrf-token. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package csrf-token

PHP CSRF — Encrypted, Container‑Aware Tokens

A secure CSRF protection library with AES‑256‑GCM encryption and per‑form containers. Each form (container) has its own session bucket, derived key (HKDF) and optional pepper. Tokens are bound to container id, client IP, User‑Agent, and origin (all configurable), with an optional session-bound HMAC proof as a second, independent field.

Features

Install

Quick Start

Validation:

Containers (Multiple Forms)

Storage Options

Storage keys are hashed; for multi‑containers we pass a composite "<container>|<token>" to avoid collisions.

Security Notes


Client Context Provider (IP / User-Agent injection)

Since version 1.3.2 the library supports external injection of client IP and User-Agent, instead of relying solely on $_SERVER.

Passing IP/UA explicitly

Container-specific:

ClientContextProviderInterface

Usage:


Origin Binding

Independent from IP/UA binding: when a container has a trusted origin configured, that origin is embedded in the encrypted payload at generation time and enforced against the resolved request Origin at validation time. Unlike allowed_origins (below), this binds one specific token instance to one specific origin, not a request to a whitelist.

Rules:

Origin Scope (allowed_origins)

A separate, coarser mechanism: a per-container whitelist checked independently of any single token, useful when multiple trusted origins must be accepted (e.g. web + mobile webview).

Supports exact origins, scheme-free hosts (example.com matches any scheme), and single-label wildcards (*.example.com matches https://api.example.com, not https://a.b.example.com). Run both mechanisms together when you want a specific token bound to a specific origin and a broader fallback whitelist for multi-origin deployments.


Dedicated Per-Container Key (private_key)

Every container normally derives its encryption and proof keys (HKDF) from the one master $cipherKey passed to the Csrf constructor — isolated from other containers, but all still ultimately decryptable by anyone holding that one master key. Setting private_key on a container replaces the master key as that container's HKDF base entirely:

Session-Bound Proof (_csrf_proof)

An optional second field, _csrf_proof, provides independent evidence that the submitted _csrf token belongs to the current authenticated session — not just that it decrypts correctly. It is an HMAC-SHA256 over the exact token, the container id, and a session-bound value, keyed by a proof key derived independently from the AES-GCM encryption key (HKDF domain separation: csrf:<container> vs. csrf-proof:<container>). This borrows the session-binding principle from OWASP's Signed Double-Submit Cookie guidance; it is not a literal double-submit-cookie implementation — no cookie is involved, the library remains a synchronizer-token design.

Behavior:

Never bind to a static account attribute (user id, email) — the value must change whenever the login session is regenerated, or a stolen token/proof pair from a previous session stays valid across logins.

Validation Order


Token Lifecycle: Single-Use, Back Button, and Refreshing

Tokens are always single-use: validateFor() burns the server-side session state on success. This is a hard-coded property of the library, not a configuration flag — plan the UX around it rather than looking for a way to disable it.

The Back button / BFCache problem

This is the correct, intended outcome from a security standpoint — a consumed token must never validate again. It is a UX problem to solve at the application layer, not a defect to patch in the library.

Refresh pattern

Expose a same-origin, no-store endpoint that issues a fresh pair on demand, and re-fetch it whenever a page may be showing stale form state:

Do not put the token or proof in a URL query string (history, logs, Referer leakage). The refresh endpoint must only issue tokens on GET — never perform a state change there.

Monitoring expiration

getExpiresInFor() reports remaining TTL without consuming the token, so a long-lived form (e.g. a multi-step wizard left idle in a tab) can warn the user or refresh proactively before submission fails:

Applications that intentionally reuse a token across multiple requests are outside this library's model — single_use is not configurable, by design.


Migration

Upgrading to this release (Origin Binding, Session-Bound Proof)

From 1.2.1 → 1.3.x

From 1.0.0 → 1.1.x

Examples

See example/ for drop‑in snippets:

View Helpers

All three helpers below render through HtmlHelper::input(), which transparently emits a _csrf_proof hidden field alongside _csrf whenever a session is active — no changes needed to benefit from it (see Session-Bound Proof).

Plates (League\Plates)

Twig

Blade (Laravel)

License

MIT


All versions of csrf-token with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package rafalmasiarek/csrf-token contains the following files

Loading the files please wait ...