rubenada commented on code in PR #5334:
URL: https://github.com/apache/calcite/pull/5334#discussion_r4400540103


##########
file/src/main/java/org/apache/calcite/adapter/file/FileSchema.java:
##########
@@ -119,10 +130,60 @@ private static String trim(String s, String suffix) {
     return builder.build();
   }
 
+  /** Checks that a model-supplied table URL may be dereferenced by this
+   * adapter: a local {@code file} URL always, a remote one (any other
+   * protocol, or a {@code file} URL naming a host, such as a Windows UNC path)
+   * only when it matches the regular expression
+   * {@link CalciteSystemProperty#FILE_URLS_ALLOWED}. */
+  static void checkUrlAllowed(String url) {
+    checkUrlAllowed(url, CalciteSystemProperty.FILE_URLS_ALLOWED.value());
+  }
+
+  static void checkUrlAllowed(String url, String allowedUrls) {
+    final URI uri;
+    try {
+      uri = new URI(url);
+      uri.toURL();
+    } catch (URISyntaxException | MalformedURLException | 
IllegalArgumentException e) {
+      // Malformed; Sources.url parses the same way and will report the error
+      return;
+    }
+    // A "file" URL is local unless it names a host, as an authority
+    // ("file://host/share") or, as Windows resolves it to a UNC path, as a
+    // path starting with two slashes or backslashes ("file:////host/share",
+    // "file:%5C%5Chost%5Cshare")
+    final String path =
+        Util.first(uri.isOpaque() ? uri.getSchemeSpecificPart() : 
uri.getPath(), "");
+    if ("file".equalsIgnoreCase(uri.getScheme())
+        && Util.isNullOrEmpty(uri.getRawAuthority())
+        && !path.replace('\\', '/').startsWith("//")) {
+      return;
+    }
+    if (!allowedUrls.isEmpty()) {
+      final Pattern pattern;
+      try {
+        pattern = Pattern.compile(allowedUrls);
+      } catch (PatternSyntaxException e) {
+        throw new IllegalArgumentException("Invalid regular expression '"
+            + allowedUrls + "' in system property 
\"calcite.file.urls.allowed\"",
+            e);
+      }

Review Comment:
   I thought about that, the problem with static final is that in case of a 
malformed regex we could get an ExceptionInitializerError and block the whole 
application; we could catch the regex exception, log a message and continue, 
but probably this issue would go under the radar for the admin.
   I tried to find a compromise by using a Supplier memoize to compite the 
Pattern only once (and throw exception in case of malformed regex the first 
time it is used).



##########
file/src/main/java/org/apache/calcite/adapter/file/FileSchema.java:
##########
@@ -119,10 +130,60 @@ private static String trim(String s, String suffix) {
     return builder.build();
   }
 
+  /** Checks that a model-supplied table URL may be dereferenced by this
+   * adapter: a local {@code file} URL always, a remote one (any other
+   * protocol, or a {@code file} URL naming a host, such as a Windows UNC path)
+   * only when it matches the regular expression
+   * {@link CalciteSystemProperty#FILE_URLS_ALLOWED}. */
+  static void checkUrlAllowed(String url) {
+    checkUrlAllowed(url, CalciteSystemProperty.FILE_URLS_ALLOWED.value());
+  }
+
+  static void checkUrlAllowed(String url, String allowedUrls) {
+    final URI uri;
+    try {
+      uri = new URI(url);
+      uri.toURL();
+    } catch (URISyntaxException | MalformedURLException | 
IllegalArgumentException e) {
+      // Malformed; Sources.url parses the same way and will report the error
+      return;
+    }
+    // A "file" URL is local unless it names a host, as an authority
+    // ("file://host/share") or, as Windows resolves it to a UNC path, as a
+    // path starting with two slashes or backslashes ("file:////host/share",
+    // "file:%5C%5Chost%5Cshare")
+    final String path =
+        Util.first(uri.isOpaque() ? uri.getSchemeSpecificPart() : 
uri.getPath(), "");
+    if ("file".equalsIgnoreCase(uri.getScheme())
+        && Util.isNullOrEmpty(uri.getRawAuthority())
+        && !path.replace('\\', '/').startsWith("//")) {
+      return;
+    }

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to