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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 org.junit.Assert.assertTrue;
  40. import static org.junit.Assert.fail;
  41. import java.io.File;
  42. import java.io.IOException;
  43. import java.util.Arrays;
  44. import org.eclipse.jgit.api.Git;
  45. import org.eclipse.jgit.api.errors.GitAPIException;
  46. import org.eclipse.jgit.dircache.InvalidPathException;
  47. import org.eclipse.jgit.junit.MockSystemReader;
  48. import org.eclipse.jgit.junit.RepositoryTestCase;
  49. import org.eclipse.jgit.revwalk.RevWalk;
  50. import org.eclipse.jgit.util.SystemReader;
  51. import org.junit.Test;
  52. public class DirCacheCheckoutMaliciousPathTest extends RepositoryTestCase {
  53. protected ObjectId theHead;
  54. protected ObjectId theMerge;
  55. @Test
  56. public void testMaliciousAbsolutePathIsOk() throws Exception {
  57. testMaliciousPathGoodFirstCheckout("ok");
  58. }
  59. @Test
  60. public void testMaliciousAbsolutePathIsOkSecondCheckout() throws Exception {
  61. testMaliciousPathGoodSecondCheckout("ok");
  62. }
  63. @Test
  64. public void testMaliciousAbsolutePathIsOkTwoLevels() throws Exception {
  65. testMaliciousPathGoodSecondCheckout("a", "ok");
  66. }
  67. @Test
  68. public void testMaliciousAbsolutePath() throws Exception {
  69. testMaliciousPathBadFirstCheckout("/tmp/x");
  70. }
  71. @Test
  72. public void testMaliciousAbsolutePathSecondCheckout() throws Exception {
  73. testMaliciousPathBadSecondCheckout("/tmp/x");
  74. }
  75. @Test
  76. public void testMaliciousAbsolutePathTwoLevelsFirstBad() throws Exception {
  77. testMaliciousPathBadFirstCheckout("/tmp/x", "y");
  78. }
  79. @Test
  80. public void testMaliciousAbsolutePathTwoLevelsSecondBad() throws Exception {
  81. testMaliciousPathBadFirstCheckout("y", "/tmp/x");
  82. }
  83. @Test
  84. public void testMaliciousAbsoluteCurDrivePathWindows() throws Exception {
  85. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  86. testMaliciousPathBadFirstCheckout("\\somepath");
  87. }
  88. @Test
  89. public void testMaliciousAbsoluteCurDrivePathWindowsOnUnix()
  90. throws Exception {
  91. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  92. testMaliciousPathGoodFirstCheckout("\\somepath");
  93. }
  94. @Test
  95. public void testMaliciousAbsoluteUNCPathWindows1() throws Exception {
  96. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  97. testMaliciousPathBadFirstCheckout("\\\\somepath");
  98. }
  99. @Test
  100. public void testMaliciousAbsoluteUNCPathWindows1OnUnix() throws Exception {
  101. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  102. testMaliciousPathGoodFirstCheckout("\\\\somepath");
  103. }
  104. @Test
  105. public void testMaliciousAbsoluteUNCPathWindows2() throws Exception {
  106. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  107. testMaliciousPathBadFirstCheckout("\\/somepath");
  108. }
  109. @Test
  110. public void testMaliciousAbsoluteUNCPathWindows2OnUnix() throws Exception {
  111. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  112. testMaliciousPathBadFirstCheckout("\\/somepath");
  113. }
  114. @Test
  115. public void testMaliciousAbsoluteWindowsPath1() throws Exception {
  116. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  117. testMaliciousPathBadFirstCheckout("c:\\temp\\x");
  118. }
  119. @Test
  120. public void testMaliciousAbsoluteWindowsPath1OnUnix() throws Exception {
  121. if (File.separatorChar == '\\')
  122. return; // cannot emulate Unix on Windows for this test
  123. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  124. testMaliciousPathGoodFirstCheckout("c:\\temp\\x");
  125. }
  126. @Test
  127. public void testMaliciousAbsoluteWindowsPath2() throws Exception {
  128. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  129. testMaliciousPathBadFirstCheckout("c:/temp/x");
  130. }
  131. @Test
  132. public void testMaliciousGitPath1() throws Exception {
  133. testMaliciousPathBadFirstCheckout(".git/konfig");
  134. }
  135. @Test
  136. public void testMaliciousGitPath2() throws Exception {
  137. testMaliciousPathBadFirstCheckout(".git", "konfig");
  138. }
  139. @Test
  140. public void testMaliciousGitPath1Case() throws Exception {
  141. ((MockSystemReader) SystemReader.getInstance()).setWindows(); // or OS X
  142. testMaliciousPathBadFirstCheckout(".Git/konfig");
  143. }
  144. @Test
  145. public void testMaliciousGitPath2Case() throws Exception {
  146. ((MockSystemReader) SystemReader.getInstance()).setWindows(); // or OS X
  147. testMaliciousPathBadFirstCheckout(".gIt", "konfig");
  148. }
  149. @Test
  150. public void testMaliciousGitPath3Case() throws Exception {
  151. ((MockSystemReader) SystemReader.getInstance()).setWindows(); // or OS X
  152. testMaliciousPathBadFirstCheckout(".giT", "konfig");
  153. }
  154. @Test
  155. public void testMaliciousGitPathEndSpaceWindows() throws Exception {
  156. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  157. testMaliciousPathBadFirstCheckout(".git ", "konfig");
  158. }
  159. @Test
  160. public void testMaliciousGitPathEndSpaceUnixOk() throws Exception {
  161. testMaliciousPathBadFirstCheckout(".git ", "konfig");
  162. }
  163. @Test
  164. public void testMaliciousGitPathEndDotWindows1() throws Exception {
  165. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  166. testMaliciousPathBadFirstCheckout(".git.", "konfig");
  167. }
  168. @Test
  169. public void testMaliciousGitPathEndDotWindows2() throws Exception {
  170. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  171. testMaliciousPathBadFirstCheckout(".f.");
  172. }
  173. @Test
  174. public void testMaliciousGitPathEndDotWindows3() throws Exception {
  175. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  176. testMaliciousPathGoodFirstCheckout(".f");
  177. }
  178. @Test
  179. public void testMaliciousGitPathEndDotUnixOk() throws Exception {
  180. testMaliciousPathBadFirstCheckout(".git.", "konfig");
  181. }
  182. @Test
  183. public void testMaliciousPathDotDot() throws Exception {
  184. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  185. testMaliciousPathBadFirstCheckout("..", "no");
  186. }
  187. @Test
  188. public void testMaliciousPathDot() throws Exception {
  189. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  190. testMaliciousPathBadFirstCheckout(".", "no");
  191. }
  192. @Test
  193. public void testMaliciousPathEmptyUnix() throws Exception {
  194. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  195. testMaliciousPathBadFirstCheckout("", "no");
  196. }
  197. @Test
  198. public void testMaliciousPathEmptyWindows() throws Exception {
  199. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  200. testMaliciousPathBadFirstCheckout("", "no");
  201. }
  202. @Test
  203. public void testMaliciousWindowsADS() throws Exception {
  204. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  205. testMaliciousPathBadFirstCheckout("some:path");
  206. }
  207. @Test
  208. public void testMaliciousWindowsADSOnUnix() throws Exception {
  209. if (File.separatorChar == '\\')
  210. return; // cannot emulate Unix on Windows for this test
  211. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  212. testMaliciousPathGoodFirstCheckout("some:path");
  213. }
  214. @Test
  215. public void testForbiddenNamesOnWindowsEgCon() throws Exception {
  216. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  217. testMaliciousPathBadFirstCheckout("con");
  218. }
  219. @Test
  220. public void testForbiddenNamesOnWindowsEgConDotSuffix() throws Exception {
  221. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  222. testMaliciousPathBadFirstCheckout("con.txt");
  223. }
  224. @Test
  225. public void testForbiddenNamesOnWindowsEgLpt1() throws Exception {
  226. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  227. testMaliciousPathBadFirstCheckout("lpt1");
  228. }
  229. @Test
  230. public void testForbiddenNamesOnWindowsEgLpt1DotSuffix() throws Exception {
  231. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  232. testMaliciousPathBadFirstCheckout("lpt1.txt");
  233. }
  234. @Test
  235. public void testForbiddenNamesOnWindowsEgDotCon() throws Exception {
  236. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  237. testMaliciousPathGoodFirstCheckout(".con");
  238. }
  239. @Test
  240. public void testForbiddenNamesOnWindowsEgLpr() throws Exception {
  241. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  242. testMaliciousPathGoodFirstCheckout("lpt"); // good name
  243. }
  244. @Test
  245. public void testForbiddenNamesOnWindowsEgCon1() throws Exception {
  246. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  247. testMaliciousPathGoodFirstCheckout("con1"); // good name
  248. }
  249. @Test
  250. public void testForbiddenWindowsNamesOnUnixEgCon() throws Exception {
  251. if (File.separatorChar == '\\')
  252. return; // cannot emulate Unix on Windows for this test
  253. testMaliciousPathGoodFirstCheckout("con");
  254. }
  255. @Test
  256. public void testForbiddenWindowsNamesOnUnixEgLpt1() throws Exception {
  257. if (File.separatorChar == '\\')
  258. return; // cannot emulate Unix on Windows for this test
  259. testMaliciousPathGoodFirstCheckout("lpt1");
  260. }
  261. private void testMaliciousPathBadFirstCheckout(String... paths)
  262. throws Exception {
  263. testMaliciousPath(false, false, paths);
  264. }
  265. private void testMaliciousPathBadSecondCheckout(String... paths) throws Exception {
  266. testMaliciousPath(false, true, paths);
  267. }
  268. private void testMaliciousPathGoodFirstCheckout(String... paths)
  269. throws Exception {
  270. testMaliciousPath(true, false, paths);
  271. }
  272. private void testMaliciousPathGoodSecondCheckout(String... paths) throws Exception {
  273. testMaliciousPath(true, true, paths);
  274. }
  275. /**
  276. * Create a bad tree and tries to check it out
  277. *
  278. * @param good
  279. * true if we expect this to pass
  280. * @param secondCheckout
  281. * perform the actual test on the second checkout
  282. * @param path
  283. * to the blob, one or more levels
  284. * @throws GitAPIException
  285. * @throws IOException
  286. */
  287. private void testMaliciousPath(boolean good, boolean secondCheckout,
  288. String... path) throws GitAPIException, IOException {
  289. Git git = new Git(db);
  290. ObjectInserter newObjectInserter;
  291. newObjectInserter = git.getRepository().newObjectInserter();
  292. ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB,
  293. "data".getBytes());
  294. newObjectInserter = git.getRepository().newObjectInserter();
  295. FileMode mode = FileMode.REGULAR_FILE;
  296. ObjectId insertId = blobId;
  297. for (int i = path.length - 1; i >= 0; --i) {
  298. TreeFormatter treeFormatter = new TreeFormatter();
  299. treeFormatter.append("goodpath", mode, insertId);
  300. insertId = newObjectInserter.insert(treeFormatter);
  301. mode = FileMode.TREE;
  302. }
  303. newObjectInserter = git.getRepository().newObjectInserter();
  304. CommitBuilder commitBuilder = new CommitBuilder();
  305. commitBuilder.setAuthor(author);
  306. commitBuilder.setCommitter(committer);
  307. commitBuilder.setMessage("foo#1");
  308. commitBuilder.setTreeId(insertId);
  309. ObjectId firstCommitId = newObjectInserter.insert(commitBuilder);
  310. newObjectInserter = git.getRepository().newObjectInserter();
  311. mode = FileMode.REGULAR_FILE;
  312. insertId = blobId;
  313. for (int i = path.length - 1; i >= 0; --i) {
  314. TreeFormatter treeFormatter = new TreeFormatter();
  315. treeFormatter.append(path[i], mode, insertId);
  316. insertId = newObjectInserter.insert(treeFormatter);
  317. mode = FileMode.TREE;
  318. }
  319. // Create another commit
  320. commitBuilder = new CommitBuilder();
  321. commitBuilder.setAuthor(author);
  322. commitBuilder.setCommitter(committer);
  323. commitBuilder.setMessage("foo#2");
  324. commitBuilder.setTreeId(insertId);
  325. commitBuilder.setParentId(firstCommitId);
  326. ObjectId commitId = newObjectInserter.insert(commitBuilder);
  327. RevWalk revWalk = new RevWalk(git.getRepository());
  328. if (!secondCheckout)
  329. git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId))
  330. .setName("refs/heads/master").setCreateBranch(true).call();
  331. try {
  332. if (secondCheckout) {
  333. git.checkout().setStartPoint(revWalk.parseCommit(commitId))
  334. .setName("refs/heads/master").setCreateBranch(true)
  335. .call();
  336. } else {
  337. git.branchCreate().setName("refs/heads/next")
  338. .setStartPoint(commitId.name()).call();
  339. git.checkout().setName("refs/heads/next")
  340. .call();
  341. }
  342. if (!good)
  343. fail("Checkout of Tree " + Arrays.asList(path) + " should fail");
  344. } catch (InvalidPathException e) {
  345. if (good)
  346. throw e;
  347. assertTrue(e.getMessage().startsWith("Invalid path"));
  348. }
  349. }
  350. }