Coder Social home page Coder Social logo

programarivm / nonce-shield Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 109 KB

This is a simple, framework-agnostic library inspired by WordPress that helps you protect your PHP web apps from CSRF attacks.

License: GNU General Public License v3.0

PHP 100.00%
nonce csrf wordpress wp security hash session php

nonce-shield's Introduction

Nonce Shield

Build Status License: GPL v3

CSRF Shield

This is a simple, framework-agnostic library inspired by WordPress nonces that helps you protect your PHP web apps from CSRF attacks. Nonce Shield nonces are temporary tokens that uniquely identify urls, using the current session identifier as a hash.

For example, the nonce below:

624fd48ceb3eddfb716572d765576e23

Identifies this URI temporarily (until the session is renewed):

/url-to-protect/something.php

Nonce Shield accepts all HTTP methods (GET, POST, PUT, PATCH and DELETE), but is specially suitable for when you want to protect GET resources that perform sensitive operations on the server side -- update a user, remove a comment, etc -- as the ones shown next.

/user/update.php?id=3452&_nonce_shield_token=693904c4e937577ed2589ea54e56a8d5

/comment/remove.php?id=3452&_nonce_shield_token=6bee0c3437199bf2e5ca1de872a9cefd

Side Note: If you are not a big fan of sending tokens in GET requests, have a look at CSRF Shield which is a OWASP-friendly CSRF protector that won't disclose tokens.

1. Where Is the Token Appended?

According to the HTTP method being used:

HTTP Method Nonce
GET $_GET['_nonce_shield_token']
POST $_POST['_nonce_shield_token']
PUT $_SERVER['HTTP_X_CSRF_TOKEN']
PATCH $_SERVER['HTTP_X_CSRF_TOKEN']
DELETE $_SERVER['HTTP_X_CSRF_TOKEN']

2. Security

Nonce Shield assumes there is an .env file in your app's root folder with a NONCE_KEY set -- otherwise it will throw an UnsecureNonceKeyException.

NONCE_KEY=5ZLXPORAl39jMH5ujR53jNZ3uLpNcz9跡

The NONCE_KEY is used as a salt when hashing the url. This value is at least 32 characters long, and must contain at least one number, one lowercase letter, one uppercase letter and a non-alphanumeric character.

3. NonceShield\Nonce Methods

3.1. getToken()

Gets a nonce token.

$nonce = (new Nonce)->getToken('/comment/remove.php?id=3452');

3.2. htmlInput()

Returns an HTML input tag with the nonce token embedded.

echo (new Nonce)->htmlInput('/comment/remove.php');

Here is an example:

<input type="hidden" name="_nonce_shield_token" id="_nonce_shield_token" value="6bee0c3437199bf2e5ca1de872a9cefd" />

3.3. validateToken()

Validates the incoming nonce token -- if not valid will respond with a 405 status code (Method Not Allowed).

(new Nonce)->validateToken();

4. License

The GNU General Public License.

5. Contributions

Would you help make this library better? Contributions are welcome.

  • Feel free to send a pull request
  • Drop an email at [email protected] with the subject "CSRF Shield Contributions"
  • Leave me a comment on Twitter
  • Say hello on Google+

Many thanks.

nonce-shield's People

Contributors

programarivm avatar

Watchers

 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.