You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by "spiritxishi (via GitHub)" <gi...@apache.org> on 2026/07/08 15:53:03 UTC

[PR] [INLONG-12149][Improve] [Agent] Add command whitelist validation in Manager [inlong]

spiritxishi opened a new pull request, #12155:
URL: https://github.com/apache/inlong/pull/12155

   …anager (#12149)
   
   <!-- Prepare a Pull Request
   Change the title of pull request refer to the following example:
     [INLONG-XYZ][Component] Title of the pull request 
   -->
   
   <!-- Specify the issue this pull request going to fix.
   The following *XYZ* should be replaced by the actual [GitHub Issue](https://github.com/apache/inlong/issues) number)-->
   
   Fixes #12149
   
   ### Motivation
   
   <!--Explain here the context, and why you're making that change. What is the problem you're trying to solve.-->
   
   ### Modifications
   
   <!--Describe the modifications you've done.-->
   
   ### Verifying this change
   
   *(Please pick either of the following options)*
   
   - [ ] This change is a trivial rework/code cleanup without any test coverage.
   
   - [ ] This change is already covered by existing tests, such as:
     *(please describe tests)*
   
   - [ ] This change added tests and can be verified as follows:
   
     *(example:)*
     - *Added integration tests for end-to-end deployment with large payloads (10MB)*
     - *Extended integration test for recovery after broker failure*
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (yes / no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
     - If a feature is not applicable for documentation, explain why?
     - If a feature is not documented yet in this PR, please create a follow-up issue for adding the documentation
   


-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
fuweng11 commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550150432


##########
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:
   This method should not be needed.



##########
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() {
+        return whitelistModeConfig;
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request);
+        }
+        Function<CommandField, String> oldReader = ModuleCommandAccessors.of(oldDto);
+        Function<CommandField, String> newReader = ModuleCommandAccessors.of(request);
+        // Skip fields whose value is unchanged (both null or equal).
+        return validateFields(newReader,
+                f -> Objects.equals(oldReader.apply(f), newReader.apply(f)));
+    }
+
+    /**
+     * Core validation loop: iterate through every {@link CommandField}, read its current
+     * value via {@code accessor}, and run {@link #validate(String)}. Fields for which
+     * {@code skip} returns {@code true} are bypassed (used for incremental validation).
+     *
+     * @return {@code "<label>: <offending>"} on the first failure, or {@code null} if all pass
+     */
+    private String validateFields(Function<CommandField, String> accessor,
+            Predicate<CommandField> skip) {
+        for (CommandField f : CommandField.values()) {
+            if (skip.test(f)) {
+                continue;
+            }
+            String offending = validate(accessor.apply(f));
+            if (StringUtils.isNotBlank(offending)) {
+                return f.label + ": " + offending;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if (InlongConstants.ASTERISK.equals(metaHit) || InlongConstants.QUESTION_MARK.equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf(InlongConstants.NEW_LINE_CHAR) >= 0
+                || rawCmd.indexOf(InlongConstants.CARRIAGE_RETURN_CHAR) >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, InlongConstants.SEMICOLON_CHAR);
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            if (StringUtils.isBlank(seg)) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(seg, InlongConstants.PIPE_CHAR);
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                if (StringUtils.isBlank(pipeSeg)) {
+                    continue;
+                }
+                List<String> argv = tokenize(pipeSeg);
+                if (argv.isEmpty()) {
+                    continue;
+                }
+                String cmdName = argv.get(0);
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.size() > 1) {

Review Comment:
   Is case sensitivity required to be ignored?



##########
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 {

Review Comment:
   This should be a utility class, and it doesn't require @Component.



##########
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() {
+        return whitelistModeConfig;
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request);
+        }
+        Function<CommandField, String> oldReader = ModuleCommandAccessors.of(oldDto);
+        Function<CommandField, String> newReader = ModuleCommandAccessors.of(request);
+        // Skip fields whose value is unchanged (both null or equal).
+        return validateFields(newReader,
+                f -> Objects.equals(oldReader.apply(f), newReader.apply(f)));
+    }
+
+    /**
+     * Core validation loop: iterate through every {@link CommandField}, read its current
+     * value via {@code accessor}, and run {@link #validate(String)}. Fields for which
+     * {@code skip} returns {@code true} are bypassed (used for incremental validation).
+     *
+     * @return {@code "<label>: <offending>"} on the first failure, or {@code null} if all pass
+     */
+    private String validateFields(Function<CommandField, String> accessor,
+            Predicate<CommandField> skip) {
+        for (CommandField f : CommandField.values()) {
+            if (skip.test(f)) {
+                continue;
+            }
+            String offending = validate(accessor.apply(f));
+            if (StringUtils.isNotBlank(offending)) {
+                return f.label + ": " + offending;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if (InlongConstants.ASTERISK.equals(metaHit) || InlongConstants.QUESTION_MARK.equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf(InlongConstants.NEW_LINE_CHAR) >= 0
+                || rawCmd.indexOf(InlongConstants.CARRIAGE_RETURN_CHAR) >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, InlongConstants.SEMICOLON_CHAR);
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            if (StringUtils.isBlank(seg)) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(seg, InlongConstants.PIPE_CHAR);
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                if (StringUtils.isBlank(pipeSeg)) {
+                    continue;
+                }
+                List<String> argv = tokenize(pipeSeg);
+                if (argv.isEmpty()) {
+                    continue;
+                }
+                String cmdName = argv.get(0);
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.size() > 1) {
+                    for (int i = 1; i < argv.size(); i++) {
+                        if (argv.get(i).startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv.get(i) + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all module command fields on a {@link ModuleRequest}. Returns
+     * {@code null} if all commands pass, or a descriptive message such as
+     * {@code "startCommand: python3"} / {@code "startCommand: DISALLOWED_META_CHAR: '*' — ..."}
+     * if one fails.
+     *
+     * <p>Preferred over the 5-argument overload — passing the request object avoids
+     * argument-order mistakes at call sites.
+     */
+    public String validateAll(ModuleRequest request) {
+        if (request == null) {
+            return null;
+        }
+        return validateFields(ModuleCommandAccessors.of(request), f -> false);
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {

Review Comment:
   Ditto.



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3549373052


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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;

Review Comment:
   default value is ""



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3549489004


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,

Review Comment:
   String return carries rich diagnostic information, not a simple true/false binary
   "startCommand: 'DISALLOWED_META_CHAR: '*', glob wildcards are not supported'"
   Pinpoints which field + which rule + which character



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "luchunliang (via GitHub)" <gi...@apache.org>.
luchunliang commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3548407069


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";

Review Comment:
   Nested single quotes.
   Like : startCommand: 'DISALLOWED_META_CHAR: '*' — glob wildcards are not supported (Agent runs commands without a shell, so '*' and '?' will NOT be expanded); please specify explicit file paths'
   



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
fuweng11 commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3549627526


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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;

Review Comment:
   If nothing is filled in, the default value is null, and a NullPointerException (NPE) will occur during startup



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550252565


##########
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() {
+        return whitelistModeConfig;
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request);
+        }
+        Function<CommandField, String> oldReader = ModuleCommandAccessors.of(oldDto);
+        Function<CommandField, String> newReader = ModuleCommandAccessors.of(request);
+        // Skip fields whose value is unchanged (both null or equal).
+        return validateFields(newReader,
+                f -> Objects.equals(oldReader.apply(f), newReader.apply(f)));
+    }
+
+    /**
+     * Core validation loop: iterate through every {@link CommandField}, read its current
+     * value via {@code accessor}, and run {@link #validate(String)}. Fields for which
+     * {@code skip} returns {@code true} are bypassed (used for incremental validation).
+     *
+     * @return {@code "<label>: <offending>"} on the first failure, or {@code null} if all pass
+     */
+    private String validateFields(Function<CommandField, String> accessor,
+            Predicate<CommandField> skip) {
+        for (CommandField f : CommandField.values()) {
+            if (skip.test(f)) {
+                continue;
+            }
+            String offending = validate(accessor.apply(f));
+            if (StringUtils.isNotBlank(offending)) {
+                return f.label + ": " + offending;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if (InlongConstants.ASTERISK.equals(metaHit) || InlongConstants.QUESTION_MARK.equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf(InlongConstants.NEW_LINE_CHAR) >= 0
+                || rawCmd.indexOf(InlongConstants.CARRIAGE_RETURN_CHAR) >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, InlongConstants.SEMICOLON_CHAR);
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            if (StringUtils.isBlank(seg)) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(seg, InlongConstants.PIPE_CHAR);
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                if (StringUtils.isBlank(pipeSeg)) {
+                    continue;
+                }
+                List<String> argv = tokenize(pipeSeg);
+                if (argv.isEmpty()) {
+                    continue;
+                }
+                String cmdName = argv.get(0);
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.size() > 1) {

Review Comment:
   Case sensitivity cannot be ignored. sh and SH are not the same command



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550032112


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";

Review Comment:
   OK remove ' '



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550000258


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleServiceImpl.java:
##########
@@ -82,6 +101,30 @@ public Boolean update(ModuleRequest request, String operator) {
             throw new BusinessException(ErrorCodeEnum.MODULE_NOT_FOUND,
                     String.format("Module does not exist with id=%s", request.getId()));
         }
+
+        // Incremental whitelist validation (mode-aware): only check command fields
+        // that actually changed compared to the stored extParams.
+        ModuleCommandValidator.WhitelistMode mode = commandValidator.getMode();
+        if (mode != ModuleCommandValidator.WhitelistMode.OFF) {
+            ModuleDTO oldDto = null;
+            if (moduleConfigEntity.getExtParams() != null) {
+                oldDto = ModuleDTO.getFromJson(moduleConfigEntity.getExtParams());
+            }
+            String violation = commandValidator.validateChanged(oldDto, request);
+            if (violation != null) {
+                if (mode == ModuleCommandValidator.WhitelistMode.STRICT) {
+                    throw new BusinessException(ErrorCodeEnum.MODULE_COMMAND_NOT_IN_WHITELIST,
+                            String.format(
+                                    ErrorCodeEnum.MODULE_COMMAND_NOT_IN_WHITELIST.getMessage(),
+                                    violation));
+                } else {
+                    // WARN mode: log only, do not block the update
+                    LOGGER.warn("ModuleCommandValidator: non-whitelisted command in update "
+                            + "(mode=WARN, not blocking): moduleId={}, {}", request.getId(),
+                            violation);
+                }

Review Comment:
   done



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String whitelistModeConfig;

Review Comment:
   done



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550393817


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {

Review Comment:
   splitTopLevel return null means single quato



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550409848


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {

Review Comment:
   splitTopLevel returns null to represent a single quota, and size ==0 indicates an empty command.
   Use CollectionUtils. isEmpty confuses the two



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {

Review Comment:
   done



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550022093


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {
+            out.add("");

Review Comment:
   the code 'out.add(""); '  is redundant indeed



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550015093


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";

Review Comment:
   ok



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3551035610


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,

Review Comment:
   ok replace string to void, and throw exception if invalid



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "luchunliang (via GitHub)" <gi...@apache.org>.
luchunliang commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3548407069


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";

Review Comment:
   Nested single quotes.
   Like : startCommand: 'DISALLOWED_META_CHAR: '\*' — glob wildcards are not supported (Agent runs commands without a shell, so '*' and '?' will NOT be expanded); please specify explicit file paths'
   



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550246679


##########
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 {

Review Comment:
   It's not a pure utility class because it used @Value injects config
   True utility classes (XxxUtils) are all-static, stateless, no external deps. Validator is a config-driven service component — @Component is the standard approach. Spring's own org.springframework.validation.Validator and Hibernate's ConstraintValidator are both beans.



##########
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() {
+        return whitelistModeConfig;
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request);
+        }
+        Function<CommandField, String> oldReader = ModuleCommandAccessors.of(oldDto);
+        Function<CommandField, String> newReader = ModuleCommandAccessors.of(request);
+        // Skip fields whose value is unchanged (both null or equal).
+        return validateFields(newReader,
+                f -> Objects.equals(oldReader.apply(f), newReader.apply(f)));
+    }
+
+    /**
+     * Core validation loop: iterate through every {@link CommandField}, read its current
+     * value via {@code accessor}, and run {@link #validate(String)}. Fields for which
+     * {@code skip} returns {@code true} are bypassed (used for incremental validation).
+     *
+     * @return {@code "<label>: <offending>"} on the first failure, or {@code null} if all pass
+     */
+    private String validateFields(Function<CommandField, String> accessor,
+            Predicate<CommandField> skip) {
+        for (CommandField f : CommandField.values()) {
+            if (skip.test(f)) {
+                continue;
+            }
+            String offending = validate(accessor.apply(f));
+            if (StringUtils.isNotBlank(offending)) {
+                return f.label + ": " + offending;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if (InlongConstants.ASTERISK.equals(metaHit) || InlongConstants.QUESTION_MARK.equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf(InlongConstants.NEW_LINE_CHAR) >= 0
+                || rawCmd.indexOf(InlongConstants.CARRIAGE_RETURN_CHAR) >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, InlongConstants.SEMICOLON_CHAR);
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            if (StringUtils.isBlank(seg)) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(seg, InlongConstants.PIPE_CHAR);
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                if (StringUtils.isBlank(pipeSeg)) {
+                    continue;
+                }
+                List<String> argv = tokenize(pipeSeg);
+                if (argv.isEmpty()) {
+                    continue;
+                }
+                String cmdName = argv.get(0);
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.size() > 1) {
+                    for (int i = 1; i < argv.size(); i++) {
+                        if (argv.get(i).startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv.get(i) + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all module command fields on a {@link ModuleRequest}. Returns
+     * {@code null} if all commands pass, or a descriptive message such as
+     * {@code "startCommand: python3"} / {@code "startCommand: DISALLOWED_META_CHAR: '*' — ..."}
+     * if one fails.
+     *
+     * <p>Preferred over the 5-argument overload — passing the request object avoids
+     * argument-order mistakes at call sites.
+     */
+    public String validateAll(ModuleRequest request) {
+        if (request == null) {
+            return null;
+        }
+        return validateFields(ModuleCommandAccessors.of(request), f -> false);
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {

Review Comment:
   ok



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550028933


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,

Review Comment:
   ok



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550011040


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {

Review Comment:
   OK



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "dockerzhang (via GitHub)" <gi...@apache.org>.
dockerzhang merged PR #12155:
URL: https://github.com/apache/inlong/pull/12155


-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "luchunliang (via GitHub)" <gi...@apache.org>.
luchunliang commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3548490401


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {
+            out.add("");
+            return out;
+        }
+        StringBuilder cur = new StringBuilder();
+        char quote = 0;
+        for (int i = 0; i < raw.length(); i++) {
+            char c = raw.charAt(i);
+            if (quote != 0) {
+                cur.append(c);
+                if (c == quote) {
+                    quote = 0;
+                }
+                continue;
+            }
+            if (c == '\'' || c == '"') {
+                quote = c;
+                cur.append(c);
+                continue;
+            }
+            if (c == delim) {
+                out.add(cur.toString());
+                cur.setLength(0);
+                continue;
+            }
+            cur.append(c);
+        }
+        if (quote != 0) {
+            return null;
+        }
+        out.add(cur.toString());
+        return out;
+    }
+
+    /**
+     * Whitespace-based tokenizer that honours single/double quotes. (Same logic as the
+     * agent-side validator.)
+     */
+    private static String[] tokenize(String s) {
+        List<String> tokens = new ArrayList<>();

Review Comment:
   It is better performance to use substring funcation.For a shell-command whitelist/blacklist validator, mixed quoted/unquoted tokens are intentionally NOT supported — it is safer that way. The code can be simplified as follows:
   
   private static String[] tokenize(String s) {
       List<String> tokens = new ArrayList<>();
       int i = 0, n = s.length();
       while (i < n) {
           while (i < n && Character.isWhitespace(s.charAt(i))) i++;
           if (i >= n) break;
           char c = s.charAt(i);
           if (c == '\'' || c == '"') {
               int start = i + 1;
               int end = s.indexOf(c, start);
               if (end < 0) {
                   tokens.add(s.substring(start));
                   break;
               }
               tokens.add(s.substring(start, end));
               i = end + 1;
           } else {
               int start = i;
               while (i < n && !Character.isWhitespace(s.charAt(i))
                       && s.charAt(i) != '\'' && s.charAt(i) != '"') {
                   i++;
               }
               tokens.add(s.substring(start, i));
           }
       }
       return tokens.toArray(new String[0]);
   }
   



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550036552


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";

Review Comment:
   ok



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {

Review Comment:
   ok



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
fuweng11 commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3548323678


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleServiceImpl.java:
##########
@@ -82,6 +101,30 @@ public Boolean update(ModuleRequest request, String operator) {
             throw new BusinessException(ErrorCodeEnum.MODULE_NOT_FOUND,
                     String.format("Module does not exist with id=%s", request.getId()));
         }
+
+        // Incremental whitelist validation (mode-aware): only check command fields
+        // that actually changed compared to the stored extParams.
+        ModuleCommandValidator.WhitelistMode mode = commandValidator.getMode();
+        if (mode != ModuleCommandValidator.WhitelistMode.OFF) {
+            ModuleDTO oldDto = null;
+            if (moduleConfigEntity.getExtParams() != null) {
+                oldDto = ModuleDTO.getFromJson(moduleConfigEntity.getExtParams());
+            }
+            String violation = commandValidator.validateChanged(oldDto, request);
+            if (violation != null) {
+                if (mode == ModuleCommandValidator.WhitelistMode.STRICT) {
+                    throw new BusinessException(ErrorCodeEnum.MODULE_COMMAND_NOT_IN_WHITELIST,
+                            String.format(
+                                    ErrorCodeEnum.MODULE_COMMAND_NOT_IN_WHITELIST.getMessage(),
+                                    violation));
+                } else {
+                    // WARN mode: log only, do not block the update
+                    LOGGER.warn("ModuleCommandValidator: non-whitelisted command in update "
+                            + "(mode=WARN, not blocking): moduleId={}, {}", request.getId(),
+                            violation);
+                }

Review Comment:
   ```suggestion
                   if (mode == ModuleCommandValidator.WhitelistMode.STRICT) {
                       throw new BusinessException(ErrorCodeEnum.MODULE_COMMAND_NOT_IN_WHITELIST,
                               String.format(
                                       ErrorCodeEnum.MODULE_COMMAND_NOT_IN_WHITELIST.getMessage(),
                                       violation));
                   }
                   
                    // WARN mode: log only, do not block the update
                    LOGGER.warn("ModuleCommandValidator: non-whitelisted command in update "
                               + "(mode=WARN, not blocking): moduleId={}, {}", request.getId(),
                               violation);
                   
   ```



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/enums/ErrorCodeEnum.java:
##########
@@ -169,6 +169,8 @@ public enum ErrorCodeEnum {
 
     MODULE_NOT_FOUND(6001, "Module does not exist/no operation authority"),
     MODULE_INFO_INCORRECT(6002, "Module info was incorrect"),
+    MODULE_COMMAND_NOT_IN_WHITELIST(6003,
+            "Command validation failed: '%s'"),

Review Comment:
   ```suggestion
       MODULE_COMMAND_NOT_IN_WHITELIST(6003,
               "Module command  not in whitlist: '%s'"),
   ```



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String whitelistModeConfig;

Review Comment:
   ```suggestion
        @Value("${module.command.whitelistMode:WARN}")
       private WhitelistMode whitelistMode;
   ```
   Enum values can be directly injected.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {

Review Comment:
   Ditto.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {
+            out.add("");

Review Comment:
   What is the purpose of this code?



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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;

Review Comment:
   Without a default value, the startup will fail.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {

Review Comment:
   Use `InlongConstants.COMMA`



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {

Review Comment:
   ```suggestion
               if (StringUtils.isBlank(offending)) {
   ```



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {

Review Comment:
   ```suggestion
           if (CollectionUtils.isNotEmpty(segments)) {
   ```



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";

Review Comment:
   ditto.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {

Review Comment:
   Ditto.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,

Review Comment:
   `String startCommand, String stopCommand, String checkCommand` are essentially all commands. Can they be combined into one parameter for passing.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,

Review Comment:
   The return value only distinguishes between null and non-null. Why not `Boolean validateAll`?



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {
+            out.add("");
+            return out;
+        }
+        StringBuilder cur = new StringBuilder();
+        char quote = 0;
+        for (int i = 0; i < raw.length(); i++) {
+            char c = raw.charAt(i);
+            if (quote != 0) {
+                cur.append(c);
+                if (c == quote) {
+                    quote = 0;
+                }
+                continue;
+            }
+            if (c == '\'' || c == '"') {

Review Comment:
   Ditto.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";

Review Comment:
   Ditto.



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3550021904


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {
+            out.add("");
+            return out;
+        }
+        StringBuilder cur = new StringBuilder();
+        char quote = 0;
+        for (int i = 0; i < raw.length(); i++) {
+            char c = raw.charAt(i);
+            if (quote != 0) {
+                cur.append(c);
+                if (c == quote) {
+                    quote = 0;
+                }
+                continue;
+            }
+            if (c == '\'' || c == '"') {

Review Comment:
   OK



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {
+            out.add("");

Review Comment:
   the code 'out.add(""); '  is redundant indeedy



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3549453963


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {
+            out.add("");

Review Comment:
   Characters inside single-quoted ('...') or double-quoted ("...") regions are treated as literals and will not cause the string to be split. 
   echo "hello;world" not split  
   echo hello; world  split to [echo hello, world]



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3549373052


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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;

Review Comment:
   Written this way, the default value is ""



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [INLONG-12149][Improve] [Manager] Add command whitelist validation in Manager [inlong]

Posted by "spiritxishi (via GitHub)" <gi...@apache.org>.
spiritxishi commented on code in PR #12155:
URL: https://github.com/apache/inlong/pull/12155#discussion_r3555881536


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/module/ModuleCommandValidator.java:
##########
@@ -0,0 +1,388 @@
+/*
+ * 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.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;
+
+/**
+ * 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 String 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;
+
+        public static WhitelistMode fromConfig(String val) {
+            if (StringUtils.isBlank(val)) {
+                return WARN;
+            }
+            String upper = val.trim().toUpperCase();
+            try {
+                return valueOf(upper);
+            } catch (IllegalArgumentException e) {
+                LOGGER.warn("ModuleCommandValidator: invalid module.command.whitelistMode '{}',"
+                        + " falling back to STRICT", val);
+                return STRICT;
+            }
+        }
+    }
+
+    /** 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(",")) {
+                    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() {
+        return WhitelistMode.fromConfig(whitelistModeConfig);
+    }
+
+    /**
+     * Incremental validation — only validates command fields that have actually changed
+     * between the existing module config (from DB) and the incoming update request.
+     * <p>
+     * This avoids blocking updates for existing modules that only change non-command
+     * fields (e.g. version number, package id).
+     *
+     * @param oldDto  the existing ModuleDTO parsed from DB extParams (may be null if
+     *                extParams was empty — all fields are treated as changed)
+     * @param request the incoming update request
+     * @return the offending field name + command, or {@code null} if all changed commands pass
+     */
+    public String validateChanged(ModuleDTO oldDto, ModuleRequest request) {
+        if (oldDto == null) {
+            // No existing data — validate everything (same as save)
+            return validateAll(request.getStartCommand(), request.getStopCommand(),
+                    request.getCheckCommand(), request.getInstallCommand(),
+                    request.getUninstallCommand());
+        }
+
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] oldValues = {
+                oldDto.getStartCommand(), oldDto.getStopCommand(), oldDto.getCheckCommand(),
+                oldDto.getInstallCommand(), oldDto.getUninstallCommand()};
+        String[] newValues = {
+                request.getStartCommand(), request.getStopCommand(), request.getCheckCommand(),
+                request.getInstallCommand(), request.getUninstallCommand()};
+
+        for (int i = 0; i < fields.length; i++) {
+            String oldVal = oldValues[i];
+            String newVal = newValues[i];
+            // Skip if unchanged (both null or equal)
+            if (Objects.equals(oldVal, newVal)) {
+                continue;
+            }
+            String offending = validate(newVal);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Scan the raw command string for any metacharacter substring from
+     * {@link #META_CHAR_BLACKLIST}. Returns the first hit, or {@code null} if clean.
+     * (Same logic as the agent-side validator.)
+     */
+    private static String firstMetaCharHit(String raw) {
+        for (String meta : META_CHAR_BLACKLIST) {
+            if (raw.contains(meta)) {
+                return meta;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Validate a raw command string. Returns a descriptive error string if invalid, or
+     * {@code null} if valid. The error prefix indicates the rejection reason:
+     * {@code DISALLOWED_META_CHAR}, {@code FORBIDDEN_SH_C_FLAG}, or just the offending
+     * command name (NOT_IN_WHITELIST).
+     *
+     * @param rawCmd the raw command string (may be null or blank — skipped)
+     * @return error description, or {@code null} if valid
+     */
+    public String validate(String rawCmd) {
+        if (StringUtils.isBlank(rawCmd)) {
+            return null;
+        }
+
+        // metacharacter blacklist (whole string, before splitting)
+        String metaHit = firstMetaCharHit(rawCmd);
+        if (metaHit != null) {
+            if ("*".equals(metaHit) || "?".equals(metaHit)) {
+                return "DISALLOWED_META_CHAR: '" + metaHit
+                        + "' — glob wildcards are not supported (Agent runs commands without a shell,"
+                        + " so '*' and '?' will NOT be expanded); please specify explicit file paths";
+            }
+            return "DISALLOWED_META_CHAR: '" + metaHit + "'";
+        }
+        if (rawCmd.indexOf('\n') >= 0 || rawCmd.indexOf('\r') >= 0) {
+            return "DISALLOWED_META_CHAR: line-break";
+        }
+
+        Set<String> whitelist = getEffectiveWhitelist();
+
+        // Split by ';' into sub-commands (quote-aware), then by '|' into pipe segments
+        List<String> segments = splitTopLevel(rawCmd, ';');
+        if (segments == null) {
+            return "DISALLOWED_META_CHAR: unterminated quote";
+        }
+        for (String seg : segments) {
+            String trimmed = seg == null ? "" : seg.trim();
+            if (trimmed.isEmpty()) {
+                continue;
+            }
+            List<String> pipeSegs = splitTopLevel(trimmed, '|');
+            if (pipeSegs == null) {
+                return "DISALLOWED_META_CHAR: unterminated quote in pipe segment";
+            }
+            for (String pipeSeg : pipeSegs) {
+                String pipeTrimmed = pipeSeg == null ? "" : pipeSeg.trim();
+                if (pipeTrimmed.isEmpty()) {
+                    continue;
+                }
+                String[] argv = tokenize(pipeTrimmed);
+                if (argv.length == 0) {
+                    continue;
+                }
+                String cmdName = argv[0];
+
+                // argv[0] whitelist
+                if (!whitelist.contains(cmdName)) {
+                    return cmdName;
+                }
+
+                // forbid sh/bash -c flag (inline script execution)
+                if (("sh".equals(cmdName) || "bash".equals(cmdName)) && argv.length > 1) {
+                    for (int i = 1; i < argv.length; i++) {
+                        if (argv[i].startsWith("-c")) {
+                            return "FORBIDDEN_SH_C_FLAG: '" + cmdName + " " + argv[i] + "'";
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Thoroughly validate all commands in a module request. Returns {@code null} if all
+     * commands pass, or a descriptive message like {@code "startCommand: 'python3'"} if
+     * one fails.
+     */
+    public String validateAll(String startCommand, String stopCommand, String checkCommand,
+            String installCommand, String uninstallCommand) {
+        String[] fields = {"startCommand", "stopCommand", "checkCommand", "installCommand", "uninstallCommand"};
+        String[] values = {startCommand, stopCommand, checkCommand, installCommand, uninstallCommand};
+        for (int i = 0; i < fields.length; i++) {
+            String offending = validate(values[i]);
+            if (offending != null) {
+                return fields[i] + ": '" + offending + "'";
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Quote-aware split of {@code raw} on the top-level {@code delim} character. Characters
+     * inside a single-quoted or double-quoted region are treated as literals and do not
+     * split the string. Returns {@code null} when the input has an unterminated quote.
+     */
+    static List<String> splitTopLevel(String raw, char delim) {
+        List<String> out = new ArrayList<>();
+        if (raw == null) {
+            out.add("");
+            return out;
+        }
+        StringBuilder cur = new StringBuilder();
+        char quote = 0;
+        for (int i = 0; i < raw.length(); i++) {
+            char c = raw.charAt(i);
+            if (quote != 0) {
+                cur.append(c);
+                if (c == quote) {
+                    quote = 0;
+                }
+                continue;
+            }
+            if (c == '\'' || c == '"') {
+                quote = c;
+                cur.append(c);
+                continue;
+            }
+            if (c == delim) {
+                out.add(cur.toString());
+                cur.setLength(0);
+                continue;
+            }
+            cur.append(c);
+        }
+        if (quote != 0) {
+            return null;
+        }
+        out.add(cur.toString());
+        return out;
+    }
+
+    /**
+     * Whitespace-based tokenizer that honours single/double quotes. (Same logic as the
+     * agent-side validator.)
+     */
+    private static String[] tokenize(String s) {
+        List<String> tokens = new ArrayList<>();

Review Comment:
   OK



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org