|
6 | 6 |
|
7 | 7 | import java.util.ArrayList; |
8 | 8 | import java.util.List; |
9 | | -import java.util.Map; |
| 9 | +import java.util.function.Function; |
10 | 10 |
|
11 | 11 | import io.modelcontextprotocol.util.Assert; |
12 | 12 |
|
@@ -47,27 +47,18 @@ private DefaultServerTransportSecurityValidator(List<String> allowedOrigins, Lis |
47 | 47 | } |
48 | 48 |
|
49 | 49 | @Override |
50 | | - public void validateHeaders(Map<String, List<String>> headers) throws ServerTransportSecurityException { |
51 | | - boolean missingHost = true; |
52 | | - for (Map.Entry<String, List<String>> entry : headers.entrySet()) { |
53 | | - if (ORIGIN_HEADER.equalsIgnoreCase(entry.getKey())) { |
54 | | - List<String> values = entry.getValue(); |
55 | | - if (values == null || values.isEmpty()) { |
56 | | - throw new ServerTransportSecurityException(403, "Invalid Origin header"); |
57 | | - } |
58 | | - validateOrigin(values.get(0)); |
59 | | - } |
60 | | - else if (HOST_HEADER.equalsIgnoreCase(entry.getKey())) { |
61 | | - missingHost = false; |
62 | | - List<String> values = entry.getValue(); |
63 | | - if (values == null || values.isEmpty()) { |
64 | | - throw new ServerTransportSecurityException(421, "Invalid Host header"); |
65 | | - } |
66 | | - validateHost(values.get(0)); |
67 | | - } |
| 50 | + public void validateHeaders(Function<String, List<String>> headerAccessor) throws ServerTransportSecurityException { |
| 51 | + List<String> originValues = headerAccessor.apply(ORIGIN_HEADER); |
| 52 | + if (originValues != null && !originValues.isEmpty()) { |
| 53 | + validateOrigin(originValues.get(0)); |
68 | 54 | } |
69 | | - if (!allowedHosts.isEmpty() && missingHost) { |
70 | | - throw new ServerTransportSecurityException(421, "Invalid Host header"); |
| 55 | + |
| 56 | + if (!allowedHosts.isEmpty()) { |
| 57 | + List<String> hostValues = headerAccessor.apply(HOST_HEADER); |
| 58 | + if (hostValues == null || hostValues.isEmpty()) { |
| 59 | + throw new ServerTransportSecurityException(421, "Invalid Host header"); |
| 60 | + } |
| 61 | + validateHost(hostValues.get(0)); |
71 | 62 | } |
72 | 63 | } |
73 | 64 |
|
|
0 commit comments