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.

SettingValidations.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.setting.ws;
  21. import com.github.erosb.jsonsKema.JsonParseException;
  22. import com.github.erosb.jsonsKema.JsonParser;
  23. import com.github.erosb.jsonsKema.JsonValue;
  24. import com.github.erosb.jsonsKema.SchemaLoader;
  25. import com.github.erosb.jsonsKema.ValidationFailure;
  26. import com.github.erosb.jsonsKema.Validator;
  27. import com.google.gson.Gson;
  28. import com.google.gson.JsonElement;
  29. import java.io.IOException;
  30. import java.nio.charset.StandardCharsets;
  31. import java.util.List;
  32. import java.util.Locale;
  33. import java.util.Optional;
  34. import java.util.Set;
  35. import javax.annotation.CheckForNull;
  36. import javax.annotation.Nullable;
  37. import org.apache.commons.io.IOUtils;
  38. import org.sonar.api.PropertyType;
  39. import org.sonar.api.config.PropertyDefinition;
  40. import org.sonar.api.config.PropertyDefinitions;
  41. import org.sonar.api.resources.Qualifiers;
  42. import org.sonar.core.i18n.I18n;
  43. import org.sonar.db.DbClient;
  44. import org.sonar.db.DbSession;
  45. import org.sonar.db.entity.EntityDto;
  46. import org.sonar.db.user.UserDto;
  47. import org.sonar.server.exceptions.BadRequestException;
  48. import static java.lang.String.format;
  49. import static java.util.Objects.requireNonNull;
  50. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  51. public class SettingValidations {
  52. private static final Set<String> SECURITY_JSON_PROPERTIES = Set.of(
  53. "sonar.security.config.javasecurity",
  54. "sonar.security.config.phpsecurity",
  55. "sonar.security.config.pythonsecurity",
  56. "sonar.security.config.roslyn.sonaranalyzer.security.cs"
  57. );
  58. private static final Set<String> SUPPORTED_QUALIFIERS = Set.of(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.APP, Qualifiers.SUBVIEW);
  59. private final PropertyDefinitions definitions;
  60. private final DbClient dbClient;
  61. private final I18n i18n;
  62. private final ValueTypeValidation valueTypeValidation;
  63. public SettingValidations(PropertyDefinitions definitions, DbClient dbClient, I18n i18n) {
  64. this.definitions = definitions;
  65. this.dbClient = dbClient;
  66. this.i18n = i18n;
  67. this.valueTypeValidation = new ValueTypeValidation();
  68. }
  69. public void validateScope(SettingData data) {
  70. PropertyDefinition definition = definitions.get(data.key);
  71. checkRequest(data.entity != null || definition == null || definition.global() || isGlobal(definition),
  72. "Setting '%s' cannot be global", data.key);
  73. }
  74. public void validateQualifier(SettingData data) {
  75. String qualifier = data.entity == null ? "" : data.entity.getQualifier();
  76. PropertyDefinition definition = definitions.get(data.key);
  77. checkRequest(checkComponentQualifier(data, definition),
  78. "Setting '%s' cannot be set on a %s", data.key, i18n.message(Locale.ENGLISH, "qualifier." + qualifier, null));
  79. }
  80. public void validateValueType(SettingData data) {
  81. valueTypeValidation.validateValueType(data);
  82. }
  83. private static boolean checkComponentQualifier(SettingData data, @Nullable PropertyDefinition definition) {
  84. EntityDto entity = data.entity;
  85. if (entity == null) {
  86. return true;
  87. }
  88. if (definition == null) {
  89. return SUPPORTED_QUALIFIERS.contains(entity.getQualifier());
  90. }
  91. return definition.qualifiers().contains(entity.getQualifier());
  92. }
  93. private static boolean isGlobal(PropertyDefinition definition) {
  94. return !definition.global() && definition.qualifiers().isEmpty();
  95. }
  96. public static class SettingData {
  97. private final String key;
  98. private final List<String> values;
  99. @CheckForNull
  100. private final EntityDto entity;
  101. SettingData(String key, List<String> values, @Nullable EntityDto entity) {
  102. this.key = requireNonNull(key);
  103. this.values = requireNonNull(values);
  104. this.entity = entity;
  105. }
  106. }
  107. private class ValueTypeValidation {
  108. private final Validator schemaValidator;
  109. public ValueTypeValidation() {
  110. this.schemaValidator = Optional.ofNullable(this.getClass().getClassLoader().getResourceAsStream("json-schemas/security.json"))
  111. .map(schemaStream -> {
  112. try {
  113. return IOUtils.toString(schemaStream, StandardCharsets.UTF_8);
  114. } catch (IOException e) {
  115. return null;
  116. }
  117. }).map(schemaString -> new JsonParser(schemaString).parse())
  118. .map(schemaJson -> new SchemaLoader(schemaJson).load())
  119. .map(Validator::forSchema)
  120. .orElseThrow(() -> new IllegalStateException("Unable to create security schema validator"));
  121. }
  122. public void validateValueType(SettingData data) {
  123. PropertyDefinition definition = definitions.get(data.key);
  124. if (definition == null) {
  125. return;
  126. }
  127. if (definition.type() == PropertyType.USER_LOGIN) {
  128. validateLogin(data);
  129. } else if (definition.type() == PropertyType.JSON) {
  130. validateJson(data, definition);
  131. } else {
  132. validateOtherTypes(data, definition);
  133. }
  134. }
  135. private void validateOtherTypes(SettingData data, PropertyDefinition definition) {
  136. data.values.stream()
  137. .map(definition::validate)
  138. .filter(result -> !result.isValid())
  139. .findAny()
  140. .ifPresent(result -> {
  141. throw BadRequestException.create(i18n.message(Locale.ENGLISH, "property.error." + result.getErrorKey(),
  142. format("Error when validating setting with key '%s' and value [%s]", data.key, String.join(", ", data.values))));
  143. });
  144. }
  145. private void validateLogin(SettingData data) {
  146. try (DbSession dbSession = dbClient.openSession(false)) {
  147. List<UserDto> users = dbClient.userDao().selectByLogins(dbSession, data.values).stream().filter(UserDto::isActive).toList();
  148. checkRequest(data.values.size() == users.size(), "Error when validating login setting with key '%s' and values [%s]. A value is not a valid login.",
  149. data.key, String.join(", ", data.values));
  150. }
  151. }
  152. private void validateJson(SettingData data, PropertyDefinition definition) {
  153. Optional<String> jsonContent = data.values.stream().findFirst();
  154. if (jsonContent.isPresent()) {
  155. try {
  156. new Gson().getAdapter(JsonElement.class).fromJson(jsonContent.get());
  157. validateJsonSchema(jsonContent.get(), definition);
  158. } catch (JsonParseException | IOException e) {
  159. throw new IllegalArgumentException("Provided JSON is invalid");
  160. }
  161. }
  162. }
  163. private void validateJsonSchema(String json, PropertyDefinition definition) {
  164. if (SECURITY_JSON_PROPERTIES.contains(definition.key())) {
  165. JsonValue jsonToValidate = new JsonParser(json).parse();
  166. Optional.ofNullable(schemaValidator.validate(jsonToValidate))
  167. .ifPresent(validationFailure -> {
  168. ValidationFailure rootCause = getRootCause(validationFailure);
  169. throw new IllegalArgumentException(String.format("Provided JSON is invalid : [%s at %s]", rootCause.getMessage(), rootCause.getInstance().getLocation()));
  170. });
  171. }
  172. }
  173. private static ValidationFailure getRootCause(ValidationFailure base) {
  174. return base.getCauses().stream()
  175. .map(ValueTypeValidation::getRootCause)
  176. .findFirst()
  177. .orElse(base);
  178. }
  179. }
  180. }