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.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. data/.aikido +6 -0
  3. data/.ruby-version +1 -0
  4. data/.simplecov +26 -0
  5. data/.standard.yml +3 -0
  6. data/LICENSE +674 -0
  7. data/README.md +146 -0
  8. data/Rakefile +67 -0
  9. data/benchmarks/README.md +23 -0
  10. data/benchmarks/rails7.1_sql_injection.js +70 -0
  11. data/docs/banner.svg +202 -0
  12. data/docs/config.md +125 -0
  13. data/docs/proxy.md +10 -0
  14. data/docs/rails.md +114 -0
  15. data/lib/aikido/zen/actor.rb +116 -0
  16. data/lib/aikido/zen/agent/heartbeats_manager.rb +66 -0
  17. data/lib/aikido/zen/agent.rb +179 -0
  18. data/lib/aikido/zen/api_client.rb +145 -0
  19. data/lib/aikido/zen/attack.rb +207 -0
  20. data/lib/aikido/zen/background_worker.rb +52 -0
  21. data/lib/aikido/zen/capped_collections.rb +68 -0
  22. data/lib/aikido/zen/collector/hosts.rb +15 -0
  23. data/lib/aikido/zen/collector/routes.rb +66 -0
  24. data/lib/aikido/zen/collector/sink_stats.rb +95 -0
  25. data/lib/aikido/zen/collector/stats.rb +111 -0
  26. data/lib/aikido/zen/collector/users.rb +30 -0
  27. data/lib/aikido/zen/collector.rb +144 -0
  28. data/lib/aikido/zen/config.rb +282 -0
  29. data/lib/aikido/zen/context/rack_request.rb +24 -0
  30. data/lib/aikido/zen/context/rails_request.rb +44 -0
  31. data/lib/aikido/zen/context.rb +112 -0
  32. data/lib/aikido/zen/detached_agent/agent.rb +78 -0
  33. data/lib/aikido/zen/detached_agent/front_object.rb +37 -0
  34. data/lib/aikido/zen/detached_agent/server.rb +78 -0
  35. data/lib/aikido/zen/detached_agent.rb +2 -0
  36. data/lib/aikido/zen/errors.rb +107 -0
  37. data/lib/aikido/zen/event.rb +71 -0
  38. data/lib/aikido/zen/internals.rb +103 -0
  39. data/lib/aikido/zen/libzen-v0.1.39-aarch64-linux.so +0 -0
  40. data/lib/aikido/zen/middleware/check_allowed_addresses.rb +26 -0
  41. data/lib/aikido/zen/middleware/middleware.rb +11 -0
  42. data/lib/aikido/zen/middleware/rack_throttler.rb +48 -0
  43. data/lib/aikido/zen/middleware/request_tracker.rb +192 -0
  44. data/lib/aikido/zen/middleware/set_context.rb +26 -0
  45. data/lib/aikido/zen/outbound_connection.rb +45 -0
  46. data/lib/aikido/zen/outbound_connection_monitor.rb +23 -0
  47. data/lib/aikido/zen/package.rb +22 -0
  48. data/lib/aikido/zen/payload.rb +50 -0
  49. data/lib/aikido/zen/rails_engine.rb +56 -0
  50. data/lib/aikido/zen/rate_limiter/breaker.rb +61 -0
  51. data/lib/aikido/zen/rate_limiter/bucket.rb +76 -0
  52. data/lib/aikido/zen/rate_limiter/result.rb +31 -0
  53. data/lib/aikido/zen/rate_limiter.rb +50 -0
  54. data/lib/aikido/zen/request/heuristic_router.rb +115 -0
  55. data/lib/aikido/zen/request/rails_router.rb +77 -0
  56. data/lib/aikido/zen/request/schema/auth_discovery.rb +86 -0
  57. data/lib/aikido/zen/request/schema/auth_schemas.rb +54 -0
  58. data/lib/aikido/zen/request/schema/builder.rb +121 -0
  59. data/lib/aikido/zen/request/schema/definition.rb +107 -0
  60. data/lib/aikido/zen/request/schema/empty_schema.rb +28 -0
  61. data/lib/aikido/zen/request/schema.rb +87 -0
  62. data/lib/aikido/zen/request.rb +122 -0
  63. data/lib/aikido/zen/route.rb +39 -0
  64. data/lib/aikido/zen/runtime_settings/endpoints.rb +49 -0
  65. data/lib/aikido/zen/runtime_settings/ip_set.rb +36 -0
  66. data/lib/aikido/zen/runtime_settings/protection_settings.rb +62 -0
  67. data/lib/aikido/zen/runtime_settings/rate_limit_settings.rb +47 -0
  68. data/lib/aikido/zen/runtime_settings.rb +65 -0
  69. data/lib/aikido/zen/scan.rb +75 -0
  70. data/lib/aikido/zen/scanners/path_traversal/helpers.rb +65 -0
  71. data/lib/aikido/zen/scanners/path_traversal_scanner.rb +63 -0
  72. data/lib/aikido/zen/scanners/shell_injection/helpers.rb +159 -0
  73. data/lib/aikido/zen/scanners/shell_injection_scanner.rb +64 -0
  74. data/lib/aikido/zen/scanners/sql_injection_scanner.rb +93 -0
  75. data/lib/aikido/zen/scanners/ssrf/dns_lookups.rb +27 -0
  76. data/lib/aikido/zen/scanners/ssrf/private_ip_checker.rb +97 -0
  77. data/lib/aikido/zen/scanners/ssrf_scanner.rb +265 -0
  78. data/lib/aikido/zen/scanners/stored_ssrf_scanner.rb +49 -0
  79. data/lib/aikido/zen/scanners.rb +7 -0
  80. data/lib/aikido/zen/sink.rb +118 -0
  81. data/lib/aikido/zen/sinks/action_controller.rb +83 -0
  82. data/lib/aikido/zen/sinks/async_http.rb +80 -0
  83. data/lib/aikido/zen/sinks/curb.rb +113 -0
  84. data/lib/aikido/zen/sinks/em_http.rb +83 -0
  85. data/lib/aikido/zen/sinks/excon.rb +118 -0
  86. data/lib/aikido/zen/sinks/file.rb +112 -0
  87. data/lib/aikido/zen/sinks/http.rb +93 -0
  88. data/lib/aikido/zen/sinks/httpclient.rb +95 -0
  89. data/lib/aikido/zen/sinks/httpx.rb +78 -0
  90. data/lib/aikido/zen/sinks/kernel.rb +33 -0
  91. data/lib/aikido/zen/sinks/mysql2.rb +31 -0
  92. data/lib/aikido/zen/sinks/net_http.rb +101 -0
  93. data/lib/aikido/zen/sinks/patron.rb +103 -0
  94. data/lib/aikido/zen/sinks/pg.rb +72 -0
  95. data/lib/aikido/zen/sinks/resolv.rb +62 -0
  96. data/lib/aikido/zen/sinks/socket.rb +78 -0
  97. data/lib/aikido/zen/sinks/sqlite3.rb +46 -0
  98. data/lib/aikido/zen/sinks/trilogy.rb +31 -0
  99. data/lib/aikido/zen/sinks/typhoeus.rb +78 -0
  100. data/lib/aikido/zen/sinks.rb +36 -0
  101. data/lib/aikido/zen/sinks_dsl.rb +250 -0
  102. data/lib/aikido/zen/synchronizable.rb +24 -0
  103. data/lib/aikido/zen/system_info.rb +84 -0
  104. data/lib/aikido/zen/version.rb +10 -0
  105. data/lib/aikido/zen/worker.rb +87 -0
  106. data/lib/aikido/zen.rb +246 -0
  107. data/lib/aikido-zen.rb +3 -0
  108. data/placeholder/.gitignore +4 -0
  109. data/placeholder/README.md +11 -0
  110. data/placeholder/Rakefile +75 -0
  111. data/placeholder/lib/placeholder.rb.template +3 -0
  112. data/placeholder/placeholder.gemspec.template +20 -0
  113. data/tasklib/bench.rake +94 -0
  114. data/tasklib/libzen.rake +133 -0
  115. data/tasklib/wrk.rb +88 -0
  116. metadata +205 -0
data/README.md ADDED
@@ -0,0 +1,146 @@
1
+ ![Zen by Aikido for Ruby](./docs/banner.svg)
2
+
3
+ # Zen, in-app firewall for Ruby | by Aikido
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/aikido-zen.svg?icon=si%3Arubygems&style=flat)](https://badge.fury.io/rb/aikido-zen)
6
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com)
7
+ [![Unit tests](https://github.com/AikidoSec/firewall-ruby/actions/workflows/main.yml/badge.svg)](https://github.com/AikidoSec/firewall-ruby/actions/workflows/main.yml)
8
+ [![Release](https://github.com/AikidoSec/firewall-ruby/actions/workflows/release.yml/badge.svg)](https://github.com/AikidoSec/firewall-ruby/actions/workflows/release.yml)
9
+
10
+ Zen, your in-app firewall for peace of mind - at runtime.
11
+
12
+ Zen by Aikido is an embedded Web Application Firewall that autonomously protects Ruby apps against common and critical attacks.
13
+
14
+ It protects your Ruby apps by preventing user input containing dangerous strings, which allow injection, pollution, and path traversal attacks. It runs on the same server as your Ruby app for simple [installation](#installation) and zero maintenance.
15
+
16
+ ## Features
17
+
18
+ Zen will autonomously protect your Ruby applications against:
19
+
20
+ * 🛡️ [SQL injection attacks](https://www.aikido.dev/blog/the-state-of-sql-injections)
21
+ * 🛡️ [Server-side request forgery (SSRF)](https://github.com/AikidoSec/firewall-node/blob/main/docs/ssrf.md)
22
+ * 🛡️ [Command injection attacks](https://www.aikido.dev/blog/command-injection-in-2024-unpacked)
23
+ * 🛡️ [Path traversal attacks](https://www.aikido.dev/blog/path-traversal-in-2024-the-year-unpacked)
24
+ * 🛡️ [NoSQL injection attacks](https://www.aikido.dev/blog/web-application-security-vulnerabilities) (coming soon)
25
+
26
+ Zen operates autonomously on the same server as your Ruby app to:
27
+
28
+ * ✅ Secure your app like a classic web application firewall (WAF), but with none of the infrastructure or cost
29
+ * ✅ Rate limit specific API endpoints by IP or by user
30
+ * ✅ Allow you to block specific users manually
31
+
32
+ ## Supported libraries and frameworks
33
+
34
+ Zen for Ruby 2.7+ is compatible with:
35
+
36
+ ### Web frameworks
37
+
38
+ * ✅ [Ruby on Rails](docs/rails.md) 7.x, 8.x
39
+
40
+ ### Database drivers
41
+
42
+ * ✅ [`sqlite3`](https://github.com/sparklemotion/sqlite3-ruby) 1.x, 2.x
43
+ * ✅ [`pg`](https://github.com/ged/ruby-pg) 1.x
44
+ * ✅ [`mysql2`](https://github.com/brianmario/mysql2) 0.x
45
+ * ✅ [`trilogy`](https://github.com/trilogy-libraries/trilogy) 2.x
46
+
47
+ ### ORMs and query builders
48
+
49
+ See list above for supported database drivers.
50
+
51
+ * ✅ [ActiveRecord](https://github.com/rails/rails)
52
+ * ✅ [Sequel](https://github.com/jeremyevans/sequel)
53
+
54
+ ### HTTP clients
55
+
56
+ * ✅ [`net-http`](https://github.com/ruby/net-http)
57
+ * ✅ [`http.rb`](https://github.com/httprb/http) 1.x, 2.x, 3.x, 4.x, 5.x
58
+ * ✅ [`httpx`](https://gitlab.com/os85/httpx) 1.x (1.1.3+)
59
+ * ✅ [`httpclient`](https://github.com/nahi/httpclient) 2.x, 3.x
60
+ * ✅ [`excon`](https://github.com/excon/excon) 0.x (0.50.0+), 1.x
61
+ * ✅ [`curb`](https://github.com/taf2/curb) 0.x (0.2.3+), 1.x
62
+ * ✅ [`patron`](https://github.com/toland/patron) 0.x (0.6.4+)
63
+ * ✅ [`typhoeus`](https://github.com/typhoeus/typhoeus) 0.x (0.5.0+), 1.x
64
+ * ✅ [`async-http`](https://github.com/socketry/async-http) 0.x (0.70.0+)
65
+ * ✅ [`em-http-request`](https://github.com/igrigorik/em-http-request) 1.x
66
+
67
+ ## Installation
68
+
69
+ We recommend testing Zen locally or on staging before deploying to production.
70
+
71
+ ```sh
72
+ bundle add aikido-zen
73
+ ```
74
+
75
+ or, if not using bundler:
76
+
77
+ ```sh
78
+ gem install aikido-zen
79
+ ```
80
+
81
+ For framework specific instructions, check out our docs:
82
+
83
+ * [Ruby on Rails](docs/rails.md)
84
+
85
+ ## Reporting to your Aikido Security dashboard
86
+
87
+ > Aikido is your no nonsense application security platform. One central system that scans your source code & cloud, shows you what vulnerabilities matter, and how to fix them - fast. So you can get back to building.
88
+
89
+ Zen is a new product by Aikido. Built for developers to level up their security. While Aikido scans, get Zen for always-on protection.
90
+
91
+ You can use some of Zen's features without Aikido, of course. Peace of mind is just a few lines of code away.
92
+
93
+ But you will get the most value by reporting your data to Aikido.
94
+
95
+ You will need an Aikido account and a token to report events to Aikido. If you don't have an account, you can [sign up for free](https://app.aikido.dev/login).
96
+
97
+ Here's how:
98
+
99
+ * [Log in to your Aikido account](https://app.aikido.dev/login).
100
+ * Go to [Zen](https://app.aikido.dev/runtime/services).
101
+ * Go to apps.
102
+ * Click on **Add app**.
103
+ * Choose a name for your app.
104
+ * Click **Generate token**.
105
+ * Copy the token.
106
+ * Set the token as an environment variable, `AIKIDO_TOKEN`, using [dotenv](https://github.com/bkeepers/dotenv) or another method of your choosing.
107
+
108
+ ## Running in production (blocking) mode
109
+
110
+ By default, Zen will only detect and report attacks to Aikido.
111
+
112
+ To block requests, set the `AIKIDO_BLOCK` environment variable to `true`.
113
+
114
+ See [Reporting to Aikido](#reporting-to-your-aikido-security-dashboard) to learn how to send events to Aikido.
115
+
116
+ ## Additional configuration
117
+
118
+ [Configure Zen using environment variables for authentication, mode settings, debugging, and more.](https://help.aikido.dev/doc/configuration-via-env-vars/docrSItUkeR9)
119
+
120
+ ## License
121
+
122
+ This program is offered under a commercial and under the AGPL license. You can be released from the requirements of the AGPL license by purchasing a commercial license. Buying such a license is mandatory as soon as you develop commercial activities involving the Zen software without disclosing the source code of your own applications.
123
+
124
+ For more information, please contact Aikido Security at this address: support@aikido.dev or create an account at https://app.aikido.dev.
125
+
126
+ ## Benchmarks
127
+
128
+ We run a benchmark on every commit to ensure Zen has a minimal impact on your application's performance.
129
+
130
+ See [benchmarks](benchmarks)
131
+
132
+ ## Bug bounty program
133
+
134
+ Our bug bounty program is public and can be found by all registered Intigriti users at: https://app.intigriti.com/researcher/programs/aikido/aikidozenbeta
135
+
136
+ ## Contributing
137
+
138
+ See [CONTRIBUTING.md](.github/CONTRIBUTING.md) for more information.
139
+
140
+ ## Code of Conduct
141
+
142
+ See [CODE_OF_CONDUCT.md](.github/CODE_OF_CONDUCT.md) for more information.
143
+
144
+ ## Security
145
+
146
+ See [SECURITY.md](.github/SECURITY.md) for more information.
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+ require "standard/rake"
6
+ require "rake/clean"
7
+
8
+ load "tasklib/libzen.rake"
9
+ load "tasklib/bench.rake"
10
+
11
+ desc "Run all benchmarks"
12
+ task bench: "bench:default"
13
+
14
+ namespace :build do
15
+ desc "Ensure Gemfile.lock is up-to-date"
16
+ task "update_gem_lockfile" do
17
+ sh "bundle check >/dev/null || bundle"
18
+ end
19
+ end
20
+ task build: ["build:update_gem_lockfile", "libzen:download:all"]
21
+
22
+ # Build all the native gems as well
23
+ Rake::Task["build"].enhance(["libzen:gems"])
24
+
25
+ # rake release wants to tag the commit and push the tag, but we run the release
26
+ # workflow after creating the tag, and so we don't need another one.
27
+ Rake::Task["release:source_control_push"].clear
28
+ task "release:source_control_push" do
29
+ # do nothing
30
+ end
31
+
32
+ # Push all the native gems before the libzen-less one.
33
+ task "release:rubygem_push" => "libzen:release"
34
+
35
+ Pathname.glob("sample_apps/*").select(&:directory?).each do |dir|
36
+ namespace :build do
37
+ desc "Ensure Gemfile.lock is up-to-date in the #{dir.basename} sample app"
38
+ task "update_#{dir.basename}_lockfile" do
39
+ Dir.chdir(dir) { sh "bundle check >/dev/null || bundle" }
40
+ end
41
+ end
42
+
43
+ task build: "build:update_#{dir.basename}_lockfile"
44
+ end
45
+
46
+ Minitest::TestTask.create do |test_task|
47
+ test_task.test_globs = FileList["test/**/{test_*,*_test}.rb"]
48
+ .exclude("test/e2e/**/*.rb")
49
+ end
50
+ task test: "libzen:download:current"
51
+
52
+ Pathname.glob("test/e2e/*").select(&:directory?).each do |dir|
53
+ namespace :e2e do
54
+ desc "Run e2e tests for the #{dir.basename} sample app"
55
+ task dir.basename do
56
+ Dir.chdir(dir) do
57
+ sh "rake ci:setup"
58
+ sh "rake test"
59
+ end
60
+ end
61
+ end
62
+
63
+ desc "Run all e2e tests"
64
+ task e2e: "e2e:#{dir.basename}"
65
+ end
66
+
67
+ task default: %i[test standard]
@@ -0,0 +1,23 @@
1
+ # Benchmarking Zen for Ruby
2
+
3
+
4
+ We use [WRK](https://github.com/wg/wrk) & [Grafana K6](https://k6.io) for these.
5
+
6
+ WRK benchmarks are only requesting a URL (`/benchmark`). In case you want to add more
7
+ of those test, you have to code them in the file `tasklib/bench.rake`.
8
+
9
+ K6 tests are defined in `benchmarks` folder. They are a javascript file, with calls
10
+ to different endpoints.
11
+
12
+ In order to run a benchmarks against a single application, run the following
13
+ from the root of the project:
14
+
15
+ ```
16
+ $ BUNDLE_GEMFILE=./sample_apps/{app}/Gemfile bundle exec rake bench:{app}:(k6|wrk)_run
17
+ ```
18
+
19
+ For example, for the WRK of `rails7.1_benchmark` application:
20
+
21
+ ```
22
+ $ BUNDLE_GEMFILE=./sample_apps/rails7.1_benchmark/Gemfile bundle exec rake bench:rails7.1_benchmark:wrk_run
23
+ ```
@@ -0,0 +1,70 @@
1
+ import http from 'k6/http';
2
+ import {Trend} from 'k6/metrics';
3
+
4
+ const HTTP = {
5
+ withZen: {
6
+ get: (path, ...args) => http.get("http://localhost:3001" + path, ...args),
7
+ post: (path, ...args) => http.post("http://localhost:3001" + path, ...args)
8
+ },
9
+ withoutZen: {
10
+ get: (path, ...args) => http.get("http://localhost:3002" + path, ...args),
11
+ post: (path, ...args) => http.post("http://localhost:3002" + path, ...args)
12
+ }
13
+ }
14
+
15
+ function test(name, fn) {
16
+ const withZen = fn(HTTP.withZen);
17
+ const withoutZen = fn(HTTP.withoutZen);
18
+ const timeWithZen = withZen.timings.duration;
19
+ const timeWithoutZen = withoutZen.timings.duration;
20
+
21
+ tests[name].delta.add(timeWithZen - timeWithoutZen);
22
+ tests[name].overhead.add(100 * (timeWithZen - timeWithoutZen) / timeWithoutZen)
23
+
24
+ tests[name].with_zen.add(timeWithZen);
25
+ tests[name].without_zen.add(timeWithoutZen);
26
+ }
27
+
28
+ function buildTestTrends(prefix) {
29
+ return {
30
+ delta: new Trend(`${prefix}_delta`),
31
+ with_zen: new Trend(`${prefix}_with_zen`),
32
+ without_zen: new Trend(`${prefix}_without_zen`),
33
+ overhead: new Trend(`${prefix}_overhead`)
34
+ };
35
+ }
36
+
37
+ const tests = {
38
+ test_post_page_with_json_body: buildTestTrends("test_post_page_with_json_body"),
39
+ test_get_page_without_attack: buildTestTrends("test_get_page_without_attack"),
40
+ test_get_page_with_sql_injection: buildTestTrends("test_get_page_with_sql_injection")
41
+ }
42
+ export const options = {
43
+ vus: 1, // Number of virtual users
44
+ iterations: 200,
45
+ thresholds: {
46
+ http_req_failed: ['rate==0'], // we are marking the attacks as expected, so we should have no errors
47
+ test_post_page_with_json_body_delta: ["med<10"],
48
+ test_get_page_without_attack_delta: ["med<10"],
49
+ test_get_page_with_sql_injection_delta: ["med<10"],
50
+ }
51
+ };
52
+
53
+ const expectAttack = http.expectedStatuses(200, 500);
54
+
55
+ export default function () {
56
+ test("test_post_page_with_json_body",
57
+ (http) => http.post("/cats", JSON.stringify({cat: {name: "Féline Dion"}}), {
58
+ headers: {
59
+ "Content-Type": "application/json",
60
+ "Accept": "application/json"
61
+ }
62
+ })
63
+ )
64
+
65
+ test("test_get_page_without_attack", (http) => http.get("/cats"))
66
+
67
+ test("test_get_page_with_sql_injection", (http) =>
68
+ http.get("/cats/1'%20OR%20''='", { responseCallback: expectAttack })
69
+ )
70
+ }