rack_do_app_platform_safelist 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a6bce93ff200590683a6ac90c0ac3d2d830336ee8dd751b688af93c86b2d4ef6
4
+ data.tar.gz: 3212a885b1e37ac65de4cd4f5bfc36f0c843bee1e1d350d5af286623d8188834
5
+ SHA512:
6
+ metadata.gz: 2518416145149e6aeff291dc2f20c71f5322c8af859be76f7121ab8863006306b73a997e4f55e060f838af055f4db9666290693fb49312db9b686ea4aa2199c2
7
+ data.tar.gz: 6109e59f4205f5e21cb0cbcbad687d32c5389f7caac7683e9e73f5e1ed0a624d201a1ae5655162791d07503e6a09ed64570e96d1dbdad428368fb13c73e98074
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2023-02-11
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "debug", "~> 1.7"
8
+ gem "minitest", "~> 5.0"
9
+ gem "rack-test", "~> 2.0"
10
+ gem "rake", "~> 13.0"
11
+ gem "rubocop"
12
+ gem "rubocop-minitest"
13
+ gem "rubocop-rake"
14
+ gem "simplecov", "~> 0.22"
data/Gemfile.lock ADDED
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack_do_app_platform_safelist (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ debug (1.7.1)
11
+ docile (1.4.0)
12
+ json (2.6.3)
13
+ minitest (5.17.0)
14
+ parallel (1.22.1)
15
+ parser (3.2.1.0)
16
+ ast (~> 2.4.1)
17
+ rack (3.0.4.1)
18
+ rack-test (2.0.2)
19
+ rack (>= 1.3)
20
+ rainbow (3.1.1)
21
+ rake (13.0.6)
22
+ regexp_parser (2.7.0)
23
+ rexml (3.2.5)
24
+ rubocop (1.45.1)
25
+ json (~> 2.3)
26
+ parallel (~> 1.10)
27
+ parser (>= 3.2.0.0)
28
+ rainbow (>= 2.2.2, < 4.0)
29
+ regexp_parser (>= 1.8, < 3.0)
30
+ rexml (>= 3.2.5, < 4.0)
31
+ rubocop-ast (>= 1.24.1, < 2.0)
32
+ ruby-progressbar (~> 1.7)
33
+ unicode-display_width (>= 2.4.0, < 3.0)
34
+ rubocop-ast (1.26.0)
35
+ parser (>= 3.2.1.0)
36
+ rubocop-minitest (0.27.0)
37
+ rubocop (>= 0.90, < 2.0)
38
+ rubocop-rake (0.6.0)
39
+ rubocop (~> 1.0)
40
+ ruby-progressbar (1.11.0)
41
+ simplecov (0.22.0)
42
+ docile (~> 1.1)
43
+ simplecov-html (~> 0.11)
44
+ simplecov_json_formatter (~> 0.1)
45
+ simplecov-html (0.12.3)
46
+ simplecov_json_formatter (0.1.4)
47
+ unicode-display_width (2.4.2)
48
+
49
+ PLATFORMS
50
+ x86_64-linux
51
+
52
+ DEPENDENCIES
53
+ debug (~> 1.7)
54
+ minitest (~> 5.0)
55
+ rack-test (~> 2.0)
56
+ rack_do_app_platform_safelist!
57
+ rake (~> 13.0)
58
+ rubocop
59
+ rubocop-minitest
60
+ rubocop-rake
61
+ simplecov (~> 0.22)
62
+
63
+ BUNDLED WITH
64
+ 2.4.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Blayne Farinha
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,85 @@
1
+ # Rack DigitalOcean AppPlatform Safelist
2
+
3
+ Simple rack middleware for ruby applications hosted on Digital Ocean app platform to block ip addresses that are not
4
+ safelisted via an environment variable.
5
+
6
+ Simply add an environment variable called ALLOWED_IPS that contains a comma seperated list of ips that are allowed to
7
+ access you application (you can use CIDR notation as well to safelist ips).
8
+
9
+ Example:
10
+
11
+ ALLOWED_IPS = "8.8.8.8, 8.8.4.4, 1.2.3.0/24"
12
+
13
+ ## Getting started
14
+
15
+ ### Installing
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ # In your Gemfile
21
+
22
+ gem "rack_do_app_platform_safelist"
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install rack_do_app_platform_safelist
32
+
33
+ ### Plugging into the application
34
+
35
+ Then tell your ruby web application to use the middleware.
36
+
37
+ a) For __rails__ applications.
38
+
39
+ ```ruby
40
+ # In config/initializers/rack_do_app_platform_safelist.rb
41
+
42
+ require "rack_do_app_platform_safelist"
43
+
44
+ Rails.application.config.middleware.insert_before(0, RackDoAppPlatformSafelist, logger: Rails.logger)
45
+ ```
46
+
47
+ This will insert the middleware at the top of the middleware stack so it can block request before reaching any other
48
+ middleware in your application
49
+
50
+ b) For __rack__ applications:
51
+
52
+ ```ruby
53
+ # In config.ru
54
+
55
+ require "rack_do_app_platform_safelist"
56
+
57
+ use RackDoAppPlatformSafelist
58
+ run App.new
59
+ ```
60
+
61
+ If you want to use a different environment variable for the whitelisted IPs you can pass it as an argument
62
+
63
+ a) For __rails__ applications.
64
+
65
+ ```ruby
66
+ # In config/initializers/rack_do_app_platform_safelist.rb
67
+
68
+ require "rack_do_app_platform_safelist"
69
+
70
+ Rails.application.config.middleware.insert_before(0, RackDoAppPlatformSafelist, logger: Rails.logger,
71
+ env_key: "SAFELISTED_IPS")
72
+ ```
73
+
74
+ b) For __rack__ applications:
75
+
76
+ ```ruby
77
+ # In config.ru
78
+
79
+ require "rack_do_app_platform_safelist"
80
+
81
+ use RackDoAppPlatformSafelist, env_key: "SAFELISTED_IPS"
82
+ run App.new
83
+ ```
84
+
85
+ The middleware will now look for the IP addresses to safelist in the SAFELISTED_IPS environment variable.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RackDoAppPlatformSafelist
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ipaddr"
4
+ require "logger"
5
+
6
+ require_relative "rack_do_app_platform_safelist/version"
7
+
8
+ # Rack middleware for digital ocean app platform that will block any ip address that is not saflisted using the
9
+ # ALLOWED_IPS enviroment variable by default.
10
+ #
11
+ # Eg:
12
+ # ALLOWED_IPS = "8.8.8.8,8.8.4.4,1.2.3.0/24"
13
+ class RackDoAppPlatformSafelist
14
+ attr_reader :allowed_ips
15
+
16
+ # @param logger [#info] the logger to use for messages. If nil it will use the value set in "rack.logger" if present
17
+ # or create a new logger instance.
18
+ # @param env_key [#to_s] the environment variable to use to get the safelisted ips.
19
+ def initialize(app, logger: nil, env_key: "ALLOWED_IPS")
20
+ @app = app
21
+ @logger = logger
22
+ @allowed_ips = ENV.fetch(env_key.to_s, "").split(",").map(&:strip).uniq.map { |ip| IPAddr.new(ip) }
23
+ end
24
+
25
+ def call(env)
26
+ client_ip = extract_client_ip(env)
27
+ return @app.call(env) if safelisted_ip?(client_ip)
28
+
29
+ logger(env).info(error_message(client_ip))
30
+ [403, { "content-type" => "text/plain" }, ["Forbidden\n"]]
31
+ end
32
+
33
+ private
34
+
35
+ def safelisted_ip?(client_ip)
36
+ return false if client_ip.nil?
37
+
38
+ IPAddr.new(client_ip).then { |ip| allowed_ips.any? { |allowed_ip| allowed_ip.include?(ip) } }
39
+ end
40
+
41
+ def extract_client_ip(env)
42
+ # DigitalOcean's load balancer appends the client ip and the load balancer's ip to the X-Forwarded-For header so
43
+ # since we know there will always be one load balancer infront of your application on App Platform we can split the
44
+ # string on commas and the client ip will always be the second to last item in the array.
45
+ env["HTTP_X_FORWARDED_FOR"].to_s.split(",")[-2]
46
+ end
47
+
48
+ def error_message(client_ip)
49
+ message = client_ip.nil? ? "Could not determine client ip address" : "IP address #{client_ip} is not safelisted"
50
+
51
+ "Blocking request - #{message}"
52
+ end
53
+
54
+ def logger(env)
55
+ if @logger
56
+ @logger
57
+ elsif env["rack.logger"]
58
+ env["rack.logger"]
59
+ else
60
+ ::Logger.new(env["rack.errors"])
61
+ end
62
+
63
+ # return @logger if @logger
64
+ # return env["rack.logger"] if env["rack.logger"]
65
+
66
+ # ::Logger.new(env["rack.errors"])
67
+ end
68
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rack_do_app_platform_safelist/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rack_do_app_platform_safelist"
7
+ spec.version = RackDoAppPlatformSafelist::VERSION
8
+ spec.authors = ["Blayne Farinha"]
9
+ spec.email = ["blayne.farinha@gmail.com"]
10
+
11
+ spec.summary = "Rack middleware for safelisting IP addresses in DigitalOcean's App Platform"
12
+ spec.description = "Rack middleware for safelisting IP addresses using an environment variables in DigitalOcean's " \
13
+ "App Platform"
14
+ spec.homepage = "https://github.com/blafri/rack_do_app_platform_whitelist"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 3.0.0"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/blafri/rack_do_app_platform_whitelist"
20
+ spec.metadata["changelog_uri"] = "https://github.com/blafri/rack_do_app_platform_whitelist/blob/main/CHANGELOG.md"
21
+ spec.metadata["rubygems_mfa_required"] = "true"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_do_app_platform_safelist
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Blayne Farinha
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Rack middleware for safelisting IP addresses using an environment variables
14
+ in DigitalOcean's App Platform
15
+ email:
16
+ - blayne.farinha@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/rack_do_app_platform_safelist.rb
29
+ - lib/rack_do_app_platform_safelist/version.rb
30
+ - rack_do_app_platform_safelist.gemspec
31
+ homepage: https://github.com/blafri/rack_do_app_platform_whitelist
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/blafri/rack_do_app_platform_whitelist
36
+ source_code_uri: https://github.com/blafri/rack_do_app_platform_whitelist
37
+ changelog_uri: https://github.com/blafri/rack_do_app_platform_whitelist/blob/main/CHANGELOG.md
38
+ rubygems_mfa_required: 'true'
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.4.6
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Rack middleware for safelisting IP addresses in DigitalOcean's App Platform
58
+ test_files: []