Code Monkey home page Code Monkey logo

slimcsrfprotection's Introduction

Slim CSRF protection

Simple protection middleware against CSRF attacks for Slim PHP framework.

  • Really protect your app from csr forgery ;)
  • Helpers for forms and jQuery
  • Versions with and without sessions usage

Usage

SlimCSRFProtection

Version with Sessions (must be enabled)

session_start();
$app = new Slim(); // init app
$app->add( new SlimCSRFProtection() );

Also, you can pass custom function as callback:

function show_message() { echo "verification not passed" }

$app->add( new SlimCSRFProtection('show_message') );

SlimCSRFProtectionNoSession

Note, if you use SlimCSRFProtectionNoSession instead of SlimCSRFProtection, you need to pass in constructor as first argument some secret string, which will be used for generating csrf token - it's required. And, optionally, constructor takes function which will be executed if submitted token is not valid.

$app->add( new SlimCSRFProtectionNoSession("It is my secret string!") );

With function:

function show_message() { echo "verification not passed" }

$app->add( new SlimCSRFProtectionNoSession("It is my secret string!", 'show_message') );

Important

SlimCSRFProtectionNoSession generates token, related to your secret string, $_SERVER['REMOTE_ADDR'] and $_SERVER['USER_AGENT'], therefore it is not full protection - users from same subnet takes same token. If it is possible, use SlimCSRFProtection instead of SlimCSRFProtectionNoSession

Usage in View

By default the middleware appends to view three variables:

$csrf_token - random secret token

$csrf_protection_input - <input> for usage in forms (<input type="hidden" value="<?= $csrf_token ?>">)

$csrf_protection_jquery - jquery code, which add 'X_CSRF_TOKEN' header to each jQuery.ajax post request

You can use those in HTML like so

<form action="/login" method="POST">
    <?= $csrf_protection_input ?>
    <input name="login">
    <input name="passwd">
    <input type="submit" value="Submit">
</form>

...
<head>
    <title>Example</title>
    ....
    <?= $csrf_protection_jquery ?>
</head>
...

API

static::create_token() - returns csrf token

$this->is_token_valid($token) - returns true, if token is valid

See also

Slim-Extras: CsrfGuard middleware

slimcsrfprotection's People

Watchers

Anton Morozov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.