GithubHelp home page GithubHelp logo

fnando / password_strength Goto Github PK

View Code? Open in Web Editor NEW
183.0 4.0 54.0 203 KB

Check password strength against several rules. Includes ActiveRecord/ActiveModel support.

Ruby 49.47% JavaScript 49.56% HTML 0.97%

password_strength's Introduction

Introduction

Check password strength against several rules. Includes ActiveRecord/ActiveModel support.

Build Status

Validates the strength of a password according to several rules:

  • size
  • 3+ numbers
  • 2+ special characters
  • uppercased and downcased letters
  • combination of numbers, letters and symbols
  • password contains username
  • sequences (123, abc, aaa)
  • repetitions
  • can't be a common password (view list at support/common.txt)

Some results:

  • 123: weak
  • 123abc: weak
  • aaaaaa: weak
  • myPass145: good
  • myPass145$: strong

Install

gem install password_strength

or put this in your Gemfile:

gem "password_strength"

The JavaScript code is also available as a NPM package.

npm install @fnando/password_strength --save

If you want the source go to http://github.com/fnando/password_strength

Usage

strength = PasswordStrength.test("johndoe", "mypass")
#=> return a object

strength.good?
#=> status == :good

strength.weak?
#=> status == :weak

strength.strong?
#=> status == :strong

strength.status
#=> can be :weak, :good, :strong

strength.valid?(:strong)
#=> strength == :strong

strength.valid?(:good)
#=> strength == :good or strength == :strong

ActiveRecord/ActiveModel

The PasswordStrength library comes with ActiveRecord/ActiveModel support.

class Person < ActiveRecord::Base
  validates_strength_of :password
end

To be honest, you can use it with plain ActiveModel objects.

class Person
  include ActiveModel::Model
  validates_strength_of :password
end

# or simply

class Person
  include ActiveModel::Validations
  validates_strength_of :password
end

The default options are :level => :good, :with => :username.

If you want to compare your password against other field, you have to set the :with option.

validates_strength_of :password, :with => :email

The available levels are: :weak, :good and :strong.

validates_strength_of :password, :with => :email, :level => :good

Also you can set level with a lambda.

validates_strength_of :password, :with => :email, :level => lambda {|u| :good }

You can also provide a custom class/module that will test that password.

validates_strength_of :password, :using => CustomPasswordTester

Your CustomPasswordTester class should override the default implementation. In practice, you're going to override only the test method that must call one of the following methods: invalid!, weak!, good! or strong!.

class CustomPasswordTester < PasswordStrength::Base
  def test
    if password != "mypass"
      invalid!
    else
      strong!
    end
  end
end

The tester above will accept only +mypass+ as password.

PasswordStrength implements two validators: PasswordStrength::Base and PasswordStrength::Validators::Windows2008.

ATTENTION: Custom validators are not supported by JavaScript yet!

JavaScript

The PasswordStrength also implements the algorithm in the JavaScript.

var strength = PasswordStrength.test("johndoe", "mypass");
strength.isGood();
strength.isStrong();
strength.isWeak();
strength.isValid("good");

The API is basically the same!

You can use the :exclude option. Only regular expressions are supported for now.

var strength = PasswordStrength.test("johndoe", "password with whitespaces", {exclude: /\s/});
strength.isInvalid();

Additionaly, a jQuery plugin is available.

$.strength("#username", "#password");

The line above will validate the #password field against #username. The result will be an image to the respective strength status. By default the image path will be /images/weak.png, /images/good.png and /images/strong.png.

You can overwrite the image path and the default callback.

$.strength.weakImage = "/weak.png";
$.strength.goodImage = "/good.png";
$.strength.strongImage = "/strong.png";
$.strength.callback = function(username, password, strength) {
    // do whatever you want
};

If you just want to overwrite the callback, you can simple do

$.strength("#username", "#password", function(username, password, strength){
    // do whatever you want
});

Get the files:

If you're using asset pipeline, just add the following lines to your application.js.

//= require jquery
//= require password_strength
//= require jquery_strength

Running tests

Ruby

  1. Install all dependencies with bundle install.
  2. Run rake test.

JavaScript

  1. Install Node.js, then run npm install.
  2. Open test/password_strength_test.html in your target browser.

License

(The MIT License)

Copyright © 2010-2016 Nando Vieira (http://nandovieira.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

password_strength's People

Contributors

chanks avatar fnando avatar jeroenj avatar mdv-mightybell 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

password_strength's Issues

dictionary attacks

Hey, thanks for posting this! It's really been helpful.

One thing I noticed is that "password1" returns a :good score.

Which was supposedly the most common password on MySpace:
http://blog.jimmyr.com/Password_analysis_of_databases_that_were_hacked_28_2009.php

I was wondering if you had any ideas on detecting common passwords. I can take a crack at writing the code for it but was just curious your thoughts.

A dictionary file could be included, I found some here: http://lastbit.com/dict.asp

But we'd still want to allow passwords like "correct horse stapler battery" to return :strong just due to their length. Thoughts?

Long password denial of service vulnerability

Issue

A user can enter a long password and take a server offline for a large amount of time
As seen below a single request with a long password of 100K characters can result in a server being frozen for ~ 8 minutes.
This means that with just a single computer an attacker can easily make a few requests to take a number of machines offline and achieve an effective DOS attack

Proposed solution

OWASP recommends using a limit of 1,000

A limit of 1000 characters is sufficient to let the user choose a very big password without impacting the system.
https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet#Input_password_size

In order to make this backwards compatible and to not affect any existing long passwords, truncating of the users input passwords could be done. In all cases, a password of 1,000 would be just as "Strong" as one of 10,000 with this gem as the length*4 is added to the score

score = password.size * 4

Not affected

Rails applications using bcrypt are not affected as they have a limit of 72 enforced:
https://github.com/rails/rails/blob/040eb98c2be6bfb2c50b4ad85e7031f9aa97f15e/activemodel/lib/active_model/secure_password.rb#L7-L10
UNLESS they are manually checking password strength before attempting to persist it to the DB

Benchmarks

Benchmarks for password lengths with this gem can be seen here: cianmce/benchmark_password_gems

A Google sheet of the results can be seen here
fnando-password_strength

Live Demo

Site: password-dos.herokuapp.com

Source: cianmce/profile_password_checkers

Is there an additional install for the jquery plugin?

Following the readme, I get errors in javascript. TypeError: undefined is not a function (evaluating '$.strength("#user_username", "#user_password")'). Is there an additional install, or is the jQuery plugin .js included in the gem?

Partial match of usernames

I noticed that an exact username match lowers the score, but not a partial match.

ree-1.8.7-2011.03 :022 > PasswordStrength.test("iloveyou2", "iloveyou2")
 => #PasswordStrength::Base:0x130e4b020 @password="iloveyou2", @status=:weak, @score=0, @exclude=nil, @username="iloveyou2"> 
ree-1.8.7-2011.03 :023 > PasswordStrength.test("iloveyou", "iloveyou2")
 => #PasswordStrength::Base:0x130e3eb18 @password="iloveyou2", @status=:good, @score=36, @exclude=nil, @username="iloveyou"> 
ree-1.8.7-2011.03 :024 >

Would it make sense to lower the score with a partial match? Maybe based on how many letters remain after gsubing out the partial match?

jQuery Uncaught TypeError: $.strength is not a function

Is there an example of this working somewhere? I can't get the jQuery element to work. I get the error: Uncaught TypeError: $.strength is not a function

I've installed via the gem into a Rails 4.2 app. Application.js has:

//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require jquery.turbolinks
//= require turbolinks
//= require password_strength
//= require jquery_strength
//= require_tree .

I have not also put the JavaScript in my assets dir. I've confirmed (using Chrome Developer tools) that the scripts are being included in the page. I have tried to include them in a different order after the jquery, but it hasn't helped.

In my application.js I have

$.strength("#user_email", "#user_password");

and get the error in the console.

A working version to look at would be useful, or any pointers if I'm doing something obviously wrong.

Thank you!

over zealous on repeating characters?

Part of my app was using a SecureRandom.hex(10) to generate random passwords.

Strangely it would occasionally fail saying the password wasn't strong enough:

irb(main):009:0> 1000.times { p = SecureRandom.hex(10); puts p if PasswordStrength.test("", p).status == :weak }
34777db2b234d4e65bdf
91a55535c5edc347bba1
abad888a000bce594cda
7cdd5222b494477828b1
306eeecfeea7dbd51a74
af9dcc632b667b7dccde
=> 1000
irb(main):010:0>

Difficult bug to track down given how rarely it happened. These passwords seem secure, but all contain 3 chars in a row repeating. Would be nice if this wasn't counted so heavily.

The work around was fairly straight forward:

def self.random_password
  begin
    p = SecureRandom.hex(10)
  end while PasswordStrength.test("", p).status == :weak
  p
end

But just wanted to pass it on. Thanks for the great gem!

Problems with special chars

Hi, i'm trying your plugin, and i've find a little annoyance. It has some problems with special chars like §, £, €, à, è, etc

This the result (ruby 1.8.7-174):

PasswordStrength.test("johndoe", "pass£")
RegexpError: premature end of regular expression: /s�/
from /usr/lib/ruby/gems/1.8/gems/password_strength-0.1.4/lib/password_strength/base.rb:147:in scan' from /usr/lib/ruby/gems/1.8/gems/password_strength-0.1.4/lib/password_strength/base.rb:147:inrepetitions'
from /usr/lib/ruby/gems/1.8/gems/password_strength-0.1.4/lib/password_strength/base.rb:141:in upto' from /usr/lib/ruby/gems/1.8/gems/password_strength-0.1.4/lib/password_strength/base.rb:141:inrepetitions'
from /usr/lib/ruby/gems/1.8/gems/password_strength-0.1.4/lib/password_strength/base.rb:104:in score_for' from /usr/lib/ruby/gems/1.8/gems/password_strength-0.1.4/lib/password_strength/base.rb:125:intest'
from /usr/lib/ruby/gems/1.8/gems/password_strength-0.1.4/lib/password_strength.rb:14:in `test'
from (irb):35
from :0

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.