ramu11 commented on code in PR #2094: URL: https://github.com/apache/camel-spring-boot/pull/2094#discussion_r4577180879
########## components-starter/camel-mcp-server-starter/src/main/java/org/apache/camel/springboot/mcp/server/SpringAiMcpTransportContext.java: ########## @@ -0,0 +1,294 @@ +/* + * 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.springboot.mcp.server; + +import java.lang.reflect.Method; +import java.security.Principal; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import io.modelcontextprotocol.common.McpTransportContext; +import io.modelcontextprotocol.server.McpSyncServerExchange; +import org.apache.camel.component.mcp.server.McpToolCallContext; +import org.springframework.web.servlet.function.ServerRequest; + +/** + * Captures the authenticated HTTP caller when Spring AI creates the MCP transport context, + * and makes that caller available to Camel tool execution. + * <p> + * The identity is taken from the servlet request principal established by the application's + * security infrastructure. It is not derived from MCP tool arguments or client-supplied + * identity headers. + * <p> + * Spring Security is optional for applications using this starter. Its APIs are accessed + * reflectively, and reflective accessors are cached per Authentication implementation. + * <p> + * Spring authorities with the ROLE_ prefix are exposed without that prefix. SCOPE_ authorities + * are mapped to OAuth scopes, and FACTOR_ authorities are not treated as application roles. + * When the principal is a JwtAuthenticationToken, its subject, scopes and claims are also + * propagated to the Camel tool exchange. + * <p> + * Applications that provide their own {@code WebMvcStreamableServerTransportProvider} + * can preserve caller identity by installing + * {@code .contextExtractor(SpringAiMcpTransportContext::extract)} on its builder. + */ +public final class SpringAiMcpTransportContext { + + static final String CALL_CONTEXT_KEY = + SpringAiMcpTransportContext.class.getName() + ".toolCallContext"; + + private static final String AUTHENTICATION_CLASS = + "org.springframework.security.core.Authentication"; + private static final String GRANTED_AUTHORITY_CLASS = + "org.springframework.security.core.GrantedAuthority"; + private static final String JWT_AUTHENTICATION_CLASS = + "org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken"; + + private static final ClassValue<AuthenticationAccessors> ACCESSORS = new ClassValue<>() { + @Override + protected AuthenticationAccessors computeValue(Class<?> type) { + ClassLoader loader = type.getClassLoader(); + if (loader == null) { + loader = SpringAiMcpTransportContext.class.getClassLoader(); + } + + try { + Class<?> authenticationType = Class.forName(AUTHENTICATION_CLASS, false, loader); + if (!authenticationType.isAssignableFrom(type)) { + return AuthenticationAccessors.NONE; + } + + Class<?> authorityType = Class.forName(GRANTED_AUTHORITY_CLASS, false, loader); + Method getAuthorities = authenticationType.getMethod("getAuthorities"); + Method getAuthority = authorityType.getMethod("getAuthority"); + + Method getTokenAttributes = null; + try { + Class<?> jwtAuthenticationType = Class.forName(JWT_AUTHENTICATION_CLASS, false, loader); + if (jwtAuthenticationType.isAssignableFrom(type)) { + getTokenAttributes = jwtAuthenticationType.getMethod("getTokenAttributes"); + } + } catch (ReflectiveOperationException | LinkageError | SecurityException ignored) { + // OAuth2 Resource Server is optional. + } + + return new AuthenticationAccessors(getAuthorities, getAuthority, getTokenAttributes); + } catch (ReflectiveOperationException | LinkageError | SecurityException e) { + return AuthenticationAccessors.NONE; + } + } + }; + + private SpringAiMcpTransportContext() { + } + + /** + * Extracts the authenticated caller from the current HTTP request. + * + * @param request the Spring MVC request received by the MCP transport + * @return an MCP transport context containing the caller, or the empty context + */ + public static McpTransportContext extract(ServerRequest request) { + Principal principal = request.principal().orElse(null); + if (principal == null) { + return McpTransportContext.EMPTY; + } + + return McpTransportContext.create(Map.of(CALL_CONTEXT_KEY, toCallContext(principal))); + } + + /** + * Retrieves the caller context attached to the MCP request by the transport extractor. + * + * @param exchange the MCP SDK server exchange + * @return the captured caller context, or the empty context + */ + static McpToolCallContext from(McpSyncServerExchange exchange) { + if (exchange == null || exchange.transportContext() == null) { + return McpToolCallContext.EMPTY; + } + + Object value = exchange.transportContext().get(CALL_CONTEXT_KEY); + return value instanceof McpToolCallContext context + ? context + : McpToolCallContext.EMPTY; + } + + private static McpToolCallContext toCallContext(Principal principal) { + AuthenticationAccessors accessors = ACCESSORS.get(principal.getClass()); + Set<String> roles = new LinkedHashSet<>(); + Set<String> scopes = new LinkedHashSet<>(); + Map<String, Object> claims = null; + + if (accessors != AuthenticationAccessors.NONE) { + readAuthorities(principal, accessors, roles, scopes); + claims = readJwtClaims(principal, accessors); + } + + String oauthSubject = null; + if (claims != null) { + Object subject = claims.get("sub"); + if (subject instanceof String value && !value.isBlank()) { + oauthSubject = value; + } + + addScopeClaim(scopes, claims.get("scope")); + addScopeClaim(scopes, claims.get("scp")); + addClaimRoles(claims, roles); Review Comment: Thanks for highlighting this security concern. Fixed by deriving Camel roles and OAuth scopes exclusively from the authorities granted by Spring Security's configured authentication provider or JWT authority converter. Raw token claims, including `roles`, `realm_access.roles`, and `groups`, are still propagated through `McpToolCallContext` as OAuth claims, but they are no longer promoted to Camel authorization roles. This preserves the application's Spring Security authority-mapping policy and prevents claims that were not converted into granted authorities from bypassing Spring Security authorization decisions. Added regression tests verifying that `realm_access.roles`, `groups`, and raw `roles` claims do not independently grant Camel roles. -- 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]
