|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../../../spec_helper" |
| 4 | + |
| 5 | +describe Hooks::App::Auth do |
| 6 | + let(:test_class) do |
| 7 | + Class.new do |
| 8 | + include Hooks::App::Auth |
| 9 | + |
| 10 | + def error!(message, code) |
| 11 | + raise StandardError, "#{message} (#{code})" |
| 12 | + end |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + let(:instance) { test_class.new } |
| 17 | + let(:payload) { '{"test": "data"}' } |
| 18 | + let(:headers) { { "Content-Type" => "application/json" } } |
| 19 | + |
| 20 | + describe "#validate_auth!" do |
| 21 | + context "when testing security vulnerabilities" do |
| 22 | + context "with missing auth configuration" do |
| 23 | + it "rejects request with no auth config" do |
| 24 | + endpoint_config = {} |
| 25 | + |
| 26 | + expect do |
| 27 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 28 | + end.to raise_error(StandardError, /authentication configuration missing or invalid/) |
| 29 | + end |
| 30 | + |
| 31 | + it "rejects request with nil auth config" do |
| 32 | + endpoint_config = { auth: nil } |
| 33 | + |
| 34 | + expect do |
| 35 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 36 | + end.to raise_error(StandardError, /authentication configuration missing or invalid/) |
| 37 | + end |
| 38 | + |
| 39 | + it "rejects request with empty auth config" do |
| 40 | + endpoint_config = { auth: {} } |
| 41 | + |
| 42 | + expect do |
| 43 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 44 | + end.to raise_error(StandardError, /authentication configuration missing or invalid/) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + context "with missing auth type" do |
| 49 | + it "rejects request with nil type" do |
| 50 | + endpoint_config = { auth: { type: nil } } |
| 51 | + |
| 52 | + expect do |
| 53 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 54 | + end.to raise_error(StandardError, /authentication configuration missing or invalid/) |
| 55 | + end |
| 56 | + |
| 57 | + it "rejects request with non-string type" do |
| 58 | + endpoint_config = { auth: { type: 123 } } |
| 59 | + |
| 60 | + expect do |
| 61 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 62 | + end.to raise_error(StandardError, /authentication configuration missing or invalid/) |
| 63 | + end |
| 64 | + |
| 65 | + it "rejects request with empty string type" do |
| 66 | + endpoint_config = { auth: { type: "" } } |
| 67 | + |
| 68 | + expect do |
| 69 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 70 | + end.to raise_error(StandardError, /authentication configuration incomplete/) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context "with missing secret configuration" do |
| 75 | + it "rejects request with missing secret_env_key" do |
| 76 | + endpoint_config = { auth: { type: "hmac" } } |
| 77 | + |
| 78 | + expect do |
| 79 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 80 | + end.to raise_error(StandardError, /authentication configuration incomplete/) |
| 81 | + end |
| 82 | + |
| 83 | + it "rejects request with nil secret_env_key" do |
| 84 | + endpoint_config = { auth: { type: "hmac", secret_env_key: nil } } |
| 85 | + |
| 86 | + expect do |
| 87 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 88 | + end.to raise_error(StandardError, /authentication configuration incomplete/) |
| 89 | + end |
| 90 | + |
| 91 | + it "rejects request with empty secret_env_key" do |
| 92 | + endpoint_config = { auth: { type: "hmac", secret_env_key: "" } } |
| 93 | + |
| 94 | + expect do |
| 95 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 96 | + end.to raise_error(StandardError, /authentication configuration incomplete/) |
| 97 | + end |
| 98 | + |
| 99 | + it "rejects request with whitespace-only secret_env_key" do |
| 100 | + endpoint_config = { auth: { type: "hmac", secret_env_key: " " } } |
| 101 | + |
| 102 | + expect do |
| 103 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 104 | + end.to raise_error(StandardError, /authentication configuration incomplete/) |
| 105 | + end |
| 106 | + |
| 107 | + it "rejects request with non-string secret_env_key" do |
| 108 | + endpoint_config = { auth: { type: "hmac", secret_env_key: 123 } } |
| 109 | + |
| 110 | + expect do |
| 111 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 112 | + end.to raise_error(StandardError, /authentication configuration incomplete/) |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + context "with missing environment variable" do |
| 117 | + it "uses generic error message for missing secrets" do |
| 118 | + endpoint_config = { auth: { type: "hmac", secret_env_key: "NONEXISTENT_SECRET" } } |
| 119 | + |
| 120 | + expect do |
| 121 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 122 | + end.to raise_error(StandardError, /authentication secret not configured/) |
| 123 | + end |
| 124 | + |
| 125 | + it "does not leak the environment variable name in error" do |
| 126 | + endpoint_config = { auth: { type: "hmac", secret_env_key: "SUPER_SECRET_WEBHOOK_KEY" } } |
| 127 | + |
| 128 | + expect do |
| 129 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 130 | + end.to raise_error do |error| |
| 131 | + expect(error.message).not_to include("SUPER_SECRET_WEBHOOK_KEY") |
| 132 | + expect(error.message).to include("authentication secret not configured") |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + context "with valid configuration but invalid secrets" do |
| 138 | + before do |
| 139 | + ENV["TEST_WEBHOOK_SECRET"] = "test-secret" |
| 140 | + end |
| 141 | + |
| 142 | + after do |
| 143 | + ENV.delete("TEST_WEBHOOK_SECRET") |
| 144 | + end |
| 145 | + |
| 146 | + it "properly validates HMAC authentication" do |
| 147 | + endpoint_config = { |
| 148 | + auth: { |
| 149 | + type: "hmac", |
| 150 | + secret_env_key: "TEST_WEBHOOK_SECRET", |
| 151 | + header: "X-Signature" |
| 152 | + } |
| 153 | + } |
| 154 | + invalid_headers = headers.merge("X-Signature" => "invalid-signature") |
| 155 | + |
| 156 | + expect do |
| 157 | + instance.validate_auth!(payload, invalid_headers, endpoint_config) |
| 158 | + end.to raise_error(StandardError, /authentication failed/) |
| 159 | + end |
| 160 | + |
| 161 | + it "properly validates shared_secret authentication" do |
| 162 | + endpoint_config = { |
| 163 | + auth: { |
| 164 | + type: "shared_secret", |
| 165 | + secret_env_key: "TEST_WEBHOOK_SECRET", |
| 166 | + header: "Authorization" |
| 167 | + } |
| 168 | + } |
| 169 | + invalid_headers = headers.merge("Authorization" => "wrong-secret") |
| 170 | + |
| 171 | + expect do |
| 172 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 173 | + end.to raise_error(StandardError, /authentication failed/) |
| 174 | + end |
| 175 | + end |
| 176 | + |
| 177 | + context "with edge case auth types" do |
| 178 | + before do |
| 179 | + ENV["TEST_WEBHOOK_SECRET"] = "test-secret" |
| 180 | + end |
| 181 | + |
| 182 | + after do |
| 183 | + ENV.delete("TEST_WEBHOOK_SECRET") |
| 184 | + end |
| 185 | + |
| 186 | + it "handles case-insensitive auth types" do |
| 187 | + endpoint_config = { |
| 188 | + auth: { |
| 189 | + type: "HMAC", |
| 190 | + secret_env_key: "TEST_WEBHOOK_SECRET", |
| 191 | + header: "X-Signature" |
| 192 | + } |
| 193 | + } |
| 194 | + invalid_headers = headers.merge("X-Signature" => "invalid") |
| 195 | + |
| 196 | + expect do |
| 197 | + instance.validate_auth!(payload, invalid_headers, endpoint_config) |
| 198 | + end.to raise_error(StandardError, /authentication failed/) |
| 199 | + end |
| 200 | + |
| 201 | + it "rejects unknown auth types" do |
| 202 | + endpoint_config = { |
| 203 | + auth: { |
| 204 | + type: "unknown_type", |
| 205 | + secret_env_key: "TEST_WEBHOOK_SECRET" |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + expect do |
| 210 | + instance.validate_auth!(payload, headers, endpoint_config) |
| 211 | + end.to raise_error(StandardError, /Custom validators not implemented/) |
| 212 | + end |
| 213 | + end |
| 214 | + end |
| 215 | + end |
| 216 | +end |
0 commit comments