Skip to content

Commit c6098eb

Browse files
committed
add IP filtering example
1 parent 2de7558 commit c6098eb

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

docs/auth_plugins.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,34 @@ auth:
417417
secret_env_key: SUPER_COOL_SECRET # the name of the environment variable containing the shared secret - used by `fetch_secret(config)` in the plugin
418418
header: Authorization
419419
```
420+
421+
Here is a mini example of how you might do some sort of IP filtering in a custom auth plugin:
422+
423+
```ruby
424+
# frozen_string_literal: true
425+
# Example custom auth plugin for IP filtering
426+
module Hooks
427+
module Plugins
428+
module Auth
429+
class IpFilteringPlugin < Base
430+
def self.valid?(payload:, headers:, config:)
431+
# Get the allowed IPs from the configuration (opts is a hash containing additional options that can be set in any endpoint configuration)
432+
allowed_ips = config.dig(:opts, :allowed_ips) || []
433+
434+
# Get the request IP from headers or payload
435+
# Find the IP via the request headers with case-insensitive matching - this is a helper method available in the base class
436+
# so it is available to all auth plugins.
437+
# This example assumes the IP is in the "X-Forwarded-For" header, which is common for proxied requests
438+
request_ip = find_header_value(headers, "X-Forwarded-For")
439+
440+
# If the request IP is not found, return false
441+
return false unless request_ip
442+
443+
# Return true if the request IP is in the allowed IPs list
444+
allowed_ips.include?(request_ip)
445+
end
446+
end
447+
end
448+
end
449+
end
450+
```

lib/hooks/app/rack_env_builder.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def build_base_environment
7272
end
7373

7474
# Add HTTP headers to the environment with proper Rack naming convention
75+
# Note: This will generally add headers like HTTP_X_CUSTOM_HEADER. For example, the HTTP_X_FORWARDED_FOR
76+
# is a common header that is used to pass the original client IP address through proxies.
7577
#
7678
# @param rack_env [Hash] Environment hash to modify
7779
def add_http_headers(rack_env)

0 commit comments

Comments
 (0)