-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix(security): block SSRF via JDBC hostname validation in all database plugins (GHSA-jq9r-32xv-wgpp) #41688
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
Open
subrata71
wants to merge
5
commits into
release
Choose a base branch
from
subrata71/fix-jdbc-ssrf
base: release
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(security): block SSRF via JDBC hostname validation in all database plugins (GHSA-jq9r-32xv-wgpp) #41688
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9ae9937
fix(security): block SSRF via JDBC hostname validation in all databas…
subrata71 f02b2a5
Merge branch 'release' into subrata71/fix-jdbc-ssrf
subrata71 f9378ae
fix(security): add SSRF protection to all JDBC plugins' validateDatas…
subrata71 f5f4d11
fix(test): use flexible assertion for localhost SSRF in Postgres test
subrata71 4bad559
fix(test): use flexible assertion for localhost SSRF in MySQL tests
subrata71 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
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
60 changes: 60 additions & 0 deletions
60
...er/appsmith-interfaces/src/main/java/com/appsmith/external/helpers/JdbcHostValidator.java
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.appsmith.external.helpers; | ||
|
|
||
| import com.appsmith.external.exceptions.pluginExceptions.BasePluginErrorMessages; | ||
| import com.appsmith.util.WebClientUtils; | ||
| import lombok.AccessLevel; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Shared hostname validation utility for JDBC database plugins. | ||
| * Blocks URI-special characters that can alter JDBC URL semantics (e.g., '#' fragment injection) | ||
| * and provides SSRF protection via {@link WebClientUtils#resolveIfAllowed(String)}. | ||
| */ | ||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public final class JdbcHostValidator { | ||
|
|
||
| private static final Pattern DISALLOWED_HOST_CHARS = Pattern.compile("[/:@#?\\\\]"); | ||
|
|
||
| /** | ||
| * Validates that a hostname does not contain URI-special characters that could | ||
| * alter JDBC URL parsing (e.g., '#' for fragment injection, '/' for path injection). | ||
| * | ||
| * @return {@link Optional#empty()} if valid, or an error message string if invalid. | ||
| */ | ||
| public static Optional<String> validateHostname(String host) { | ||
| if (host == null || host.isBlank()) { | ||
| return Optional.of(BasePluginErrorMessages.DS_MISSING_HOSTNAME_ERROR_MSG); | ||
| } | ||
|
|
||
| var matcher = DISALLOWED_HOST_CHARS.matcher(host); | ||
| if (matcher.find()) { | ||
| return Optional.of( | ||
| String.format(BasePluginErrorMessages.DS_INVALID_HOSTNAME_CHARS_ERROR_MSG, matcher.group(), host)); | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the hostname is allowed by SSRF protection rules. | ||
| * Blocks cloud metadata endpoints, loopback, and link-local addresses. | ||
| * Allows RFC 1918 private ranges for self-hosted deployments. | ||
| * | ||
| * @return {@link Optional#empty()} if allowed, or an error message string if blocked. | ||
| */ | ||
| public static Optional<String> checkSsrfProtection(String host) { | ||
| if (host == null || host.isBlank()) { | ||
| return Optional.of(BasePluginErrorMessages.DS_MISSING_HOSTNAME_ERROR_MSG); | ||
| } | ||
|
|
||
| var resolved = WebClientUtils.resolveIfAllowed(host); | ||
| if (resolved.isEmpty()) { | ||
| return Optional.of(String.format(BasePluginErrorMessages.DS_SSRF_HOST_BLOCKED_ERROR_MSG, host)); | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
...ppsmith-interfaces/src/test/java/com/appsmith/external/helpers/JdbcHostValidatorTest.java
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.appsmith.external.helpers; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class JdbcHostValidatorTest { | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"myhost", "my.db.host.com", "my-db-host", "10.0.1.5", "192.168.1.100"}) | ||
| public void validateHostname_allowsValidHostnames(String host) { | ||
| Optional<String> result = JdbcHostValidator.validateHostname(host); | ||
| assertTrue(result.isEmpty(), "Expected host '" + host + "' to be allowed, but got: " + result.orElse("")); | ||
| } | ||
|
|
||
| @Test | ||
| public void validateHostname_blocksNullAndEmpty() { | ||
| assertTrue(JdbcHostValidator.validateHostname(null).isPresent()); | ||
| assertTrue(JdbcHostValidator.validateHostname("").isPresent()); | ||
| assertTrue(JdbcHostValidator.validateHostname(" ").isPresent()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"evil.com#", "evil.com/path", "evil.com:1234", "user@evil.com", "evil.com?", "evil.com\\"}) | ||
| public void validateHostname_blocksDisallowedCharacters(String host) { | ||
| Optional<String> result = JdbcHostValidator.validateHostname(host); | ||
| assertTrue(result.isPresent(), "Expected host '" + host + "' to be blocked"); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"169.254.169.254", "100.100.100.200", "168.63.129.16"}) | ||
| public void checkSsrfProtection_blocksMetadataIps(String host) { | ||
| Optional<String> result = JdbcHostValidator.checkSsrfProtection(host); | ||
| assertTrue(result.isPresent(), "Expected metadata IP '" + host + "' to be blocked by SSRF check"); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"10.0.1.5", "192.168.1.1", "172.16.0.1"}) | ||
| public void checkSsrfProtection_allowsPrivateIps(String host) { | ||
| Optional<String> result = JdbcHostValidator.checkSsrfProtection(host); | ||
| assertTrue(result.isEmpty(), "Expected private IP '" + host + "' to be allowed for self-hosted deployments"); | ||
| } | ||
| } |
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
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import com.appsmith.external.models.PsParameterDTO; | ||
| import com.appsmith.external.models.RequestParamDTO; | ||
| import com.appsmith.external.models.SSLDetails; | ||
| import com.external.plugins.exceptions.MssqlErrorMessages; | ||
| import com.external.plugins.exceptions.MssqlPluginError; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
@@ -24,6 +25,8 @@ | |
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
| import org.testcontainers.containers.MSSQLServerContainer; | ||
| import org.testcontainers.junit.jupiter.Container; | ||
| import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
|
@@ -773,4 +776,71 @@ public void testGetEndpointIdentifierForRateLimit_HostPresentPortAbsent_ReturnsC | |
| }) | ||
| .verifyComplete(); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"evil.com#fragment", "evil.com/"}) | ||
| public void testValidateDatasource_withInvalidHostname_returnsInvalid(String hostname) { | ||
| DatasourceConfiguration dsConfig = new DatasourceConfiguration(); | ||
| Endpoint endpoint = new Endpoint(); | ||
| endpoint.setHost(hostname); | ||
| endpoint.setPort(1433L); | ||
| dsConfig.setEndpoints(List.of(endpoint)); | ||
| DBAuth auth = new DBAuth(); | ||
| auth.setUsername("test"); | ||
| auth.setPassword("test"); | ||
| dsConfig.setAuthentication(auth); | ||
| Set<String> output = mssqlPluginExecutor.validateDatasource(dsConfig); | ||
| assertTrue( | ||
| output.stream().anyMatch(msg -> msg.contains("Host value cannot contain")), | ||
| "Expected hostname validation error for '" + hostname + "', got: " + output); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidateDatasource_withEmptyHostname_returnsInvalid() { | ||
| DatasourceConfiguration dsConfig = new DatasourceConfiguration(); | ||
| Endpoint endpoint = new Endpoint(); | ||
| endpoint.setHost(""); | ||
| endpoint.setPort(1433L); | ||
| dsConfig.setEndpoints(List.of(endpoint)); | ||
| DBAuth auth = new DBAuth(); | ||
| auth.setUsername("test"); | ||
| auth.setPassword("test"); | ||
| dsConfig.setAuthentication(auth); | ||
| Set<String> output = mssqlPluginExecutor.validateDatasource(dsConfig); | ||
| assertTrue(output.contains(MssqlErrorMessages.DS_MISSING_HOSTNAME_ERROR_MSG)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidateDatasource_withMetadataIp_returnsInvalid() { | ||
| DatasourceConfiguration dsConfig = new DatasourceConfiguration(); | ||
| Endpoint endpoint = new Endpoint(); | ||
| endpoint.setHost("169.254.169.254"); | ||
| endpoint.setPort(1433L); | ||
| dsConfig.setEndpoints(List.of(endpoint)); | ||
| DBAuth auth = new DBAuth(); | ||
| auth.setUsername("test"); | ||
| auth.setPassword("test"); | ||
| dsConfig.setAuthentication(auth); | ||
| Set<String> output = mssqlPluginExecutor.validateDatasource(dsConfig); | ||
| assertTrue( | ||
| output.stream().anyMatch(msg -> msg.contains("not allowed")), | ||
| "Expected SSRF blocked error for metadata IP, got: " + output); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidateDatasource_withValidHostname_noHostErrors() { | ||
| DatasourceConfiguration dsConfig = new DatasourceConfiguration(); | ||
| Endpoint endpoint = new Endpoint(); | ||
| endpoint.setHost("mydb.internal.com"); | ||
| endpoint.setPort(1433L); | ||
| dsConfig.setEndpoints(List.of(endpoint)); | ||
| DBAuth auth = new DBAuth(); | ||
| auth.setUsername("test"); | ||
| auth.setPassword("test"); | ||
| dsConfig.setAuthentication(auth); | ||
| Set<String> output = mssqlPluginExecutor.validateDatasource(dsConfig); | ||
| assertTrue( | ||
| output.stream().noneMatch(msg -> msg.contains("Host value") || msg.contains("hostname")), | ||
| "Expected no hostname errors for valid host, got: " + output); | ||
| } | ||
|
Comment on lines
+780
to
+845
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add MSSQL plugin-level SSRF regression cases. These tests cover invalid characters, but they don’t verify blocking of metadata/loopback/link-local hosts. Please add cases like 🤖 Prompt for AI Agents |
||
| } | ||
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
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
Oops, something went wrong.
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.