mead_captcha 0.1.2

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: a04e27c80f5f8484a18466ee1d3a564ac7be2e21f5fdfa931b63b62c21ac62b2
4
+ data.tar.gz: 92bbc84b1c51c418c117bb1630d9e58f3dbae94940e09d38a12d38d942b5646e
5
+ SHA512:
6
+ metadata.gz: 28cd36b307aa865a5260ad72335ad23c4ea98f8c166eff7c4fb736f54e68a061f4ca6e0f1fab93bd037b6bafa63841d9d3c6f0386a23d097445b9f7696366898
7
+ data.tar.gz: 39df99478e79b1f9e590a1ad0a33ff68e78e0032c951da3f8e0a31ac4c00818221a9b73573cbcdf3d55986970b73aeb9ccbb5607cc9792fd629c4a6c94331d22
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
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mead.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,180 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mead_captcha (0.1.2)
5
+ hashie
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.1.7)
11
+ actionpack (= 6.1.7)
12
+ activesupport (= 6.1.7)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailbox (6.1.7)
16
+ actionpack (= 6.1.7)
17
+ activejob (= 6.1.7)
18
+ activerecord (= 6.1.7)
19
+ activestorage (= 6.1.7)
20
+ activesupport (= 6.1.7)
21
+ mail (>= 2.7.1)
22
+ actionmailer (6.1.7)
23
+ actionpack (= 6.1.7)
24
+ actionview (= 6.1.7)
25
+ activejob (= 6.1.7)
26
+ activesupport (= 6.1.7)
27
+ mail (~> 2.5, >= 2.5.4)
28
+ rails-dom-testing (~> 2.0)
29
+ actionpack (6.1.7)
30
+ actionview (= 6.1.7)
31
+ activesupport (= 6.1.7)
32
+ rack (~> 2.0, >= 2.0.9)
33
+ rack-test (>= 0.6.3)
34
+ rails-dom-testing (~> 2.0)
35
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
36
+ actiontext (6.1.7)
37
+ actionpack (= 6.1.7)
38
+ activerecord (= 6.1.7)
39
+ activestorage (= 6.1.7)
40
+ activesupport (= 6.1.7)
41
+ nokogiri (>= 1.8.5)
42
+ actionview (6.1.7)
43
+ activesupport (= 6.1.7)
44
+ builder (~> 3.1)
45
+ erubi (~> 1.4)
46
+ rails-dom-testing (~> 2.0)
47
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
48
+ activejob (6.1.7)
49
+ activesupport (= 6.1.7)
50
+ globalid (>= 0.3.6)
51
+ activemodel (6.1.7)
52
+ activesupport (= 6.1.7)
53
+ activerecord (6.1.7)
54
+ activemodel (= 6.1.7)
55
+ activesupport (= 6.1.7)
56
+ activestorage (6.1.7)
57
+ actionpack (= 6.1.7)
58
+ activejob (= 6.1.7)
59
+ activerecord (= 6.1.7)
60
+ activesupport (= 6.1.7)
61
+ marcel (~> 1.0)
62
+ mini_mime (>= 1.1.0)
63
+ activesupport (6.1.7)
64
+ concurrent-ruby (~> 1.0, >= 1.0.2)
65
+ i18n (>= 1.6, < 2)
66
+ minitest (>= 5.1)
67
+ tzinfo (~> 2.0)
68
+ zeitwerk (~> 2.3)
69
+ builder (3.2.4)
70
+ coderay (1.1.3)
71
+ concurrent-ruby (1.1.10)
72
+ crass (1.0.6)
73
+ diff-lcs (1.5.0)
74
+ erubi (1.11.0)
75
+ globalid (1.0.0)
76
+ activesupport (>= 5.0)
77
+ hashie (5.0.0)
78
+ i18n (1.12.0)
79
+ concurrent-ruby (~> 1.0)
80
+ loofah (2.19.0)
81
+ crass (~> 1.0.2)
82
+ nokogiri (>= 1.5.9)
83
+ mail (2.7.1)
84
+ mini_mime (>= 0.1.1)
85
+ marcel (1.0.2)
86
+ method_source (1.0.0)
87
+ mini_mime (1.1.2)
88
+ minitest (5.15.0)
89
+ nio4r (2.5.8)
90
+ nokogiri (1.12.5-x86_64-darwin)
91
+ racc (~> 1.4)
92
+ pry (0.14.1)
93
+ coderay (~> 1.1)
94
+ method_source (~> 1.0)
95
+ racc (1.6.0)
96
+ rack (2.2.4)
97
+ rack-test (2.0.2)
98
+ rack (>= 1.3)
99
+ rails (6.1.7)
100
+ actioncable (= 6.1.7)
101
+ actionmailbox (= 6.1.7)
102
+ actionmailer (= 6.1.7)
103
+ actionpack (= 6.1.7)
104
+ actiontext (= 6.1.7)
105
+ actionview (= 6.1.7)
106
+ activejob (= 6.1.7)
107
+ activemodel (= 6.1.7)
108
+ activerecord (= 6.1.7)
109
+ activestorage (= 6.1.7)
110
+ activesupport (= 6.1.7)
111
+ bundler (>= 1.15.0)
112
+ railties (= 6.1.7)
113
+ sprockets-rails (>= 2.0.0)
114
+ rails-dom-testing (2.0.3)
115
+ activesupport (>= 4.2.0)
116
+ nokogiri (>= 1.6)
117
+ rails-html-sanitizer (1.4.3)
118
+ loofah (~> 2.3)
119
+ railties (6.1.7)
120
+ actionpack (= 6.1.7)
121
+ activesupport (= 6.1.7)
122
+ method_source
123
+ rake (>= 12.2)
124
+ thor (~> 1.0)
125
+ rake (13.0.6)
126
+ rspec (3.12.0)
127
+ rspec-core (~> 3.12.0)
128
+ rspec-expectations (~> 3.12.0)
129
+ rspec-mocks (~> 3.12.0)
130
+ rspec-core (3.12.0)
131
+ rspec-support (~> 3.12.0)
132
+ rspec-expectations (3.12.0)
133
+ diff-lcs (>= 1.2.0, < 2.0)
134
+ rspec-support (~> 3.12.0)
135
+ rspec-html-matchers (0.10.0)
136
+ nokogiri (~> 1)
137
+ rspec (>= 3.0.0.a)
138
+ rspec-mocks (3.12.0)
139
+ diff-lcs (>= 1.2.0, < 2.0)
140
+ rspec-support (~> 3.12.0)
141
+ rspec-rails (6.0.1)
142
+ actionpack (>= 6.1)
143
+ activesupport (>= 6.1)
144
+ railties (>= 6.1)
145
+ rspec-core (~> 3.11)
146
+ rspec-expectations (~> 3.11)
147
+ rspec-mocks (~> 3.11)
148
+ rspec-support (~> 3.11)
149
+ rspec-support (3.12.0)
150
+ sprockets (4.1.1)
151
+ concurrent-ruby (~> 1.0)
152
+ rack (> 1, < 3)
153
+ sprockets-rails (3.4.2)
154
+ actionpack (>= 5.2)
155
+ activesupport (>= 5.2)
156
+ sprockets (>= 3.0.0)
157
+ thor (1.2.1)
158
+ tzinfo (2.0.5)
159
+ concurrent-ruby (~> 1.0)
160
+ websocket-driver (0.7.5)
161
+ websocket-extensions (>= 0.1.0)
162
+ websocket-extensions (0.1.5)
163
+ zeitwerk (2.6.6)
164
+
165
+ PLATFORMS
166
+ x86_64-darwin-20
167
+
168
+ DEPENDENCIES
169
+ actionview
170
+ mead_captcha!
171
+ pry
172
+ rails (> 4.0)
173
+ railties
174
+ rake (~> 13.0)
175
+ rspec (~> 3.0)
176
+ rspec-html-matchers
177
+ rspec-rails
178
+
179
+ BUNDLED WITH
180
+ 2.2.19
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 cwagrant
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,68 @@
1
+ # Mead
2
+
3
+ Mead is a simple honeypot gem and field name obfuscator. It allows you to add a honeypot to any form as easily as calling `honeypot_field_tag`.
4
+
5
+ # Usage
6
+ ### Honeypots
7
+ Generating a simple honeypot
8
+ ```ruby
9
+ honeypot_field_tag
10
+
11
+ # => <div>
12
+ # <label for="pseudo_random_field_name">
13
+ # <input type="text" name="pseudo_random_field_name" id="pseudo_random_field_name">
14
+ # </div>
15
+ ```
16
+
17
+ You can also get more creative by using it as a block to generate a content tag and nest your honeypot. This can allow you to make your honeypot blend in as seamlessly as you like to the DOM.
18
+ ```ruby
19
+ honeypot_field_tag(:label) do |name|
20
+ check_box_tag(:do_not_check, name, false, class: 'mead-input-attributes')
21
+
22
+ # => <label class="mead-label-attributes">
23
+ # <input id="name", name="name", type="checkbox", value="false">
24
+ # </label>
25
+ ```
26
+
27
+ ### Obfuscation
28
+ ```ruby
29
+ mead_obfuscate_tag(:first_name) do |first_name|
30
+ label_tag first_name
31
+ text_field_tag first_name
32
+
33
+ # => <label for="obfuscated_first_name">
34
+ # <input name="obfuscated_fist_name" id="obfuscated_first_name" type="text">
35
+ ```
36
+
37
+ To deobfuscate your params you can call mead_params to get a hash returned of the deobfuscated values.
38
+
39
+ ```ruby
40
+ def user_params
41
+ params.
42
+ require(:user).
43
+ permit(:first_name, :last_name)
44
+ .merge(mead_params)
45
+ end
46
+
47
+ user_params
48
+ # => {first_name: foo, last_name: bar, email: "foo@bar.com"}
49
+ ```
50
+
51
+ In addition to the above form tag helpers, Mead also implements these tags as form helpers and form builders as well.
52
+
53
+
54
+
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cwagrant/mead.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
67
+
68
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "mead"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,10 @@
1
+ module MeadCaptcha
2
+ class Configuration
3
+ attr_accessor :protect_controller_actions, :ignore_controller_actions
4
+
5
+ def initialize
6
+ @protect_controller_actions = nil
7
+ @ignore_controller_actions = nil
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module MeadCaptcha
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,144 @@
1
+ module ActionView
2
+ module Helpers
3
+ module FormHelper
4
+ # Creates a honeypot on forms that will be appropriately namespaced.
5
+ #
6
+ # Returns a FormHelper tag of type +form_tag+ that will be namespaced
7
+ # via the +object_name+ and that will use the hash of +options+.
8
+ # Allows you to declare a +name+, or if nil grabs a pseudo-random
9
+ # name. Also allows you to pass in a hash of +options+
10
+ # that will be passed to the +form_tag+.
11
+ # Takes a +form_tag+ as a string or symbol, the +object_name+ as a string
12
+ # or symbol, a +name+ as a string or symbol, a hash of +options+ that will
13
+ # be passed to the +form_tag+, and an optional +block+.
14
+ #
15
+ # If you pass in a +block+ you will have access to the pre-made tag and the
16
+ # pseudo-randomly created name.
17
+ #
18
+ # ==== Examples
19
+ #
20
+ # mead_honeypot(:text_field, :user)
21
+ # # => <input type="text" name="user[honeypot]" id=user_honeypot">
22
+ #
23
+ # mead_honeypot(:text_field, :user) do |honeypot, name|
24
+ # label(:user, name)
25
+ # honeypot
26
+ # # => <label for="name">
27
+ # # <input type="text" name="user[name]" id="user_name">
28
+ def mead_honeypot(form_tag, object_name, name = nil, options = {}, &block)
29
+ defaults = {value: nil}.merge(mead_input_attributes).merge(options)
30
+ name ||= mead_field_name
31
+ form_tag = :text_field unless [:text_field, :text_area].include?(form_tag)
32
+
33
+ html = tag_class(form_tag).new(object_name, name, self, defaults).render
34
+
35
+ if block_given?
36
+ capture(html, name, &block)
37
+ else
38
+ html
39
+ end
40
+ end
41
+
42
+ # Creates an obfuscation on forms that will be appropriately namespaced.
43
+ #
44
+ # Returns an input tag of type +input_type+ that will get the +method+ value
45
+ # from the +object_name+ that is passed in. Can alternatively be provided with
46
+ # a +block+ that will give the user access to a pre-made input tag, the
47
+ # obfuscated name, and the name of the +method+ that was passed in.
48
+ #
49
+ # ==== Examples
50
+ #
51
+ # mead_obfuscate(:checkbox, :user, :active)
52
+ # # => <input type="checkbox" name="user[foo]" id="user_foo">
53
+ #
54
+ # mead_obfuscate(:checkbox, :user, :active) do |html, obfus_name, real_name|
55
+ # label(:user, obfus_name, real_name)
56
+ # html
57
+ # # => <label for="user_foo">Active</label>
58
+ # # <input type="hidden" value="0" name="user[foo]" id="user_foo">
59
+ # # <input type="checkbox" value="1" name="user[foo]" id="user_foo">
60
+ def mead_obfuscate(input_type, object_name, method, options = {}, &block)
61
+ real_name = method.to_s
62
+ obfuscated_name = mead_obfuscate_field(real_name)
63
+ options.merge!({
64
+ obfuscated_name: obfuscated_name,
65
+ type: input_type.to_s
66
+ })
67
+
68
+ html = Tags::ObfuscatedTag.new(object_name, real_name, self, options).render
69
+
70
+ if block_given?
71
+ capture(html, obfuscated_name, real_name.titleize, &block)
72
+ else
73
+ html
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def tag_class(value)
80
+ "ActionView::Helpers::Tags::#{value.to_s.camelize}".constantize
81
+ end
82
+ end
83
+
84
+ module Tags
85
+ class ObfuscatedTag < Base
86
+ def render
87
+ options = @options.stringify_keys
88
+ options['type'] ||= 'text'
89
+
90
+ if options['type'].to_s == 'checkbox'
91
+ include_hidden = options.fetch('include_hidden') { true }
92
+ options['value'] = options.fetch('value') { '1' }
93
+ @unchecked_value = options.fetch('unchecked_value') { '0' }
94
+ end
95
+
96
+ options['value'] = options.fetch('value') { value_before_type_cast(object) }
97
+ add_default_name_and_id(options)
98
+
99
+ options.delete 'obfuscated_name'
100
+ checkbox = tag 'input', options
101
+
102
+ if include_hidden
103
+ hidden = hidden_field_for_checkbox(options)
104
+ hidden + checkbox
105
+ else
106
+ checkbox
107
+ end
108
+ end
109
+
110
+ private
111
+
112
+ def sanitized_method_name
113
+ @sanitized_method_name ||= @options.stringify_keys['obfuscated_name']
114
+ end
115
+
116
+ def hidden_field_for_checkbox(options)
117
+ @unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value)) : "".html_safe
118
+ end
119
+ # this is how it is done in some later versions
120
+ # We may need to consider different ways of setting the tag_name and tag_id
121
+ # independent of versions
122
+ # def tag_name(multiple = false, index = nil)
123
+ # obfuscated = @options.stringify_keys['obfuscated_name'] || sanitized_method_name
124
+ # @template_object.field_name(@object_name, obfuscated, multiple: multiple, index: index)
125
+ # end
126
+
127
+ # def tag_id(index = nil, namespace = nil)
128
+ # obfuscated = @options.stringify_keys['obfuscated_name'] || @method_name
129
+ # @template_object.field_id(@object_name, obfuscated, index: index, namespace: namespace)
130
+ # end
131
+ end
132
+ end
133
+
134
+ class FormBuilder
135
+ def mead_obfuscate(form_tag, method, options = {}, &block)
136
+ @template.public_send(:mead_obfuscate, form_tag, @object_name, method, options, &block)
137
+ end
138
+
139
+ def mead_honeypot(form_tag = nil, name = nil, options = {}, &block)
140
+ @template.public_send(:mead_honeypot, form_tag, @object_name, name, options, &block)
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,63 @@
1
+ module ActionView
2
+ module Helpers
3
+ module FormTagHelper
4
+
5
+ # Creates a honeypot on forms
6
+ # By default it creates a <div> that wraps around a <label> and
7
+ # <input type="text">. Can also use it as a content_tag that will
8
+ # allow you to do more exotic layouts.
9
+ #
10
+ # Examples
11
+ #
12
+ # honeypot_field_tag
13
+ #
14
+ # # => <div>
15
+ # # <label for="pseudo_random_field_name">
16
+ # # <input type="text" name="pseudo_random_field_name" id="pseudo_random_field_name">
17
+ # # </div>
18
+ #
19
+ # honeypot_field_tag(:label) do |name|
20
+ # check_box_tag(:do_not_check, name, false, class: 'mead-input-attributes')
21
+ #
22
+ # # => <label class="mead-label-attributes">
23
+ # # <input id="name", name="name", type="checkbox", value="false">
24
+ # # </label>
25
+ def mead_honeypot_tag(options = {}, tag: nil, name: nil, value: nil, &block)
26
+ options = options.stringify_keys
27
+ label_options = options.delete('label_options') || {}
28
+ wrapper_options = options.delete('wrapper_options') || {}
29
+
30
+ name ||= mead_field_name
31
+ tag ||= :div
32
+
33
+ if block_given?
34
+ content_tag(tag, options, &block)
35
+ else
36
+ label = label_tag(name, name.titleize, mead_label_attributes.merge(label_options))
37
+ field = text_field_tag(name, value, mead_input_attributes.merge(options))
38
+
39
+ content_tag(tag, label + field, mead_wrapper_attributes.merge(wrapper_options))
40
+ end
41
+ end
42
+
43
+ # Obfuscates the name of a field and provides the name to a block.
44
+ #
45
+ # Examples
46
+ # mead_obfuscate_tag(:first_name) do |first_name|
47
+ # label_tag first_name
48
+ # text_field_tag first_name
49
+ #
50
+ # # => <label for="obfuscated_first_name">
51
+ # # <input name="obfuscated_fist_name" id="obfuscated_first_name" type="text">
52
+ #
53
+ # By default it creates a text_field_tag and obfuscates the name of the field.
54
+ def mead_obfuscate_tag(name, &block)
55
+ if block_given?
56
+ capture(mead_obfuscate_field(name), &block)
57
+ else
58
+ mead_obfuscate_field(name)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,134 @@
1
+ module MeadCaptcha
2
+ module Helpers
3
+ def self.included(base)
4
+ helper_methods = self.protected_instance_methods
5
+
6
+ base.send :helper_method, helper_methods
7
+ end
8
+
9
+ def on_honeypot_failure
10
+ head :ok if honeypot_present?
11
+ end
12
+
13
+ def honeypot_present?
14
+ honeypot_params = params.permit!.to_hash
15
+
16
+ honeypot_params.extend Hashie::Extensions::DeepFind
17
+
18
+ honeypot_field_names.each do |honeypot|
19
+ return true if honeypot_params.deep_find(honeypot).present?
20
+ end
21
+
22
+ false
23
+ rescue StandardError => e
24
+ false
25
+ end
26
+
27
+ def mead_params(masked = nil, parameterize: true)
28
+ masked = if masked.nil?
29
+ params.permit!.to_hash
30
+ elsif masked.is_a?(ActionController::Parameters)
31
+ masked.permit!.to_hash
32
+ else
33
+ masked
34
+ end
35
+
36
+ unmasked = {}
37
+
38
+ masked.map do |key, value|
39
+ key = deobfuscate(key)
40
+ if value.is_a? Hash
41
+ value = mead_params(value, parameterize: false)
42
+ elsif value.is_a? Array
43
+ value = value.flat_map { |v| mead_params(v, parameterize: false) }
44
+ end
45
+
46
+ unmasked[key] = value
47
+ end
48
+
49
+ if parameterize
50
+ ActionController::Parameters.new(unmasked)
51
+ else
52
+ unmasked
53
+ end
54
+ end
55
+
56
+ protected
57
+ # Each of the below methods can be overwritten. Of note are
58
+ # the *_attributes methods which can be used to easily customize
59
+ # the options of the <div>, <label>, and <input> fields.
60
+ #
61
+ # You can also provide your own custom list of honeypot field names
62
+ # by writing your own honeypot_field_names method.
63
+
64
+ def mead_field_name
65
+ @mead_field_names ||= honeypot_field_names
66
+
67
+ raise NoAvailableHoneypotFieldNames if @mead_field_names.empty?
68
+
69
+ @mead_field_names.delete @mead_field_names.sample
70
+ end
71
+
72
+ def mead_wrapper_attributes
73
+ {
74
+ aria: { hidden: true},
75
+ class: 'mead-style-attributes'
76
+ }
77
+ end
78
+
79
+ def mead_input_attributes
80
+ {
81
+ aria: { hidden: true},
82
+ class: 'mead-style-attributes',
83
+ tabindex: -1
84
+ }
85
+ end
86
+
87
+ def mead_label_attributes
88
+ {
89
+ aria: { hidden: true},
90
+ tabindex: -1
91
+ }
92
+ end
93
+
94
+ def honeypot_field_names
95
+ %w(
96
+ secret
97
+ passphrase
98
+ real_password
99
+ a_password
100
+ your_comments
101
+ a_comment
102
+ )
103
+ end
104
+
105
+ def mead_obfuscate_field(name)
106
+ real_name = extract_name(name.to_s)
107
+ encrypted = obfuscate(real_name)
108
+
109
+ name.to_s.gsub(/#{real_name}/, encrypted)
110
+ end
111
+
112
+ private
113
+
114
+ def obfuscate(value)
115
+ hash = {value: value, random: SecureRandom.hex(12)}
116
+ Base64.urlsafe_encode64(JSON.dump(hash)).tr('=', '')
117
+ end
118
+
119
+ def deobfuscate(value)
120
+ hash = JSON.load(Base64.urlsafe_decode64(value))
121
+ hash['value']
122
+ rescue JSON::ParserError, ArgumentError
123
+ value
124
+ end
125
+
126
+ def extract_name(name)
127
+ real_name = name.scan(/\[[^\[\]]+\]/).last || name
128
+ real_name.tr('[]', '')
129
+ end
130
+ end
131
+ end
132
+
133
+ # This will give access to the views/tags to any of the above needed public/protected methods.
134
+ ActionController::Base.send(:include, MeadCaptcha::Helpers) if defined?(ActionController::Base)
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MeadCaptcha
4
+ VERSION = "0.1.2"
5
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mead_captcha/configuration'
4
+ require_relative 'mead_captcha/version'
5
+ require 'mead_captcha/form_tag_helper'
6
+ require 'mead_captcha/form_helper'
7
+ require 'mead_captcha/engine'
8
+ require 'mead_captcha/helpers'
9
+ require 'hashie'
10
+ require 'json'
11
+
12
+ class NoAvailableHoneypotFieldNames < StandardError; end
13
+
14
+ module MeadCaptcha
15
+ class << self
16
+ attr_accessor :configuration
17
+ end
18
+
19
+ def self.configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def self.configure
24
+ yield(configuration)
25
+ end
26
+
27
+ def self.included(base)
28
+ helper_methods = self.protected_instance_methods
29
+
30
+ base.send :helper_method, helper_methods
31
+
32
+ if base.respond_to? :before_action
33
+ base.send :prepend_before_action, :on_honeypot_failure, self.protect_controller_actions
34
+
35
+ elsif base.respond_to? :before_filter
36
+ base.send :prepend_before_filter, :on_honeypot_failure, self.protect_controller_actions
37
+ end
38
+ end
39
+
40
+ def self.protect_controller_actions
41
+ options = {}
42
+
43
+ if configuration.protect_controller_actions.present?
44
+ options[:only] = configuration.protect_controller_actions
45
+ elsif configuration.ignore_controller_actions.present?
46
+ options[:except] = configuration.ignore_controller_actions
47
+ end
48
+
49
+ options
50
+ end
51
+ end
52
+
53
+ ActionController::Base.send(:include, MeadCaptcha) if defined?(ActionController::Base)
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/mead_captcha/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mead_captcha"
7
+ spec.version = MeadCaptcha::VERSION
8
+ spec.authors = ["cwagrant"]
9
+ spec.email = ["cwagrant@gmail.com"]
10
+
11
+ spec.summary = "Makes it easy to add honeypots to your forms and obfuscate your params"
12
+ spec.description = "To help with keep the bots at bay this gem helps you easily add honeypots to forms and/or obfuscate your form field names"
13
+ spec.homepage = "https://github.com/cwagrant/mead"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+ #
33
+ spec.add_development_dependency "actionview"
34
+ spec.add_development_dependency "rake", "~> 13.0"
35
+ spec.add_development_dependency "rails", ">4.0"
36
+ spec.add_development_dependency "rspec-rails"
37
+ spec.add_development_dependency "pry"
38
+ spec.add_development_dependency "railties", [">= 0"]
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency "rspec-html-matchers"
41
+
42
+ spec.add_dependency "hashie"
43
+
44
+ # For more information and examples about making a new gem, checkout our
45
+ # guide at: https://bundler.io/guides/creating_gem.html
46
+ end
@@ -0,0 +1,9 @@
1
+ .mead-style-attributes {
2
+ opacity: 0;
3
+ position: absolute;
4
+ top: 0;
5
+ left: 0;
6
+ height: 0;
7
+ width: 0;
8
+ z-index: -1;
9
+ }
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mead_captcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - cwagrant
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionview
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: railties
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-html-matchers
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: hashie
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: To help with keep the bots at bay this gem helps you easily add honeypots
140
+ to forms and/or obfuscate your form field names
141
+ email:
142
+ - cwagrant@gmail.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - lib/mead_captcha.rb
157
+ - lib/mead_captcha/configuration.rb
158
+ - lib/mead_captcha/engine.rb
159
+ - lib/mead_captcha/form_helper.rb
160
+ - lib/mead_captcha/form_tag_helper.rb
161
+ - lib/mead_captcha/helpers.rb
162
+ - lib/mead_captcha/version.rb
163
+ - mead_captcha.gemspec
164
+ - vendor/assets/stylesheets/mead/style.css
165
+ homepage: https://github.com/cwagrant/mead
166
+ licenses:
167
+ - MIT
168
+ metadata:
169
+ homepage_uri: https://github.com/cwagrant/mead
170
+ source_code_uri: https://github.com/cwagrant/mead
171
+ changelog_uri: https://github.com/cwagrant/mead
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: 2.4.0
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubygems_version: 3.2.19
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Makes it easy to add honeypots to your forms and obfuscate your params
191
+ test_files: []