|
| 1 | +/* |
| 2 | + * Copyright 2025 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package com.linecorp.armeria.server.docs; |
| 17 | + |
| 18 | +import static com.google.common.base.Preconditions.checkArgument; |
| 19 | +import static java.util.Objects.requireNonNull; |
| 20 | + |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +import com.google.common.collect.ImmutableSet; |
| 24 | + |
| 25 | +/** |
| 26 | + * Util class for DocServiceBuilder#injectedScripts method. |
| 27 | + */ |
| 28 | +public final class DocServiceInjectedScriptsUtil { |
| 29 | + |
| 30 | + private static final String HEX_COLOR_PATTERN = "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$"; |
| 31 | + private static final int MAX_COLOR_LENGTH = 7; |
| 32 | + private static final String SAFE_DOM_HOOK = "data-js-target"; |
| 33 | + private static final ImmutableSet<String> ALLOWED_FAVICON_EXTENSIONS = |
| 34 | + ImmutableSet.of(".ico", ".png", ".svg"); |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns a js script to change the title background color. |
| 38 | + * |
| 39 | + * @param color the color string to set |
| 40 | + * @return the js script |
| 41 | + */ |
| 42 | + public static String withTitleBackground(String color) { |
| 43 | + final String titleBackgroundKey = "titleBackground"; |
| 44 | + final String targetAttr = "main-app-bar"; |
| 45 | + validateHexColor(color, titleBackgroundKey); |
| 46 | + |
| 47 | + return buildStyleScript(color, targetAttr); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns a js script to change the goto component background color. |
| 52 | + * |
| 53 | + * @param color the color string to set |
| 54 | + * @return the js script |
| 55 | + */ |
| 56 | + public static String withGotoBackground(String color) { |
| 57 | + final String gotoBackgroundKey = "gotoBackground"; |
| 58 | + final String targetAttr = "goto-app-bar"; |
| 59 | + validateHexColor(color, gotoBackgroundKey); |
| 60 | + |
| 61 | + return buildStyleScript(color, targetAttr); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns a js script to change the web favicon. |
| 66 | + * |
| 67 | + * @param url the url string to set |
| 68 | + * @return the js script |
| 69 | + */ |
| 70 | + public static String withFavicon(String url) { |
| 71 | + final String faviconKey = "favicon"; |
| 72 | + validateFaviconUrl(url, faviconKey); |
| 73 | + |
| 74 | + return buildFaviconScript(escapeForJavaScriptUrl(url)); |
| 75 | + } |
| 76 | + |
| 77 | + private DocServiceInjectedScriptsUtil() {} |
| 78 | + |
| 79 | + /** |
| 80 | + * Validates the favicon url. |
| 81 | + * |
| 82 | + * @param url the url string to validate |
| 83 | + * @param key the name used in error messages |
| 84 | + */ |
| 85 | + private static void validateFaviconUrl(String url, String key) { |
| 86 | + requireNonNull(url, key); |
| 87 | + checkArgument(!url.trim().isEmpty(), "%s is empty.", key); |
| 88 | + checkArgument(hasValidFaviconExtension(url), "%s extension not allowed.",key); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Validates the favicon extension. |
| 93 | + * |
| 94 | + * @param url the url string |
| 95 | + * @return the result of validation |
| 96 | + */ |
| 97 | + private static boolean hasValidFaviconExtension(String url) { |
| 98 | + final String lowerUrl = url.toLowerCase(); |
| 99 | + return ALLOWED_FAVICON_EXTENSIONS.stream() |
| 100 | + .anyMatch(lowerUrl::endsWith); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Escapes special characters in a string to safely embed it in a JavaScript string literal. |
| 105 | + * |
| 106 | + * @param url the input string to escape |
| 107 | + * @return the escaped string |
| 108 | + */ |
| 109 | + private static String escapeForJavaScriptUrl(String url) { |
| 110 | + final StringBuilder escaped = new StringBuilder(url.length()); |
| 111 | + |
| 112 | + for (char c : url.toCharArray()) { |
| 113 | + switch (c) { |
| 114 | + case '\\': |
| 115 | + escaped.append("\\\\"); |
| 116 | + break; |
| 117 | + case '\'': |
| 118 | + escaped.append("\\'"); |
| 119 | + break; |
| 120 | + case '"': |
| 121 | + escaped.append("\\\""); |
| 122 | + break; |
| 123 | + case ';': |
| 124 | + case '\n': |
| 125 | + case '\r': |
| 126 | + break; |
| 127 | + default: |
| 128 | + escaped.append(c); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return escaped.toString(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Validates that the given color is a non-null, non-empty, character hex color string. |
| 137 | + * |
| 138 | + * @param color the color string to validate |
| 139 | + * @param key the name used in error messages |
| 140 | + */ |
| 141 | + private static void validateHexColor(String color, String key) { |
| 142 | + requireNonNull(color, key); |
| 143 | + checkArgument(!color.trim().isEmpty(), "%s is empty.", key); |
| 144 | + checkArgument(color.length() <= MAX_COLOR_LENGTH, |
| 145 | + "%s length exceeds %s.", key, MAX_COLOR_LENGTH); |
| 146 | + checkArgument(Pattern.matches(HEX_COLOR_PATTERN, color), |
| 147 | + "%s not in hex format: %s.", key, color); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Builds a JavaScript snippet that sets the background color of a DOM element. |
| 152 | + * |
| 153 | + * @param color the background color in hex format |
| 154 | + * @param targetAttr the value of the target attribute to match |
| 155 | + * @return a JavaScript string that applies the background color to the element |
| 156 | + */ |
| 157 | + private static String buildStyleScript(String color, String targetAttr) { |
| 158 | + return "{\n" + |
| 159 | + " const element = document.querySelector('[" + SAFE_DOM_HOOK + "=\"" + targetAttr + "\"]');\n" + |
| 160 | + " if (element) {\n" + |
| 161 | + " element.style.backgroundColor = '" + color + "';\n" + |
| 162 | + " }\n}\n"; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Builds a JavaScript snippet that sets the new favicon. |
| 167 | + * |
| 168 | + * @param url the url string to set |
| 169 | + * @return a JavaScript string that applies the favicon change |
| 170 | + */ |
| 171 | + private static String buildFaviconScript(String url) { |
| 172 | + return "{\n" + |
| 173 | + " let link = document.querySelector('link[rel~=\"icon\"]');\n" + |
| 174 | + " if (link) {\n" + |
| 175 | + " document.head.removeChild(link);\n" + |
| 176 | + " }\n" + |
| 177 | + " link = document.createElement('link');\n" + |
| 178 | + " link.rel = 'icon';\n" + |
| 179 | + " link.type = 'image/x-icon';\n" + |
| 180 | + " link.href = '" + url + "';\n" + |
| 181 | + " document.head.appendChild(link);\n" + |
| 182 | + "}\n"; |
| 183 | + } |
| 184 | +} |
0 commit comments