Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: encode url query parameters DataSource (#1201)
BaseDataSource did not properly encode url parameters, meaning that users could not log in if their password contained illegal characters. The bug can be reproduced by setting the test user password to ';/?:@&=+$,' (a bunch of illegal characters for query parameters). Encode the parameters. Strictly speaking the parameter names should also be encoded but in this case they are a fixed set of words which only consist of safe characters. With the problem password, DriverTest also fails because it did not encode the parameters either. Encode the parameters in the test too. * fix: do not leak password to URL round-tripping a datasource through JNDI added the user and password to the ds properties as well as to the instance variables - which was not possible to do via setProperty. This may be a security issue if the URL is logged, and was in part why passwords with non-url-safe characters failed to connect in some circumstances. Set properties using setProperty, so that consistent logic applies to processing the user/password keys.
- Loading branch information
Showing
7 changed files
with
100 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,55 @@ | ||
/* | ||
* Copyright (c) 2018, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.util; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.net.URLEncoder; | ||
|
||
/** | ||
* This class helps with URL encoding and decoding. UTF-8 encoding is used by default to make | ||
* encoding consistent across the driver, and encoding might be changed via {@code | ||
* postgresql.url.encoding} property | ||
* | ||
* Note: this should not be used outside of PostgreSQL source, this is not a public API of the | ||
* driver. | ||
*/ | ||
public final class URLCoder { | ||
private static final String ENCODING_FOR_URL = | ||
System.getProperty("postgresql.url.encoding", "UTF-8"); | ||
|
||
/** | ||
* Decodes {@code x-www-form-urlencoded} string into Java string. | ||
* | ||
* @param encoded encoded value | ||
* @return decoded value | ||
* @see URLDecoder#decode(String, String) | ||
*/ | ||
public static String decode(String encoded) { | ||
try { | ||
return URLDecoder.decode(encoded, ENCODING_FOR_URL); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new IllegalStateException( | ||
"Unable to decode URL entry via " + ENCODING_FOR_URL + ". This should not happen", e); | ||
} | ||
} | ||
|
||
/** | ||
* Encodes Java string into {@code x-www-form-urlencoded} format | ||
* | ||
* @param plain input value | ||
* @return encoded value | ||
* @see URLEncoder#encode(String, String) | ||
*/ | ||
public static String encode(String plain) { | ||
try { | ||
return URLEncoder.encode(plain, "UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new IllegalStateException( | ||
"Unable to encode URL entry via " + ENCODING_FOR_URL + ". This should not happen", e); | ||
} | ||
} | ||
} |
This file contains 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 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 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