Prevent project.config corruption when plugin name is invalid
If a plugin name is not sane (i. e. contains characters, that are not compatible to git config file), then all capabilities from that plugin are dropped and can not be granted. Change-Id: I8bcad28c5ed92a44e2761169b6b70c38d949bf70
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
|
||||
package com.google.gerrit.server.config;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.gerrit.common.data.GlobalCapability;
|
||||
import com.google.gerrit.extensions.config.CapabilityDefinition;
|
||||
@@ -25,10 +26,14 @@ import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/** List capabilities visible to the calling user. */
|
||||
public class ListCapabilities implements RestReadView<ConfigResource> {
|
||||
private static final Logger log = LoggerFactory.getLogger(ListCapabilities.class);
|
||||
private final DynamicMap<CapabilityDefinition> pluginCapabilities;
|
||||
|
||||
@Inject
|
||||
@@ -60,6 +65,13 @@ public class ListCapabilities implements RestReadView<ConfigResource> {
|
||||
|
||||
private void collectPluginCapabilities(Map<String, CapabilityInfo> output) {
|
||||
for (String pluginName : pluginCapabilities.plugins()) {
|
||||
if (!isPluginNameSane(pluginName)) {
|
||||
log.warn(String.format(
|
||||
"Plugin name %s must match [A-Za-z0-9-]+ to use capabilities;"
|
||||
+ " rename the plugin",
|
||||
pluginName));
|
||||
continue;
|
||||
}
|
||||
for (Map.Entry<String, Provider<CapabilityDefinition>> entry :
|
||||
pluginCapabilities.byPlugin(pluginName).entrySet()) {
|
||||
String id = String.format("%s-%s", pluginName, entry.getKey());
|
||||
@@ -70,6 +82,12 @@ public class ListCapabilities implements RestReadView<ConfigResource> {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isPluginNameSane(String pluginName) {
|
||||
return CharMatcher.JAVA_LETTER_OR_DIGIT
|
||||
.or(CharMatcher.is('-'))
|
||||
.matchesAllOf(pluginName);
|
||||
}
|
||||
|
||||
public static class CapabilityInfo {
|
||||
final String kind = "gerritcodereview#capability";
|
||||
public String id;
|
||||
|
||||
Reference in New Issue
Block a user