|
1 | 1 | package org.hjug.git; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; |
5 | 6 |
|
6 | 7 | import java.io.File; |
@@ -100,4 +101,128 @@ void testGetRepoUrlWithNoOrigin() throws Exception { |
100 | 101 | assertEquals("", repoUrl); |
101 | 102 | } |
102 | 103 | } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testGetRepoUrl_returnsEmpty_whenOriginUrlIsJavascriptScheme() throws Exception { |
| 107 | + git.remoteAdd() |
| 108 | + .setName("origin") |
| 109 | + .setUri(new URIish("javascript:alert(1)")) |
| 110 | + .call(); |
| 111 | + |
| 112 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 113 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 114 | + assertEquals("", repoUrl, "javascript: scheme should be rejected"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void testGetRepoUrl_returnsEmpty_whenOriginUrlIsDataScheme() throws Exception { |
| 120 | + git.remoteAdd() |
| 121 | + .setName("origin") |
| 122 | + .setUri(new URIish("data:text/html,<script>alert(1)</script>")) |
| 123 | + .call(); |
| 124 | + |
| 125 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 126 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 127 | + assertEquals("", repoUrl, "data: scheme should be rejected"); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void testGetRepoUrl_returnsEmpty_whenOriginUrlIsFileScheme() throws Exception { |
| 133 | + git.remoteAdd() |
| 134 | + .setName("origin") |
| 135 | + .setUri(new URIish("file:///etc/passwd")) |
| 136 | + .call(); |
| 137 | + |
| 138 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 139 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 140 | + assertEquals("", repoUrl, "file: scheme should be rejected"); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void testGetRepoUrl_returnsEmpty_whenOriginUrlIsNotHttpOrHttps() throws Exception { |
| 146 | + git.remoteAdd() |
| 147 | + .setName("origin") |
| 148 | + .setUri(new URIish("ssh://git@example.com/repo.git")) |
| 149 | + .call(); |
| 150 | + |
| 151 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 152 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 153 | + assertEquals("", repoUrl, "ssh: scheme should be rejected"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testGetRepoUrl_sanitizesUrl_removingMarkupCharacters() throws Exception { |
| 159 | + git.getRepository().getConfig().setString("remote", "origin", "url", "https://example.com/repo\"<unsafe>.git"); |
| 160 | + git.getRepository().getConfig().save(); |
| 161 | + |
| 162 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 163 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 164 | + assertTrue(repoUrl.startsWith("https://example.com/repounsafe")); |
| 165 | + assertFalse(repoUrl.contains("\""), "URL must not retain quotes: " + repoUrl); |
| 166 | + assertFalse(repoUrl.contains("<"), "URL must not retain angle brackets: " + repoUrl); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void testGetRepoUrl_allowsValidHttpsUrl() throws Exception { |
| 172 | + git.remoteAdd() |
| 173 | + .setName("origin") |
| 174 | + .setUri(new URIish("https://github.com/user/repo.git")) |
| 175 | + .call(); |
| 176 | + |
| 177 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 178 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 179 | + assertTrue( |
| 180 | + repoUrl.startsWith("https://github.com/user/repo/blob/"), |
| 181 | + "Valid HTTPS URL should be allowed: " + repoUrl); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + void testGetRepoUrl_allowsValidHttpUrl() throws Exception { |
| 187 | + git.remoteAdd() |
| 188 | + .setName("origin") |
| 189 | + .setUri(new URIish("http://example.com/repo.git")) |
| 190 | + .call(); |
| 191 | + |
| 192 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 193 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 194 | + assertTrue( |
| 195 | + repoUrl.startsWith("http://example.com/repo/blob/"), |
| 196 | + "Valid HTTP URL should be allowed: " + repoUrl); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + void testGetRepoUrl_stripsGitSuffix() throws Exception { |
| 202 | + git.remoteAdd() |
| 203 | + .setName("origin") |
| 204 | + .setUri(new URIish("https://github.com/user/repo.git")) |
| 205 | + .call(); |
| 206 | + |
| 207 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 208 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 209 | + assertTrue(!repoUrl.contains(".git"), "URL should not contain .git suffix"); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + void testGetRepoUrl_appendsBlobPath_forNonGithubHosts() throws Exception { |
| 215 | + git.remoteAdd() |
| 216 | + .setName("origin") |
| 217 | + .setUri(new URIish("https://example.com/user/repo.git")) |
| 218 | + .call(); |
| 219 | + |
| 220 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 221 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 222 | + String commitHash = git.log().call().iterator().next().getName(); |
| 223 | + assertTrue( |
| 224 | + repoUrl.endsWith("/blob/" + commitHash + "/"), |
| 225 | + "Non-GitHub hosts should get /blob/ path: " + repoUrl); |
| 226 | + } |
| 227 | + } |
103 | 228 | } |
0 commit comments