github-code-scanning[bot] commented on code in PR #14460: URL: https://github.com/apache/druid/pull/14460#discussion_r1240457592
########## processing/src/main/java/org/apache/druid/query/expression/RegexpReplaceExprMacro.java: ########## @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.query.expression; + +import org.apache.druid.common.config.NullHandling; +import org.apache.druid.math.expr.Expr; +import org.apache.druid.math.expr.ExprEval; +import org.apache.druid.math.expr.ExprMacroTable; +import org.apache.druid.math.expr.ExpressionType; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RegexpReplaceExprMacro implements ExprMacroTable.ExprMacro +{ + private static final String FN_NAME = "regexp_replace"; + + @Override + public String name() + { + return FN_NAME; + } + + @Override + public Expr apply(final List<Expr> args) + { + validationHelperCheckArgumentCount(args, 3); + + if (args.stream().skip(1).allMatch(Expr::isLiteral)) { + return new RegexpReplaceExpr(args); + } else { + return new RegexpReplaceDynamicExpr(args); + } + } + + abstract class BaseRegexpReplaceExpr extends ExprMacroTable.BaseScalarMacroFunctionExpr + { + public BaseRegexpReplaceExpr(final List<Expr> args) + { + super(FN_NAME, args); + } + + @Nullable + @Override + public ExpressionType getOutputType(InputBindingInspector inspector) + { + return ExpressionType.STRING; + } + + @Override + public Expr visit(Shuttle shuttle) + { + return shuttle.visit(apply(shuttle.visitAll(args))); + } + } + + /** + * Expr when pattern and replacement are literals. + */ + class RegexpReplaceExpr extends BaseRegexpReplaceExpr + { + private final Expr arg; + private final Pattern pattern; + private final String replacement; + + private RegexpReplaceExpr(List<Expr> args) + { + super(args); + + final Expr patternExpr = args.get(1); + final Expr replacementExpr = args.get(2); + + if (!ExprUtils.isStringLiteral(patternExpr) + && !(patternExpr.isLiteral() && patternExpr.getLiteralValue() == null)) { + throw validationFailed("pattern must be a string literal"); + } + + if (!ExprUtils.isStringLiteral(replacementExpr) + && !(replacementExpr.isLiteral() && replacementExpr.getLiteralValue() == null)) { + throw validationFailed("replacement must be a string literal"); + } + + final String patternString = NullHandling.nullToEmptyIfNeeded((String) patternExpr.getLiteralValue()); + + this.arg = args.get(0); + this.pattern = patternString != null ? Pattern.compile(patternString) : null; Review Comment: ## Regular expression injection This regular expression is constructed from a [user-provided value](1). This regular expression is constructed from a [user-provided value](2). This regular expression is constructed from a [user-provided value](3). This regular expression is constructed from a [user-provided value](4). This regular expression is constructed from a [user-provided value](5). [Show more details](https://github.com/apache/druid/security/code-scanning/5191) -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
