oscerd commented on code in PR #25020: URL: https://github.com/apache/camel/pull/25020#discussion_r3636820602
########## 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: Added a TODO comment documenting this limitation. CDI interceptors don't have access to the MCP connection context — these fields will be populated once the Quarkus MCP Server supports global guardrails. Meanwhile, `McpAccessFilter` now extracts connection context where available. _Claude Code on behalf of oscerd_ -- 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]
