Skip to content

Commit

Permalink
[hibernate#1425] Fix URL parsing error
Browse files Browse the repository at this point in the history
Relaxed host value character constraints and added test to verify
  • Loading branch information
blafond committed Nov 29, 2022
1 parent dc13463 commit 0be65b1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public SqlConnectOptions connectOptions(URI uri) {

String host = scheme.equals( "oracle" )
? oracleHost( uri )
: uri.getHost();
: findHost( uri );

int port = scheme.equals( "oracle" )
? oraclePort( uri )
Expand Down Expand Up @@ -305,6 +305,23 @@ private String oraclePath(URI uri) {
return string.substring( start, end );
}

// if the host contains invalid characters
// {@link java.net.URI} parseAuthority(int start, int n) will end up setting host to NULL
// when a {@link java.net.URISyntaxException} is thrown
// this method will reparse and set the host value
private String findHost( URI uri ) {
if( uri.getHost() == null ) {
// postgresql://my_db:5432.com/my_schema
String rawUri = uri.toString();
final int hostStart = rawUri.indexOf( "://" ) + 3;
String remainder = rawUri.substring( hostStart );
final int hostEnd = remainder.indexOf( ':' ) + hostStart;
return rawUri.substring( hostStart, hostEnd );
}

return uri.getHost();
}

private int defaultPort(String scheme) {
switch ( scheme ) {
case "postgresql":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,31 @@ public void testDatabaseAsProperty() {
assertOptions( url, "helloDatabase", params );
}

// URI regex does not include the '_' underscore character, so URI parsing will set the `host` and `userInfo`
// to NULL. This test verifies that the processing captures the actual host value and includes it in the
// connect options. Example: postgresql://local_host:5432/my_schema
@Test
public void testInvalidHostSucceeds() {
Map<String, String> params = new HashMap<>();
params.put( "user", "hello" );

String url = createJdbcUrl( "local_host", dbType().getDefaultPort(), "my_db", params );
assertOptions( url, "my_db", "local_host", params );
}

/**
* Create the default {@link SqlConnectOptions} with the given extra properties
* and assert that's correct.
*/
private void assertOptions(String url, String expectedDbName, Map<String, String> parameters) {
assertOptions( url, expectedDbName, "localhost", parameters);
}

/**
* Create the default {@link SqlConnectOptions} with the given extra properties
* and assert that's correct.
*/
private void assertOptions(String url, String expectedDbName, String expectedHost, Map<String, String> parameters) {
URI uri = DefaultSqlClientPool.parse( url );
SqlConnectOptions options = new DefaultSqlClientPoolConfiguration().connectOptions( uri );

Expand All @@ -125,7 +145,7 @@ private void assertOptions(String url, String expectedDbName, Map<String, String
assertThat( options.getUser() ).as( "URL: " + url ).isEqualTo( username );
assertThat( options.getPassword() ).as( "URL: " + url ).isEqualTo( password );
assertThat( options.getDatabase() ).as( "URL: " + url ).isEqualTo( expectedDbName );
assertThat( options.getHost() ).as( "URL: " + url ).isEqualTo( "localhost" );
assertThat( options.getHost() ).as( "URL: " + url ).isEqualTo( expectedHost );
assertThat( options.getPort() ).as( "URL: " + url ).isEqualTo( dbType().getDefaultPort() );

// Check extra properties
Expand Down

0 comments on commit 0be65b1

Please sign in to comment.