You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirCacheCheckoutMaliciousPathTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Copyright (C) 2011, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v1.0 which accompanies this
  7. * distribution, is reproduced below, and is available at
  8. * http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice, this
  16. * list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
  23. * contributors may be used to endorse or promote products derived from this
  24. * software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. package org.eclipse.jgit.lib;
  39. import static java.nio.charset.StandardCharsets.UTF_8;
  40. import static org.junit.Assert.assertTrue;
  41. import static org.junit.Assert.fail;
  42. import java.io.File;
  43. import java.io.IOException;
  44. import java.util.Arrays;
  45. import org.eclipse.jgit.api.Git;
  46. import org.eclipse.jgit.api.errors.GitAPIException;
  47. import org.eclipse.jgit.dircache.InvalidPathException;
  48. import org.eclipse.jgit.junit.MockSystemReader;
  49. import org.eclipse.jgit.junit.RepositoryTestCase;
  50. import org.eclipse.jgit.revwalk.RevWalk;
  51. import org.eclipse.jgit.util.SystemReader;
  52. import org.junit.Test;
  53. public class DirCacheCheckoutMaliciousPathTest extends RepositoryTestCase {
  54. protected ObjectId theHead;
  55. protected ObjectId theMerge;
  56. @Test
  57. public void testMaliciousAbsolutePathIsOk() throws Exception {
  58. testMaliciousPathGoodFirstCheckout("ok");
  59. }
  60. @Test
  61. public void testMaliciousAbsolutePathIsOkSecondCheckout() throws Exception {
  62. testMaliciousPathGoodSecondCheckout("ok");
  63. }
  64. @Test
  65. public void testMaliciousAbsolutePathIsOkTwoLevels() throws Exception {
  66. testMaliciousPathGoodSecondCheckout("a", "ok");
  67. }
  68. @Test
  69. public void testMaliciousAbsolutePath() throws Exception {
  70. testMaliciousPathBadFirstCheckout("/tmp/x");
  71. }
  72. @Test
  73. public void testMaliciousAbsolutePathSecondCheckout() throws Exception {
  74. testMaliciousPathBadSecondCheckout("/tmp/x");
  75. }
  76. @Test
  77. public void testMaliciousAbsolutePathTwoLevelsFirstBad() throws Exception {
  78. testMaliciousPathBadFirstCheckout("/tmp/x", "y");
  79. }
  80. @Test
  81. public void testMaliciousAbsolutePathTwoLevelsSecondBad() throws Exception {
  82. testMaliciousPathBadFirstCheckout("y", "/tmp/x");
  83. }
  84. @Test
  85. public void testMaliciousAbsoluteCurDrivePathWindows() throws Exception {
  86. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  87. testMaliciousPathBadFirstCheckout("\\somepath");
  88. }
  89. @Test
  90. public void testMaliciousAbsoluteCurDrivePathWindowsOnUnix()
  91. throws Exception {
  92. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  93. testMaliciousPathGoodFirstCheckout("\\somepath");
  94. }
  95. @Test
  96. public void testMaliciousAbsoluteUNCPathWindows1() throws Exception {
  97. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  98. testMaliciousPathBadFirstCheckout("\\\\somepath");
  99. }
  100. @Test
  101. public void testMaliciousAbsoluteUNCPathWindows1OnUnix() throws Exception {
  102. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  103. testMaliciousPathGoodFirstCheckout("\\\\somepath");
  104. }
  105. @Test
  106. public void testMaliciousAbsoluteUNCPathWindows2() throws Exception {
  107. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  108. testMaliciousPathBadFirstCheckout("\\/somepath");
  109. }
  110. @Test
  111. public void testMaliciousAbsoluteUNCPathWindows2OnUnix() throws Exception {
  112. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  113. testMaliciousPathBadFirstCheckout("\\/somepath");
  114. }
  115. @Test
  116. public void testMaliciousAbsoluteWindowsPath1() throws Exception {
  117. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  118. testMaliciousPathBadFirstCheckout("c:\\temp\\x");
  119. }
  120. @Test
  121. public void testMaliciousAbsoluteWindowsPath1OnUnix() throws Exception {
  122. if (File.separatorChar == '\\')
  123. return; // cannot emulate Unix on Windows for this test
  124. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  125. testMaliciousPathGoodFirstCheckout("c:\\temp\\x");
  126. }
  127. @Test
  128. public void testMaliciousAbsoluteWindowsPath2() throws Exception {
  129. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  130. testMaliciousPathBadFirstCheckout("c:/temp/x");
  131. }
  132. @Test
  133. public void testMaliciousGitPath1() throws Exception {
  134. testMaliciousPathBadFirstCheckout(".git/konfig");
  135. }
  136. @Test
  137. public void testMaliciousGitPath2() throws Exception {
  138. testMaliciousPathBadFirstCheckout(".git", "konfig");
  139. }
  140. @Test
  141. public void testMaliciousGitPath1Case() throws Exception {
  142. ((MockSystemReader) SystemReader.getInstance()).setWindows(); // or OS X
  143. testMaliciousPathBadFirstCheckout(".Git/konfig");
  144. }
  145. @Test
  146. public void testMaliciousGitPath2Case() throws Exception {
  147. ((MockSystemReader) SystemReader.getInstance()).setWindows(); // or OS X
  148. testMaliciousPathBadFirstCheckout(".gIt", "konfig");
  149. }
  150. @Test
  151. public void testMaliciousGitPath3Case() throws Exception {
  152. ((MockSystemReader) SystemReader.getInstance()).setWindows(); // or OS X
  153. testMaliciousPathBadFirstCheckout(".giT", "konfig");
  154. }
  155. @Test
  156. public void testMaliciousGitPathEndSpaceWindows() throws Exception {
  157. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  158. testMaliciousPathBadFirstCheckout(".git ", "konfig");
  159. }
  160. @Test
  161. public void testMaliciousGitPathEndSpaceUnixOk() throws Exception {
  162. testMaliciousPathBadFirstCheckout(".git ", "konfig");
  163. }
  164. @Test
  165. public void testMaliciousGitPathEndDotWindows1() throws Exception {
  166. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  167. testMaliciousPathBadFirstCheckout(".git.", "konfig");
  168. }
  169. @Test
  170. public void testMaliciousGitPathEndDotWindows2() throws Exception {
  171. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  172. testMaliciousPathBadFirstCheckout(".f.");
  173. }
  174. @Test
  175. public void testMaliciousGitPathEndDotWindows3() throws Exception {
  176. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  177. testMaliciousPathGoodFirstCheckout(".f");
  178. }
  179. @Test
  180. public void testMaliciousGitPathEndDotUnixOk() throws Exception {
  181. testMaliciousPathBadFirstCheckout(".git.", "konfig");
  182. }
  183. @Test
  184. public void testMaliciousPathDotDot() throws Exception {
  185. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  186. testMaliciousPathBadFirstCheckout("..", "no");
  187. }
  188. @Test
  189. public void testMaliciousPathDot() throws Exception {
  190. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  191. testMaliciousPathBadFirstCheckout(".", "no");
  192. }
  193. @Test
  194. public void testMaliciousPathEmptyUnix() throws Exception {
  195. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  196. testMaliciousPathBadFirstCheckout("", "no");
  197. }
  198. @Test
  199. public void testMaliciousPathEmptyWindows() throws Exception {
  200. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  201. testMaliciousPathBadFirstCheckout("", "no");
  202. }
  203. @Test
  204. public void testMaliciousWindowsADS() throws Exception {
  205. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  206. testMaliciousPathBadFirstCheckout("some:path");
  207. }
  208. @Test
  209. public void testMaliciousWindowsADSOnUnix() throws Exception {
  210. if (File.separatorChar == '\\')
  211. return; // cannot emulate Unix on Windows for this test
  212. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  213. testMaliciousPathGoodFirstCheckout("some:path");
  214. }
  215. @Test
  216. public void testForbiddenNamesOnWindowsEgCon() throws Exception {
  217. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  218. testMaliciousPathBadFirstCheckout("con");
  219. }
  220. @Test
  221. public void testForbiddenNamesOnWindowsEgConDotSuffix() throws Exception {
  222. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  223. testMaliciousPathBadFirstCheckout("con.txt");
  224. }
  225. @Test
  226. public void testForbiddenNamesOnWindowsEgLpt1() throws Exception {
  227. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  228. testMaliciousPathBadFirstCheckout("lpt1");
  229. }
  230. @Test
  231. public void testForbiddenNamesOnWindowsEgLpt1DotSuffix() throws Exception {
  232. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  233. testMaliciousPathBadFirstCheckout("lpt1.txt");
  234. }
  235. @Test
  236. public void testForbiddenNamesOnWindowsEgDotCon() throws Exception {
  237. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  238. testMaliciousPathGoodFirstCheckout(".con");
  239. }
  240. @Test
  241. public void testForbiddenNamesOnWindowsEgLpr() throws Exception {
  242. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  243. testMaliciousPathGoodFirstCheckout("lpt"); // good name
  244. }
  245. @Test
  246. public void testForbiddenNamesOnWindowsEgCon1() throws Exception {
  247. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  248. testMaliciousPathGoodFirstCheckout("con1"); // good name
  249. }
  250. @Test
  251. public void testForbiddenWindowsNamesOnUnixEgCon() throws Exception {
  252. if (File.separatorChar == '\\')
  253. return; // cannot emulate Unix on Windows for this test
  254. testMaliciousPathGoodFirstCheckout("con");
  255. }
  256. @Test
  257. public void testForbiddenWindowsNamesOnUnixEgLpt1() throws Exception {
  258. if (File.separatorChar == '\\')
  259. return; // cannot emulate Unix on Windows for this test
  260. testMaliciousPathGoodFirstCheckout("lpt1");
  261. }
  262. private void testMaliciousPathBadFirstCheckout(String... paths)
  263. throws Exception {
  264. testMaliciousPath(false, false, paths);
  265. }
  266. private void testMaliciousPathBadSecondCheckout(String... paths) throws Exception {
  267. testMaliciousPath(false, true, paths);
  268. }
  269. private void testMaliciousPathGoodFirstCheckout(String... paths)
  270. throws Exception {
  271. testMaliciousPath(true, false, paths);
  272. }
  273. private void testMaliciousPathGoodSecondCheckout(String... paths) throws Exception {
  274. testMaliciousPath(true, true, paths);
  275. }
  276. /**
  277. * Create a bad tree and tries to check it out
  278. *
  279. * @param good
  280. * true if we expect this to pass
  281. * @param secondCheckout
  282. * perform the actual test on the second checkout
  283. * @param path
  284. * to the blob, one or more levels
  285. * @throws GitAPIException
  286. * @throws IOException
  287. */
  288. private void testMaliciousPath(boolean good, boolean secondCheckout,
  289. String... path) throws GitAPIException, IOException {
  290. try (Git git = new Git(db);
  291. RevWalk revWalk = new RevWalk(git.getRepository())) {
  292. ObjectInserter newObjectInserter;
  293. newObjectInserter = git.getRepository().newObjectInserter();
  294. ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB,
  295. "data".getBytes(UTF_8));
  296. newObjectInserter = git.getRepository().newObjectInserter();
  297. FileMode mode = FileMode.REGULAR_FILE;
  298. ObjectId insertId = blobId;
  299. for (int i = path.length - 1; i >= 0; --i) {
  300. TreeFormatter treeFormatter = new TreeFormatter();
  301. treeFormatter.append("goodpath", mode, insertId);
  302. insertId = newObjectInserter.insert(treeFormatter);
  303. mode = FileMode.TREE;
  304. }
  305. newObjectInserter = git.getRepository().newObjectInserter();
  306. CommitBuilder commitBuilder = new CommitBuilder();
  307. commitBuilder.setAuthor(author);
  308. commitBuilder.setCommitter(committer);
  309. commitBuilder.setMessage("foo#1");
  310. commitBuilder.setTreeId(insertId);
  311. ObjectId firstCommitId = newObjectInserter.insert(commitBuilder);
  312. newObjectInserter = git.getRepository().newObjectInserter();
  313. mode = FileMode.REGULAR_FILE;
  314. insertId = blobId;
  315. for (int i = path.length - 1; i >= 0; --i) {
  316. TreeFormatter treeFormatter = new TreeFormatter();
  317. treeFormatter.append(path[i].getBytes(UTF_8), 0,
  318. path[i].getBytes(UTF_8).length,
  319. mode, insertId, true);
  320. insertId = newObjectInserter.insert(treeFormatter);
  321. mode = FileMode.TREE;
  322. }
  323. // Create another commit
  324. commitBuilder = new CommitBuilder();
  325. commitBuilder.setAuthor(author);
  326. commitBuilder.setCommitter(committer);
  327. commitBuilder.setMessage("foo#2");
  328. commitBuilder.setTreeId(insertId);
  329. commitBuilder.setParentId(firstCommitId);
  330. ObjectId commitId = newObjectInserter.insert(commitBuilder);
  331. if (!secondCheckout)
  332. git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId))
  333. .setName("refs/heads/master").setCreateBranch(true).call();
  334. try {
  335. if (secondCheckout) {
  336. git.checkout().setStartPoint(revWalk.parseCommit(commitId))
  337. .setName("refs/heads/master").setCreateBranch(true)
  338. .call();
  339. } else {
  340. git.branchCreate().setName("refs/heads/next")
  341. .setStartPoint(commitId.name()).call();
  342. git.checkout().setName("refs/heads/next")
  343. .call();
  344. }
  345. if (!good)
  346. fail("Checkout of Tree " + Arrays.asList(path) + " should fail");
  347. } catch (InvalidPathException e) {
  348. if (good)
  349. throw e;
  350. assertTrue(e.getMessage().startsWith("Invalid path"));
  351. }
  352. }
  353. }
  354. }