Handle Git LFS requests, with error message

We don't support Git LFS. This change adds a handler for
POST requests to "<project>.git/info/lfs/objects/batch",
and sends back a 406 response, with json data that the
client can use to print a nice error message.
This commit is contained in:
Shane Kilkelly 2020-08-05 10:08:40 +01:00
parent 395c558d77
commit f7125b9159
3 changed files with 74 additions and 0 deletions

View file

@ -130,6 +130,7 @@ public class GitBridgeServer {
HandlerCollection handlers = new HandlerList(); HandlerCollection handlers = new HandlerList();
handlers.addHandler(new StatusHandler(bridge)); handlers.addHandler(new StatusHandler(bridge));
handlers.addHandler(new HealthCheckHandler(bridge)); handlers.addHandler(new HealthCheckHandler(bridge));
handlers.addHandler(new GitLfsHandler(bridge));
base.setHandler(handlers); base.setHandler(handlers);
return base; return base;
} }

View file

@ -0,0 +1,46 @@
package uk.ac.ic.wlgitbridge.server;
import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ic.wlgitbridge.bridge.Bridge;
import uk.ac.ic.wlgitbridge.util.Log;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class GitLfsHandler extends AbstractHandler {
private final Bridge bridge;
public GitLfsHandler(Bridge bridge) {
this.bridge = bridge;
}
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response
) throws IOException {
String method = baseRequest.getMethod();
if (
("POST".equals(method))
&& target != null
&& target.matches("^/[0-9a-z]+\\.git/info/lfs/objects/batch/?$")
) {
Log.info(method + " <- /<project>.git/info/lfs/objects/batch");
response.setContentType("application/vnd.git-lfs+json");
response.setStatus(406);
response.getWriter().println("{\"message\": \"ERROR: Git LFS is not supported on Overleaf\"}");
baseRequest.setHandled(true);
}
}
}

View file

@ -5,12 +5,18 @@ import org.apache.commons.io.IOUtils;
import static org.asynchttpclient.Dsl.*; import static org.asynchttpclient.Dsl.*;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.apache.http.ParseException;
import org.asynchttpclient.*; import org.asynchttpclient.*;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.After; import org.junit.After;
@ -968,6 +974,27 @@ public class WLGitBridgeIntegrationTest {
assertEquals(200, healthCheckResponse.getStatusLine().getStatusCode()); assertEquals(200, healthCheckResponse.getStatusLine().getStatusCode());
} }
@Test
public void gitLfsBatchEndpoint() throws ClientProtocolException, IOException, ParseException {
int gitBridgePort = 33890;
int mockServerPort = 3890;
server = new MockSnapshotServer(mockServerPort, getResource("/canCloneARepository").toFile());
server.start();
server.setState(states.get("canCloneARepository").get("state"));
wlgb = new GitBridgeApp(new String[] {
makeConfigFile(gitBridgePort, mockServerPort)
});
wlgb.run();
HttpClient client = HttpClients.createDefault();
String urlBase = "http://127.0.0.1:" + gitBridgePort;
HttpPost gitLfsRequest = new HttpPost(urlBase+"/5f2419407929eb0026641967.git/info/lfs/objects/batch");
HttpResponse gitLfsResponse = client.execute(gitLfsRequest);
assertEquals(406, gitLfsResponse.getStatusLine().getStatusCode());
HttpEntity entity = gitLfsResponse.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
assertTrue(responseString.contains("Git LFS is not supported on Overleaf"));
}
private String makeConfigFile( private String makeConfigFile(
int port, int port,
int apiPort int apiPort