Skip to content

Commit b8be643

Browse files
Remove scheme from URI before fetching path for CRL path check (#2622)
* Remove scheme from URI before fetching path for CRL path check * Update src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java Co-authored-by: Copilot <[email protected]> * Committed nitpick mentioned by copilot * Added additional check + greater clarity * The issue was with jar:file:/ NOT jar:/, corrected. * Forgot a case where there would be no scheme --------- Co-authored-by: Copilot <[email protected]>
1 parent 8a77b15 commit b8be643

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/ConfigurableRetryLogic.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.net.URI;
1414
import java.net.URISyntaxException;
1515
import java.nio.file.Files;
16+
import java.nio.file.InvalidPathException;
1617
import java.nio.file.Paths;
1718
import java.text.MessageFormat;
1819
import java.util.Collections;
@@ -39,9 +40,11 @@ public class ConfigurableRetryLogic {
3940
private static final String SEMI_COLON = ";";
4041
private static final String COMMA = ",";
4142
private static final String EQUALS_SIGN = "=";
43+
private static final String FORWARD_SLASH = "/";
4244
private static final String RETRY_EXEC = "retryExec";
4345
private static final String RETRY_CONN = "retryConn";
4446
private static final String STATEMENT = "statement";
47+
private static final String CLASS_FILES_SUFFIX = "target/classes/";
4548
private static boolean replaceFlag = false; // Are we replacing the list of transient errors?
4649
/**
4750
* The time the properties file was last modified.
@@ -284,20 +287,36 @@ private static void createConnectionRules(LinkedList<String> listOfRules) throws
284287
private static String getCurrentClassPath() throws SQLServerException {
285288
String location = "";
286289
String className = "";
290+
String uriToString = "";
287291

288292
try {
289293
className = new Object() {}.getClass().getEnclosingClass().getName();
290294
location = Class.forName(className).getProtectionDomain().getCodeSource().getLocation().getPath();
295+
URI uri = ConfigurableRetryLogic.class.getProtectionDomain().getCodeSource().getLocation()
296+
.toURI();
297+
298+
uriToString = uri.toString();
299+
300+
int initialIndexOfForwardSlash = uriToString.indexOf(FORWARD_SLASH);
301+
302+
if (!uri.getScheme().isEmpty() && initialIndexOfForwardSlash > 0) {
303+
// If the URI has a scheme, i.e. jar:file:, jar:, or file: then we create a substring from the
304+
// forward slash onwards.
305+
uriToString = uriToString.substring(initialIndexOfForwardSlash + 1);
306+
}
291307

292-
if (Files.isDirectory(Paths
293-
.get(ConfigurableRetryLogic.class.getProtectionDomain().getCodeSource().getLocation().toURI()))) {
308+
if (Files.isDirectory(Paths.get(uriToString))) {
294309
// We check if the Path we get from the CodeSource location is a directory. If so, we are running
295310
// from class files and should remove a suffix (i.e. the props file is in a different location from the
296311
// location returned)
297-
location = location.substring(0, location.length() - ("target/classes/").length());
312+
location = location.substring(0, location.length() - CLASS_FILES_SUFFIX.length());
298313
}
299314

300315
return new URI(location).getPath() + DEFAULT_PROPS_FILE; // TODO: Allow custom paths
316+
} catch (InvalidPathException e) {
317+
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_PathInvalid"));
318+
Object[] msgArgs = {uriToString};
319+
throw new SQLServerException(form.format(msgArgs), null, 0, e);
301320
} catch (URISyntaxException e) {
302321
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_URLInvalid"));
303322
Object[] msgArgs = {location};

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ protected Object[][] getContents() {
369369
{"R_ForceEncryptionTrue_HonorAETrue_UnencryptedColumn", "Cannot execute statement or procedure {0} because Force Encryption was set as true for parameter {1} and the database expects this parameter to be sent as plaintext. This may be due to a configuration error."},
370370
{"R_ForceEncryptionTrue_HonorAEFalseRS", "Cannot set Force Encryption to true for parameter {0} because encryption is not enabled for the statement or procedure."},
371371
{"R_ForceEncryptionTrue_HonorAETrue_UnencryptedColumnRS", "Cannot execute update because Force Encryption was set as true for parameter {0} and the database expects this parameter to be sent as plaintext. This may be due to a configuration error."},
372-
{"R_NullValue", "{0} cannot be null."},
372+
{"R_NullValue", "{0} cannot be null."},
373+
{"R_PathInvalid", "Invalid path specified: {0}."},
373374
{"R_URLInvalid", "Invalid URL specified: {0}."},
374375
{"R_AKVPathNull", "Azure Key Vault key path cannot be null."},
375376
{"R_AKVMasterKeyPathInvalid", "Invalid Azure Key Vault key path specified: {0}."},

0 commit comments

Comments
 (0)