-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ENT-14161: Limit RPC login attempts #8033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adelel1
merged 14 commits into
release/os/4.11
from
jzadroga/ent-14229/limit-rpc-access-attempts
Dec 23, 2025
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f22f47c
RPC login attempt limiter - first commit - hardcoded values
jakubzadroga 0261b2b
Make login rate limiter parameters configurable
jakubzadroga f8f535a
Address PR comments
jakubzadroga 4c0d7b1
Fix detekt
jakubzadroga ee07724
Address PR comment: add IP blocking
jakubzadroga 46fd9bf
Refactor RateLimitingActiveMQJAASSecurityManager.kt so it is testable
jakubzadroga 38105af
Fix Detekt
jakubzadroga 9913dde
PR comments
jakubzadroga bd26868
Improve comment
jakubzadroga 7335319
Fix Detekt
jakubzadroga 60ea11a
Cleanup
jakubzadroga adb82a7
Fix compilation errors in ArtemisRpcTests.kt
jakubzadroga b200bff
Address PR comments
jakubzadroga 92f9518
Address PR comments
jakubzadroga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PR comments
- Loading branch information
commit 9913dde56ef6fec3db651f6b4c706a551a2b1c9a
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 91 additions & 47 deletions
138
.../main/kotlin/net/corda/node/services/messaging/RateLimitingActiveMQJAASSecurityManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,132 @@ | ||
| package net.corda.node.services.messaging | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Cache | ||
| import com.github.benmanes.caffeine.cache.Caffeine | ||
| import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration | ||
| import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection | ||
| import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager | ||
| import java.security.MessageDigest | ||
| import java.time.Duration | ||
| import java.time.Instant | ||
| import java.util.Base64 | ||
| import java.util.concurrent.TimeUnit | ||
| import javax.security.auth.Subject | ||
| import javax.security.auth.login.FailedLoginException | ||
| import kotlin.math.pow | ||
|
|
||
| class RateLimitingActiveMQJAASSecurityManager( | ||
| configurationName: String, | ||
| configuration: SecurityConfiguration | ||
| configuration: SecurityConfiguration, | ||
| rateLimitConfig: net.corda.node.services.config.SecurityConfiguration.AuthService.Options.RateLimit? | ||
| ) : ActiveMQJAASSecurityManager(configurationName, configuration) { | ||
|
|
||
| private val ipLimiter = IpRateLimiter() | ||
| private data class Attempt(val count: Int, val nextAllowed: Instant) | ||
|
|
||
| private val baseDelaySeconds = rateLimitConfig?.backoffBaseSeconds ?: 2L | ||
| @Suppress("MagicNumber") | ||
| private val maxDelaySeconds = rateLimitConfig?.backoffMaxSeconds ?: 60L | ||
| @Suppress("MagicNumber") | ||
| private val attemptExpireMinutes = rateLimitConfig?.attemptExpireMinutes ?: 15L | ||
|
|
||
| private val userFreeAttempts = 3 | ||
| private val ipFreeAttempts = 10 | ||
|
|
||
| private val userAttempts = | ||
| Caffeine.newBuilder() | ||
| .expireAfterWrite(attemptExpireMinutes, TimeUnit.MINUTES) | ||
| .maximumSize(10_000) | ||
| .build<String, Attempt>() | ||
|
|
||
| private val ipAttempts = | ||
| Caffeine.newBuilder() | ||
| .expireAfterWrite(15, TimeUnit.MINUTES) | ||
| .maximumSize(10_000) | ||
| .build<String, Attempt>() | ||
|
|
||
| override fun authenticate(user: String?, password: String?, remotingConnection: RemotingConnection?, securityDomain: String?): Subject? { | ||
|
|
||
| val now = Instant.now() | ||
| val ip = extractIp(remotingConnection) | ||
| val userKey = user?.let { hash("$it|$ip") } | ||
| val ipKey = hash(ip) | ||
|
|
||
| // 1. Block if IP suspended | ||
| ipLimiter.checkAllowed(ip) | ||
| // 1. If user+IP suspended -> immediately reject | ||
| if (userKey != null) { | ||
| userAttempts.getIfPresent(userKey)?.let { userAttempt -> | ||
| if (now.isBefore(userAttempt.nextAllowed)) { | ||
jakubzadroga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val remaining = Duration.between(now, userAttempt.nextAllowed).seconds | ||
jakubzadroga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| throw FailedLoginException("Login temporarily suspended for user '$user'. Try again in $remaining seconds.") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 2. Attempt authentication | ||
| return try { | ||
| val subject = super.authenticate(user, password, remotingConnection, securityDomain) | ||
| // 2. Success - clear IP state | ||
| ipLimiter.recordSuccess(ip) | ||
| subject | ||
| } catch (e: FailedLoginException) { | ||
| // 3. Failure - record IP failure | ||
| ipLimiter.recordFailure(ip) | ||
| throw e | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the remote IP address without the port. | ||
| */ | ||
| private fun extractIp(remotingConnection: RemotingConnection?): String { | ||
| val raw = remotingConnection?.remoteAddress ?: "unknown" | ||
| return raw.substringAfter('/').substringBefore(':') | ||
| } | ||
| } | ||
| // success - clear user cache only | ||
| if (userKey != null) { | ||
| userAttempts.invalidate(userKey) | ||
| } | ||
| subject | ||
| } catch (fle: FailedLoginException) { | ||
|
|
||
| internal class IpRateLimiter( | ||
| private val maxFailuresBeforeBackoff: Int = 100, | ||
| private val baseDelaySeconds: Long = 2, | ||
| private val maxDelaySeconds: Long = 60 | ||
| ) { | ||
| // 3. Record IP failure | ||
| recordFailure(ipAttempts, ipKey, ipFreeAttempts, now) | ||
|
|
||
| private data class Attempt(val count: Int, val nextAllowed: Instant) | ||
| // 4, If IP suspended -> reject | ||
| ipAttempts.getIfPresent(ipKey)?.let { ipAttempt -> | ||
| if (now.isBefore(ipAttempt.nextAllowed)) { | ||
jakubzadroga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val remaining = Duration.between(now, ipAttempt.nextAllowed).seconds | ||
jakubzadroga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| throw FailedLoginException("Login temporarily suspended from IP $ip. Try again in $remaining seconds.") | ||
| } | ||
| } | ||
|
|
||
| @Suppress("MagicNumber") | ||
| private val attempts = | ||
| Caffeine.newBuilder() | ||
| .expireAfterWrite(15, TimeUnit.MINUTES) | ||
| .maximumSize(10_000) | ||
| .build<String, Attempt>() | ||
| // 5. Record user+IP failure | ||
| if (userKey != null) { | ||
| recordFailure(userAttempts, userKey, userFreeAttempts, now) | ||
| } | ||
|
|
||
| fun checkAllowed(ip: String, now: Instant = Instant.now()) { | ||
| val attempt = attempts.getIfPresent(ip) ?: return | ||
| if (now.isBefore(attempt.nextAllowed)) { | ||
| val remaining = attempt.nextAllowed.epochSecond - now.epochSecond | ||
| throw FailedLoginException( | ||
| "Login temporarily suspended from IP $ip. Try again in $remaining seconds." | ||
| ) | ||
| // 6. If user+IP suspected -> reject | ||
| if (userKey != null) { | ||
| userAttempts.getIfPresent(userKey)?.let { userAttempt -> | ||
| if (now.isBefore(userAttempt.nextAllowed)) { | ||
jakubzadroga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val remaining = Duration.between(now, userAttempt.nextAllowed).seconds | ||
jakubzadroga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| throw FailedLoginException("Login temporarily suspended for user '$user'. Try again in $remaining seconds.") | ||
| } | ||
| } | ||
| } | ||
| // 7. Plain authentication failure | ||
| throw fle | ||
| } | ||
| } | ||
|
|
||
| fun recordFailure(ip: String, now: Instant = Instant.now()) { | ||
| val prev = attempts.getIfPresent(ip) | ||
| private fun recordFailure( | ||
| cache: Cache<String, Attempt>, | ||
| key: String, | ||
| freeAttempts: Int, | ||
| now: Instant | ||
| ) { | ||
| val prev = cache.getIfPresent(key) | ||
jakubzadroga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val newCount = (prev?.count ?: 0) + 1 | ||
|
|
||
| val delay = | ||
| if (newCount <= maxFailuresBeforeBackoff) 0 | ||
| if (newCount <= freeAttempts) 0 | ||
jakubzadroga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else { | ||
| val exp = newCount - maxFailuresBeforeBackoff - 1 | ||
| val exp = newCount - freeAttempts - 1 | ||
| (baseDelaySeconds * 2.0.pow(exp)).toLong().coerceAtMost(maxDelaySeconds) | ||
| } | ||
| cache.put(key, Attempt(newCount, now.plusSeconds(delay))) | ||
| } | ||
|
|
||
| attempts.put(ip, Attempt(newCount, now.plusSeconds(delay))) | ||
| private fun extractIp(remotingConnection: RemotingConnection?): String { | ||
| val raw = remotingConnection?.remoteAddress ?: "unknown" | ||
| return raw.substringAfter('/').substringBefore(':') | ||
| } | ||
|
|
||
| fun recordSuccess(ip: String) { | ||
| attempts.invalidate(ip) | ||
| private fun hash(value: String): String { | ||
| return Base64.getEncoder().encodeToString( | ||
| MessageDigest.getInstance("SHA-256").digest(value.toByteArray()) | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.