zabetak commented on code in PR #5334:
URL: https://github.com/apache/calcite/pull/5334#discussion_r4402241427
##########
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:
You are right the `ExceptionInitializerError` is definitely something to
avoid. The supplier and memoize is a reasonable solution. Alternatively we
could follow the paradigm followed by many `CalciteSystemProperty` entries that
is return the default in case of an error. All boolean and int based properties
fall back to some default when the input is invalid. In the same spirit we
could introduce a static factory for `Pattern` objects:
```java
private static CalciteSystemProperty<Pattern> patternProperty(String key,
Pattern defaultValue)
```
--
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]