rubenada commented on code in PR #5334:
URL: https://github.com/apache/calcite/pull/5334#discussion_r4402347074
##########
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:
The issue I saw about default value in case of error is that it will "hide"
or "delay" the actual problem and the system might work in a surprising manner
in terms of the new check or fail unexpectedly with an unclear exception,
instead of a clear "Invalid regular expression". Considering how error-prone is
regex definition, I think it might be safer to fail when we detect a
PatternSyntaxException.
--
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]