oscerd commented on code in PR #25020:
URL: https://github.com/apache/camel/pull/25020#discussion_r3636818315


##########
dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/McpOutputGuardrail.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.util.ArrayList;
+import java.util.List;
+
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+
+import io.quarkiverse.mcp.server.Content;
+import io.quarkiverse.mcp.server.TextContent;
+import io.quarkiverse.mcp.server.ToolOutputGuardrail;
+import io.quarkiverse.mcp.server.ToolResponse;
+
+/**
+ * Global output guardrail that performs secret redaction on tool responses 
and logs audit trail entries for tool
+ * results.
+ */
+@ApplicationScoped
+public class McpOutputGuardrail implements ToolOutputGuardrail {

Review Comment:
   Fixed — output guardrail was removed and replaced by the CDI interceptor. 
Duration is now tracked via `System.nanoTime()` and the `redacted` flag is 
properly set when redaction occurs.
   
   _Claude Code on behalf of oscerd_



##########
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:
   Fixed — introduced `wasRedacted` variable that's set to `true` when 
redaction occurs, and passed to the audit logger in the `finally` block. Good 
catch!
   
   _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]

Reply via email to