spiritxishi commented on code in PR #12155: URL: https://github.com/apache/inlong/pull/12155#discussion_r3550256425
########## inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java: ########## @@ -0,0 +1,375 @@ +/* + * 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.inlong.manager.service.module; + +import org.apache.inlong.manager.common.consts.InlongConstants; +import org.apache.inlong.manager.pojo.module.ModuleDTO; +import org.apache.inlong.manager.pojo.module.ModuleRequest; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * Manager-side command whitelist validator. Validates that command names (argv[0]) in module + * commands (start/stop/check/install/uninstall) are in the allowed whitelist. This catches + * misconfiguration at save time rather than waiting until the agent tries to execute. + * + * <p>Unlike the agent-side validator, this class does <b>not</b> perform path-under-root + * checks, which are filesystem-dependent and only meaningful at the agent runtime. + * + * <p>Whitelist baseline: + * <pre>{@code + * cd, sh, bash, ps, grep, awk, kill, rm, mkdir, cp, mv, ln, + * tar, unzip, chmod, chown, echo, cat, test, [, true, false, java + * }</pre> + * + * <p>Extend via {@code module.command.extraWhitelist} (comma-separated) in application + * properties, e.g.: {@code module.command.extraWhitelist=python3,nohup,curl} + */ +@Component +public class ModuleCommandValidator { + + private static final Logger LOGGER = LoggerFactory.getLogger(ModuleCommandValidator.class); + + /** Baseline argv[0] whitelist — kept in sync with agent-side ModuleCommandValidator. */ + private static final Set<String> BASELINE_WHITELIST = buildImmutableSet( + "cd", "sh", "bash", "ps", "grep", "awk", "kill", "rm", "mkdir", "cp", "mv", "ln", + "tar", "unzip", "chmod", "chown", "echo", "cat", "test", "[", "true", "false", "java"); + + /** + * Metacharacter substring blacklist — kept in sync with agent-side + * {@code ModuleCommandValidator.META_CHAR_BLACKLIST}. These characters indicate shell + * injection attempts (command substitution, chaining, redirection, escaping). Unlike + * path-under-root checks, metacharacter validation is filesystem-independent and + * belongs on both the Manager and Agent side. + */ + private static final String[] META_CHAR_BLACKLIST = new String[]{ + "`", "$(", "${", "&&", "||", ">>", ">", "<", "\\", "\u0000", + /* + * glob wildcards — Agent uses ProcessBuilder without shell, so '*' and '?' are NOT expanded. Passing e.g. + * 'rm *.log' as argv[1] would delete a literal file named "*.log" (or silently no-op), which is far more + * dangerous than an explicit error. Reject them here with a clear hint. + */ + "*", "?" + }; + + /** Extra whitelist entries from Spring config, comma-separated. */ + @Value("${module.command.extraWhitelist:}") + private String extraWhitelist; + + /** Whitelist enforcement mode: STRICT (block), WARN (log only), OFF (skip). */ + @Value("${module.command.whitelistMode:WARN}") + private WhitelistMode whitelistModeConfig; + + /** + * Whitelist enforcement mode. + * + * <ul> + * <li>{@code STRICT} — save + update both block non-whitelisted commands.</li> + * <li>{@code WARN} — save blocks, update only logs a warning (for + * gradually tightening existing modules).</li> + * <li>{@code OFF} — all validation is skipped.</li> + * </ul> + */ + public enum WhitelistMode { + + STRICT, WARN, OFF; + } + + /** Lazily-computed effective whitelist (baseline + config extras). */ + private volatile Set<String> effectiveWhitelist; + + private static Set<String> buildImmutableSet(String... items) { + Set<String> s = new HashSet<>(items.length * 2); + Collections.addAll(s, items); + return Collections.unmodifiableSet(s); + } + + /** + * Get the effective (merged) whitelist — baseline + config extras. Lazy-init and + * cached after the first call. + */ + private Set<String> getEffectiveWhitelist() { + if (effectiveWhitelist != null) { + return effectiveWhitelist; + } + synchronized (this) { + if (effectiveWhitelist != null) { + return effectiveWhitelist; + } + Set<String> merged = new LinkedHashSet<>(BASELINE_WHITELIST); + if (StringUtils.isNotBlank(extraWhitelist)) { + for (String item : extraWhitelist.split(InlongConstants.COMMA)) { + String trimmed = item.trim(); + if (trimmed.isEmpty()) { + continue; + } + merged.add(trimmed); + } + } + effectiveWhitelist = Collections.unmodifiableSet(new HashSet<>(merged)); + LOGGER.info("ModuleCommandValidator initialized: whitelist={}", + new java.util.TreeSet<>(effectiveWhitelist)); + } + return effectiveWhitelist; + } + + /** + * Get the current whitelist enforcement mode from configuration. + */ + public WhitelistMode getMode() { Review Comment: ok directly use @Getter and called by getWhitelistModeConfig() -- 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]
