001package ca.uhn.fhir.util; 002 003import ca.uhn.fhir.context.FhirContext; 004import ca.uhn.fhir.context.RuntimeResourceDefinition; 005import ca.uhn.fhir.model.primitive.IdDt; 006import ca.uhn.fhir.parser.DataFormatException; 007import ca.uhn.fhir.rest.api.Constants; 008import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 009import com.google.common.escape.Escaper; 010import com.google.common.net.PercentEscaper; 011import org.apache.http.NameValuePair; 012import org.apache.http.client.utils.URLEncodedUtils; 013import org.hl7.fhir.instance.model.api.IPrimitiveType; 014 015import java.io.UnsupportedEncodingException; 016import java.net.MalformedURLException; 017import java.net.URL; 018import java.net.URLDecoder; 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023import java.util.Map.Entry; 024import java.util.StringTokenizer; 025 026import static org.apache.commons.lang3.StringUtils.defaultIfBlank; 027import static org.apache.commons.lang3.StringUtils.defaultString; 028import static org.apache.commons.lang3.StringUtils.isBlank; 029 030/* 031 * #%L 032 * HAPI FHIR - Core Library 033 * %% 034 * Copyright (C) 2014 - 2020 University Health Network 035 * %% 036 * Licensed under the Apache License, Version 2.0 (the "License"); 037 * you may not use this file except in compliance with the License. 038 * You may obtain a copy of the License at 039 * 040 * http://www.apache.org/licenses/LICENSE-2.0 041 * 042 * Unless required by applicable law or agreed to in writing, software 043 * distributed under the License is distributed on an "AS IS" BASIS, 044 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 045 * See the License for the specific language governing permissions and 046 * limitations under the License. 047 * #L% 048 */ 049 050public class UrlUtil { 051 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(UrlUtil.class); 052 053 private static final String URL_FORM_PARAMETER_OTHER_SAFE_CHARS = "-_.*"; 054 private static final Escaper PARAMETER_ESCAPER = new PercentEscaper(URL_FORM_PARAMETER_OTHER_SAFE_CHARS, false); 055 056 public static class UrlParts { 057 private String myParams; 058 private String myResourceId; 059 private String myResourceType; 060 private String myVersionId; 061 062 public String getParams() { 063 return myParams; 064 } 065 066 public void setParams(String theParams) { 067 myParams = theParams; 068 } 069 070 public String getResourceId() { 071 return myResourceId; 072 } 073 074 public void setResourceId(String theResourceId) { 075 myResourceId = theResourceId; 076 } 077 078 public String getResourceType() { 079 return myResourceType; 080 } 081 082 public void setResourceType(String theResourceType) { 083 myResourceType = theResourceType; 084 } 085 086 public String getVersionId() { 087 return myVersionId; 088 } 089 090 public void setVersionId(String theVersionId) { 091 myVersionId = theVersionId; 092 } 093 } 094 095 /** 096 * Resolve a relative URL - THIS METHOD WILL NOT FAIL but will log a warning and return theEndpoint if the input is invalid. 097 */ 098 public static String constructAbsoluteUrl(String theBase, String theEndpoint) { 099 if (theEndpoint == null) { 100 return null; 101 } 102 if (isAbsolute(theEndpoint)) { 103 return theEndpoint; 104 } 105 if (theBase == null) { 106 return theEndpoint; 107 } 108 109 try { 110 return new URL(new URL(theBase), theEndpoint).toString(); 111 } catch (MalformedURLException e) { 112 ourLog.warn("Failed to resolve relative URL[" + theEndpoint + "] against absolute base[" + theBase + "]", e); 113 return theEndpoint; 114 } 115 } 116 117 public static String constructRelativeUrl(String theParentExtensionUrl, String theExtensionUrl) { 118 if (theParentExtensionUrl == null) { 119 return theExtensionUrl; 120 } 121 if (theExtensionUrl == null) { 122 return null; 123 } 124 125 int parentLastSlashIdx = theParentExtensionUrl.lastIndexOf('/'); 126 int childLastSlashIdx = theExtensionUrl.lastIndexOf('/'); 127 128 if (parentLastSlashIdx == -1 || childLastSlashIdx == -1) { 129 return theExtensionUrl; 130 } 131 132 if (parentLastSlashIdx != childLastSlashIdx) { 133 return theExtensionUrl; 134 } 135 136 if (!theParentExtensionUrl.substring(0, parentLastSlashIdx).equals(theExtensionUrl.substring(0, parentLastSlashIdx))) { 137 return theExtensionUrl; 138 } 139 140 if (theExtensionUrl.length() > parentLastSlashIdx) { 141 return theExtensionUrl.substring(parentLastSlashIdx + 1); 142 } 143 144 return theExtensionUrl; 145 } 146 147 /** 148 * URL encode a value according to RFC 3986 149 * <p> 150 * This method is intended to be applied to an individual parameter 151 * name or value. For example, if you are creating the URL 152 * <code>http://example.com/fhir/Patient?key=føø</code> 153 * it would be appropriate to pass the string "føø" to this method, 154 * but not appropriate to pass the entire URL since characters 155 * such as "/" and "?" would also be escaped. 156 * </P> 157 */ 158 public static String escapeUrlParam(String theUnescaped) { 159 if (theUnescaped == null) { 160 return null; 161 } 162 return PARAMETER_ESCAPER.escape(theUnescaped); 163 } 164 165 public static boolean isAbsolute(String theValue) { 166 String value = theValue.toLowerCase(); 167 return value.startsWith("http://") || value.startsWith("https://"); 168 } 169 170 public static boolean isNeedsSanitization(CharSequence theString) { 171 if (theString != null) { 172 for (int i = 0; i < theString.length(); i++) { 173 char nextChar = theString.charAt(i); 174 switch (nextChar) { 175 case '\'': 176 case '"': 177 case '<': 178 case '>': 179 case '\n': 180 case '\r': 181 return true; 182 } 183 if (nextChar < ' ') { 184 return true; 185 } 186 } 187 } 188 return false; 189 } 190 191 public static boolean isValid(String theUrl) { 192 if (theUrl == null || theUrl.length() < 8) { 193 return false; 194 } 195 196 String url = theUrl.toLowerCase(); 197 if (url.charAt(0) != 'h') { 198 return false; 199 } 200 if (url.charAt(1) != 't') { 201 return false; 202 } 203 if (url.charAt(2) != 't') { 204 return false; 205 } 206 if (url.charAt(3) != 'p') { 207 return false; 208 } 209 int slashOffset; 210 if (url.charAt(4) == ':') { 211 slashOffset = 5; 212 } else if (url.charAt(4) == 's') { 213 if (url.charAt(5) != ':') { 214 return false; 215 } 216 slashOffset = 6; 217 } else { 218 return false; 219 } 220 221 if (url.charAt(slashOffset) != '/') { 222 return false; 223 } 224 if (url.charAt(slashOffset + 1) != '/') { 225 return false; 226 } 227 228 return true; 229 } 230 231 public static RuntimeResourceDefinition parseUrlResourceType(FhirContext theCtx, String theUrl) throws DataFormatException { 232 int paramIndex = theUrl.indexOf('?'); 233 String resourceName = theUrl.substring(0, paramIndex); 234 if (resourceName.contains("/")) { 235 resourceName = resourceName.substring(resourceName.lastIndexOf('/') + 1); 236 } 237 return theCtx.getResourceDefinition(resourceName); 238 } 239 240 public static Map<String, String[]> parseQueryString(String theQueryString) { 241 HashMap<String, List<String>> map = new HashMap<>(); 242 parseQueryString(theQueryString, map); 243 return toQueryStringMap(map); 244 } 245 246 private static void parseQueryString(String theQueryString, HashMap<String, List<String>> map) { 247 String query = defaultString(theQueryString); 248 if (query.startsWith("?")) { 249 query = query.substring(1); 250 } 251 252 253 StringTokenizer tok = new StringTokenizer(query, "&"); 254 while (tok.hasMoreTokens()) { 255 String nextToken = tok.nextToken(); 256 if (isBlank(nextToken)) { 257 continue; 258 } 259 260 int equalsIndex = nextToken.indexOf('='); 261 String nextValue; 262 String nextKey; 263 if (equalsIndex == -1) { 264 nextKey = nextToken; 265 nextValue = ""; 266 } else { 267 nextKey = nextToken.substring(0, equalsIndex); 268 nextValue = nextToken.substring(equalsIndex + 1); 269 } 270 271 nextKey = unescape(nextKey); 272 nextValue = unescape(nextValue); 273 274 List<String> list = map.computeIfAbsent(nextKey, k -> new ArrayList<>()); 275 list.add(nextValue); 276 } 277 } 278 279 public static Map<String, String[]> parseQueryStrings(String... theQueryString) { 280 HashMap<String, List<String>> map = new HashMap<>(); 281 for (String next : theQueryString) { 282 parseQueryString(next, map); 283 } 284 return toQueryStringMap(map); 285 } 286 287 /** 288 * Parse a URL in one of the following forms: 289 * <ul> 290 * <li>[Resource Type]?[Search Params] 291 * <li>[Resource Type]/[Resource ID] 292 * <li>[Resource Type]/[Resource ID]/_history/[Version ID] 293 * </ul> 294 */ 295 public static UrlParts parseUrl(String theUrl) { 296 String url = theUrl; 297 UrlParts retVal = new UrlParts(); 298 if (url.startsWith("http")) { 299 if (url.startsWith("/")) { 300 url = url.substring(1); 301 } 302 303 int qmIdx = url.indexOf('?'); 304 if (qmIdx != -1) { 305 retVal.setParams(defaultIfBlank(url.substring(qmIdx + 1), null)); 306 url = url.substring(0, qmIdx); 307 } 308 309 IdDt id = new IdDt(url); 310 retVal.setResourceType(id.getResourceType()); 311 retVal.setResourceId(id.getIdPart()); 312 retVal.setVersionId(id.getVersionIdPart()); 313 return retVal; 314 } 315 316 int parsingStart = 0; 317 if (url.length() > 2) { 318 if (url.charAt(0) == '/') { 319 if (Character.isLetter(url.charAt(1))) { 320 parsingStart = 1; 321 } 322 } 323 } 324 325 if (url.length() > 1 && url.charAt(0) == '/' && Character.isLetter(url.charAt(1)) && url.contains("?")) { 326 url = url.substring(1); 327 } 328 int nextStart = 0; 329 boolean nextIsHistory = false; 330 331 for (int idx = parsingStart; idx < url.length(); idx++) { 332 char nextChar = url.charAt(idx); 333 boolean atEnd = (idx + 1) == url.length(); 334 if (nextChar == '?' || nextChar == '/' || atEnd) { 335 int endIdx = (atEnd && nextChar != '?') ? idx + 1 : idx; 336 String nextSubstring = url.substring(nextStart, endIdx); 337 if (retVal.getResourceType() == null) { 338 retVal.setResourceType(nextSubstring); 339 } else if (retVal.getResourceId() == null) { 340 retVal.setResourceId(nextSubstring); 341 } else if (nextIsHistory) { 342 retVal.setVersionId(nextSubstring); 343 } else { 344 if (nextSubstring.equals(Constants.URL_TOKEN_HISTORY)) { 345 nextIsHistory = true; 346 } else { 347 throw new InvalidRequestException("Invalid FHIR resource URL: " + url); 348 } 349 } 350 if (nextChar == '?') { 351 if (url.length() > idx + 1) { 352 retVal.setParams(url.substring(idx + 1)); 353 } 354 break; 355 } 356 nextStart = idx + 1; 357 } 358 } 359 360 return retVal; 361 362 } 363 364 /** 365 * This method specifically HTML-encodes the " and 366 * < characters in order to prevent injection attacks 367 */ 368 public static String sanitizeUrlPart(IPrimitiveType<?> theString) { 369 String retVal = null; 370 if (theString != null) { 371 retVal = sanitizeUrlPart(theString.getValueAsString()); 372 } 373 return retVal; 374 } 375 376 /** 377 * This method specifically HTML-encodes the " and 378 * < characters in order to prevent injection attacks. 379 * <p> 380 * The following characters are escaped: 381 * <ul> 382 * <li>'</li> 383 * <li>"</li> 384 * <li><</li> 385 * <li>></li> 386 * <li>\n (newline)</li> 387 * </ul> 388 */ 389 public static String sanitizeUrlPart(CharSequence theString) { 390 if (theString == null) { 391 return null; 392 } 393 394 boolean needsSanitization = isNeedsSanitization(theString); 395 396 if (needsSanitization) { 397 // Ok, we're sanitizing 398 StringBuilder buffer = new StringBuilder(theString.length() + 10); 399 for (int j = 0; j < theString.length(); j++) { 400 401 char nextChar = theString.charAt(j); 402 switch (nextChar) { 403 /* 404 * NB: If you add a constant here, you also need to add it 405 * to isNeedsSanitization()!! 406 */ 407 case '\'': 408 buffer.append("'"); 409 break; 410 case '"': 411 buffer.append("""); 412 break; 413 case '<': 414 buffer.append("<"); 415 break; 416 case '>': 417 buffer.append(">"); 418 break; 419 case '\n': 420 buffer.append(" "); 421 break; 422 case '\r': 423 buffer.append(" "); 424 break; 425 default: 426 if (nextChar >= ' ') { 427 buffer.append(nextChar); 428 } 429 break; 430 } 431 432 } // for build escaped string 433 434 return buffer.toString(); 435 } 436 437 return theString.toString(); 438 } 439 440 /** 441 * Applies the same logic as {@link #sanitizeUrlPart(CharSequence)} but against an array, returning an array with the 442 * same strings as the input but with sanitization applied 443 */ 444 public static String[] sanitizeUrlPart(String[] theParameterValues) { 445 String[] retVal = null; 446 if (theParameterValues != null) { 447 retVal = new String[theParameterValues.length]; 448 for (int i = 0; i < theParameterValues.length; i++) { 449 retVal[i] = sanitizeUrlPart(theParameterValues[i]); 450 } 451 } 452 return retVal; 453 } 454 455 private static Map<String, String[]> toQueryStringMap(HashMap<String, List<String>> map) { 456 HashMap<String, String[]> retVal = new HashMap<>(); 457 for (Entry<String, List<String>> nextEntry : map.entrySet()) { 458 retVal.put(nextEntry.getKey(), nextEntry.getValue().toArray(new String[0])); 459 } 460 return retVal; 461 } 462 463 public static String unescape(String theString) { 464 if (theString == null) { 465 return null; 466 } 467 for (int i = 0; i < theString.length(); i++) { 468 char nextChar = theString.charAt(i); 469 if (nextChar == '%' || nextChar == '+') { 470 try { 471 // Yes it would be nice to not use a string "UTF-8" but the equivalent 472 // method that takes Charset is JDK10+ only... sigh.... 473 return URLDecoder.decode(theString, "UTF-8"); 474 } catch (UnsupportedEncodingException e) { 475 throw new Error("UTF-8 not supported, this shouldn't happen", e); 476 } 477 } 478 } 479 return theString; 480 } 481 482 public static List<NameValuePair> translateMatchUrl(String theMatchUrl) { 483 List<NameValuePair> parameters; 484 String matchUrl = theMatchUrl; 485 int questionMarkIndex = matchUrl.indexOf('?'); 486 if (questionMarkIndex != -1) { 487 matchUrl = matchUrl.substring(questionMarkIndex + 1); 488 } 489 matchUrl = matchUrl.replace("|", "%7C"); 490 matchUrl = matchUrl.replace("=>=", "=%3E%3D"); 491 matchUrl = matchUrl.replace("=<=", "=%3C%3D"); 492 matchUrl = matchUrl.replace("=>", "=%3E"); 493 matchUrl = matchUrl.replace("=<", "=%3C"); 494 if (matchUrl.contains(" ")) { 495 throw new InvalidRequestException("Failed to parse match URL[" + theMatchUrl + "] - URL is invalid (must not contain spaces)"); 496 } 497 498 parameters = URLEncodedUtils.parse((matchUrl), Constants.CHARSET_UTF8, '&'); 499 return parameters; 500 } 501}