2017 © Pedro Peláez
 

library easycsrf

A simple, standalone CSRF protection library

image

gilbitron/easycsrf

A simple, standalone CSRF protection library

  • Tuesday, January 17, 2017
  • by gilbitron
  • Repository
  • 0 Watchers
  • 25 Stars
  • 3,929 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 8 Forks
  • 1 Open issues
  • 3 Versions
  • 40 % Grown

The README.md

Build Status Packagist Downloads PHP version License, (*1)

EasyCSRF

EasyCSRF is a simple, standalone CSRF protection library written in PHP. It can be used to protect your forms from Cross Site Request Forgery attacks., (*2)

Requirements

  • PHP 7.3+

Install

Install via composer:, (*3)

composer require gilbitron/easycsrf

Run composer install then use as normal:, (*4)

require 'vendor/autoload.php';

$sessionProvider = new EasyCSRF\NativeSessionProvider();
$easyCSRF = new EasyCSRF\EasyCSRF($sessionProvider);

Usage

To use EasyCSRF first you need to generate a token:, (*5)

$sessionProvider = new EasyCSRF\NativeSessionProvider();
$easyCSRF = new EasyCSRF\EasyCSRF($sessionProvider);

$token = $easyCSRF->generate('my_token');

You then include this token with any forms you create:, (*6)

<form>
    ...
    <input type="hidden" name="token" value="<?php echo $token; ?>">
    ...
</form>

Then before you do any data processing, you check the token is valid:, (*7)

use EasyCSRF\Exceptions\InvalidCsrfTokenException;

try {
    $easyCSRF->check('my_token', $_POST['token']);
} catch(InvalidCsrfTokenException $e) {
    echo $e->getMessage();
}

Token Expiration

You can set a time limit on tokens by passing a timespan (in seconds) to the check method. Tokens older than the timespan will not be valid., (*8)

// Example 1 hour expiration
$easyCSRF->check('my_token', $_POST['token'], 60 * 60);

Reusable Tokens

Tokens can be made reusable and not one-time only (useful for ajax-heavy requests)., (*9)

// Make token reusable
$easyCSRF->check('my_token', $_POST['token'], null, true);

Custom SessionProvider

Your app might use a third party library for managing sessions, or you may want to store tokens somewhere other than $_SESSION (as the NativeSessionProvider does). In this case you can create a custom SessionProvider and use that when instantiating EasyCSRF., (*10)

<?php

use EasyCSRF\Interfaces\SessionProvider;

class CustomSessionProvider implements SessionProvider
{
    /**
     * Get a session value.
     *
     * @param string $key
     * @return mixed
     */
    public function get($key)
    {
        // Return your stored data
    }

    /**
     * Set a session value.
     *
     * @param string $key
     * @param mixed $value
     * @return void
     */
    public function set($key, $value)
    {
        // Store your data
    }

}
$sessionProvider = new CustomSessionProvider();
$easyCSRF = new EasyCSRF\EasyCSRF($sessionProvider);

Credits

EasyCSRF was created by Gilbert Pellegrom from Dev7studios. Released under the MIT license., (*11)

The Versions

17/01 2017

dev-master

9999999-dev

A simple, standalone CSRF protection library

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

19/03 2015

1.0.1

1.0.1.0

A simple, standalone CSRF protection library

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

13/02 2015

1.0.0

1.0.0.0

A simple, standalone CSRF protection library

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires