gnodet commented on code in PR #25020: URL: https://github.com/apache/camel/pull/25020#discussion_r3631027098
########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/McpSecurityInterceptor.java: ########## @@ -0,0 +1,170 @@ +/* + * 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.camel.dsl.jbang.core.commands.mcp; + +import java.lang.reflect.Method; + +import jakarta.annotation.Priority; +import jakarta.inject.Inject; +import jakarta.interceptor.AroundInvoke; +import jakarta.interceptor.Interceptor; +import jakarta.interceptor.InvocationContext; + +import io.quarkiverse.mcp.server.Tool; + +/** + * CDI interceptor for the MCP security execution layer. + * <p> + * Intercepts {@code @Tool}-annotated methods on classes marked with {@link McpSecured} to provide input sanitization, + * audit logging, and secret redaction. Authorization is handled by {@link McpAccessFilter} which is a global + * {@code ToolFilter}. + * <p> + * This replaces the ToolInputGuardrail/ToolOutputGuardrail approach since those require per-tool + * {@code @ToolGuardrails} annotations and are not auto-discovered as global CDI beans. + */ +@McpSecured +@Interceptor +@Priority(Interceptor.Priority.APPLICATION) +public class McpSecurityInterceptor { + + @Inject + McpSecurityConfig config; + + @Inject + McpAuditLogger auditLogger; + + @Inject + McpSecretRedactor redactor; + + @AroundInvoke + Object intercept(InvocationContext ctx) throws Exception { + if (!config.isEnabled()) { + return ctx.proceed(); + } + + Method method = ctx.getMethod(); + if (!method.isAnnotationPresent(Tool.class)) { + return ctx.proceed(); + } + + String toolName = method.getName(); + + // Input sanitization + sanitizeParameters(ctx); + + // Audit: log tool call + if (config.isAuditEnabled()) { + String arguments = null; + if (config.isAuditIncludeArguments()) { + arguments = summarizeParameters(ctx); + } + auditLogger.logToolCall(toolName, "", "", config.getAccessLevel().name(), arguments); + } + + long start = System.nanoTime(); + boolean isError = false; + try { + Object result = ctx.proceed(); + + // Secret redaction on string results + if (config.isRedactionEnabled() && result instanceof String s) { + if (redactor.containsSecret(s)) { + result = redactor.redact(s); + } + } + + return result; + } catch (Exception e) { + isError = true; + throw e; + } finally { + if (config.isAuditEnabled()) { + long durationMs = (System.nanoTime() - start) / 1_000_000; + auditLogger.logToolResult(toolName, "", isError, false, durationMs); + } + } + } + + private void sanitizeParameters(InvocationContext ctx) { + Object[] params = ctx.getParameters(); + if (params == null) { + return; + } + + int maxLen = config.getMaxArgumentLength(); + boolean modified = false; + + for (int i = 0; i < params.length; i++) { + if (params[i] instanceof String s) { + String clean = stripControlChars(s); + if (clean.length() > maxLen) { + clean = clean.substring(0, maxLen); + } + if (!clean.equals(s)) { + params[i] = clean; + modified = true; + } + } + } + + if (modified) { + ctx.setParameters(params); + } + } + + static String stripControlChars(String input) { + if (input == null) { Review Comment: ⚠️ Bug: The `redacted` parameter is always hardcoded to `false` here, but redaction can actually occur at lines 117-120 above. When `redactor.containsSecret(s)` is true and `redactor.redact(s)` is called, this audit entry should report `redacted: true`. Consider tracking it as a local variable: ```java boolean wasRedacted = false; // ... in try block: if (config.isRedactionEnabled() && result instanceof String s) { if (redactor.containsSecret(s)) { result = redactor.redact(s); wasRedacted = true; } } // ... in finally block: auditLogger.logToolResult(toolName, "", isError, wasRedacted, durationMs); ``` ########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/McpAuditLogger.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.camel.dsl.jbang.core.commands.mcp; + +import java.time.Instant; + +import jakarta.enterprise.context.ApplicationScoped; + +import org.jboss.logging.Logger; + +/** + * Structured audit logger for MCP tool invocations. + * <p> + * Produces single-line JSON log entries to a dedicated logger category, allowing operators to route audit logs + * independently via Quarkus logging configuration. + */ +@ApplicationScoped +public class McpAuditLogger { + + static final String AUDIT_LOGGER_NAME = "org.apache.camel.mcp.security.audit"; + + private static final Logger AUDIT_LOG = Logger.getLogger(AUDIT_LOGGER_NAME); + + public void logToolCall( + String tool, String connectionId, String clientName, + String accessLevel, String arguments) { + AUDIT_LOG.infof( + "{\"event\":\"tool_call\",\"tool\":\"%s\",\"connectionId\":\"%s\"," Review Comment: ⚠️ `logAccessDenied()` is defined and unit-tested but never called in any production code path. When `McpAccessFilter` denies a tool, the framework itself handles the rejection — but no audit log entry is written for denied attempts. This means denied access attempts are invisible in the audit trail, which undermines the security audit story. Consider calling this from `McpAccessFilter.test()` when it returns `false`, or removing it if execution-time denial auditing is out of scope for this PR. ########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/McpSecurityInterceptor.java: ########## @@ -0,0 +1,170 @@ +/* + * 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.camel.dsl.jbang.core.commands.mcp; + +import java.lang.reflect.Method; + +import jakarta.annotation.Priority; +import jakarta.inject.Inject; +import jakarta.interceptor.AroundInvoke; +import jakarta.interceptor.Interceptor; +import jakarta.interceptor.InvocationContext; + +import io.quarkiverse.mcp.server.Tool; + +/** + * CDI interceptor for the MCP security execution layer. + * <p> + * Intercepts {@code @Tool}-annotated methods on classes marked with {@link McpSecured} to provide input sanitization, + * audit logging, and secret redaction. Authorization is handled by {@link McpAccessFilter} which is a global + * {@code ToolFilter}. + * <p> + * This replaces the ToolInputGuardrail/ToolOutputGuardrail approach since those require per-tool + * {@code @ToolGuardrails} annotations and are not auto-discovered as global CDI beans. + */ +@McpSecured +@Interceptor +@Priority(Interceptor.Priority.APPLICATION) +public class McpSecurityInterceptor { + + @Inject + McpSecurityConfig config; + + @Inject + McpAuditLogger auditLogger; + + @Inject + McpSecretRedactor redactor; + + @AroundInvoke + Object intercept(InvocationContext ctx) throws Exception { + if (!config.isEnabled()) { + return ctx.proceed(); + } + + Method method = ctx.getMethod(); + if (!method.isAnnotationPresent(Tool.class)) { + return ctx.proceed(); + } + + String toolName = method.getName(); + + // Input sanitization + sanitizeParameters(ctx); + + // Audit: log tool call + if (config.isAuditEnabled()) { + String arguments = null; + if (config.isAuditIncludeArguments()) { + arguments = summarizeParameters(ctx); + } + auditLogger.logToolCall(toolName, "", "", config.getAccessLevel().name(), arguments); + } + + long start = System.nanoTime(); + boolean isError = false; + try { + Object result = ctx.proceed(); + + // Secret redaction on string results + if (config.isRedactionEnabled() && result instanceof String s) { + if (redactor.containsSecret(s)) { + result = redactor.redact(s); + } + } + + return result; + } catch (Exception e) { + isError = true; + throw e; + } finally { + if (config.isAuditEnabled()) { + long durationMs = (System.nanoTime() - start) / 1_000_000; + auditLogger.logToolResult(toolName, "", isError, false, durationMs); + } + } + } + + private void sanitizeParameters(InvocationContext ctx) { + Object[] params = ctx.getParameters(); + if (params == null) { + return; + } + Review Comment: Minor: `connectionId` and `clientName` are always empty strings here. These are key forensic fields in the audit schema. If the MCP connection context provides this info (e.g., via `InvocationContext` or request scope), it would strengthen the audit trail. If not currently available, a TODO comment noting the limitation would help future contributors. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
