has_unpublished_password 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f3b72f37de09c2757a24ed07a9324a635caeb962a3cc8c947593ffe082bda976
4
+ data.tar.gz: a7717f80f508d92fe81359111d1e1ac79e4b73faf19dbcd64ad806d2a82025f4
5
+ SHA512:
6
+ metadata.gz: 337475d798ece49c9cb9863a42c590dc300905d885d42622d037ce6d6376ccc4c41bc7724184f0ccd2494aab10efc675f051991548e6da16e5df5ef902b35dba
7
+ data.tar.gz: 527ca7b0b75f9cc5ac6d9e496c1eb8caa374efbfd81484808dd8d083bb738c6d4090b2e3d4510be217214ed87f18c70b0707e1389d4717031d6d2c05b72df245
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bloom
10
+ pwned-passwords-sha1-ordered-by-count-v4.txt
11
+ *.gem
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.6.3
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at daniel@heath.cc. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in has_unpublished_password.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ has_unpublished_password (0.2.0)
5
+ ruby-bitfield
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ minitest (5.11.3)
11
+ rake (10.5.0)
12
+ ruby-bitfield (0.1.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.17)
19
+ has_unpublished_password!
20
+ minitest (~> 5.0)
21
+ rake (~> 10.0)
22
+
23
+ BUNDLED WITH
24
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Daniel Heath
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # HasUnpublishedPassword
2
+
3
+ ## What is it?
4
+
5
+ This is a gem which performs offline checks against the [HIBP](https://haveibeenpwned.com/) master list.
6
+
7
+ It can be used to ensure that your users are not using credentials which have previously been leaked.
8
+
9
+ The checks are performed using a pre-built cuckoo filter.
10
+
11
+ ## Status
12
+
13
+ I just threw this together and haven't used it in production yet.
14
+
15
+ I've pre-built filters from three datasets (found in the `hibp-cuckoo-filter` gem):
16
+
17
+ * The 'top 560k passwords' filter is 992kb, with a 1.7% false positive rate. Uses 1mb ram once loaded.
18
+ * The 'top 5600k passwords' filter is 9.6mb, with a 2.1% false positive rate. Uses 8mb ram once loaded.
19
+ * The 'top 56m passwords' filter is 80mb, with a 2.6% false positive rate. Uses 67mb ram once loaded.
20
+
21
+ On my recent macbook pro, checking a single password against the largest filter takes about 3.2 microseconds.
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'has_unpublished_password'
29
+ gem 'hibp-cuckoo-filter' # optional data files, if you don't want to build your own.
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install has_unpublished_password
39
+
40
+ ## Usage
41
+
42
+ ### Configuration
43
+
44
+ Add an initializer (eg `config/initializers/has_unpublished_password.rb`):
45
+
46
+ HasUnpublishedPassword.configure do |config|
47
+ # Valid values: :small, :medium, :large
48
+ config.filter = :large
49
+ end
50
+
51
+
52
+ ### Validation
53
+
54
+ `validates :password, never_leaked_to_hibp: true`
55
+
56
+ ### Low level usage
57
+ ```
58
+ filter = HasUnpublishedPassword.import('serialized.json.gz')
59
+ filter.has? Digest::SHA1.hexdigest('password') # true
60
+ ```
61
+
62
+ ## Development
63
+
64
+ ### Native component
65
+
66
+ The [native component](https://github.com/DanielHeath/rust-cuckoofilter) is written in rust.
67
+
68
+ To update it to a new version, cross compile it in release mode, then:
69
+ ```
70
+ cd ../rust-cuckoofilter/cabi
71
+
72
+ cargo build --target=x86_64-unknown-linux-gnu --release
73
+ cp target/release/libcuckoofilter_cabi.d ../../has_unpublished_password/nativeext/x86_64/
74
+
75
+ cargo build --target=x86_64-apple-darwin --release
76
+ cp target/release/libcuckoofilter_cabi.dylib ../../has_unpublished_password/nativeext/x86_64/
77
+ ```
78
+
79
+ ### Building the filter
80
+
81
+ First, download the master list from HIBP (I used the 'ordered by frequency' list) and decompress it.
82
+
83
+ Then, run `data/prepare.sh <path-to-master-list-file>`.
84
+
85
+ This takes quite awhile; it'll print how many lines it's completed periodically.
86
+
87
+ This process writes its result to `serialized.json.gz` when complete.
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/danielheath/has_unpublished_password. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
96
+
97
+ ## Code of Conduct
98
+
99
+ Everyone interacting in the HasUnpublishedPassword project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/has_unpublished_password/blob/master/CODE_OF_CONDUCT.md).
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+ 24 bits is 2mb. 1/3rd of values will be filled by 5 million rows.
112
+
113
+ take 6 24-bit sequences (for 12mb). That yields a 0.13% collision chance. That's promising.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "has_unpublished_password"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "has_unpublished_password/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "has_unpublished_password"
8
+ spec.version = HasUnpublishedPassword::VERSION
9
+ spec.authors = ["Daniel Heath"]
10
+ spec.email = ["daniel@heath.cc"]
11
+
12
+ spec.summary = %q{Prevent the use of passwords found in the HIBP breach dataset}
13
+ spec.description = %q{Adds a new validation for ActiveRecord, 'has_unpublished_password'}
14
+ spec.homepage = "https://github.com/danielheath/has_unpublished_password"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|data)/}) } +
21
+ Dir.glob('serialized-top-11200000-p*.bloom')
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.17"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ spec.add_dependency "ruby-bitfield"
31
+ end
@@ -0,0 +1,9 @@
1
+ # Must be a top-level constant so rails can find it.
2
+
3
+ class NeverLeakedToHibpValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ if HasUnpublishedPassword.has_been_published? value
6
+ record.errors[attribute] << (options[:message] || "That's one of the first passwords a hacker would try.")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,93 @@
1
+ require 'bitfield'
2
+
3
+ module HasUnpublishedPassword
4
+
5
+ class ShaBloom
6
+ BITS_PER_HEX_CHAR=4
7
+ class << self
8
+ attr_accessor :hash_bits_per_filter, :filter_count, :filter_window, :filter_step
9
+ def filter_window
10
+ self.hash_bits_per_filter / BITS_PER_HEX_CHAR
11
+ end
12
+ end
13
+ @hash_bits_per_filter = 24
14
+ @filter_count = 34
15
+ @filter_step = 1
16
+
17
+ class DefaultConfig < ShaBloom
18
+ @filter_count = 16
19
+ @hash_bits_per_filter = 24
20
+ @filter_step = 1
21
+ end
22
+
23
+ def initialize(filters=nil)
24
+ unless filters
25
+ filters = []
26
+ self.class.filter_count.times do |i|
27
+ filters[i] ||= Bitfield.new(2**self.class.hash_bits_per_filter)
28
+ end
29
+ end
30
+
31
+ @bitfields = filters
32
+ end
33
+
34
+ def inspect
35
+ to_s
36
+ end
37
+
38
+ def to_s
39
+ "#{self.class} filter with #{self.class.filter_count} blocks of size #{2**self.class.hash_bits_per_filter / 8 / 1024}kb"
40
+ end
41
+
42
+ def add_value(value)
43
+ add_shahash Digest::SHA1.hexdigest value
44
+ end
45
+
46
+ def check_value(value)
47
+ check_shahash Digest::SHA1.hexdigest value
48
+ end
49
+
50
+ def add_shahash(hash)
51
+ walk_hash(hash) do |row, idx|
52
+ row.set(idx)
53
+ end
54
+ end
55
+
56
+ def check_shahash(hash)
57
+ walk_hash(hash) do |row, idx|
58
+ return false unless row.get(idx)
59
+ end
60
+ return true
61
+ end
62
+
63
+ def serialize(pattern, verbose=false)
64
+ self.class.filter_count.times do |i|
65
+ fn = "#{pattern}-p#{i}.bloom"
66
+ bitfields[i].serialize(fn)
67
+ puts "wrote #{fn}" if verbose
68
+ puts `du -h #{fn}` if verbose
69
+ end
70
+ end
71
+
72
+ def self.deserialize(pattern)
73
+ filters = []
74
+ filter_count.times do |i|
75
+ fn = "#{pattern}-p#{i}.bloom"
76
+ filters << Bitfield.deserialize(fn, 2**hash_bits_per_filter)
77
+ end
78
+ new(filters)
79
+ end
80
+
81
+ private
82
+ attr_reader :bitfields
83
+
84
+ def walk_hash(hash)
85
+ self.class.filter_count.times do |i|
86
+ start = i * self.class.filter_step
87
+ finish = i * self.class.filter_step + self.class.filter_window - 1
88
+ idx = hash[start..finish].to_i(16)
89
+ yield bitfields[i], idx
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ module HasUnpublishedPassword
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'digest/sha1'
2
+
3
+ if defined? ::ActiveModel
4
+ require 'has_unpublished_password/never_leaked_to_hibp_validator'
5
+ end
6
+
7
+ require "has_unpublished_password/version"
8
+ require 'has_unpublished_password/sha_bloom'
9
+
10
+ module HasUnpublishedPassword
11
+
12
+ class << self
13
+ attr_writer :filter
14
+ def filter
15
+ @filter ||= HasUnpublishedPassword::ShaBloom::DefaultConfig.deserialize(
16
+ File.join(File.dirname(__FILE__), '..', 'serialized-top-11200000')
17
+ )
18
+ end
19
+
20
+ def has_been_published?(password)
21
+ filter.check_value(password)
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ __END__
28
+
29
+ u = User.last
30
+ u.password = "password"
31
+ u.valid?
32
+ u.errors[:password]
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_unpublished_password
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Heath
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ruby-bitfield
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Adds a new validation for ActiveRecord, 'has_unpublished_password'
70
+ email:
71
+ - daniel@heath.cc
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".ruby-version"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - has_unpublished_password.gemspec
88
+ - lib/has_unpublished_password.rb
89
+ - lib/has_unpublished_password/never_leaked_to_hibp_validator.rb
90
+ - lib/has_unpublished_password/sha_bloom.rb
91
+ - lib/has_unpublished_password/version.rb
92
+ - serialized-top-11200000-p0.bloom
93
+ - serialized-top-11200000-p1.bloom
94
+ - serialized-top-11200000-p10.bloom
95
+ - serialized-top-11200000-p11.bloom
96
+ - serialized-top-11200000-p12.bloom
97
+ - serialized-top-11200000-p13.bloom
98
+ - serialized-top-11200000-p14.bloom
99
+ - serialized-top-11200000-p15.bloom
100
+ - serialized-top-11200000-p2.bloom
101
+ - serialized-top-11200000-p3.bloom
102
+ - serialized-top-11200000-p4.bloom
103
+ - serialized-top-11200000-p5.bloom
104
+ - serialized-top-11200000-p6.bloom
105
+ - serialized-top-11200000-p7.bloom
106
+ - serialized-top-11200000-p8.bloom
107
+ - serialized-top-11200000-p9.bloom
108
+ homepage: https://github.com/danielheath/has_unpublished_password
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Prevent the use of passwords found in the HIBP breach dataset
131
+ test_files: []