|
| 1 | +/* |
| 2 | + * Copyright 2018 Red Hat, Inc. and/or its affiliates |
| 3 | + * and other contributors as indicated by the @author tags. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * 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 | +package org.keycloak.saml.validators; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.net.URISyntaxException; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.regex.Matcher; |
| 25 | +import java.util.regex.Pattern; |
| 26 | + |
| 27 | +/** |
| 28 | + * Check that Destination field in SAML request/response is either unset or matches the expected one. |
| 29 | + * @author hmlnarik |
| 30 | + */ |
| 31 | +public class DestinationValidator { |
| 32 | + |
| 33 | + private static final Pattern PROTOCOL_MAP_PATTERN = Pattern.compile("\\s*([a-zA-Z][a-zA-Z\\d+-.]*)\\s*=\\s*(\\d+)\\s*"); |
| 34 | + private static final String[] DEFAULT_PROTOCOL_TO_PORT_MAP = new String[] { "http=80", "https=443" }; |
| 35 | + |
| 36 | + private final Map<String, Integer> knownPorts; |
| 37 | + private final Map<Integer, String> knownProtocols; |
| 38 | + |
| 39 | + private DestinationValidator(Map<String, Integer> knownPorts, Map<Integer, String> knownProtocols) { |
| 40 | + this.knownPorts = knownPorts; |
| 41 | + this.knownProtocols = knownProtocols; |
| 42 | + } |
| 43 | + |
| 44 | + public static DestinationValidator forProtocolMap(String[] protocolMappings) { |
| 45 | + if (protocolMappings == null) { |
| 46 | + protocolMappings = DEFAULT_PROTOCOL_TO_PORT_MAP; |
| 47 | + } |
| 48 | + |
| 49 | + Map<String, Integer> knownPorts = new HashMap<>(); |
| 50 | + Map<Integer, String> knownProtocols = new HashMap<>(); |
| 51 | + |
| 52 | + for (String protocolMapping : protocolMappings) { |
| 53 | + Matcher m = PROTOCOL_MAP_PATTERN.matcher(protocolMapping); |
| 54 | + if (m.matches()) { |
| 55 | + Integer port = Integer.valueOf(m.group(2)); |
| 56 | + String proto = m.group(1); |
| 57 | + |
| 58 | + knownPorts.put(proto, port); |
| 59 | + knownProtocols.put(port, proto); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return new DestinationValidator(knownPorts, knownProtocols); |
| 64 | + } |
| 65 | + |
| 66 | + public boolean validate(String expectedDestination, String actualDestination) { |
| 67 | + try { |
| 68 | + return validate(expectedDestination == null ? null : URI.create(expectedDestination), actualDestination); |
| 69 | + } catch (IllegalArgumentException ex) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public boolean validate(String expectedDestination, URI actualDestination) { |
| 75 | + try { |
| 76 | + return validate(expectedDestination == null ? null : URI.create(expectedDestination), actualDestination); |
| 77 | + } catch (IllegalArgumentException ex) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public boolean validate(URI expectedDestination, String actualDestination) { |
| 83 | + try { |
| 84 | + return validate(expectedDestination, actualDestination == null ? null : URI.create(actualDestination)); |
| 85 | + } catch (IllegalArgumentException ex) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public boolean validate(URI expectedDestination, URI actualDestination) { |
| 91 | + if (actualDestination == null) { |
| 92 | + return true; // destination is optional |
| 93 | + } |
| 94 | + |
| 95 | + if (expectedDestination == null) { |
| 96 | + return false; // expected destination is mandatory |
| 97 | + } |
| 98 | + |
| 99 | + if (Objects.equals(expectedDestination, actualDestination)) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + Integer portByScheme = knownPorts.get(expectedDestination.getScheme()); |
| 104 | + String protocolByPort = knownProtocols.get(expectedDestination.getPort()); |
| 105 | + |
| 106 | + URI updatedUri = null; |
| 107 | + try { |
| 108 | + if (expectedDestination.getPort() < 0 && portByScheme != null) { |
| 109 | + updatedUri = new URI( |
| 110 | + expectedDestination.getScheme(), |
| 111 | + expectedDestination.getUserInfo(), |
| 112 | + expectedDestination.getHost(), |
| 113 | + portByScheme, |
| 114 | + expectedDestination.getPath(), |
| 115 | + expectedDestination.getQuery(), |
| 116 | + expectedDestination.getFragment() |
| 117 | + ); |
| 118 | + } else if (expectedDestination.getPort() >= 0 && Objects.equals(protocolByPort, expectedDestination.getScheme())) { |
| 119 | + updatedUri = new URI( |
| 120 | + expectedDestination.getScheme(), |
| 121 | + expectedDestination.getUserInfo(), |
| 122 | + expectedDestination.getHost(), |
| 123 | + -1, |
| 124 | + expectedDestination.getPath(), |
| 125 | + expectedDestination.getQuery(), |
| 126 | + expectedDestination.getFragment() |
| 127 | + ); |
| 128 | + } |
| 129 | + } catch (URISyntaxException ex) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + return Objects.equals(updatedUri, actualDestination); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments