diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java index 4ad7fa314..0e97d7b15 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java @@ -2726,6 +2726,50 @@ public void testNotAdvertisedWantsV2FetchRequestPolicyReachableCommit() throws E assertEquals(1, stats.getNotAdvertisedWants()); } + @Test + public void testAllowAnySha1InWantConfig() { + server.getConfig().setBoolean("uploadpack", null, "allowanysha1inwant", + true); + + try (UploadPack uploadPack = new UploadPack(server)) { + assertEquals(RequestPolicy.ANY, uploadPack.getRequestPolicy()); + } + } + + @Test + public void testAllowReachableSha1InWantConfig() { + server.getConfig().setBoolean("uploadpack", null, + "allowreachablesha1inwant", true); + + try (UploadPack uploadPack = new UploadPack(server)) { + assertEquals(RequestPolicy.REACHABLE_COMMIT, + uploadPack.getRequestPolicy()); + } + } + + @Test + public void testAllowTipSha1InWantConfig() { + server.getConfig().setBoolean("uploadpack", null, "allowtipsha1inwant", + true); + + try (UploadPack uploadPack = new UploadPack(server)) { + assertEquals(RequestPolicy.TIP, uploadPack.getRequestPolicy()); + } + } + + @Test + public void testAllowReachableTipSha1InWantConfig() { + server.getConfig().setBoolean("uploadpack", null, + "allowreachablesha1inwant", true); + server.getConfig().setBoolean("uploadpack", null, "allowtipsha1inwant", + true); + + try (UploadPack uploadPack = new UploadPack(server)) { + assertEquals(RequestPolicy.REACHABLE_COMMIT_TIP, + uploadPack.getRequestPolicy()); + } + } + private class RefCallsCountingRepository extends InMemoryRepository { private final InMemoryRepository.MemRefDatabase refdb; private int numRefCalls; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java index 805166a40..064201a62 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008, 2020 Google Inc. and others + * Copyright (C) 2008, 2023 Google Inc. and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0 which is available at @@ -118,6 +118,7 @@ static ProtocolVersion parse(@Nullable String name) { private final boolean allowRefInWant; private final boolean allowTipSha1InWant; private final boolean allowReachableSha1InWant; + private final boolean allowAnySha1InWant; private final boolean allowFilter; private final boolean allowSidebandAll; @@ -202,6 +203,8 @@ public TransferConfig(Config rc) { "uploadpack", "allowtipsha1inwant", false); allowReachableSha1InWant = rc.getBoolean( "uploadpack", "allowreachablesha1inwant", false); + allowAnySha1InWant = rc.getBoolean("uploadpack", "allowanysha1inwant", + false); allowFilter = rc.getBoolean( "uploadpack", "allowfilter", false); protocolVersion = ProtocolVersion.parse(rc @@ -283,6 +286,16 @@ public boolean isAllowReachableSha1InWant() { return allowReachableSha1InWant; } + /** + * Whether to allow clients to request any SHA-1s + * + * @return allow clients to request any SHA-1s? + * @since 6.5 + */ + public boolean isAllowAnySha1InWant() { + return allowAnySha1InWant; + } + /** * @return true if clients are allowed to specify a "filter" line * @since 5.0 diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java index 38c7cb94b..b64870647 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -681,6 +681,10 @@ public void setPackConfig(@Nullable PackConfig pc) { */ public void setTransferConfig(@Nullable TransferConfig tc) { this.transferConfig = tc != null ? tc : new TransferConfig(db); + if (transferConfig.isAllowAnySha1InWant()) { + setRequestPolicy(RequestPolicy.ANY); + return; + } if (transferConfig.isAllowTipSha1InWant()) { setRequestPolicy(transferConfig.isAllowReachableSha1InWant() ? RequestPolicy.REACHABLE_COMMIT_TIP : RequestPolicy.TIP);