|
| 1 | +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | + ~ Copyright 2023 Adobe |
| 3 | + ~ |
| 4 | + ~ Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + ~ you may not use this file except in compliance with the License. |
| 6 | + ~ You may obtain a copy of the License at |
| 7 | + ~ |
| 8 | + ~ http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + ~ |
| 10 | + ~ Unless required by applicable law or agreed to in writing, software |
| 11 | + ~ distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + ~ See the License for the specific language governing permissions and |
| 14 | + ~ limitations under the License. |
| 15 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 16 | +package com.adobe.cq.wcm.core.components.internal.link; |
| 17 | + |
| 18 | +import java.io.UnsupportedEncodingException; |
| 19 | +import java.math.BigInteger; |
| 20 | +import java.net.URLDecoder; |
| 21 | +import java.net.URLEncoder; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.security.SecureRandom; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.LinkedHashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.regex.Matcher; |
| 29 | +import java.util.regex.Pattern; |
| 30 | + |
| 31 | +import org.apache.commons.httpclient.URI; |
| 32 | +import org.apache.commons.httpclient.URIException; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +/** |
| 37 | + * Utility methods for handling links |
| 38 | + */ |
| 39 | +public class LinkUtil { |
| 40 | + |
| 41 | + private final static Logger LOG = LoggerFactory.getLogger(LinkUtil.class); |
| 42 | + |
| 43 | + private final static List<Pattern> PATTERNS = Collections.singletonList(Pattern.compile("(<%[=@].*?%>)")); |
| 44 | + |
| 45 | + /** |
| 46 | + * Decodes and encoded or escaped URL taking care to not break Adobe Campaign expressions |
| 47 | + * like: /content/path/to/page.html?recipient=<%= recipient.id %> |
| 48 | + * |
| 49 | + * @param url The URL to decode |
| 50 | + * @return The decoded URL |
| 51 | + * @throws UnsupportedEncodingException |
| 52 | + */ |
| 53 | + public static String decode(final String url) throws UnsupportedEncodingException { |
| 54 | + // The link contain character sequences that are not well formatted and cannot be decoded, for example |
| 55 | + // Adobe Campaign expressions like: /content/path/to/page.html?recipient=<%= recipient.id %> |
| 56 | + final Map<String, String> placeholders = new LinkedHashMap<>(); |
| 57 | + final String masked = LinkUtil.mask(url, placeholders); |
| 58 | + final String decoded = URLDecoder.decode(masked, StandardCharsets.UTF_8.name()); |
| 59 | + final String unmasked = unmask(decoded, placeholders); |
| 60 | + return unmasked; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Escapes an URI based on path, query string and fragment: path?queryString#fragment |
| 65 | + * |
| 66 | + * @param path The URI path |
| 67 | + * @param queryString The URI query string |
| 68 | + * @param fragment The URI fragment |
| 69 | + * @return The escaped fragment |
| 70 | + */ |
| 71 | + public static String escape(final String path, final String queryString, final String fragment) { |
| 72 | + final Map<String, String> placeholders = new LinkedHashMap<>(); |
| 73 | + final String maskedQueryString = mask(queryString, placeholders); |
| 74 | + String escaped; |
| 75 | + URI parsed; |
| 76 | + try { |
| 77 | + parsed = new URI(path, false); |
| 78 | + } catch (URIException e) { |
| 79 | + parsed = null; |
| 80 | + LOG.error(e.getMessage(), e); |
| 81 | + } |
| 82 | + try { |
| 83 | + if (parsed != null) { |
| 84 | + escaped = new URI(parsed.getScheme(), parsed.getAuthority(), parsed.getPath(), maskedQueryString, null).toString(); |
| 85 | + } else { |
| 86 | + escaped = new URI(null, null, path, maskedQueryString, null).toString(); |
| 87 | + } |
| 88 | + if (fragment != null) { |
| 89 | + StringBuilder sb = new StringBuilder(escaped); |
| 90 | + escaped = sb.append("#") |
| 91 | + .append(URLEncoder.encode(fragment, StandardCharsets.UTF_8.name()).replace("+", "%20")) |
| 92 | + .toString(); |
| 93 | + } |
| 94 | + } catch (Exception e) { |
| 95 | + LOG.error(e.getMessage(), e); |
| 96 | + StringBuilder sb = new StringBuilder(path); |
| 97 | + if (queryString != null) { |
| 98 | + sb.append("?").append(maskedQueryString); |
| 99 | + } |
| 100 | + if (fragment != null) { |
| 101 | + sb.append("#").append(fragment); |
| 102 | + } |
| 103 | + escaped = sb.toString(); |
| 104 | + } |
| 105 | + final String unmasked = LinkUtil.unmask(escaped, placeholders); |
| 106 | + return unmasked; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Masks a given {@link String} by replacing all occurrences of {@link LinkUtil#PATTERNS} with a placeholder. |
| 111 | + * The generated placeholders are put into the given {@link Map} and can be used to unmask a {@link String} later on. |
| 112 | + * <p> |
| 113 | + * For example the given original {@link String} {@code /path/to/page.html?r=<%= recipient.id %>} will be transformed to |
| 114 | + * {@code /path/to/page.html?r=_abcd_} and the placeholder with the expression will be put into the given {@link Map}. |
| 115 | + * |
| 116 | + * @param original the original {@link String} |
| 117 | + * @param placeholders a {@link Map} the generated placeholders will be put in |
| 118 | + * @return the masked {@link String} |
| 119 | + * @see LinkUtil#unmask(String, Map) |
| 120 | + */ |
| 121 | + private static String mask(final String original, final Map<String, String> placeholders) { |
| 122 | + if (original == null) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + String masked = original; |
| 126 | + for (Pattern pattern : PATTERNS) { |
| 127 | + Matcher matcher = pattern.matcher(masked); |
| 128 | + while (matcher.find()) { |
| 129 | + String expression = matcher.group(1); |
| 130 | + String placeholder = newPlaceholder(masked); |
| 131 | + masked = masked.replaceFirst(Pattern.quote(expression), placeholder); |
| 132 | + placeholders.put(placeholder, expression); |
| 133 | + } |
| 134 | + } |
| 135 | + return masked; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Unmasks the given {@link String} by replacing the given placeholders with their original value. |
| 140 | + * <p> |
| 141 | + * For example the given masked {@link String} {@code /path/to/page.html?r=_abcd_} will be transformed to |
| 142 | + * {@code /path/to/page.html?r=<%= recipient.id %>} by replacing each of the given {@link Map}s keys with the corresponding value. |
| 143 | + * |
| 144 | + * @param masked the masked {@link String} |
| 145 | + * @param placeholders the {@link Map} of placeholders to replace |
| 146 | + * @return the unmasked {@link String} |
| 147 | + */ |
| 148 | + private static String unmask(final String masked, final Map<String, String> placeholders) { |
| 149 | + if (masked == null) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + String unmasked = masked; |
| 153 | + for (Map.Entry<String, String> placeholder : placeholders.entrySet()) { |
| 154 | + unmasked = unmasked.replaceFirst(placeholder.getKey(), placeholder.getValue()); |
| 155 | + } |
| 156 | + return unmasked; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Generate a new random placeholder that is not conflicting with any character sequence in the given {@link String}. |
| 161 | + * <p> |
| 162 | + * For example the given {@link String} {@code "foo"} a new random {@link String} will be returned that is not contained in the |
| 163 | + * given {@link String}. In this example the following {@link String}s will never be returned "f", "fo", "foo", "o", "oo". |
| 164 | + * |
| 165 | + * @param str the given {@link String} |
| 166 | + * @return the placeholder name |
| 167 | + */ |
| 168 | + private static String newPlaceholder(final String str) { |
| 169 | + SecureRandom random = new SecureRandom(); |
| 170 | + StringBuilder placeholderBuilder = new StringBuilder(5); |
| 171 | + |
| 172 | + do { |
| 173 | + placeholderBuilder.setLength(0); |
| 174 | + placeholderBuilder |
| 175 | + .append("_") |
| 176 | + .append(new BigInteger(16, random).toString(16)) |
| 177 | + .append("_"); |
| 178 | + } while (str.contains(placeholderBuilder)); |
| 179 | + |
| 180 | + return placeholderBuilder.toString(); |
| 181 | + } |
| 182 | +} |
0 commit comments