|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.calcite.model; |
| 18 | + |
| 19 | +import org.apache.calcite.config.CalciteSystemProperty; |
| 20 | + |
| 21 | +import com.google.common.collect.ImmutableList; |
| 22 | + |
| 23 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 24 | + |
| 25 | +import java.util.concurrent.ConcurrentHashMap; |
| 26 | +import java.util.concurrent.ConcurrentMap; |
| 27 | +import java.util.function.Predicate; |
| 28 | + |
| 29 | +/** |
| 30 | + * Filters class names that may be loaded by reflection from a Calcite |
| 31 | + * model: user-defined functions, custom schemas, custom tables, JDBC |
| 32 | + * drivers, dialect factories, and lattice statistic providers. |
| 33 | + * |
| 34 | + * <p>{@link #standard()} returns the filter applied by |
| 35 | + * {@link ModelHandler}: the built-in {@link #DEFAULT_DENYLIST} together |
| 36 | + * with any patterns from |
| 37 | + * {@link CalciteSystemProperty#MODEL_CLASSES_DENIED} (which |
| 38 | + * <em>extends</em> the denylist). |
| 39 | + * |
| 40 | + * <p>The denylist is a comma-separated pattern string. A pattern ending |
| 41 | + * in {@code "."} matches any class in that package or its sub-packages; |
| 42 | + * otherwise the pattern matches a class name exactly. Whitespace around |
| 43 | + * commas is ignored. |
| 44 | + * |
| 45 | + * <p>The denylist is not a sandbox. Any string passed to a |
| 46 | + * {@code className}, {@code factory}, {@code jdbcDriver}, |
| 47 | + * {@code sqlDialectFactory}, or {@code statisticProvider} field is |
| 48 | + * classpath-equivalent; only accept models from trusted sources. |
| 49 | + */ |
| 50 | +class ClassNameFilter implements Predicate<String> { |
| 51 | + /** Built-in denylist: class-name patterns known to enable RCE when |
| 52 | + * registered as UDFs, schema/table factories, JDBC drivers, dialect |
| 53 | + * factories, or lattice statistic providers. */ |
| 54 | + static final String DEFAULT_DENYLIST = "" |
| 55 | + + "javax.naming.," |
| 56 | + + "com.sun.jndi.," |
| 57 | + + "java.lang.Runtime," |
| 58 | + + "java.lang.ProcessBuilder," |
| 59 | + + "java.lang.ProcessImpl," |
| 60 | + + "java.lang.System," |
| 61 | + + "java.lang.Class," |
| 62 | + + "java.lang.reflect.," |
| 63 | + + "java.lang.invoke.," |
| 64 | + + "javax.script.," |
| 65 | + + "bsh.," |
| 66 | + + "groovy.," |
| 67 | + + "org.codehaus.groovy.," |
| 68 | + + "org.python.util.PythonInterpreter," |
| 69 | + + "org.springframework.expression.," |
| 70 | + + "org.apache.commons.collections.functors.," |
| 71 | + + "org.apache.commons.collections4.functors.," |
| 72 | + + "org.apache.commons.beanutils.," |
| 73 | + + "com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl," |
| 74 | + + "sun.misc.Unsafe," |
| 75 | + + "jdk.internal."; |
| 76 | + |
| 77 | + /** Cache shared by all factory calls; filters are immutable and small, |
| 78 | + * so identical denylist inputs need only be parsed once. */ |
| 79 | + private static final ConcurrentMap<String, ClassNameFilter> CACHE = |
| 80 | + new ConcurrentHashMap<>(); |
| 81 | + |
| 82 | + /** The standard filter, built once from the built-in denylist plus |
| 83 | + * the {@link CalciteSystemProperty#MODEL_CLASSES_DENIED} extension. |
| 84 | + * Initialized via {@link #of} so it shares the same cache. */ |
| 85 | + private static final ClassNameFilter STANDARD = |
| 86 | + of( |
| 87 | + append(DEFAULT_DENYLIST, |
| 88 | + CalciteSystemProperty.MODEL_CLASSES_DENIED.value())); |
| 89 | + |
| 90 | + private final ImmutableList<String> denylist; |
| 91 | + |
| 92 | + private ClassNameFilter(String denylist) { |
| 93 | + this.denylist = parse(denylist); |
| 94 | + } |
| 95 | + |
| 96 | + /** Returns the standard filter used by {@link ModelHandler}: the |
| 97 | + * built-in {@link #DEFAULT_DENYLIST} (extended by |
| 98 | + * {@link CalciteSystemProperty#MODEL_CLASSES_DENIED}). */ |
| 99 | + static ClassNameFilter standard() { |
| 100 | + return STANDARD; |
| 101 | + } |
| 102 | + |
| 103 | + /** Returns a filter parsed from a comma-separated denylist pattern |
| 104 | + * string; may be empty. Filters are cached, so repeated calls with |
| 105 | + * the same argument return the same instance. */ |
| 106 | + static ClassNameFilter of(String denylist) { |
| 107 | + return CACHE.computeIfAbsent(denylist, ClassNameFilter::new); |
| 108 | + } |
| 109 | + |
| 110 | + /** Returns whether {@code classRef} is allowed (not on the denylist). |
| 111 | + * A null reference is allowed. |
| 112 | + * |
| 113 | + * <p>{@code classRef} may be a plain class name or the |
| 114 | + * {@code "ClassName#STATIC_FIELD"} form accepted by |
| 115 | + * {@link org.apache.calcite.avatica.AvaticaUtils#instantiatePlugin}; |
| 116 | + * the field portion is stripped before matching. */ |
| 117 | + @Override public boolean test(@Nullable String classRef) { |
| 118 | + if (classRef == null) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + String className = stripFieldRef(classRef); |
| 122 | + for (String pattern : denylist) { |
| 123 | + if (matches(pattern, className)) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + /** Throws {@link SecurityException} if {@code classRef} is on the |
| 131 | + * denylist. A null reference is a no-op. */ |
| 132 | + void check(@Nullable String classRef) { |
| 133 | + if (classRef == null) { |
| 134 | + return; |
| 135 | + } |
| 136 | + String className = stripFieldRef(classRef); |
| 137 | + for (String pattern : denylist) { |
| 138 | + if (matches(pattern, className)) { |
| 139 | + throw new SecurityException("Class '" + className |
| 140 | + + "' is rejected by the Calcite class-name filter " |
| 141 | + + "(matches denylist pattern '" + pattern + "'). " |
| 142 | + + "If this load is unintended, adjust the model; the " |
| 143 | + + "denylist cannot be loosened at runtime."); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static String stripFieldRef(String classRef) { |
| 149 | + int hash = classRef.indexOf('#'); |
| 150 | + return hash >= 0 ? classRef.substring(0, hash) : classRef; |
| 151 | + } |
| 152 | + |
| 153 | + private static boolean matches(String pattern, String className) { |
| 154 | + if (pattern.endsWith(".")) { |
| 155 | + return className.startsWith(pattern); |
| 156 | + } |
| 157 | + return className.equals(pattern); |
| 158 | + } |
| 159 | + |
| 160 | + /** Returns the concatenation of two comma-separated pattern strings, |
| 161 | + * inserting a comma if needed and tolerating empty inputs. */ |
| 162 | + static String append(String first, String second) { |
| 163 | + if (first.isEmpty()) { |
| 164 | + return second; |
| 165 | + } |
| 166 | + if (second.isEmpty()) { |
| 167 | + return first; |
| 168 | + } |
| 169 | + return first + "," + second; |
| 170 | + } |
| 171 | + |
| 172 | + private static ImmutableList<String> parse(String list) { |
| 173 | + if (list.isEmpty()) { |
| 174 | + return ImmutableList.of(); |
| 175 | + } |
| 176 | + ImmutableList.Builder<String> b = ImmutableList.builder(); |
| 177 | + for (String s : list.split(",")) { |
| 178 | + String trimmed = s.trim(); |
| 179 | + if (!trimmed.isEmpty()) { |
| 180 | + b.add(trimmed); |
| 181 | + } |
| 182 | + } |
| 183 | + return b.build(); |
| 184 | + } |
| 185 | +} |
0 commit comments