katalyst-basic-auth 1.0.0 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6986b0c9b3f2e9731aaead2c1b74db0008be2ab82ee91e377b772caad2a9397d
4
- data.tar.gz: 87daff02ae81a92bcb90d3d24be50e7109e70fcf865521fc5edb8bcf861dfde5
3
+ metadata.gz: d8a5aad0435c2f6a2b342329168f0ed68d0c541803f80bbf1fab1993545545d7
4
+ data.tar.gz: 4906fb1519f090f47d5b5a5ac93b4cdeebd0bcc633b45ade268299f4150f9ee5
5
5
  SHA512:
6
- metadata.gz: 7936209ed48a5403081c7a89f5842af9fd9bb5828cb15a6c82a57fa0a8ae2708aa25dd797acbd6b92bd675174853365fd2b8a01fc30d2de356235d151b5f7ad5
7
- data.tar.gz: 1371ef21dd3f0a059d929e2e4e095567873b440002a449fd2e35a622cc85753e9679dd368ea432d8595465db03c4cc3ebc238215af64d6849d2f0a1a46d3e915
6
+ metadata.gz: 1617b9b1a07663056906985e029d413fb765e8b42b6031d559f2b4fba95235c339fa4ae95682a94a1d90c656bf706021d294680a08ba29b6dbd82427af075795
7
+ data.tar.gz: ffe15df4fdd8a3eda37ac7c11a53d7fd6a1c6c3a702c4c7a8ad2c57c16da28a1a9b913426d1a25b1ae3d95cdb55d6b45bea7609416ffecad1780d1797a4428ac
data/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## [2.0.0] - 2026-11-26
2
+
3
+ - Breaking: when more than one rule matches a request, the rule added last applies; previously the most specific path
4
+ applied regardless of order
5
+ - Breaking: drops support for Ruby < 4
6
+ - Add support for `Regexp` paths, e.g. `exclude(%r{/clients/[^/]+/webhooks/[^/]+})`; patterns must match the whole
7
+ request path
8
+ - String paths are matched literally, as in 1.0.1; apps that relied on regular expressions in string paths should
9
+ upgrade to 2.0 and pass a `Regexp` instead
10
+
11
+ ## [1.0.1] - 2026-11-26
12
+
13
+ - Fix: normalise repeated slashes before matching paths, e.g. `//admin` is matched as `/admin`
14
+ - Fix: match configured paths on segment boundaries, e.g. excluding `/up` no longer excludes `/uploads`
15
+ - Breaking (unintentional): string paths are matched literally, so regular expressions in string paths such as
16
+ `/clients/.*/webhooks/.*` no longer match; upgrade to 2.0 and use a `Regexp` for patterns
17
+
18
+ ## [1.0.0] - 2025-08-04
19
+
20
+ - Drop support for old ruby versions
21
+
1
22
  ## [0.5.0] - 2024-04-26
2
23
 
3
24
  - Add `/up` as a default allowed path (Rails 7.1+ health check)
data/README.md CHANGED
@@ -29,7 +29,7 @@ The following environment variables can optionally be defined to configure the g
29
29
 
30
30
  | Environment Variable | Description |
31
31
  | ------ | ----- |
32
- | KATALYST_BASIC_AUTH_ENABLED | If "yes" or "true", the middleware will be enabled. By default, the middleware is enabled on staging and uat Rails environments |
32
+ | KATALYST_BASIC_AUTH_ENABLED | If "yes" or "true", the middleware will be enabled. By default, the middleware is enabled on staging and uat Rails environments. To disable basic auth in these environments, you must explicitly set this to a non-true value. |
33
33
  | KATALYST_BASIC_AUTH_USER | The username for basic authentication. Default is the Rails application name in lowercase. |
34
34
  | KATALYST_BASIC_AUTH_PASS | The password for basic authentication. A password will be generated if not set. |
35
35
  | KATALYST_BASIC_AUTH_IP_ALLOWLIST | Comma or space separated list of IP addresses or CIDR ranges to allow without basic auth |
@@ -48,6 +48,18 @@ Paths can also be excluded from basic auth. e.g. to allow access to `/public` wi
48
48
 
49
49
  Katalyst::Basic::Auth.exclude("/public")
50
50
 
51
+ Paths match themselves and anything below them, e.g. `/public` matches `/public` and `/public/robots.txt` but not
52
+ `/publications`. Use a `Regexp` to match other paths. A pattern must match the whole request path:
53
+
54
+ Katalyst::Basic::Auth.exclude(%r{/clients/[^/]+/webhooks/[^/]+})
55
+
56
+ When more than one rule matches a request, the rule added last applies, so add general rules before their exceptions:
57
+
58
+ Katalyst::Basic::Auth.exclude("/admin")
59
+ Katalyst::Basic::Auth.add("/admin/secure", username: "user", password: "pass")
60
+
61
+ Requests that match no rule use the global settings.
62
+
51
63
  ## Development
52
64
 
53
65
  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.
@@ -15,20 +15,26 @@ module Katalyst
15
15
  include Enumerable
16
16
 
17
17
  # @param path [String] Request path
18
- # @return [Config] The config for the given path
18
+ # @return [Config] The last rule added that matches the given path
19
19
  def for_path(path)
20
- path ||= ROOT_PATH
21
- all.sort_by(&:path)
22
- .reverse
23
- .detect { |i| path.match(/^#{i.path}/) } || global
20
+ path = normalize_path(path)
21
+ all.rfind { |i| i.match?(path) }
22
+ end
23
+
24
+ # Compare paths the way the Rails router will see them, e.g. "//admin" routes to "/admin"
25
+ # @param path [String, nil] Request path
26
+ # @return [String] Path with repeated slashes collapsed
27
+ def normalize_path(path)
28
+ "/#{path}".squeeze("/")
24
29
  end
25
30
 
26
31
  # @return [Config] The global configuration
27
32
  def global
28
- all[0]
33
+ all.first
29
34
  end
30
35
 
31
- # @param path [String] Relative path
36
+ # Adds a rule that takes priority over the rules added before it
37
+ # @param path [String, Regexp] Relative path, or a pattern that must match the whole request path
32
38
  # @param username [String] Basic auth user name
33
39
  # @param password [String] Basic auth password
34
40
  # @param enabled [Boolean] True to enable basic auth for this path
@@ -41,7 +47,6 @@ module Katalyst
41
47
  enabled: enabled,
42
48
  ip_allowlist: ip_allowlist,
43
49
  )
44
- all.delete(all.detect { |i| i.path == config.path })
45
50
  all << config
46
51
  config
47
52
  end
@@ -50,12 +55,14 @@ module Katalyst
50
55
  new(path: path, enabled: false)
51
56
  end
52
57
 
58
+ # @return [Array<Config>] Rules in the order they were added, starting with the global configuration and
59
+ # the Rails health check
53
60
  def all
54
61
  @all ||= [new, up]
55
62
  end
56
63
 
57
64
  def reset!
58
- @all = [new, up]
65
+ @all = nil
59
66
  end
60
67
 
61
68
  def each(&)
@@ -126,6 +133,20 @@ module Katalyst
126
133
  path == ROOT_PATH
127
134
  end
128
135
 
136
+ def pattern?
137
+ path.is_a?(Regexp)
138
+ end
139
+
140
+ # Paths match themselves and anything below them, on path segment boundaries,
141
+ # e.g. "/up" matches "/up" and "/up/db" but not "/uploads".
142
+ # Patterns must match the whole request path, e.g. %r{/clients/[^/]+/webhooks/[^/]+}.
143
+ # @param request_path [String] Normalized request path
144
+ def match?(request_path)
145
+ return /\A(?:#{path})\z/.match?(request_path) if pattern?
146
+
147
+ request_path == path || request_path.start_with?("#{path.chomp('/')}/")
148
+ end
149
+
129
150
  def allow_ip?(env)
130
151
  request = ::Rack::Request.new(env)
131
152
  return false unless request.ip
@@ -136,7 +157,7 @@ module Katalyst
136
157
 
137
158
  def description
138
159
  output = []
139
- output << "path: #{root_path? ? '(global)' : path}"
160
+ output << "path: #{root_path? ? '(global)' : path_description}"
140
161
  output << "enabled: #{enabled?}"
141
162
  output << "username: #{username}"
142
163
  output << "password: #{password}"
@@ -146,7 +167,11 @@ module Katalyst
146
167
 
147
168
  private
148
169
 
149
- # @param path [String] Relative path
170
+ def path_description
171
+ pattern? ? path.inspect : path
172
+ end
173
+
174
+ # @param path [String, Regexp] Relative path, or a pattern that must match the whole request path
150
175
  # @param username [String] Basic auth user name
151
176
  # @param password [String] Basic auth password
152
177
  # @param enabled [Boolean] True to enable basic auth for this path
@@ -161,9 +186,10 @@ module Katalyst
161
186
 
162
187
  def sanitize_path(path)
163
188
  return ROOT_PATH if path.nil?
189
+ return path if path.is_a?(Regexp)
164
190
 
165
191
  path = "/#{path}" unless path.start_with?("/")
166
- path
192
+ path == ROOT_PATH ? path : path.chomp("/")
167
193
  end
168
194
 
169
195
  def initialize_ip_allowlist(ip_allowlist)
@@ -9,7 +9,7 @@ module Katalyst
9
9
  module Auth
10
10
  class << self
11
11
  # Add a path to be protected by basic authentication
12
- # @param path [String] Relative path
12
+ # @param path [String, Regexp] Relative path, or a pattern that must match the whole request path
13
13
  # @param username [String] Basic auth user name
14
14
  # @param password [String] Basic auth password
15
15
  # @param ip_allowlist [Array<String>] List of IP addresses or network ranges to allow without basic auth
@@ -22,7 +22,7 @@ module Katalyst
22
22
  end
23
23
 
24
24
  # Add a path to be excluded from basic authentication
25
- # @param path [String] Relative path
25
+ # @param path [String, Regexp] Relative path, or a pattern that must match the whole request path
26
26
  def exclude(path)
27
27
  Config.add(path: path, enabled: false)
28
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-basic-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
@@ -41,14 +41,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: '3.3'
44
+ version: '4.0'
45
45
  required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  requirements: []
51
- rubygems_version: 3.6.9
51
+ rubygems_version: 4.0.20
52
52
  specification_version: 4
53
53
  summary: Gem to add basic auth on staging websites
54
54
  test_files: []