Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

OAuth2AuthenticationParametersImpl.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.authentication;
  21. import com.google.common.base.Strings;
  22. import com.google.common.reflect.TypeToken;
  23. import com.google.gson.Gson;
  24. import com.google.gson.GsonBuilder;
  25. import java.io.UnsupportedEncodingException;
  26. import java.lang.reflect.Type;
  27. import java.nio.file.Path;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import java.util.Optional;
  31. import java.util.regex.Pattern;
  32. import javax.annotation.Nullable;
  33. import javax.servlet.http.HttpServletRequest;
  34. import javax.servlet.http.HttpServletResponse;
  35. import static java.net.URLDecoder.decode;
  36. import static java.nio.charset.StandardCharsets.UTF_8;
  37. import static java.util.Optional.empty;
  38. import static org.sonar.server.authentication.AuthenticationRedirection.encodeMessage;
  39. import static org.sonar.server.authentication.Cookies.findCookie;
  40. import static org.sonar.server.authentication.Cookies.newCookieBuilder;
  41. public class OAuth2AuthenticationParametersImpl implements OAuth2AuthenticationParameters {
  42. private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
  43. private static final int FIVE_MINUTES_IN_SECONDS = 5 * 60;
  44. private static final Pattern VALID_RETURN_TO = Pattern.compile("^/\\w.*");
  45. /**
  46. * The HTTP parameter that contains the path where the user should be redirect to.
  47. * Please note that the web context is included.
  48. */
  49. private static final String RETURN_TO_PARAMETER = "return_to";
  50. private static final Type JSON_MAP_TYPE = new TypeToken<HashMap<String, String>>() {
  51. }.getType();
  52. @Override
  53. public void init(HttpServletRequest request, HttpServletResponse response) {
  54. String returnTo = request.getParameter(RETURN_TO_PARAMETER);
  55. Map<String, String> parameters = new HashMap<>();
  56. Optional<String> sanitizeRedirectUrl = sanitizeRedirectUrl(returnTo);
  57. sanitizeRedirectUrl.ifPresent(s -> parameters.put(RETURN_TO_PARAMETER, s));
  58. if (parameters.isEmpty()) {
  59. return;
  60. }
  61. response.addCookie(newCookieBuilder(request)
  62. .setName(AUTHENTICATION_COOKIE_NAME)
  63. .setValue(toJson(parameters))
  64. .setHttpOnly(true)
  65. .setExpiry(FIVE_MINUTES_IN_SECONDS)
  66. .build());
  67. }
  68. @Override
  69. public Optional<String> getReturnTo(HttpServletRequest request) {
  70. return getParameter(request, RETURN_TO_PARAMETER)
  71. .flatMap(OAuth2AuthenticationParametersImpl::sanitizeRedirectUrl);
  72. }
  73. private static Optional<String> getParameter(HttpServletRequest request, String parameterKey) {
  74. Optional<javax.servlet.http.Cookie> cookie = findCookie(AUTHENTICATION_COOKIE_NAME, request);
  75. if (!cookie.isPresent()) {
  76. return empty();
  77. }
  78. Map<String, String> parameters = fromJson(cookie.get().getValue());
  79. if (parameters.isEmpty()) {
  80. return empty();
  81. }
  82. return Optional.ofNullable(parameters.get(parameterKey));
  83. }
  84. @Override
  85. public void delete(HttpServletRequest request, HttpServletResponse response) {
  86. response.addCookie(newCookieBuilder(request)
  87. .setName(AUTHENTICATION_COOKIE_NAME)
  88. .setValue(null)
  89. .setHttpOnly(true)
  90. .setExpiry(0)
  91. .build());
  92. }
  93. private static String toJson(Map<String, String> map) {
  94. Gson gson = new GsonBuilder().create();
  95. return encodeMessage(gson.toJson(map));
  96. }
  97. private static Map<String, String> fromJson(String json) {
  98. Gson gson = new GsonBuilder().create();
  99. try {
  100. return gson.fromJson(decode(json, UTF_8.name()), JSON_MAP_TYPE);
  101. } catch (UnsupportedEncodingException e) {
  102. throw new IllegalStateException(e);
  103. }
  104. }
  105. /**
  106. * This sanitization has been inspired by 'IsUrlLocalToHost()' method in
  107. * https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/preventing-open-redirection-attacks
  108. */
  109. private static Optional<String> sanitizeRedirectUrl(@Nullable String url) {
  110. if (Strings.isNullOrEmpty(url)) {
  111. return empty();
  112. }
  113. String trimmedUrl = url.trim();
  114. boolean isValidUrl = VALID_RETURN_TO.matcher(trimmedUrl).matches();
  115. if (!isValidUrl) {
  116. return empty();
  117. }
  118. Path sanitizedPath = escapePathTraversalChars(trimmedUrl);
  119. return Optional.of(sanitizedPath.toString());
  120. }
  121. private static Path escapePathTraversalChars(String sanitizedUrl) {
  122. return Path.of(sanitizedUrl).normalize();
  123. }
  124. }