Files
gerrit/java/com/google/gerrit/server/restapi/project/DeleteBranches.java
Edwin Kempin 40e1ab1bf0 DeleteBranch/DeleteBranches: Disallow deletion of HEAD to prevent NPE
Attempting to delete HEAD fails with a NullPointerException [1]. This is
because 'HEAD' gets prefixed with 'refs/heads/' and then the ref is not
found.

We may consider to support deleting HEAD, but that's outside the scope
of this change. E.g. deleting HEAD may rather be separate REST endpoint
that lives next to SetHead since setting HEAD requires a different
permission than deleting a ref.

[1]
java.lang.NullPointerException
        at com.google.gerrit.server.restapi.project.DeleteRef.deleteSingleRef(DeleteRef.java:122)
        at com.google.gerrit.server.restapi.project.DeleteBranch.apply(DeleteBranch.java:59)
        at com.google.gerrit.server.restapi.project.DeleteBranch.apply(DeleteBranch.java:34)
        at com.google.gerrit.httpd.restapi.RestApiServlet.lambda$invokeRestModifyViewWithRetry$4(RestApiServlet.java:740)
        at com.github.rholder.retry.AttemptTimeLimiters$NoAttemptTimeLimit.call(AttemptTimeLimiters.java:78)
        at com.github.rholder.retry.Retryer.call(Retryer.java:160)
        at com.google.gerrit.server.update.RetryHelper.executeWithTimeoutCount(RetryHelper.java:417)
        at com.google.gerrit.server.update.RetryHelper.executeWithAttemptAndTimeoutCount(RetryHelper.java:368)
        at com.google.gerrit.server.update.RetryHelper.execute(RetryHelper.java:271)
        at com.google.gerrit.httpd.restapi.RestApiServlet.invokeRestEndpointWithRetry(RestApiServlet.java:820)
        at com.google.gerrit.httpd.restapi.RestApiServlet.invokeRestModifyViewWithRetry(RestApiServlet.java:735)
        at com.google.gerrit.httpd.restapi.RestApiServlet.service(RestApiServlet.java:511)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        ...

Change-Id: I44c23f4553f05300b7c79bf257b417ec0b496b09
Signed-off-by: Edwin Kempin <ekempin@google.com>
2019-11-15 11:13:02 +01:00

61 lines
2.4 KiB
Java

// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.restapi.project;
import static org.eclipse.jgit.lib.Constants.R_HEADS;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.entities.RefNames;
import com.google.gerrit.extensions.api.projects.DeleteBranchesInput;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.project.ProjectResource;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
@Singleton
public class DeleteBranches implements RestModifyView<ProjectResource, DeleteBranchesInput> {
private final DeleteRef deleteRef;
@Inject
DeleteBranches(DeleteRef deleteRef) {
this.deleteRef = deleteRef;
}
@Override
public Response<?> apply(ProjectResource project, DeleteBranchesInput input)
throws IOException, RestApiException, PermissionBackendException {
if (input == null || input.branches == null || input.branches.isEmpty()) {
throw new BadRequestException("branches must be specified");
}
if (input.branches.contains(RefNames.HEAD)) {
throw new MethodNotAllowedException("not allowed to delete HEAD");
} else if (input.branches.stream().anyMatch(RefNames::isConfigRef)) {
// Never allow to delete the meta config branch.
throw new MethodNotAllowedException("not allowed to delete branch " + RefNames.REFS_CONFIG);
}
deleteRef.deleteMultipleRefs(
project.getProjectState(), ImmutableSet.copyOf(input.branches), R_HEADS);
return Response.none();
}
}