From 23943971846437c8bc24504d3bd4b681edea5433 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Sat, 7 Nov 2020 19:56:51 +0100 Subject: [PATCH] raw: Fix exceptions when no path info is given to raw servlet --- .../java/com/gitblit/servlet/RawServlet.java | 5 +- .../com/gitblit/servlet/RawServletTest.java | 112 +++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gitblit/servlet/RawServlet.java b/src/main/java/com/gitblit/servlet/RawServlet.java index 5fba78b5..211d847d 100644 --- a/src/main/java/com/gitblit/servlet/RawServlet.java +++ b/src/main/java/com/gitblit/servlet/RawServlet.java @@ -124,6 +124,7 @@ public class RawServlet extends HttpServlet { protected String getBranch(String repository, HttpServletRequest request) { String pi = request.getPathInfo(); + if (pi == null || pi.isEmpty() || pi.equals("/")) return ""; String branch = pi.substring(pi.indexOf(repository) + repository.length() + 1); int fs = branch.indexOf('/'); if (fs > -1) { @@ -135,7 +136,9 @@ public class RawServlet extends HttpServlet { protected String getPath(String repository, String branch, HttpServletRequest request) { String base = repository + "/" + branch; - String pi = request.getPathInfo().substring(1); + String pi = request.getPathInfo(); + if (pi == null || pi.isEmpty() || pi.equals("/")) return ""; + pi = pi.substring(1); if (pi.equals(base)) { return ""; } diff --git a/src/test/java/com/gitblit/servlet/RawServletTest.java b/src/test/java/com/gitblit/servlet/RawServletTest.java index 9f2b9e71..1deacf7e 100644 --- a/src/test/java/com/gitblit/servlet/RawServletTest.java +++ b/src/test/java/com/gitblit/servlet/RawServletTest.java @@ -3,6 +3,7 @@ package com.gitblit.servlet; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.manager.IRepositoryManager; import com.gitblit.tests.mock.MockGitblitContext; import com.gitblit.tests.mock.MockRuntimeManager; import org.junit.Before; @@ -10,20 +11,28 @@ import org.junit.BeforeClass; import org.junit.Test; +import javax.servlet.http.HttpServletRequest; + import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class RawServletTest { private final static String FSC = "!"; + private static MockRuntimeManager mockRuntimeManager = new MockRuntimeManager(); private static IStoredSettings settings; + private IRepositoryManager repositoryMngr; + + private RawServlet rawServlet; + @BeforeClass public static void init() { MockGitblitContext gitblitContext = new MockGitblitContext(); - MockRuntimeManager mockRuntimeManager = new MockRuntimeManager(); gitblitContext.addManager(mockRuntimeManager); settings = mockRuntimeManager.getSettings(); } @@ -32,6 +41,9 @@ public class RawServletTest public void setUp() { settings.overrideSetting(Keys.web.forwardSlashCharacter, "/"); + + repositoryMngr = mock(IRepositoryManager.class); + rawServlet = new RawServlet(mockRuntimeManager, repositoryMngr); } @@ -672,4 +684,102 @@ public class RawServletTest public void getPath() { } + + @Test + public void getBranch_Repo() + { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getPathInfo()).thenReturn("/test.git/"); + + String branch = rawServlet.getBranch("test.git", request); + + assertEquals("Branch was supposed to be empty as no branch was given.", "", branch); + } + + @Test + public void getBranch_RepoNoTrailingSlash() + { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getPathInfo()).thenReturn("/test.git"); + + String branch = rawServlet.getBranch("test.git", request); + + assertEquals("Branch was supposed to be empty as no branch was given.", "", branch); + } + + @Test + public void getBranch_PiNull() + { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getPathInfo()).thenReturn(null); + + String branch = rawServlet.getBranch("test.git", request); + + assertEquals("Branch was supposed to be empty as path info is null.", "", branch); + } + + + @Test + public void getBranch_PiEmpty() + { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getPathInfo()).thenReturn(""); + + String branch = rawServlet.getBranch("test.git", request); + + assertEquals("Branch was supposed to be empty as no path info exists.", "", branch); + } + + + + + + + + + @Test + public void getPath_Repo() + { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getPathInfo()).thenReturn("/test.git/"); + + String path = rawServlet.getPath("test.git", "", request); + + assertEquals("Path was supposed to be empty as no path was given.", "", path); + } + + @Test + public void getPath_RepoNoTrailingSlash() + { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getPathInfo()).thenReturn("/test.git"); + + String path = rawServlet.getPath("test.git", "", request); + + assertEquals("Path was supposed to be empty as no path was given.", "", path); + } + + @Test + public void getPath_PiNull() + { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getPathInfo()).thenReturn(null); + + String path = rawServlet.getPath("test.git", "", request); + + assertEquals("Path was supposed to be empty as path info is null.", "", path); + } + + + @Test + public void getPath_PiEmpty() + { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getPathInfo()).thenReturn(""); + + String path = rawServlet.getPath("test.git", "", request); + + assertEquals("Path was supposed to be empty as no path info exists.", "", path); + } + } -- 2.39.5