You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OAuth2AuthenticationParametersImpl.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.gson.Gson;
  23. import com.google.gson.GsonBuilder;
  24. import com.google.gson.reflect.TypeToken;
  25. import java.io.UnsupportedEncodingException;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.Optional;
  29. import java.util.regex.Pattern;
  30. import javax.annotation.Nullable;
  31. import org.sonar.api.server.http.Cookie;
  32. import org.sonar.api.server.http.HttpRequest;
  33. import org.sonar.api.server.http.HttpResponse;
  34. import static java.net.URLDecoder.decode;
  35. import static java.nio.charset.StandardCharsets.UTF_8;
  36. import static java.util.Optional.empty;
  37. import static org.sonar.server.authentication.AuthenticationRedirection.encodeMessage;
  38. import static org.sonar.server.authentication.Cookies.findCookie;
  39. import static org.sonar.server.authentication.Cookies.newCookieBuilder;
  40. public class OAuth2AuthenticationParametersImpl implements OAuth2AuthenticationParameters {
  41. private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
  42. private static final int FIVE_MINUTES_IN_SECONDS = 5 * 60;
  43. private static final Pattern VALID_RETURN_TO = Pattern.compile("^/\\w.*");
  44. /**
  45. * The HTTP parameter that contains the path where the user should be redirect to.
  46. * Please note that the web context is included.
  47. */
  48. private static final String RETURN_TO_PARAMETER = "return_to";
  49. private static final TypeToken<Map<String, String>> JSON_MAP_TYPE = new TypeToken<>() {
  50. };
  51. @Override
  52. public void init(HttpRequest request, HttpResponse response) {
  53. String returnTo = request.getParameter(RETURN_TO_PARAMETER);
  54. Map<String, String> parameters = new HashMap<>();
  55. Optional<String> sanitizeRedirectUrl = sanitizeRedirectUrl(returnTo);
  56. sanitizeRedirectUrl.ifPresent(s -> parameters.put(RETURN_TO_PARAMETER, s));
  57. if (parameters.isEmpty()) {
  58. return;
  59. }
  60. response.addCookie(newCookieBuilder(request)
  61. .setName(AUTHENTICATION_COOKIE_NAME)
  62. .setValue(toJson(parameters))
  63. .setHttpOnly(true)
  64. .setExpiry(FIVE_MINUTES_IN_SECONDS)
  65. .build());
  66. }
  67. @Override
  68. public Optional<String> getReturnTo(HttpRequest request) {
  69. return getParameter(request, RETURN_TO_PARAMETER)
  70. .flatMap(OAuth2AuthenticationParametersImpl::sanitizeRedirectUrl);
  71. }
  72. private static Optional<String> getParameter(HttpRequest request, String parameterKey) {
  73. Optional<Cookie> cookie = findCookie(AUTHENTICATION_COOKIE_NAME, request);
  74. if (cookie.isEmpty()) {
  75. return empty();
  76. }
  77. Map<String, String> parameters = fromJson(cookie.get().getValue());
  78. if (parameters.isEmpty()) {
  79. return empty();
  80. }
  81. return Optional.ofNullable(parameters.get(parameterKey));
  82. }
  83. @Override
  84. public void delete(HttpRequest request, HttpResponse response) {
  85. response.addCookie(newCookieBuilder(request)
  86. .setName(AUTHENTICATION_COOKIE_NAME)
  87. .setValue(null)
  88. .setHttpOnly(true)
  89. .setExpiry(0)
  90. .build());
  91. }
  92. private static String toJson(Map<String, String> map) {
  93. Gson gson = new GsonBuilder().create();
  94. return encodeMessage(gson.toJson(map));
  95. }
  96. private static Map<String, String> fromJson(String json) {
  97. Gson gson = new GsonBuilder().create();
  98. try {
  99. return gson.fromJson(decode(json, UTF_8.name()), JSON_MAP_TYPE);
  100. } catch (UnsupportedEncodingException e) {
  101. throw new IllegalStateException(e);
  102. }
  103. }
  104. /**
  105. * This sanitization has been inspired by 'IsUrlLocalToHost()' method in
  106. * https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/preventing-open-redirection-attacks
  107. */
  108. private static Optional<String> sanitizeRedirectUrl(@Nullable String url) {
  109. if (Strings.isNullOrEmpty(url)) {
  110. return empty();
  111. }
  112. String sanitizedUrl = url.trim();
  113. boolean isValidUrl = VALID_RETURN_TO.matcher(sanitizedUrl).matches();
  114. if (!isValidUrl) {
  115. return empty();
  116. }
  117. return Optional.of(sanitizedUrl);
  118. }
  119. }