Giter Club home page Giter Club logo

laravel-nist-password-rules's Introduction

šŸ”’ Laravel NIST Password Rules

Build Status Coverage Status StyleCI Packagist

This package provides Laravel validation rules that follow the password related recommendations found in NIST Special Publication 800-63B section 5.

Laravel NIST Password Rules implements the following recommendations.

Recommendation Implementation
[...] at least 8 characters in length A standard validation rule in all rule sets to validate against this minimum length of 8 characters.
Passwords obtained from previous breach corpuses The BreachedPasswords rule securely checks the password against previous 3rd party data breaches, using the Have I Been Pwned - Pwned Passwords API.
Dictionary words The DictionaryWords rule checks the password against a list of over 102k dictionary words.
Context-specific words, such as the name of the service, the username The ContextSpecificWords rule checks the password does not contain the provided username, and any words defined the configured app name or app URL.
Context-specific words, [...] and derivatives thereof The DerivativesOfContextSpecificWords rule checks the password is not too similar to the provided username, and any words defined the configured app name or app URL.
Repetitive or sequential characters (e.g. ā€˜aaaaaaā€™, ā€˜1234abcdā€™) The RepetitiveCharacters and SequentialCharacters rules checks if the password consists of only repetitive or sequential characters.

It also provides methods to return validation rules arrays for various scenarios, such as register, login, and password changes. These arrays can be passed directly into the Laravel validator.

Installation

Laravel NIST Password Rules can be easily installed using Composer. Just run the following command from the root of your project.

composer require langleyfoxall/laravel-nist-password-rules

If you have never used the Composer dependency manager before, head to the Composer website for more information on how to get started.

Optionally, you may publish the package's translation files with the following Artisan command.

php artisan vendor:publish --provider="LangleyFoxall\LaravelNISTPasswordRules\ServiceProvider"

Usage

To use the Laravel NIST Password Rules in your project, first use the PasswordRules class, then call the appropriate static methods to return an array of appropriate validation rules. There are methods available for the following scenerios.

  • Register
  • Change password, with old password
  • Change password, without old password
  • Optionally change password, with old password
  • Optionally change password, without old password
  • Login

See the code below for example usage syntax.

use LangleyFoxall\LaravelNISTPasswordRules\PasswordRules;

// Register
$this->validate($request, [
    'email' => 'required',
    'password' => PasswordRules::register($request->email),
]);

// Register, without requiring password confirmation
$this->validate($request, [
    'email' => 'required',
    'password' => PasswordRules::register($request->email, false),
]);

// Change password, with old password
$this->validate($request, [
    'old_password' => 'required',
    'password' => PasswordRules::changePassword($request->email, 'old_password'),
]);

// Change password, without old password
$this->validate($request, [
    'password' => PasswordRules::changePassword($request->email),
]);

// Optionally change password, with old password
$this->validate($request, [
    'old_password' => 'required',
    'password' => PasswordRules::optionallyChangePassword($request->email, 'old_password'),
]);

// Optionally change password, without old password
$this->validate($request, [
    'password' => PasswordRules::optionallyChangePassword($request->email),
]);

// Login
$this->validate($request, [
    'email' => 'required',
    'password' => PasswordRules::login(),
]);

The optionallyChangePassword method supplies validation rules that are appropriate for forms in which the password can be optionally changed if filled in.

laravel-nist-password-rules's People

Contributors

divineomega avatar jameswilddev avatar laravel-shift avatar lloricode avatar lnch avatar stejaysulli avatar ziming avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-nist-password-rules's Issues

Custom messages for rules validation

I'm trying to give my own custom messages to the validation rules but can't find how to setMessage for these ones:

laravel-nist-password-rules::validation.can-not-be-sequential-characters
laravel-nist-password-rules::validation.found-in-data-breach

i'm using form request for this purpose:

public function rules()
{
    return [
        'password' => array_merge(
            [
                new UnusedPassword((int) $this->segment(4)),
                (new BreachedPasswords())->setMessage(__('La contraseƱa ha sido expuesta en una violaciĆ³n de datos.')),
            ],
            PasswordRules::changePassword($this->email)
        ),
    ];
}

public function messages()
{
    return [
        'password.min' => __('La contraseƱa debe tener al menos 8 caracteres.'),
    ];
}

Custom message for BreachedPasswords() works but can't do the same for:

        new SequentialCharacters(),
        new RepetitiveCharacters(),

is this possible ?

Thanks

Confusing/incorrect message and failure to validate correctly

In testing (specifically using PasswordRules::optionallyChangePassword) I entered the password "CorrectHorseBatteryStaple"; the message reads:

The password and password confirmation must be different.

This is confusing for the user, as their password and password confirmation must match - Please consider changing this to something that better represents the actual error, such as "Your password cannot be based on dictionary words".

single dictionary words

Hi

while testing, managed to create a password of telephone134 - Should this be allowed? It's a single dictionary word or does the addition of non-sequential chars make that ok?

Thanks

Password composition rules shouldn't be checked

According to NIST rules:

Verifiers SHOULD NOT impose other composition rules (e.g., requiring mixtures of different character types or prohibiting consecutively repeated characters) for memorized secrets.

Those rules prevent consecutively repeated characters due to misunderstanding another requirement in NIST Password Guidelines.

When processing requests to establish and change memorized secrets, verifiers SHALL compare the prospective secrets against a list that contains values known to be commonly-used, expected, or compromised. For example, the list MAY include, but is not limited to:

  • Passwords obtained from previous breach corpuses.
  • Dictionary words.
  • Repetitive or sequential characters (e.g. ā€˜aaaaaaā€™, ā€˜1234abcdā€™).
  • Context-specific words, such as the name of the service, the username, and derivatives thereof.

This is about what such a list of known bad passwords may contain. aaaaaa is obviously a bad password, but say, something like Bg(=SRjSxxxx5[=thpmm?God)X~$i_[:(t44m=kF('waq7H}k8\r<]2Q]~>\($9zD<[G4Y5a,/="L`?{E]Af(;`jsk,mPk}*/y)kuZ:7ps\k{NqAb:Fx[AWL"%G92$Gk I would argue isn't insecure, even if x happens to repeat four times.

Specifically, the intent of the rule is that it should be a finite list of known insecure passwords, not a rule intending to find bad passwords (that can find an infinite number of "insecure" passwords).

PHP_EOL Windows

  • DictionaryWords
  • RepetitiveCharacters
  • SequentialCharacters

rules are won't work on windows, PHP_EOL on Windows is not "\n" but "\r\n".

The passwords property has incorrect value, only has one item with the full content of txt, instead of one item per row.

I have 2 suggestion:

  1. change explode to preg_split. preg_split('/\n|\r\n?/', file_get_contents(self::PASSWORDS_FILE))
  2. or just hardcode the \n line end character. explode('\n', file_get_contents(self::PASSWORDS_FILE))

decouple this package from laravel

I would love to use the nist-password-rules part of this package in applications which are not built on laravel.

do you think it would be possible to devide this package into 2 parts.. one only containg of the nist-password-rules part and another one which bridges this nist-password-rules with laravel?

guzzlehttp/guzzle ~7.0

i have installed guzzlehttp/guzzle ~7.0, when trying to install this package i received that

Problem 1
- Installation request for langleyfoxall/laravel-nist-password-rules ^4.3 -> satisfiable by langleyfoxall/laravel-nist-password-rules[v4.3.0].
- Can only install one of: guzzlehttp/guzzle[7.0.1, 6.5.x-dev].
- Can only install one of: guzzlehttp/guzzle[6.5.x-dev, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.5.x-dev, 7.0.1].
- Conclusion: install guzzlehttp/guzzle 6.5.x-dev
- Installation request for guzzlehttp/guzzle (locked at 7.0.1, required as ^7.0) -> satisfiable by guzzlehttp/guzzle[7.0.1].

is there away to upgrade please, thank you

Can this package be used inside FormRequests?

In my application I have an area where user's can update their password by providing:

  • Their current password
  • Their new password
  • Their new password again

I have a method that looks like this:

/**
 * Change the password for the current logged in user
 *
 * @param  Request $request
 * @return void
 */
public function changePassword(UpdatePassword $request)
{
    $data = $request->validated();

    auth()->user()->update(['password' => $data['new-password']]);

    event(new PasswordChanged(auth()->user()));

    return redirect()->back()->with('success', 'Your password has been updated successfully');
}

This uses a Form Request called UpdatePassword.

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use LangleyFoxall\LaravelNISTPasswordRules\PasswordRules;

class UpdatePassword extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'current-password' => 'required|string',
            'new-password' => array_merge(
                [
                    'regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$/',
                ],
                PasswordRules::changePassword($this->email, $this->current_password),
            ),
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'current-password.required' => 'Please enter your current password',
            'new-password.regex' => 'The password provided does not match the minimum strength requirement',
            'new-password.different' => 'Please ensure your new password is different to your old password',
            'new-password.confirmed' => 'Please ensure your new passwords match',
        ];
    }
}

I've tried to simply pass in the email and pass, but they're not real properties.

Is there any way to do what I'm attempting in this way?

Additional improvements for later adoption once Laravel 9 update is merged & tested

@ziming submitted pr #44 which includes several improvements we should consider for future releases:

At the time of our most recent update we did not want to merge this as it could break support for legacy apps, but I have created this issue for reference as these are good ideas which we may wish to include in the next major version.

Implement Rate Limiting (Throttling)

We should attempt to implement login rate limiting as part of these validation rules, as described in NIST SP800-63b section 5.2.2.

the verifier SHALL implement controls to protect against online guessing attacks.
the verifier SHALL limit consecutive failed authentication attempts on a single account to no more than 100.
Requiring the claimant to wait following a failed attempt for a period of time that increases as the account approaches its maximum allowance for consecutive failed attempts (e.g., 30 seconds up to an hour).

Source: https://pages.nist.gov/800-63-3/sp800-63b.html#throttle

It would also be useful to provide Artisan commands that will remove login bans / delays entirely or for specific users / IPs.

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.