Merge "Limit the maximum length of topic"

This commit is contained in:
Changcheng Xiao
2017-09-15 12:01:33 +00:00
committed by Gerrit Code Review
5 changed files with 45 additions and 0 deletions

View File

@@ -149,6 +149,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Constants;
@@ -3288,6 +3289,16 @@ public class ChangeIT extends AbstractDaemonTest {
gApi.changes().id(createChange().getChangeId()).pureRevert();
}
@Test
public void putTopicExceedLimitFails() throws Exception {
String changeId = createChange().getChangeId();
String topic = Stream.generate(() -> "t").limit(2049).collect(Collectors.joining());
exception.expect(BadRequestException.class);
exception.expectMessage("topic length exceeds the limit");
gApi.changes().id(changeId).topic(topic);
}
private String getCommitMessage(String changeId) throws RestApiException, IOException {
return gApi.changes().id(changeId).current().file("/COMMIT_MSG").content().asString();
}

View File

@@ -88,6 +88,8 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.ObjectId;
@@ -344,6 +346,20 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
r.assertPushOptions(pushOptions);
}
@Test
public void pushForMasterWithTopicInRefExceedLimitFails() throws Exception {
String topic = Stream.generate(() -> "t").limit(2049).collect(Collectors.joining());
PushOneCommit.Result r = pushTo("refs/for/master/" + topic);
r.assertErrorStatus("topic length exceeds the limit (2048)");
}
@Test
public void pushForMasterWithTopicAsOptionExceedLimitFails() throws Exception {
String topic = Stream.generate(() -> "t").limit(2049).collect(Collectors.joining());
PushOneCommit.Result r = pushTo("refs/for/master%topic=" + topic);
r.assertErrorStatus("topic length exceeds the limit (2048)");
}
@Test
public void pushForMasterWithNotify() throws Exception {
// create a user that watches the project

View File

@@ -32,6 +32,8 @@ import org.eclipse.jgit.lib.Repository;
@Singleton
public class ChangeUtil {
public static final int TOPIC_MAX_LENGTH = 2048;
private static final Random UUID_RANDOM = new SecureRandom();
private static final BaseEncoding UUID_ENCODING = BaseEncoding.base16().lowerCase();

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.change;
import com.google.common.base.Strings;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
@@ -24,6 +25,7 @@ import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeMessagesUtil;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.change.PutTopic.Input;
import com.google.gerrit.server.extensions.events.TopicEdited;
import com.google.gerrit.server.notedb.ChangeUpdate;
@@ -69,6 +71,14 @@ public class PutTopic extends RetryingRestModifyView<ChangeResource, Input, Resp
BatchUpdate.Factory updateFactory, ChangeResource req, Input input)
throws UpdateException, RestApiException, PermissionBackendException {
req.permissions().check(ChangePermission.EDIT_TOPIC_NAME);
if (input != null
&& input.topic != null
&& input.topic.length() > ChangeUtil.TOPIC_MAX_LENGTH) {
throw new BadRequestException(
String.format("topic length exceeds the limit (%s)", ChangeUtil.TOPIC_MAX_LENGTH));
}
Op op = new Op(input != null ? input : new Input());
try (BatchUpdate u =
updateFactory.create(

View File

@@ -1435,6 +1435,12 @@ class ReceiveCommits {
}
ref = null; // never happen
}
if (magicBranch.topic != null && magicBranch.topic.length() > ChangeUtil.TOPIC_MAX_LENGTH) {
reject(
cmd, String.format("topic length exceeds the limit (%s)", ChangeUtil.TOPIC_MAX_LENGTH));
}
if (clp.wasHelpRequestedByOption()) {
StringWriter w = new StringWriter();
w.write("\nHelp for refs/for/branch:\n\n");