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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.hamcrest.Matchers.startsWith;
  40. import static org.junit.Assert.assertThat;
  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.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. if (File.separatorChar == '\\')
  162. return; // cannot emulate Unix on Windows for this test
  163. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  164. testMaliciousPathGoodFirstCheckout(".git ", "konfig");
  165. }
  166. @Test
  167. public void testMaliciousGitPathEndDotWindows1() throws Exception {
  168. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  169. testMaliciousPathBadFirstCheckout(".git.", "konfig");
  170. }
  171. @Test
  172. public void testMaliciousGitPathEndDotWindows2() throws Exception {
  173. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  174. testMaliciousPathBadFirstCheckout(".f.");
  175. }
  176. @Test
  177. public void testMaliciousGitPathEndDotWindows3() throws Exception {
  178. ((MockSystemReader) SystemReader.getInstance()).setWindows();
  179. testMaliciousPathGoodFirstCheckout(".f");
  180. }
  181. @Test
  182. public void testMaliciousGitPathEndDotUnixOk() throws Exception {
  183. if (File.separatorChar == '\\')
  184. return; // cannot emulate Unix on Windows for this test
  185. ((MockSystemReader) SystemReader.getInstance()).setUnix();
  186. testMaliciousPathGoodFirstCheckout(".git.", "konfig");
  187. }
  188. @Test
  189. public void testMaliciousPathDotDot() throws Exception {
  190. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  191. testMaliciousPathBadFirstCheckout("..", "no");
  192. }
  193. @Test
  194. public void testMaliciousPathDot() throws Exception {
  195. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  196. testMaliciousPathBadFirstCheckout(".", "no");
  197. }
  198. @Test
  199. public void testMaliciousPathEmpty() throws Exception {
  200. ((MockSystemReader) SystemReader.getInstance()).setCurrentPlatform();
  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. Git git = new Git(db);
  291. ObjectInserter newObjectInserter;
  292. newObjectInserter = git.getRepository().newObjectInserter();
  293. ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB,
  294. "data".getBytes());
  295. newObjectInserter = git.getRepository().newObjectInserter();
  296. FileMode mode = FileMode.REGULAR_FILE;
  297. ObjectId insertId = blobId;
  298. for (int i = path.length - 1; i >= 0; --i) {
  299. TreeFormatter treeFormatter = new TreeFormatter();
  300. treeFormatter.append("goodpath", mode, insertId);
  301. insertId = newObjectInserter.insert(treeFormatter);
  302. mode = FileMode.TREE;
  303. }
  304. newObjectInserter = git.getRepository().newObjectInserter();
  305. CommitBuilder commitBuilder = new CommitBuilder();
  306. commitBuilder.setAuthor(author);
  307. commitBuilder.setCommitter(committer);
  308. commitBuilder.setMessage("foo#1");
  309. commitBuilder.setTreeId(insertId);
  310. ObjectId firstCommitId = newObjectInserter.insert(commitBuilder);
  311. newObjectInserter = git.getRepository().newObjectInserter();
  312. mode = FileMode.REGULAR_FILE;
  313. insertId = blobId;
  314. for (int i = path.length - 1; i >= 0; --i) {
  315. TreeFormatter treeFormatter = new TreeFormatter();
  316. treeFormatter.append(path[i], mode, insertId);
  317. insertId = newObjectInserter.insert(treeFormatter);
  318. mode = FileMode.TREE;
  319. }
  320. // Create another commit
  321. commitBuilder = new CommitBuilder();
  322. commitBuilder.setAuthor(author);
  323. commitBuilder.setCommitter(committer);
  324. commitBuilder.setMessage("foo#2");
  325. commitBuilder.setTreeId(insertId);
  326. commitBuilder.setParentId(firstCommitId);
  327. ObjectId commitId = newObjectInserter.insert(commitBuilder);
  328. RevWalk revWalk = new RevWalk(git.getRepository());
  329. if (!secondCheckout)
  330. git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId))
  331. .setName("refs/heads/master").setCreateBranch(true).call();
  332. try {
  333. if (secondCheckout) {
  334. git.checkout().setStartPoint(revWalk.parseCommit(commitId))
  335. .setName("refs/heads/master").setCreateBranch(true)
  336. .call();
  337. } else {
  338. git.branchCreate().setName("refs/heads/next")
  339. .setStartPoint(commitId.name()).call();
  340. git.checkout().setName("refs/heads/next")
  341. .call();
  342. }
  343. if (!good)
  344. fail("Checkout of Tree " + Arrays.asList(path) + " should fail");
  345. } catch (InvalidPathException e) {
  346. if (good)
  347. throw e;
  348. assertThat(e.getMessage(), startsWith("Invalid path: "));
  349. }
  350. }
  351. }