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.

PluginsRiskConsentFilter.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.plugins;
  21. import java.io.IOException;
  22. import java.util.Set;
  23. import javax.servlet.FilterChain;
  24. import javax.servlet.FilterConfig;
  25. import javax.servlet.ServletException;
  26. import javax.servlet.ServletRequest;
  27. import javax.servlet.ServletResponse;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import org.sonar.api.config.Configuration;
  31. import org.sonar.api.web.ServletFilter;
  32. import org.sonar.core.extension.PluginRiskConsent;
  33. import org.sonar.server.user.ThreadLocalUserSession;
  34. import static org.sonar.api.web.ServletFilter.UrlPattern.Builder.staticResourcePatterns;
  35. import static org.sonar.core.config.CorePropertyDefinitions.PLUGINS_RISK_CONSENT;
  36. import static org.sonar.core.extension.PluginRiskConsent.NOT_ACCEPTED;
  37. import static org.sonar.core.extension.PluginRiskConsent.REQUIRED;
  38. import static org.sonar.server.authentication.AuthenticationRedirection.redirectTo;
  39. public class PluginsRiskConsentFilter extends ServletFilter {
  40. private static final String PLUGINS_RISK_CONSENT_PATH = "/admin/plugin_risk_consent"; //NOSONAR this path will be the same in every environment
  41. private static final Set<String> SKIPPED_URLS = Set.of(
  42. PLUGINS_RISK_CONSENT_PATH,
  43. "/account/reset_password",
  44. "/admin/change_admin_password",
  45. "/batch/*", "/api/*");
  46. private final ThreadLocalUserSession userSession;
  47. private final Configuration config;
  48. public PluginsRiskConsentFilter(Configuration config, ThreadLocalUserSession userSession) {
  49. this.userSession = userSession;
  50. this.config = config;
  51. }
  52. @Override
  53. public void init(FilterConfig filterConfig) throws ServletException {
  54. //nothing to do
  55. }
  56. @Override
  57. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
  58. HttpServletRequest request = (HttpServletRequest) servletRequest;
  59. HttpServletResponse response = (HttpServletResponse) servletResponse;
  60. PluginRiskConsent riskConsent = PluginRiskConsent.valueOf(config.get(PLUGINS_RISK_CONSENT).orElse(NOT_ACCEPTED.name()));
  61. if (userSession.hasSession() && userSession.isLoggedIn()
  62. && userSession.isSystemAdministrator() && riskConsent == REQUIRED) {
  63. redirectTo(response, request.getContextPath() + PLUGINS_RISK_CONSENT_PATH);
  64. }
  65. chain.doFilter(request, response);
  66. }
  67. @Override
  68. public UrlPattern doGetPattern() {
  69. return UrlPattern.builder()
  70. .includes("/*")
  71. .excludes(staticResourcePatterns())
  72. .excludes(SKIPPED_URLS)
  73. .build();
  74. }
  75. @Override
  76. public void destroy() {
  77. //nothing to do
  78. }
  79. }