aikido-zen 1.0.2.beta.2-aarch64-linux
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 +7 -0
- data/.aikido +6 -0
- data/.ruby-version +1 -0
- data/.simplecov +26 -0
- data/.standard.yml +3 -0
- data/LICENSE +674 -0
- data/README.md +146 -0
- data/Rakefile +67 -0
- data/benchmarks/README.md +23 -0
- data/benchmarks/rails7.1_sql_injection.js +70 -0
- data/docs/banner.svg +202 -0
- data/docs/config.md +125 -0
- data/docs/proxy.md +10 -0
- data/docs/rails.md +114 -0
- data/lib/aikido/zen/actor.rb +116 -0
- data/lib/aikido/zen/agent/heartbeats_manager.rb +66 -0
- data/lib/aikido/zen/agent.rb +179 -0
- data/lib/aikido/zen/api_client.rb +145 -0
- data/lib/aikido/zen/attack.rb +207 -0
- data/lib/aikido/zen/background_worker.rb +52 -0
- data/lib/aikido/zen/capped_collections.rb +68 -0
- data/lib/aikido/zen/collector/hosts.rb +15 -0
- data/lib/aikido/zen/collector/routes.rb +66 -0
- data/lib/aikido/zen/collector/sink_stats.rb +95 -0
- data/lib/aikido/zen/collector/stats.rb +111 -0
- data/lib/aikido/zen/collector/users.rb +30 -0
- data/lib/aikido/zen/collector.rb +144 -0
- data/lib/aikido/zen/config.rb +282 -0
- data/lib/aikido/zen/context/rack_request.rb +24 -0
- data/lib/aikido/zen/context/rails_request.rb +44 -0
- data/lib/aikido/zen/context.rb +112 -0
- data/lib/aikido/zen/detached_agent/agent.rb +78 -0
- data/lib/aikido/zen/detached_agent/front_object.rb +37 -0
- data/lib/aikido/zen/detached_agent/server.rb +78 -0
- data/lib/aikido/zen/detached_agent.rb +2 -0
- data/lib/aikido/zen/errors.rb +107 -0
- data/lib/aikido/zen/event.rb +71 -0
- data/lib/aikido/zen/internals.rb +103 -0
- data/lib/aikido/zen/libzen-v0.1.39-aarch64-linux.so +0 -0
- data/lib/aikido/zen/middleware/check_allowed_addresses.rb +26 -0
- data/lib/aikido/zen/middleware/middleware.rb +11 -0
- data/lib/aikido/zen/middleware/rack_throttler.rb +48 -0
- data/lib/aikido/zen/middleware/request_tracker.rb +192 -0
- data/lib/aikido/zen/middleware/set_context.rb +26 -0
- data/lib/aikido/zen/outbound_connection.rb +45 -0
- data/lib/aikido/zen/outbound_connection_monitor.rb +23 -0
- data/lib/aikido/zen/package.rb +22 -0
- data/lib/aikido/zen/payload.rb +50 -0
- data/lib/aikido/zen/rails_engine.rb +56 -0
- data/lib/aikido/zen/rate_limiter/breaker.rb +61 -0
- data/lib/aikido/zen/rate_limiter/bucket.rb +76 -0
- data/lib/aikido/zen/rate_limiter/result.rb +31 -0
- data/lib/aikido/zen/rate_limiter.rb +50 -0
- data/lib/aikido/zen/request/heuristic_router.rb +115 -0
- data/lib/aikido/zen/request/rails_router.rb +77 -0
- data/lib/aikido/zen/request/schema/auth_discovery.rb +86 -0
- data/lib/aikido/zen/request/schema/auth_schemas.rb +54 -0
- data/lib/aikido/zen/request/schema/builder.rb +121 -0
- data/lib/aikido/zen/request/schema/definition.rb +107 -0
- data/lib/aikido/zen/request/schema/empty_schema.rb +28 -0
- data/lib/aikido/zen/request/schema.rb +87 -0
- data/lib/aikido/zen/request.rb +122 -0
- data/lib/aikido/zen/route.rb +39 -0
- data/lib/aikido/zen/runtime_settings/endpoints.rb +49 -0
- data/lib/aikido/zen/runtime_settings/ip_set.rb +36 -0
- data/lib/aikido/zen/runtime_settings/protection_settings.rb +62 -0
- data/lib/aikido/zen/runtime_settings/rate_limit_settings.rb +47 -0
- data/lib/aikido/zen/runtime_settings.rb +65 -0
- data/lib/aikido/zen/scan.rb +75 -0
- data/lib/aikido/zen/scanners/path_traversal/helpers.rb +65 -0
- data/lib/aikido/zen/scanners/path_traversal_scanner.rb +63 -0
- data/lib/aikido/zen/scanners/shell_injection/helpers.rb +159 -0
- data/lib/aikido/zen/scanners/shell_injection_scanner.rb +64 -0
- data/lib/aikido/zen/scanners/sql_injection_scanner.rb +93 -0
- data/lib/aikido/zen/scanners/ssrf/dns_lookups.rb +27 -0
- data/lib/aikido/zen/scanners/ssrf/private_ip_checker.rb +97 -0
- data/lib/aikido/zen/scanners/ssrf_scanner.rb +265 -0
- data/lib/aikido/zen/scanners/stored_ssrf_scanner.rb +49 -0
- data/lib/aikido/zen/scanners.rb +7 -0
- data/lib/aikido/zen/sink.rb +118 -0
- data/lib/aikido/zen/sinks/action_controller.rb +83 -0
- data/lib/aikido/zen/sinks/async_http.rb +80 -0
- data/lib/aikido/zen/sinks/curb.rb +113 -0
- data/lib/aikido/zen/sinks/em_http.rb +83 -0
- data/lib/aikido/zen/sinks/excon.rb +118 -0
- data/lib/aikido/zen/sinks/file.rb +112 -0
- data/lib/aikido/zen/sinks/http.rb +93 -0
- data/lib/aikido/zen/sinks/httpclient.rb +95 -0
- data/lib/aikido/zen/sinks/httpx.rb +78 -0
- data/lib/aikido/zen/sinks/kernel.rb +33 -0
- data/lib/aikido/zen/sinks/mysql2.rb +31 -0
- data/lib/aikido/zen/sinks/net_http.rb +101 -0
- data/lib/aikido/zen/sinks/patron.rb +103 -0
- data/lib/aikido/zen/sinks/pg.rb +72 -0
- data/lib/aikido/zen/sinks/resolv.rb +62 -0
- data/lib/aikido/zen/sinks/socket.rb +78 -0
- data/lib/aikido/zen/sinks/sqlite3.rb +46 -0
- data/lib/aikido/zen/sinks/trilogy.rb +31 -0
- data/lib/aikido/zen/sinks/typhoeus.rb +78 -0
- data/lib/aikido/zen/sinks.rb +36 -0
- data/lib/aikido/zen/sinks_dsl.rb +250 -0
- data/lib/aikido/zen/synchronizable.rb +24 -0
- data/lib/aikido/zen/system_info.rb +84 -0
- data/lib/aikido/zen/version.rb +10 -0
- data/lib/aikido/zen/worker.rb +87 -0
- data/lib/aikido/zen.rb +246 -0
- data/lib/aikido-zen.rb +3 -0
- data/placeholder/.gitignore +4 -0
- data/placeholder/README.md +11 -0
- data/placeholder/Rakefile +75 -0
- data/placeholder/lib/placeholder.rb.template +3 -0
- data/placeholder/placeholder.gemspec.template +20 -0
- data/tasklib/bench.rake +94 -0
- data/tasklib/libzen.rake +133 -0
- data/tasklib/wrk.rb +88 -0
- metadata +205 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../context"
|
4
|
+
|
5
|
+
module Aikido::Zen
|
6
|
+
# @!visibility private
|
7
|
+
ENV_KEY = "aikido.context"
|
8
|
+
|
9
|
+
module Middleware
|
10
|
+
# Rack middleware that keeps the current context in a Thread/Fiber-local
|
11
|
+
# variable so that other parts of the agent/firewall can access it.
|
12
|
+
class SetContext
|
13
|
+
def initialize(app)
|
14
|
+
@app = app
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
Aikido::Zen.current_context = env[ENV_KEY] = Context.from_rack_env(env)
|
19
|
+
|
20
|
+
@app.call(env)
|
21
|
+
ensure
|
22
|
+
Aikido::Zen.current_context = nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Aikido::Zen
|
4
|
+
# Simple data object to identify connections performed to outbound servers.
|
5
|
+
class OutboundConnection
|
6
|
+
# Convenience factory to create connection descriptions out of URI objects.
|
7
|
+
#
|
8
|
+
# @param uri [URI]
|
9
|
+
# @return [Aikido::Zen::OutboundConnection]
|
10
|
+
def self.from_uri(uri)
|
11
|
+
new(host: uri.hostname, port: uri.port)
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [String] the hostname or IP address to which the connection was
|
15
|
+
# attempted.
|
16
|
+
attr_reader :host
|
17
|
+
|
18
|
+
# @return [Integer] the port number to which the connection was attempted.
|
19
|
+
attr_reader :port
|
20
|
+
|
21
|
+
def initialize(host:, port:)
|
22
|
+
@host = host
|
23
|
+
@port = port
|
24
|
+
end
|
25
|
+
|
26
|
+
def as_json
|
27
|
+
{hostname: host, port: port}
|
28
|
+
end
|
29
|
+
|
30
|
+
def ==(other)
|
31
|
+
other.is_a?(OutboundConnection) &&
|
32
|
+
host == other.host &&
|
33
|
+
port == other.port
|
34
|
+
end
|
35
|
+
alias_method :eql?, :==
|
36
|
+
|
37
|
+
def hash
|
38
|
+
[host, port].hash
|
39
|
+
end
|
40
|
+
|
41
|
+
def inspect
|
42
|
+
"#<#{self.class.name} #{host}:#{port}>"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Aikido::Zen
|
4
|
+
# This simple callable follows the Scanner API so that it can be injected into
|
5
|
+
# any Sink that wraps an HTTP library, and lets us keep track of any hosts to
|
6
|
+
# which the app communicates over HTTP.
|
7
|
+
module OutboundConnectionMonitor
|
8
|
+
def self.skips_on_nil_context?
|
9
|
+
false
|
10
|
+
end
|
11
|
+
|
12
|
+
# This simply reports the connection to the Agent, and always returns +nil+
|
13
|
+
# as it's not scanning for any particular attack.
|
14
|
+
#
|
15
|
+
# @param connection [Aikido::Zen::OutboundConnection]
|
16
|
+
# @return [nil]
|
17
|
+
def self.call(connection:, **)
|
18
|
+
Aikido::Zen.track_outbound(connection)
|
19
|
+
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "sink"
|
4
|
+
|
5
|
+
module Aikido::Zen
|
6
|
+
Package = Struct.new(:name, :version) do
|
7
|
+
def initialize(name, version, sinks = Aikido::Zen::Sinks.registry)
|
8
|
+
super(name, version)
|
9
|
+
@sinks = sinks
|
10
|
+
end
|
11
|
+
|
12
|
+
# @return [Boolean] whether we explicitly protect against exploits in this
|
13
|
+
# library.
|
14
|
+
def supported?
|
15
|
+
@sinks.include?(name)
|
16
|
+
end
|
17
|
+
|
18
|
+
def as_json
|
19
|
+
{name => version.to_s}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Aikido::Zen
|
4
|
+
# An individual user input in a request, which may come from different
|
5
|
+
# sources (query string, body, cookies, etc).
|
6
|
+
class Payload
|
7
|
+
attr_reader :value, :source, :path
|
8
|
+
|
9
|
+
def initialize(value, source, path)
|
10
|
+
@value = value
|
11
|
+
@source = source
|
12
|
+
@path = path
|
13
|
+
end
|
14
|
+
|
15
|
+
UNKNOWN_PAYLOAD = Payload.new("unknown", "unknown", "unknown")
|
16
|
+
|
17
|
+
alias_method :to_s, :value
|
18
|
+
|
19
|
+
def ==(other)
|
20
|
+
other.is_a?(Payload) &&
|
21
|
+
other.value == value &&
|
22
|
+
other.source == source &&
|
23
|
+
other.path == path
|
24
|
+
end
|
25
|
+
|
26
|
+
def as_json
|
27
|
+
{
|
28
|
+
payload: value.to_s,
|
29
|
+
source: SOURCE_SERIALIZATIONS[source],
|
30
|
+
path: path.to_s
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
SOURCE_SERIALIZATIONS = {
|
35
|
+
query: "query",
|
36
|
+
body: "body",
|
37
|
+
header: "headers",
|
38
|
+
cookie: "cookies",
|
39
|
+
route: "routeParams",
|
40
|
+
graphql: "graphql",
|
41
|
+
xml: "xml",
|
42
|
+
subdomain: "subdomains"
|
43
|
+
}
|
44
|
+
|
45
|
+
def inspect
|
46
|
+
val = (value.to_s.size > 128) ? value[0..125] + "..." : value
|
47
|
+
"#<Aikido::Zen::Payload #{source}(#{path}) #{val.inspect}>"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_dispatch"
|
4
|
+
|
5
|
+
module Aikido::Zen
|
6
|
+
class RailsEngine < ::Rails::Engine
|
7
|
+
config.before_configuration do
|
8
|
+
# Access library configuration at `Rails.application.config.zen`.
|
9
|
+
config.zen = Aikido::Zen.config
|
10
|
+
end
|
11
|
+
|
12
|
+
initializer "aikido.add_middleware" do |app|
|
13
|
+
app.middleware.use Aikido::Zen::Middleware::SetContext
|
14
|
+
app.middleware.use Aikido::Zen::Middleware::CheckAllowedAddresses
|
15
|
+
# Request Tracker stats do not consider failed request or 40x, so the middleware
|
16
|
+
# must be the last one wrapping the request.
|
17
|
+
app.middleware.use Aikido::Zen::Middleware::RequestTracker
|
18
|
+
|
19
|
+
ActiveSupport.on_load(:action_controller) do
|
20
|
+
# Due to how Rails sets up its middleware chain, the routing is evaluated
|
21
|
+
# (and the Request object constructed) in the app that terminates the
|
22
|
+
# chain, so no amount of middleware will be able to access it.
|
23
|
+
#
|
24
|
+
# This way, we overwrite the Request object as early as we can in the
|
25
|
+
# request handling, so that by the time we start evaluating inputs, we
|
26
|
+
# have assigned the request correctly.
|
27
|
+
before_action { Aikido::Zen.current_context.update_request(request) }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
initializer "aikido.configuration" do |app|
|
32
|
+
# Allow the logger to be configured before checking if disabled? so we can
|
33
|
+
# let the user know that the agent is disabled.
|
34
|
+
logger = ::Rails.logger
|
35
|
+
logger = logger.tagged("aikido") if logger.respond_to?(:tagged)
|
36
|
+
app.config.zen.logger = logger
|
37
|
+
|
38
|
+
app.config.zen.request_builder = Aikido::Zen::Context::RAILS_REQUEST_BUILDER
|
39
|
+
|
40
|
+
# Plug Rails' JSON encoder/decoder, but only if the user hasn't changed
|
41
|
+
# them for something else.
|
42
|
+
if app.config.zen.json_encoder == Aikido::Zen::Config::DEFAULT_JSON_ENCODER
|
43
|
+
app.config.zen.json_encoder = ActiveSupport::JSON.method(:encode)
|
44
|
+
end
|
45
|
+
|
46
|
+
if app.config.zen.json_decoder == Aikido::Zen::Config::DEFAULT_JSON_DECODER
|
47
|
+
app.config.zen.json_decoder = ActiveSupport::JSON.method(:decode)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
config.after_initialize do
|
52
|
+
# Start the Aikido Agent only once the application starts.
|
53
|
+
Aikido::Zen.start!
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "bucket"
|
4
|
+
|
5
|
+
module Aikido::Zen
|
6
|
+
# @api private
|
7
|
+
#
|
8
|
+
# Circuit breaker that rate limits internal API requests in two ways: By using
|
9
|
+
# a sliding window, to allow only a certain number of events over that window,
|
10
|
+
# and with the ability of manually being tripped open when the API responds to
|
11
|
+
# a request with a 429.
|
12
|
+
class RateLimiter::Breaker
|
13
|
+
def initialize(config: Aikido::Zen.config, clock: RateLimiter::Bucket::DEFAULT_CLOCK)
|
14
|
+
@config = config
|
15
|
+
@clock = clock
|
16
|
+
|
17
|
+
@bucket = RateLimiter::Bucket.new(
|
18
|
+
ttl: config.client_rate_limit_period,
|
19
|
+
max_size: config.client_rate_limit_max_events,
|
20
|
+
clock: clock
|
21
|
+
)
|
22
|
+
@opened_at = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
# Trip the circuit open to force all events to be throttled until the
|
26
|
+
# deadline passes.
|
27
|
+
#
|
28
|
+
# @see Aikido::Zen::Config#server_rate_limit_deadline
|
29
|
+
# @return [void]
|
30
|
+
def open!
|
31
|
+
@opened_at = @clock.call
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param event_type [String] an event type which we'll use to decide
|
35
|
+
# if we should throttle it.
|
36
|
+
# @return [Boolean]
|
37
|
+
def throttle?(event_type)
|
38
|
+
return true if open? && !try_close
|
39
|
+
|
40
|
+
result = @bucket.increment(event_type)
|
41
|
+
result.throttled?
|
42
|
+
end
|
43
|
+
|
44
|
+
# @!visibility private
|
45
|
+
# @return [Boolean]
|
46
|
+
def open?
|
47
|
+
@opened_at
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def past_deadline?
|
53
|
+
@opened_at < @clock.call - @config.server_rate_limit_deadline
|
54
|
+
end
|
55
|
+
|
56
|
+
def try_close
|
57
|
+
@opened_at = nil if past_deadline?
|
58
|
+
@opened_at.nil?
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../synchronizable"
|
4
|
+
require_relative "result"
|
5
|
+
|
6
|
+
module Aikido::Zen
|
7
|
+
# This models a "sliding window" rate limiting bucket (where we keep a bucket
|
8
|
+
# per endpoint). The timestamps of requests are kept grouped by client, and
|
9
|
+
# when a new request is made, we check if the number of requests falls within
|
10
|
+
# the configured limit.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# bucket = Aikido::Zen::RateLimiter::Bucket.new(ttl: 60, max_size: 3)
|
14
|
+
# bucket.increment("1.2.3.4") #=> true (count for this key: 1)
|
15
|
+
# bucket.increment("1.2.3.4") #=> true (count for this key: 2)
|
16
|
+
#
|
17
|
+
# # 30 seconds go by
|
18
|
+
# bucket.increment("1.2.3.4") #=> true (count for this key: 3)
|
19
|
+
#
|
20
|
+
# # 20 more seconds go by
|
21
|
+
# bucket.increment("1.2.3.4") #=> false (count for this key: 3)
|
22
|
+
#
|
23
|
+
# # 20 more seconds go by
|
24
|
+
# bucket.increment("1.2.3.4") #=> true (count for this key: 2)
|
25
|
+
#
|
26
|
+
class RateLimiter::Bucket
|
27
|
+
prepend Synchronizable
|
28
|
+
|
29
|
+
# @!visibility private
|
30
|
+
#
|
31
|
+
# Use the monotonic clock to ensure time differences are consistent
|
32
|
+
# and not affected by timezones.or daylight savings changes.
|
33
|
+
DEFAULT_CLOCK = -> { Process.clock_gettime(Process::CLOCK_MONOTONIC).round }
|
34
|
+
|
35
|
+
def initialize(ttl:, max_size:, clock: DEFAULT_CLOCK)
|
36
|
+
@ttl = ttl
|
37
|
+
@max_size = max_size
|
38
|
+
@data = Hash.new { |h, k| h[k] = [] }
|
39
|
+
@clock = clock
|
40
|
+
end
|
41
|
+
|
42
|
+
# Increments the key if the number of entries within the current TTL window
|
43
|
+
# is below the configured threshold.
|
44
|
+
#
|
45
|
+
# @param key [String] discriminating key to identify a client.
|
46
|
+
# See {Aikido::Zen::Config#rate_limiting_discriminator}.
|
47
|
+
#
|
48
|
+
# @return [Aikido::Zen::RateLimiter::Result] the result of the operation and
|
49
|
+
# statistics on this bucket for the given key.
|
50
|
+
def increment(key)
|
51
|
+
synchronize do
|
52
|
+
time = @clock.call
|
53
|
+
evict(key, at: time)
|
54
|
+
|
55
|
+
entries = @data[key]
|
56
|
+
throttled = entries.size >= @max_size
|
57
|
+
|
58
|
+
entries << time unless throttled
|
59
|
+
|
60
|
+
RateLimiter::Result.new(
|
61
|
+
throttled: throttled,
|
62
|
+
discriminator: key,
|
63
|
+
current_requests: entries.size,
|
64
|
+
max_requests: @max_size,
|
65
|
+
time_remaining: @ttl - (time - entries.min)
|
66
|
+
)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def evict(key, at: @clock.call)
|
73
|
+
synchronize { @data[key].delete_if { |time| time < (at - @ttl) } }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Aikido::Zen
|
2
|
+
# Holds the stats after checking if a request should be rate limited, which
|
3
|
+
# will be added to the Rack env.
|
4
|
+
class RateLimiter::Result
|
5
|
+
# @return [String] the output of the configured discriminator block, used to
|
6
|
+
# uniquely identify a client (e.g. the remote IP).
|
7
|
+
attr_reader :discriminator
|
8
|
+
|
9
|
+
# @return [Integer] number of requests for the client in the current window.
|
10
|
+
attr_reader :current_requests
|
11
|
+
|
12
|
+
# @return [Integer] configured max number of requests per client.
|
13
|
+
attr_reader :max_requests
|
14
|
+
|
15
|
+
# @return [Integer] number of seconds remaining until the window resets.
|
16
|
+
attr_reader :time_remaining
|
17
|
+
|
18
|
+
def initialize(throttled:, discriminator:, current_requests:, max_requests:, time_remaining:)
|
19
|
+
@throttled = throttled
|
20
|
+
@discriminator = discriminator
|
21
|
+
@current_requests = current_requests
|
22
|
+
@max_requests = max_requests
|
23
|
+
@time_remaining = time_remaining
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Boolean] whether the current request was throttled or not.
|
27
|
+
def throttled?
|
28
|
+
@throttled
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "synchronizable"
|
4
|
+
require_relative "middleware/rack_throttler"
|
5
|
+
|
6
|
+
module Aikido::Zen
|
7
|
+
# Keeps track of all requests in this process, broken up by Route and further
|
8
|
+
# discriminated by client. Provides a single method that checks if a certain
|
9
|
+
# Request needs to be throttled or not.
|
10
|
+
class RateLimiter
|
11
|
+
prepend Synchronizable
|
12
|
+
|
13
|
+
def initialize(
|
14
|
+
config: Aikido::Zen.config,
|
15
|
+
settings: Aikido::Zen.runtime_settings
|
16
|
+
)
|
17
|
+
@config = config
|
18
|
+
@settings = settings
|
19
|
+
@buckets = Hash.new { |store, route|
|
20
|
+
synchronize {
|
21
|
+
settings = settings_for(route)
|
22
|
+
store[route] = Bucket.new(ttl: settings.period, max_size: settings.max_requests)
|
23
|
+
}
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
# Calculate based on the configuration whether a request will be
|
28
|
+
# rate-limited or not.
|
29
|
+
#
|
30
|
+
# @param request [Aikido::Zen::Request]
|
31
|
+
# @return [Aikido::Zen::RateLimiter::Result, nil]
|
32
|
+
def calculate_rate_limits(request)
|
33
|
+
settings = settings_for(request.route)
|
34
|
+
return nil unless settings.enabled?
|
35
|
+
|
36
|
+
bucket = @buckets[request.route]
|
37
|
+
key = @config.rate_limiting_discriminator.call(request)
|
38
|
+
bucket.increment(key)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def settings_for(route)
|
44
|
+
@settings.endpoints[route].rate_limiting
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
require_relative "rate_limiter/bucket"
|
50
|
+
require_relative "rate_limiter/breaker"
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ipaddr"
|
4
|
+
require_relative "../route"
|
5
|
+
require_relative "../request"
|
6
|
+
|
7
|
+
module Aikido::Zen
|
8
|
+
# Simple router implementation that just identifies the currently requested
|
9
|
+
# URL as a route, attempting to heuristically substitute any path segments
|
10
|
+
# that may look like a parameterized value by something descriptive.
|
11
|
+
#
|
12
|
+
# For example, "/categories/123/events/2024-10-01" would be matched as
|
13
|
+
# "/categories/:number/events/:date"
|
14
|
+
class Request::HeuristicRouter
|
15
|
+
# @param request [Aikido::Zen::Request]
|
16
|
+
# @return [Aikido::Zen::Route, nil]
|
17
|
+
def recognize(request)
|
18
|
+
path = parameterize(request.path)
|
19
|
+
Route.new(verb: request.request_method, path: path)
|
20
|
+
end
|
21
|
+
|
22
|
+
private def parameterize(path)
|
23
|
+
return if path.nil?
|
24
|
+
|
25
|
+
path = path.split("/").map { |part| parameterize_segment(part) }.join("/")
|
26
|
+
path.prepend("/") unless path.start_with?("/")
|
27
|
+
path.chomp!("/") if path.size > 1
|
28
|
+
path
|
29
|
+
end
|
30
|
+
|
31
|
+
private def parameterize_segment(segment)
|
32
|
+
case segment
|
33
|
+
when ULID
|
34
|
+
":ulid"
|
35
|
+
when OBJECT_ID
|
36
|
+
":objectId"
|
37
|
+
when NUMBER
|
38
|
+
":number"
|
39
|
+
when UUID
|
40
|
+
":uuid"
|
41
|
+
when DATE
|
42
|
+
":date"
|
43
|
+
when EMAIL
|
44
|
+
":email"
|
45
|
+
when IP
|
46
|
+
":ip"
|
47
|
+
when HASH
|
48
|
+
":hash"
|
49
|
+
when SecretMatcher
|
50
|
+
":secret"
|
51
|
+
else
|
52
|
+
segment
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
NUMBER = /\A\d+\z/
|
57
|
+
HEX = /\A[a-f0-9]+\z/i
|
58
|
+
DATE = /\A\d{4}-\d{2}-\d{2}|\d{2}-\d{2}-\d{4}\z/
|
59
|
+
UUID = /\A
|
60
|
+
(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}
|
61
|
+
| 00000000-0000-0000-0000-000000000000
|
62
|
+
| ffffffff-ffff-ffff-ffff-ffffffffffff
|
63
|
+
)\z/ix
|
64
|
+
ULID = /\A[0-9A-HJKMNP-TV-Z]{26}\z/i
|
65
|
+
OBJECT_ID = /\A[0-9a-f]{24}\z/i
|
66
|
+
EMAIL = /\A
|
67
|
+
[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+
|
68
|
+
@
|
69
|
+
[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
|
70
|
+
(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*
|
71
|
+
\z/x
|
72
|
+
IP = ->(segment) {
|
73
|
+
IPAddr::RE_IPV4ADDRLIKE.match?(segment) ||
|
74
|
+
IPAddr::RE_IPV6ADDRLIKE_COMPRESSED.match?(segment) ||
|
75
|
+
IPAddr::RE_IPV6ADDRLIKE_FULL.match?(segment)
|
76
|
+
}
|
77
|
+
HASH = ->(segment) { [32, 40, 64, 128].include?(segment.size) && HEX === segment }
|
78
|
+
|
79
|
+
class SecretMatcher
|
80
|
+
# Decides if a given string looks random enough to be a "secret".
|
81
|
+
#
|
82
|
+
# @param candidate [String]
|
83
|
+
# @return [Boolean]
|
84
|
+
def self.===(candidate)
|
85
|
+
new(candidate).matches?
|
86
|
+
end
|
87
|
+
|
88
|
+
private def initialize(string)
|
89
|
+
@string = string
|
90
|
+
end
|
91
|
+
|
92
|
+
def matches?
|
93
|
+
return false if @string.size <= MIN_LENGTH
|
94
|
+
return false if SEPARATORS === @string
|
95
|
+
return false unless DIGIT === @string
|
96
|
+
return false if [LOWER, UPPER, SPECIAL].none? { |pattern| pattern === @string }
|
97
|
+
|
98
|
+
ratios = @string.chars.each_cons(MIN_LENGTH).map do |window|
|
99
|
+
window.to_set.size / MIN_LENGTH.to_f
|
100
|
+
end
|
101
|
+
|
102
|
+
ratios.sum / ratios.size > SECRET_THRESHOLD
|
103
|
+
end
|
104
|
+
|
105
|
+
MIN_LENGTH = 10
|
106
|
+
SECRET_THRESHOLD = 0.75
|
107
|
+
|
108
|
+
LOWER = /[[:lower:]]/
|
109
|
+
UPPER = /[[:upper:]]/
|
110
|
+
DIGIT = /[[:digit:]]/
|
111
|
+
SPECIAL = /[!#\$%^&*|;:<>]/
|
112
|
+
SEPARATORS = /[[:space:]]|-/
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../route"
|
4
|
+
require_relative "../request"
|
5
|
+
|
6
|
+
module Aikido::Zen
|
7
|
+
# The Rails router relies on introspecting the routes defined in the Rails
|
8
|
+
# app to match the current request to the correct route, building Route
|
9
|
+
# objects that have the exact pattern defined by the developer, rather than
|
10
|
+
# a heuristic approximation.
|
11
|
+
#
|
12
|
+
# For example, given the following route definitions:
|
13
|
+
#
|
14
|
+
# resources :posts do
|
15
|
+
# resources :comments
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# The router will map a request to "/posts/123/comments/234" to
|
19
|
+
# "/posts/:post_id/comments/:id(.:format)".
|
20
|
+
#
|
21
|
+
# @see Aikido::Zen::Router::HeuristicRouter
|
22
|
+
class Request::RailsRouter
|
23
|
+
def initialize(route_set)
|
24
|
+
@route_set = route_set
|
25
|
+
end
|
26
|
+
|
27
|
+
def recognize(request)
|
28
|
+
recognize_in_route_set(request, @route_set)
|
29
|
+
end
|
30
|
+
|
31
|
+
private def recognize_in_route_set(request, route_set, prefix: nil)
|
32
|
+
# ActionDispatch::Journey::Router#recognize modifies the Rack environment.
|
33
|
+
# This is correct for Rails routing, but it is not expected to be used in
|
34
|
+
# Rack middleware, and using it here can break Rails routing.
|
35
|
+
#
|
36
|
+
# To avoid this, the Rack environment is duplicated when building request.
|
37
|
+
route_set.router.recognize(request) do |route, _|
|
38
|
+
app = route.app
|
39
|
+
next unless app.matches?(request)
|
40
|
+
|
41
|
+
if app.dispatcher?
|
42
|
+
return build_route(route, request, prefix: prefix)
|
43
|
+
end
|
44
|
+
|
45
|
+
if app.engine?
|
46
|
+
# If the SCRIPT_NAME has any path parameters, we want those to be
|
47
|
+
# captured by the router. (eg `mount API => "/api/:version/`)
|
48
|
+
prefix = ActionDispatch::Routing::RouteWrapper.new(route).path
|
49
|
+
return recognize_in_route_set(request, app.rack_app.routes, prefix: prefix)
|
50
|
+
end
|
51
|
+
|
52
|
+
if app.rack_app.respond_to?(:redirect?) && app.rack_app.redirect?
|
53
|
+
return build_route(route, request, prefix: prefix)
|
54
|
+
end
|
55
|
+
|
56
|
+
# At this point we're matching plain Rack apps, where Rails does not
|
57
|
+
# remove the SCRIPT_NAME from PATH_INFO, so we should avoid adding
|
58
|
+
# SCRIPT_NAME twice.
|
59
|
+
return build_route(route, request, prefix: nil)
|
60
|
+
end
|
61
|
+
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
|
65
|
+
private def build_route(route, request, prefix: request.script_name)
|
66
|
+
route_wrapper = ActionDispatch::Routing::RouteWrapper.new(route)
|
67
|
+
|
68
|
+
path = if prefix.present?
|
69
|
+
File.join(prefix.to_s, route_wrapper.path).chomp("/")
|
70
|
+
else
|
71
|
+
route_wrapper.path
|
72
|
+
end
|
73
|
+
|
74
|
+
Aikido::Zen::Route.new(verb: request.request_method, path: path)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|