Skip to content

Commit 8fa7b3b

Browse files
committed
[fix](jdbc) Address review: encoded traversal, remote URI components, config cleanup
- Decode file:// driver URLs once via URI.getPath() in JdbcResource.toLocalPath so percent-encoded parent segments (e.g. %2e%2e) are resolved into the same representation the classloader uses; the connector rule now checks the decoded path too. Fixes an encoded path-traversal bypass. - remoteUrlMatches now also compares user-info and raw query, so an allowed prefix like ".../download?id=approved" no longer authorizes ".../download?id=evil". - Remove the orphaned http_load_submitter_max_worker_threads config (its only consumer, the deleted LoadSubmitter, is gone). - Fix the jdbc_driver_secure_path description to match the new empty-value behavior (empty now denies all scheme-qualified URLs). - Tests: encoded traversal (JdbcResource + connector rule), query/user-info mismatch, and empty secure_path denial.
1 parent c3a92c8 commit 8fa7b3b

5 files changed

Lines changed: 98 additions & 10 deletions

File tree

‎fe/fe-common/src/main/java/org/apache/doris/common/Config.java‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ public class Config extends ConfigBase {
159159
@ConfField(description = {"The safe path of the JDBC driver. When creating a JDBC Catalog, "
160160
+ "you can configure multiple files or network paths that are allowed to be used, "
161161
+ "separated by semicolons. "
162-
+ "The default is * to allow all; if set to empty, it also means to allow all"})
162+
+ "The default is * to allow all. An empty value denies all scheme-qualified driver URLs "
163+
+ "(file://, http(s)://); bare jar file names resolved under jdbc_drivers_dir are always allowed."})
163164
public static String jdbc_driver_secure_path = "*";
164165

165166
@ConfField(description = {"Functions that MySQL JDBC Catalog does not support pushing down"})
@@ -2848,10 +2849,6 @@ public class Config extends ConfigBase {
28482849
"The maximum number of worker threads for the HTTP SQL submitter."})
28492850
public static int http_sql_submitter_max_worker_threads = 2;
28502851

2851-
@ConfField(mutable = false, masterOnly = false, description = {
2852-
"The maximum number of worker threads for the HTTP upload submitter."})
2853-
public static int http_load_submitter_max_worker_threads = 2;
2854-
28552852
@ConfField(mutable = true, masterOnly = true, description = {
28562853
"The threshold of load labels' number. After this number is exceeded, "
28572854
+ "the labels of the completed import jobs or tasks will be deleted, "

‎fe/fe-connector/fe-connector-jdbc/src/main/java/org/apache/doris/connector/jdbc/JdbcDorisConnector.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
import java.io.File;
4040
import java.io.IOException;
41+
import java.net.URI;
42+
import java.net.URISyntaxException;
4143
import java.util.Collections;
4244
import java.util.EnumSet;
4345
import java.util.HashMap;
@@ -155,7 +157,20 @@ public static void checkDriverUrlSecurityRule(String driverUrl) {
155157
if (driverUrl == null || driverUrl.isEmpty()) {
156158
return;
157159
}
158-
String probe = driverUrl.replace('\\', '/');
160+
// Check traversal on the decoded path so percent-encoded segments (e.g. %2e%2e) — which the
161+
// driver-loading consumers decode — cannot slip a ".." past this rule.
162+
String pathToCheck = driverUrl;
163+
if (driverUrl.contains("://")) {
164+
try {
165+
String decoded = new URI(driverUrl).getPath();
166+
if (decoded != null) {
167+
pathToCheck = decoded;
168+
}
169+
} catch (URISyntaxException e) {
170+
throw new IllegalArgumentException("Invalid driver_url: " + driverUrl);
171+
}
172+
}
173+
String probe = pathToCheck.replace('\\', '/');
159174
for (String segment : probe.split("/")) {
160175
if ("..".equals(segment)) {
161176
throw new IllegalArgumentException(

‎fe/fe-connector/fe-connector-jdbc/src/test/java/org/apache/doris/connector/jdbc/JdbcDriverUrlSecurityRuleTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public void testHttpUrlTraversalRejected() {
6060
() -> JdbcDorisConnector.checkDriverUrlSecurityRule("http://host/a/../b.jar"));
6161
}
6262

63+
@Test
64+
public void testEncodedTraversalRejected() {
65+
// %2e%2e decodes to "..", which must be caught on the decoded path.
66+
Assertions.assertThrows(IllegalArgumentException.class,
67+
() -> JdbcDorisConnector.checkDriverUrlSecurityRule(
68+
"file:///opt/doris/plugins/jdbc_drivers/%2e%2e/%2e%2e/etc/evil.jar"));
69+
}
70+
6371
// ---- allowed ----
6472

6573
@Test

‎fe/fe-core/src/main/java/org/apache/doris/catalog/JdbcResource.java‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.Arrays;
5555
import java.util.List;
5656
import java.util.Map;
57+
import java.util.Objects;
5758

5859
/**
5960
* External JDBC Catalog resource for external table query.
@@ -360,13 +361,25 @@ private static boolean isDriverUrlAllowed(String driverUrl, URI uri) {
360361

361362
/**
362363
* Turn a {@code file://} URL or a plain filesystem path into a {@link Path} for structural comparison.
364+
* A {@code file://} URL is decoded exactly once via {@link URI#getPath()} so that percent-encoded
365+
* segments (e.g. {@code %2e%2e}) are resolved into the same representation the driver-loading
366+
* consumers ({@code URL.openStream} / {@code URLClassLoader}) will use; otherwise an encoded parent
367+
* segment would survive normalization and escape the allowed directory.
363368
*/
364369
private static Path toLocalPath(String pathOrUrl) {
365-
String path = pathOrUrl;
366-
if (path.startsWith("file://")) {
367-
path = path.substring("file://".length());
370+
if (pathOrUrl.startsWith("file:")) {
371+
try {
372+
String decoded = new URI(pathOrUrl).getPath();
373+
if (decoded != null && !decoded.isEmpty()) {
374+
return Paths.get(decoded);
375+
}
376+
} catch (URISyntaxException ignored) {
377+
// fall through to literal stripping below
378+
}
379+
int sep = pathOrUrl.indexOf("//");
380+
return Paths.get(sep >= 0 ? pathOrUrl.substring(sep + 2) : pathOrUrl.substring("file:".length()));
368381
}
369-
return Paths.get(path);
382+
return Paths.get(pathOrUrl);
370383
}
371384

372385
/**
@@ -384,9 +397,14 @@ private static boolean remoteUrlMatches(URI candidate, String allowedPath) {
384397
if (base.getScheme() == null) {
385398
return false;
386399
}
400+
// Scheme/host/port and the path prefix must match, and the resource-selecting components
401+
// (user-info and query) that the checksum/classloader consumers act on must match exactly too,
402+
// otherwise e.g. ".../download?id=approved" would authorize ".../download?id=evil".
387403
return base.getScheme().equalsIgnoreCase(candidate.getScheme())
388404
&& base.getHost() != null && base.getHost().equalsIgnoreCase(candidate.getHost())
389405
&& base.getPort() == candidate.getPort()
406+
&& Objects.equals(base.getUserInfo(), candidate.getUserInfo())
407+
&& Objects.equals(base.getRawQuery(), candidate.getRawQuery())
390408
&& pathIsUnder(candidate.getPath(), base.getPath());
391409
}
392410

‎fe/fe-core/src/test/java/org/apache/doris/catalog/JdbcResourceTest.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,54 @@ public void testSecurePathWildcardAllowsAll() {
321321
Config.jdbc_driver_secure_path = saved;
322322
}
323323
}
324+
325+
@Test
326+
public void testSecurePathRejectsEncodedTraversal() {
327+
String saved = Config.jdbc_driver_secure_path;
328+
try {
329+
Config.jdbc_driver_secure_path = "file:///opt/doris/jdbc_drivers";
330+
// %2e%2e decodes to "..", which must be resolved the same way the classloader resolves it.
331+
Assert.assertThrows(IllegalArgumentException.class, () ->
332+
JdbcResource.getFullDriverUrl("file:///opt/doris/jdbc_drivers/%2e%2e/%2e%2e/etc/x.jar"));
333+
} finally {
334+
Config.jdbc_driver_secure_path = saved;
335+
}
336+
}
337+
338+
@Test
339+
public void testSecurePathRejectsRemoteQueryMismatch() {
340+
String saved = Config.jdbc_driver_secure_path;
341+
try {
342+
Config.jdbc_driver_secure_path = "http://good.com/drivers";
343+
// A query-bearing URL must not be authorized by a query-less allowed prefix.
344+
Assert.assertThrows(IllegalArgumentException.class, () ->
345+
JdbcResource.getFullDriverUrl("http://good.com/drivers/x.jar?id=evil"));
346+
} finally {
347+
Config.jdbc_driver_secure_path = saved;
348+
}
349+
}
350+
351+
@Test
352+
public void testSecurePathRejectsRemoteUserInfoMismatch() {
353+
String saved = Config.jdbc_driver_secure_path;
354+
try {
355+
Config.jdbc_driver_secure_path = "http://good.com/drivers";
356+
Assert.assertThrows(IllegalArgumentException.class, () ->
357+
JdbcResource.getFullDriverUrl("http://[email protected]/drivers/x.jar"));
358+
} finally {
359+
Config.jdbc_driver_secure_path = saved;
360+
}
361+
}
362+
363+
@Test
364+
public void testEmptySecurePathDeniesSchemeUrls() {
365+
String saved = Config.jdbc_driver_secure_path;
366+
try {
367+
Config.jdbc_driver_secure_path = "";
368+
Assert.assertThrows(IllegalArgumentException.class, () ->
369+
JdbcResource.getFullDriverUrl("file:///opt/doris/jdbc_drivers/x.jar"));
370+
} finally {
371+
Config.jdbc_driver_secure_path = saved;
372+
}
373+
}
324374
}

0 commit comments

Comments
 (0)