|
| 1 | +#include <catch2/catch_test_macros.hpp> |
| 2 | + |
| 3 | +#include "rate_limit_key_builder.hpp" |
| 4 | + |
| 5 | +namespace flapi { |
| 6 | +namespace test { |
| 7 | + |
| 8 | +namespace { |
| 9 | + |
| 10 | +constexpr const char* kIp = "203.0.113.5"; |
| 11 | +constexpr const char* kBearer = "Bearer abc.def.ghi"; |
| 12 | +constexpr const char* kBearer2 = "Bearer xyz.uvw.rst"; |
| 13 | +constexpr const char* kPath = "/customers"; |
| 14 | + |
| 15 | +} // namespace |
| 16 | + |
| 17 | +TEST_CASE("RateLimitKeyBuilder: ip strategy ignores the Authorization header", |
| 18 | + "[security][ratelimit]") { |
| 19 | + RateLimitKeyBuilder builder; |
| 20 | + auto with_auth = builder.buildKey(RateLimitKeyStrategy::Ip, kIp, kBearer, kPath); |
| 21 | + auto without_auth = builder.buildKey(RateLimitKeyStrategy::Ip, kIp, "", kPath); |
| 22 | + REQUIRE(with_auth == without_auth); |
| 23 | + REQUIRE(with_auth.find(kIp) != std::string::npos); |
| 24 | + REQUIRE(with_auth.find(kPath) != std::string::npos); |
| 25 | +} |
| 26 | + |
| 27 | +TEST_CASE("RateLimitKeyBuilder: user strategy yields different keys for different tokens", |
| 28 | + "[security][ratelimit]") { |
| 29 | + // Two callers from the same IP with different bearer tokens must |
| 30 | + // end up in different buckets when key=user. |
| 31 | + RateLimitKeyBuilder builder; |
| 32 | + auto a = builder.buildKey(RateLimitKeyStrategy::User, kIp, kBearer, kPath); |
| 33 | + auto b = builder.buildKey(RateLimitKeyStrategy::User, kIp, kBearer2, kPath); |
| 34 | + REQUIRE(a != b); |
| 35 | +} |
| 36 | + |
| 37 | +TEST_CASE("RateLimitKeyBuilder: user strategy ignores the client IP", |
| 38 | + "[security][ratelimit]") { |
| 39 | + // One user reaching the same endpoint from two IPs (mobile + Wi-Fi) |
| 40 | + // must stay in a single bucket when key=user. |
| 41 | + RateLimitKeyBuilder builder; |
| 42 | + auto a = builder.buildKey(RateLimitKeyStrategy::User, "10.0.0.1", kBearer, kPath); |
| 43 | + auto b = builder.buildKey(RateLimitKeyStrategy::User, "10.0.0.2", kBearer, kPath); |
| 44 | + REQUIRE(a == b); |
| 45 | +} |
| 46 | + |
| 47 | +TEST_CASE("RateLimitKeyBuilder: user strategy with no auth header maps to anonymous bucket", |
| 48 | + "[security][ratelimit]") { |
| 49 | + // Anonymous callers must not share a bucket with any real user, but |
| 50 | + // they DO share a single anonymous bucket among themselves — this is |
| 51 | + // intentional: anonymous traffic is the easiest to abuse, so the |
| 52 | + // tightest grouping is the safest default. |
| 53 | + RateLimitKeyBuilder builder; |
| 54 | + auto a = builder.buildKey(RateLimitKeyStrategy::User, kIp, "", kPath); |
| 55 | + auto b = builder.buildKey(RateLimitKeyStrategy::User, "10.0.0.99", "", kPath); |
| 56 | + REQUIRE(a == b); |
| 57 | + REQUIRE(a.find("anonymous") != std::string::npos); |
| 58 | +} |
| 59 | + |
| 60 | +TEST_CASE("RateLimitKeyBuilder: user-or-ip falls back to ip when auth absent", |
| 61 | + "[security][ratelimit]") { |
| 62 | + RateLimitKeyBuilder builder; |
| 63 | + auto user_or_ip_anon = builder.buildKey(RateLimitKeyStrategy::UserOrIp, kIp, "", kPath); |
| 64 | + auto ip_only = builder.buildKey(RateLimitKeyStrategy::Ip, kIp, "", kPath); |
| 65 | + REQUIRE(user_or_ip_anon == ip_only); |
| 66 | +} |
| 67 | + |
| 68 | +TEST_CASE("RateLimitKeyBuilder: user-or-ip uses auth when present", |
| 69 | + "[security][ratelimit]") { |
| 70 | + RateLimitKeyBuilder builder; |
| 71 | + auto user_or_ip_auth = builder.buildKey(RateLimitKeyStrategy::UserOrIp, kIp, kBearer, kPath); |
| 72 | + auto user_only_auth = builder.buildKey(RateLimitKeyStrategy::User, kIp, kBearer, kPath); |
| 73 | + REQUIRE(user_or_ip_auth == user_only_auth); |
| 74 | +} |
| 75 | + |
| 76 | +TEST_CASE("RateLimitKeyBuilder: path is part of every key for per-endpoint isolation", |
| 77 | + "[security][ratelimit]") { |
| 78 | + // Two endpoints hit by the same caller stay in separate buckets. |
| 79 | + RateLimitKeyBuilder builder; |
| 80 | + auto a = builder.buildKey(RateLimitKeyStrategy::Ip, kIp, "", "/foo"); |
| 81 | + auto b = builder.buildKey(RateLimitKeyStrategy::Ip, kIp, "", "/bar"); |
| 82 | + REQUIRE(a != b); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_CASE("RateLimitKeyBuilder: token derivation is stable across calls", |
| 86 | + "[security][ratelimit]") { |
| 87 | + // The hash must be deterministic; otherwise the same caller's |
| 88 | + // requests would land in different buckets and the counter would |
| 89 | + // never decrement properly. |
| 90 | + RateLimitKeyBuilder builder; |
| 91 | + auto a = builder.buildKey(RateLimitKeyStrategy::User, kIp, kBearer, kPath); |
| 92 | + auto b = builder.buildKey(RateLimitKeyStrategy::User, kIp, kBearer, kPath); |
| 93 | + REQUIRE(a == b); |
| 94 | +} |
| 95 | + |
| 96 | +TEST_CASE("RateLimitKeyBuilder: hashed auth token does not appear verbatim in the key", |
| 97 | + "[security][ratelimit]") { |
| 98 | + // The raw Authorization header value is sensitive; the key is |
| 99 | + // logged at debug level and persisted in the per-process map. It |
| 100 | + // must not contain the bearer token in plaintext. |
| 101 | + RateLimitKeyBuilder builder; |
| 102 | + auto key = builder.buildKey(RateLimitKeyStrategy::User, kIp, |
| 103 | + "Bearer super-secret-token-abc123", kPath); |
| 104 | + REQUIRE(key.find("super-secret-token-abc123") == std::string::npos); |
| 105 | +} |
| 106 | + |
| 107 | +TEST_CASE("RateLimitKeyStrategy::parse: known names map to the right enum", |
| 108 | + "[security][ratelimit]") { |
| 109 | + REQUIRE(RateLimitKeyStrategyUtils::parse("ip") == RateLimitKeyStrategy::Ip); |
| 110 | + REQUIRE(RateLimitKeyStrategyUtils::parse("user") == RateLimitKeyStrategy::User); |
| 111 | + REQUIRE(RateLimitKeyStrategyUtils::parse("user-or-ip") == RateLimitKeyStrategy::UserOrIp); |
| 112 | +} |
| 113 | + |
| 114 | +TEST_CASE("RateLimitKeyStrategy::parse: empty or unknown falls back to ip (backward compat)", |
| 115 | + "[security][ratelimit]") { |
| 116 | + REQUIRE(RateLimitKeyStrategyUtils::parse("") == RateLimitKeyStrategy::Ip); |
| 117 | + REQUIRE(RateLimitKeyStrategyUtils::parse("garbage") == RateLimitKeyStrategy::Ip); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace test |
| 121 | +} // namespace flapi |
0 commit comments