|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shenyu.common.dto; |
| 19 | + |
| 20 | +import com.google.gson.JsonElement; |
| 21 | +import com.google.gson.JsonObject; |
| 22 | + |
| 23 | +import java.net.URI; |
| 24 | +import java.net.URISyntaxException; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +/** |
| 30 | + * Shared immutable MCP rule configuration for Admin and the gateway. |
| 31 | + */ |
| 32 | +public final class AgentGatewayMcpConfig { |
| 33 | + |
| 34 | + private static final Set<String> FIELDS = Set.of("allowedTools", "allowedOrigins", "responseMode", "timeoutMs", "maxRequestBytes", "maxResponseBytes"); |
| 35 | + |
| 36 | + private final Set<String> allowedTools; |
| 37 | + |
| 38 | + private final Set<String> allowedOrigins; |
| 39 | + |
| 40 | + private final String responseMode; |
| 41 | + |
| 42 | + private final int timeoutMs; |
| 43 | + |
| 44 | + private final int maxRequestBytes; |
| 45 | + |
| 46 | + private final int maxResponseBytes; |
| 47 | + |
| 48 | + private AgentGatewayMcpConfig(final JsonObject object) { |
| 49 | + allowedTools = strings(object, "allowedTools", false); |
| 50 | + allowedOrigins = strings(object, "allowedOrigins", true); |
| 51 | + responseMode = text(object, "responseMode", "json"); |
| 52 | + if (!Set.of("json", "sse").contains(responseMode)) { |
| 53 | + throw invalid("responseMode", "must be json or sse"); |
| 54 | + } |
| 55 | + timeoutMs = integer(object, "timeoutMs", 30000, 100, 120000); |
| 56 | + maxRequestBytes = integer(object, "maxRequestBytes", 262144, 1024, 1048576); |
| 57 | + maxResponseBytes = integer(object, "maxResponseBytes", 1048576, 1024, 4194304); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Validate known values consistently while allowing a strict Admin boundary. |
| 62 | + * |
| 63 | + * @param element MCP configuration object |
| 64 | + * @param rejectUnknown whether to reject unknown configuration fields |
| 65 | + * @return immutable configuration snapshot |
| 66 | + */ |
| 67 | + public static AgentGatewayMcpConfig parse(final JsonElement element, final boolean rejectUnknown) { |
| 68 | + if (Objects.isNull(element) || !element.isJsonObject()) { |
| 69 | + throw new IllegalArgumentException("mcp must be an object"); |
| 70 | + } |
| 71 | + JsonObject object = element.getAsJsonObject(); |
| 72 | + if (rejectUnknown) { |
| 73 | + for (String field : object.keySet()) { |
| 74 | + if (!FIELDS.contains(field)) { |
| 75 | + throw new IllegalArgumentException("mcp contains an unknown field"); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return new AgentGatewayMcpConfig(object); |
| 80 | + } |
| 81 | + |
| 82 | + public Set<String> getAllowedTools() { |
| 83 | + return allowedTools; |
| 84 | + } |
| 85 | + |
| 86 | + public Set<String> getAllowedOrigins() { |
| 87 | + return allowedOrigins; |
| 88 | + } |
| 89 | + |
| 90 | + public String getResponseMode() { |
| 91 | + return responseMode; |
| 92 | + } |
| 93 | + |
| 94 | + public int getTimeoutMs() { |
| 95 | + return timeoutMs; |
| 96 | + } |
| 97 | + |
| 98 | + public int getMaxRequestBytes() { |
| 99 | + return maxRequestBytes; |
| 100 | + } |
| 101 | + |
| 102 | + public int getMaxResponseBytes() { |
| 103 | + return maxResponseBytes; |
| 104 | + } |
| 105 | + |
| 106 | + private Set<String> strings(final JsonObject object, final String field, final boolean origins) { |
| 107 | + if (!object.has(field)) { |
| 108 | + return Set.of(); |
| 109 | + } |
| 110 | + JsonElement element = object.get(field); |
| 111 | + if (!element.isJsonArray()) { |
| 112 | + throw invalid(field, "must be an array"); |
| 113 | + } |
| 114 | + Set<String> result = new HashSet<>(); |
| 115 | + for (JsonElement entry : element.getAsJsonArray()) { |
| 116 | + if (!entry.isJsonPrimitive() || !entry.getAsJsonPrimitive().isString()) { |
| 117 | + throw invalid(field, "must contain strings"); |
| 118 | + } |
| 119 | + String value = entry.getAsString(); |
| 120 | + if (value.isBlank() || "*".equals(value) || !value.equals(value.trim()) || !result.add(value)) { |
| 121 | + throw invalid(field, "must contain unique explicit nonblank values"); |
| 122 | + } |
| 123 | + if (origins && !isOrigin(value)) { |
| 124 | + throw invalid(field, "must contain exact HTTP origins without paths or credentials"); |
| 125 | + } |
| 126 | + } |
| 127 | + return Set.copyOf(result); |
| 128 | + } |
| 129 | + |
| 130 | + private boolean isOrigin(final String value) { |
| 131 | + try { |
| 132 | + URI origin = new URI(value); |
| 133 | + return Objects.nonNull(origin.getScheme()) && Set.of("http", "https").contains(origin.getScheme()) && Objects.nonNull(origin.getHost()) |
| 134 | + && Objects.isNull(origin.getRawUserInfo()) && Objects.isNull(origin.getRawQuery()) && Objects.isNull(origin.getRawFragment()) |
| 135 | + && (Objects.isNull(origin.getRawPath()) || origin.getRawPath().isEmpty()) |
| 136 | + && origin.getPort() >= -1 && origin.getPort() <= 65535; |
| 137 | + } catch (URISyntaxException error) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private String text(final JsonObject object, final String field, final String defaultValue) { |
| 143 | + if (!object.has(field)) { |
| 144 | + return defaultValue; |
| 145 | + } |
| 146 | + JsonElement value = object.get(field); |
| 147 | + if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { |
| 148 | + throw invalid(field, "must be a string"); |
| 149 | + } |
| 150 | + return value.getAsString(); |
| 151 | + } |
| 152 | + |
| 153 | + private int integer(final JsonObject object, final String field, final int defaultValue, final int minimum, final int maximum) { |
| 154 | + if (!object.has(field)) { |
| 155 | + return defaultValue; |
| 156 | + } |
| 157 | + JsonElement value = object.get(field); |
| 158 | + if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isNumber()) { |
| 159 | + throw invalid(field, "must be an integer"); |
| 160 | + } |
| 161 | + try { |
| 162 | + int number = value.getAsBigDecimal().intValueExact(); |
| 163 | + if (number < minimum || number > maximum) { |
| 164 | + throw invalid(field, "is outside its allowed range"); |
| 165 | + } |
| 166 | + return number; |
| 167 | + } catch (ArithmeticException | NumberFormatException error) { |
| 168 | + throw invalid(field, "must be an integer within its allowed range"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private IllegalArgumentException invalid(final String field, final String message) { |
| 173 | + return new IllegalArgumentException("mcp." + field + " " + message); |
| 174 | + } |
| 175 | +} |
0 commit comments