verboten_keys 1.0.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: 25f79502f4a51d148ef9fc89c1c6883fd754868bbc27f8c53db220e4a4ad227b
4
+ data.tar.gz: c688bf6e1dc5ebb4e75dd670cb9b81950ae161f454e7258356ce46c454d3b73d
5
+ SHA512:
6
+ metadata.gz: 06c3248d1379afb108ee4f98f3805a44162fb986f3b58a483079c1afd205dae9c128a679e4f3aff0aed0220bea6c3cc83988050cdd10be0da3816b3d80f41a45
7
+ data.tar.gz: 6ced8772a5aa49723113b76f109be121332a06ed6401adc7ed2b7063afc5e768b25e91e2573fe183b6f9f764e40f61c226e2ce357502597d877eb07ef0ece01c
@@ -0,0 +1,26 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ name: 'Test Suite'
6
+ strategy:
7
+ matrix:
8
+ ruby: [2.5, 2.6, 2.7, 3.0]
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - uses: ruby/setup-ruby@v1
13
+ with:
14
+ ruby-version: ${{ matrix.ruby }}
15
+ bundler-cache: true
16
+ - run: bundle exec rake
17
+ lint:
18
+ name: 'Rubocop'
19
+ runs-on: ubuntu-latest
20
+ steps:
21
+ - uses: actions/checkout@v2
22
+ - uses: ruby/setup-ruby@v1
23
+ with:
24
+ ruby-version: 3.0
25
+ bundler-cache: true
26
+ - run: bundle exec rubocop
data/.gitignore ADDED
@@ -0,0 +1,13 @@
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
12
+
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,39 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.5
4
+
5
+ Lint/DuplicateMethods:
6
+ Enabled: false
7
+
8
+ Layout/HashAlignment:
9
+ Enabled: false
10
+
11
+ Layout/LineLength:
12
+ Max: 120
13
+ Exclude:
14
+ - 'lib/verboten_keys/errors.rb'
15
+ - '**/*.gemspec'
16
+
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - 'Rakefile'
20
+ - '**/*.rake'
21
+ - 'spec/**/*.rb'
22
+ - '**/*.gemspec'
23
+
24
+ Metrics/CyclomaticComplexity:
25
+ Enabled: false
26
+
27
+ Metrics/MethodLength:
28
+ Enabled: false
29
+
30
+ Style/Documentation:
31
+ Enabled: false
32
+
33
+ Style/StringLiterals:
34
+ Enabled: true
35
+ EnforcedStyle: single_quotes
36
+
37
+ Style/StringLiteralsInInterpolation:
38
+ Enabled: true
39
+ EnforcedStyle: single_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 - May 11, 2021
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at hi@tpritc.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,119 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ verboten_keys (1.0.0)
5
+ rack (>= 1.0, < 3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.1.3.2)
11
+ actionview (= 6.1.3.2)
12
+ activesupport (= 6.1.3.2)
13
+ rack (~> 2.0, >= 2.0.9)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.1.3.2)
18
+ activesupport (= 6.1.3.2)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activesupport (6.1.3.2)
24
+ concurrent-ruby (~> 1.0, >= 1.0.2)
25
+ i18n (>= 1.6, < 2)
26
+ minitest (>= 5.1)
27
+ tzinfo (~> 2.0)
28
+ zeitwerk (~> 2.3)
29
+ ast (2.4.2)
30
+ builder (3.2.4)
31
+ concurrent-ruby (1.1.8)
32
+ crass (1.0.6)
33
+ diff-lcs (1.4.4)
34
+ erubi (1.10.0)
35
+ i18n (1.8.10)
36
+ concurrent-ruby (~> 1.0)
37
+ loofah (2.9.1)
38
+ crass (~> 1.0.2)
39
+ nokogiri (>= 1.5.9)
40
+ method_source (1.0.0)
41
+ minitest (5.14.4)
42
+ nokogiri (1.11.3-x86_64-darwin)
43
+ racc (~> 1.4)
44
+ nokogiri (1.11.3-x86_64-linux)
45
+ racc (~> 1.4)
46
+ parallel (1.20.1)
47
+ parser (3.0.1.1)
48
+ ast (~> 2.4.1)
49
+ racc (1.5.2)
50
+ rack (2.2.3)
51
+ rack-test (1.1.0)
52
+ rack (>= 1.0, < 3)
53
+ rails-dom-testing (2.0.3)
54
+ activesupport (>= 4.2.0)
55
+ nokogiri (>= 1.6)
56
+ rails-html-sanitizer (1.3.0)
57
+ loofah (~> 2.3)
58
+ railties (6.1.3.2)
59
+ actionpack (= 6.1.3.2)
60
+ activesupport (= 6.1.3.2)
61
+ method_source
62
+ rake (>= 0.8.7)
63
+ thor (~> 1.0)
64
+ rainbow (3.0.0)
65
+ rake (13.0.3)
66
+ regexp_parser (2.1.1)
67
+ rexml (3.2.5)
68
+ rspec (3.10.0)
69
+ rspec-core (~> 3.10.0)
70
+ rspec-expectations (~> 3.10.0)
71
+ rspec-mocks (~> 3.10.0)
72
+ rspec-core (3.10.1)
73
+ rspec-support (~> 3.10.0)
74
+ rspec-expectations (3.10.1)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.10.0)
77
+ rspec-mocks (3.10.2)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.10.0)
80
+ rspec-support (3.10.2)
81
+ rubocop (1.14.0)
82
+ parallel (~> 1.10)
83
+ parser (>= 3.0.0.0)
84
+ rainbow (>= 2.2.2, < 4.0)
85
+ regexp_parser (>= 1.8, < 3.0)
86
+ rexml
87
+ rubocop-ast (>= 1.5.0, < 2.0)
88
+ ruby-progressbar (~> 1.7)
89
+ unicode-display_width (>= 1.4.0, < 3.0)
90
+ rubocop-ast (1.5.0)
91
+ parser (>= 3.0.1.1)
92
+ rubocop-rake (0.5.1)
93
+ rubocop
94
+ rubocop-rspec (2.3.0)
95
+ rubocop (~> 1.0)
96
+ rubocop-ast (>= 1.1.0)
97
+ ruby-progressbar (1.11.0)
98
+ thor (1.1.0)
99
+ tzinfo (2.0.4)
100
+ concurrent-ruby (~> 1.0)
101
+ unicode-display_width (2.0.0)
102
+ zeitwerk (2.4.2)
103
+
104
+ PLATFORMS
105
+ x86_64-darwin-20
106
+ x86_64-linux
107
+
108
+ DEPENDENCIES
109
+ activesupport (~> 6.1)
110
+ railties (>= 4.2)
111
+ rake (~> 13.0)
112
+ rspec (~> 3.0)
113
+ rubocop (~> 1.7)
114
+ rubocop-rake (~> 0.5)
115
+ rubocop-rspec (~> 2.3)
116
+ verboten_keys!
117
+
118
+ BUNDLED WITH
119
+ 2.2.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Tom Pritchard
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,90 @@
1
+ # Verboten Keys
2
+
3
+ Verboten Keys is a last line of defense to help prevent you and your team from accidentally leaking private information via your APIs. It's Rack middleware that seamlessly integrates into any Rails or Sinatra project (or really anything that's based on Rack) and strips out any data that matches your list of forbidden keys.
4
+
5
+ It's a quick, easy, set-it-and-forget-it way to have peace of mind that nothing's getting out of your API that should be.
6
+
7
+ ## What it does
8
+
9
+ Imagine you've got an API endpoint that returns a user's profile, and you've accidentally serialized the user incorrectly, and now it's returning your entire user object serialized as JSON:
10
+
11
+ ```
12
+ GET /api/v1/users/123
13
+ {
14
+ 'id': 123,
15
+ 'name': 'Jane Doe',
16
+ 'email': 'jane.doe@example.com',
17
+ 'password_digest': '$FEUNCqbSZIOu7e1QblI...'
18
+ }
19
+ ```
20
+
21
+ Oh no, this is a disaster!
22
+
23
+ If only there was a way to automatically filter out accidents like this! This is where Verboten Keys helps out. If you had Verboten Keys in your application, and had `password_digest` set as a forbidden key, the exact same response would look like this:
24
+
25
+ ```
26
+ GET /api/v1/users/123
27
+ {
28
+ 'id': 123,
29
+ 'name': 'Jane Doe',
30
+ 'email': 'jane.doe@example.com'
31
+ }
32
+ ```
33
+
34
+ Verboten Keys is your last line of defense. When all else fails, we prevent you accidentally leaking private data.
35
+
36
+ ## Installation
37
+
38
+ To install Verboten Keys in your app, simply add this line to your application's `Gemfile` and run `bundle install`:
39
+
40
+ ```ruby
41
+ gem 'verboten-keys'
42
+ ```
43
+
44
+ ### Rails
45
+
46
+ If you're using Rails, you don't need to do anything else to install Verboten Keys. The gem will automatically plug itself into Rails when your application boots.
47
+
48
+ ### Sinatra
49
+
50
+ If your application is using Sinatra, simply add the Verboten Keys middleware into your app the same way you would any other piece of middleware:
51
+
52
+ ```ruby
53
+ require 'sinatra'
54
+ require 'verboten-keys'
55
+
56
+ use Rack::Lint
57
+ use VerbotenKeys::Middleware
58
+
59
+ get '/hello' do
60
+ 'Hello World'
61
+ end
62
+ ```
63
+
64
+ You should include it last, so nothing gets missed when the middleware parses and evaluates your application's response.
65
+
66
+ ## Configuration
67
+
68
+ Every application has its own security needs, and Verboten Keys is designed to be configurable, so you can get it just so. To configure Verboten Keys, simply call its `configure` method, which takes a block and yields the current configuration:
69
+
70
+ ```ruby
71
+ VerbotenKeys.configure do |config|
72
+ config.forbidden_keys = [:password, :password_digest, :secret_token]
73
+ config.strategy = :remove
74
+ end
75
+ ```
76
+
77
+ The `forbidden_keys` option lets you set the keys that will be filtered out of the response. It takes an array of symbols, and will raise an error if it's not in the right format. You should include all of the columns and attributes you absolutely _do not_ want to ever leak from your API. The default value is `[]`.
78
+
79
+ The `strategy` option lets you pick how Verboten Keys should handle a forbidden key it finds. The default value is `:remove`. Acceptable options are `:remove` and `:nullify`:
80
+
81
+ * `:remove` removes the key-value pair from the JSON response body, so it looks like the JSON object never had the key-value pair in the first place.
82
+ * `:nullify` leaves the key in the JSON response, but it will nullify the value, so any forbidden values will always appear to be `nil`.
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/tpritc/verboten-keys). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tpritc/verboten-keys/blob/main/CODE_OF_CONDUCT.md) while interacting in the project's codebases, issue trackers, chat rooms, and mailing lists.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). If you or your organization need a custom, commercial license for any reason, [send me an email](mailto:hi@tpritc.com) and I'll be happy to set something up for you.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
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 'verboten_keys'
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,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack'
4
+
5
+ require_relative 'verboten_keys/version'
6
+ require_relative 'verboten_keys/errors'
7
+ require_relative 'verboten_keys/configuration'
8
+ require_relative 'verboten_keys/filterer'
9
+ require_relative 'verboten_keys/middleware'
10
+ require_relative 'verboten_keys/railtie' if defined?(::Rails)
11
+
12
+ module VerbotenKeys
13
+ class << self
14
+ attr_accessor :configuration
15
+
16
+ def configure
17
+ yield(configuration)
18
+ end
19
+
20
+ def configuration
21
+ @configuration ||= Configuration.new
22
+ end
23
+
24
+ def reset
25
+ @configuration = Configuration.new
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VerbotenKeys
4
+ class Configuration
5
+ STRATEGIES = %i[remove nullify].freeze
6
+
7
+ attr_accessor :forbidden_keys, :strategy
8
+
9
+ def initialize
10
+ @forbidden_keys = []
11
+ @strategy = :remove
12
+ end
13
+
14
+ def forbidden_keys=(new_forbidden_keys)
15
+ unless new_forbidden_keys.is_a? Array
16
+ raise VerbotenKeys::Errors::ForbiddenKeysMustBeAnArrayError, new_forbidden_keys
17
+ end
18
+
19
+ new_forbidden_keys.each do |new_forbidden_key|
20
+ next if new_forbidden_key.is_a? Symbol
21
+
22
+ raise VerbotenKeys::Errors::ForbiddenKeysMustOnlyContainSymbolsError.new(new_forbidden_keys, new_forbidden_key)
23
+ end
24
+
25
+ @forbidden_keys = new_forbidden_keys
26
+ end
27
+
28
+ def strategy=(new_strategy)
29
+ raise VerbotenKeys::Errors::StrategyMustBeASymbolError, new_strategy unless new_strategy.is_a? Symbol
30
+
31
+ raise VerbotenKeys::Errors::StrategyNotFoundError, new_strategy unless STRATEGIES.include? new_strategy
32
+
33
+ @strategy = new_strategy
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VerbotenKeys
4
+ module Errors
5
+ class ForbiddenKeysMustBeAnArrayError < StandardError
6
+ def initialize(invalid_forbidden_keys)
7
+ invalid_forbidden_keys_string = invalid_forbidden_keys&.to_s || 'nil'
8
+ invalid_forbidden_keys_class = invalid_forbidden_keys.class.to_s
9
+ super("VerbotenKeys' forbidden_keys must be an array of symbols. You passed in #{invalid_forbidden_keys_string}, which was a #{invalid_forbidden_keys_class}.")
10
+ end
11
+ end
12
+
13
+ class ForbiddenKeysMustOnlyContainSymbolsError < StandardError
14
+ def initialize(invalid_forbidden_keys, invalid_forbidden_key)
15
+ invalid_forbidden_key_string = invalid_forbidden_key&.to_s || 'nil'
16
+ invalid_forbidden_key_class = invalid_forbidden_key.class.to_s
17
+ super("VerbotenKeys' forbidden_keys must be an array of symbols. You passed in #{invalid_forbidden_keys}, which included #{invalid_forbidden_key_string}, which was a #{invalid_forbidden_key_class}")
18
+ end
19
+ end
20
+
21
+ class StrategyMustBeASymbolError < StandardError
22
+ def initialize(invalid_strategy)
23
+ invalid_strategy_string = invalid_strategy&.to_s || 'nil'
24
+ invalid_strategy_class = invalid_strategy.class.to_s
25
+ super("VerbotenKeys' strategy must be a symbol. You passed in #{invalid_strategy_string}, which was a #{invalid_strategy_class}.")
26
+ end
27
+ end
28
+
29
+ class StrategyNotFoundError < StandardError
30
+ def initialize(invalid_strategy)
31
+ super("VerbotenKeys' strategy must be a valid option. You passed in :#{invalid_strategy}, but the only valid options are: #{VerbotenKeys::Configuration::STRATEGIES}.")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VerbotenKeys
4
+ class Filterer
5
+ def self.filter_forbidden_keys(hash)
6
+ hash
7
+ .map { |k, v| evaluate_key_value_pair(k, v) }
8
+ .to_h
9
+ .delete_if { |k, _v| k.nil? }
10
+ end
11
+
12
+ def self.evaluate_key_value_pair(key, value)
13
+ key = key.to_sym
14
+
15
+ if VerbotenKeys.configuration.forbidden_keys.include?(key)
16
+ case VerbotenKeys.configuration.strategy
17
+ when :remove
18
+ return [nil, nil]
19
+ when :nullify
20
+ return [key, nil]
21
+ else
22
+ raise VerbotenKeys::Errors::StrategyNotFoundError(VerbotenKeys.configuration.strategy)
23
+ end
24
+ end
25
+
26
+ case value
27
+ when Hash
28
+ return [key, VerbotenKeys::Filterer.filter_forbidden_keys(value)]
29
+ when Array
30
+ return [key, value.map { |item| item.is_a?(Hash) ? VerbotenKeys::Filterer.filter_forbidden_keys(item) : item }]
31
+ end
32
+
33
+ [key, value]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VerbotenKeys
4
+ class Middleware
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ # Get the result of the request from the app.
11
+ @status, @headers, @response = @app.call(env)
12
+
13
+ # Perform all of our work here...
14
+ remove_forbidden_keys
15
+
16
+ # Let the app continue as-is.
17
+ [@status, @headers, @response]
18
+ end
19
+
20
+ private
21
+
22
+ def remove_forbidden_keys
23
+ return unless content_type_is_json?
24
+
25
+ new_response = VerbotenKeys::Filterer.filter_forbidden_keys(response_body_as_a_hash).to_json
26
+
27
+ @headers['Content-Length'] = new_response.bytesize.to_s
28
+ @response = [new_response]
29
+ end
30
+
31
+ # Check's the response's headers to see if the response's content-type is a
32
+ # JSON object.
33
+ #
34
+ # @return [Boolean] True if the content type of the response if JSON, and
35
+ # false if it is not.
36
+ def content_type_is_json?
37
+ return false if @headers['Content-Type'].nil?
38
+
39
+ @headers['Content-Type'].split(';').first == 'application/json'
40
+ end
41
+
42
+ def response_body_as_a_hash
43
+ if @response.is_a? Array
44
+ JSON.parse(@response.first || '{}')
45
+ else
46
+ JSON.parse(@response.body || '{}')
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VerbotenKeys
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'verboten_keys.middleware' do |app|
6
+ app.middleware.use VerbotenKeys::Middleware
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VerbotenKeys
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/verboten_keys/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'verboten_keys'
7
+ spec.version = VerbotenKeys::VERSION
8
+ spec.authors = ['Tom Pritchard']
9
+ spec.email = ['hi@tpritc.com']
10
+
11
+ spec.summary = 'Verboten Keys is a last line of defense to help prevent you and your team from accidentally leaking private information via your APIs.'
12
+ spec.homepage = 'https://github.com/tpritc/verboten_keys'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
15
+
16
+ spec.metadata['source_code_uri'] = 'https://github.com/tpritc/verboten_keys'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/tpritc/verboten_keys/blob/main/CHANGELOG.md'
18
+
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_runtime_dependency 'rack', '>= 1.0', '< 3'
27
+ spec.add_development_dependency 'activesupport', '~> 6.1'
28
+ spec.add_development_dependency 'railties', '>= 4.2'
29
+ spec.add_development_dependency 'rake', '~> 13.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rubocop', '~> 1.7'
32
+ spec.add_development_dependency 'rubocop-rake', '~> 0.5'
33
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.3'
34
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: verboten_keys
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Pritchard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '6.1'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '6.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: railties
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '4.2'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '4.2'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '13.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '13.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.7'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.7'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.5'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.5'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rubocop-rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '2.3'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '2.3'
131
+ description:
132
+ email:
133
+ - hi@tpritc.com
134
+ executables:
135
+ - console
136
+ - setup
137
+ extensions: []
138
+ extra_rdoc_files: []
139
+ files:
140
+ - ".github/workflows/ci.yml"
141
+ - ".gitignore"
142
+ - ".rspec"
143
+ - ".rubocop.yml"
144
+ - CHANGELOG.md
145
+ - CODE_OF_CONDUCT.md
146
+ - Gemfile
147
+ - Gemfile.lock
148
+ - LICENSE.txt
149
+ - README.md
150
+ - Rakefile
151
+ - bin/console
152
+ - bin/setup
153
+ - lib/verboten_keys.rb
154
+ - lib/verboten_keys/configuration.rb
155
+ - lib/verboten_keys/errors.rb
156
+ - lib/verboten_keys/filterer.rb
157
+ - lib/verboten_keys/middleware.rb
158
+ - lib/verboten_keys/railtie.rb
159
+ - lib/verboten_keys/version.rb
160
+ - verboten_keys.gemspec
161
+ homepage: https://github.com/tpritc/verboten_keys
162
+ licenses:
163
+ - MIT
164
+ metadata:
165
+ source_code_uri: https://github.com/tpritc/verboten_keys
166
+ changelog_uri: https://github.com/tpritc/verboten_keys/blob/main/CHANGELOG.md
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: 2.5.0
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubygems_version: 3.2.3
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Verboten Keys is a last line of defense to help prevent you and your team
186
+ from accidentally leaking private information via your APIs.
187
+ test_files: []