|
|
|
@ -338,68 +338,69 @@ public class DirCacheCheckoutMaliciousPathTest extends RepositoryTestCase {
|
|
|
|
|
*/ |
|
|
|
|
private void testMaliciousPath(boolean good, boolean secondCheckout, |
|
|
|
|
String... path) throws GitAPIException, IOException { |
|
|
|
|
Git git = new Git(db); |
|
|
|
|
ObjectInserter newObjectInserter; |
|
|
|
|
newObjectInserter = git.getRepository().newObjectInserter(); |
|
|
|
|
ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB, |
|
|
|
|
"data".getBytes()); |
|
|
|
|
newObjectInserter = git.getRepository().newObjectInserter(); |
|
|
|
|
FileMode mode = FileMode.REGULAR_FILE; |
|
|
|
|
ObjectId insertId = blobId; |
|
|
|
|
for (int i = path.length - 1; i >= 0; --i) { |
|
|
|
|
TreeFormatter treeFormatter = new TreeFormatter(); |
|
|
|
|
treeFormatter.append("goodpath", mode, insertId); |
|
|
|
|
insertId = newObjectInserter.insert(treeFormatter); |
|
|
|
|
mode = FileMode.TREE; |
|
|
|
|
} |
|
|
|
|
newObjectInserter = git.getRepository().newObjectInserter(); |
|
|
|
|
CommitBuilder commitBuilder = new CommitBuilder(); |
|
|
|
|
commitBuilder.setAuthor(author); |
|
|
|
|
commitBuilder.setCommitter(committer); |
|
|
|
|
commitBuilder.setMessage("foo#1"); |
|
|
|
|
commitBuilder.setTreeId(insertId); |
|
|
|
|
ObjectId firstCommitId = newObjectInserter.insert(commitBuilder); |
|
|
|
|
|
|
|
|
|
newObjectInserter = git.getRepository().newObjectInserter(); |
|
|
|
|
mode = FileMode.REGULAR_FILE; |
|
|
|
|
insertId = blobId; |
|
|
|
|
for (int i = path.length - 1; i >= 0; --i) { |
|
|
|
|
TreeFormatter treeFormatter = new TreeFormatter(); |
|
|
|
|
treeFormatter.append(path[i], mode, insertId); |
|
|
|
|
insertId = newObjectInserter.insert(treeFormatter); |
|
|
|
|
mode = FileMode.TREE; |
|
|
|
|
} |
|
|
|
|
try (Git git = new Git(db); |
|
|
|
|
RevWalk revWalk = new RevWalk(git.getRepository())) { |
|
|
|
|
ObjectInserter newObjectInserter; |
|
|
|
|
newObjectInserter = git.getRepository().newObjectInserter(); |
|
|
|
|
ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB, |
|
|
|
|
"data".getBytes()); |
|
|
|
|
newObjectInserter = git.getRepository().newObjectInserter(); |
|
|
|
|
FileMode mode = FileMode.REGULAR_FILE; |
|
|
|
|
ObjectId insertId = blobId; |
|
|
|
|
for (int i = path.length - 1; i >= 0; --i) { |
|
|
|
|
TreeFormatter treeFormatter = new TreeFormatter(); |
|
|
|
|
treeFormatter.append("goodpath", mode, insertId); |
|
|
|
|
insertId = newObjectInserter.insert(treeFormatter); |
|
|
|
|
mode = FileMode.TREE; |
|
|
|
|
} |
|
|
|
|
newObjectInserter = git.getRepository().newObjectInserter(); |
|
|
|
|
CommitBuilder commitBuilder = new CommitBuilder(); |
|
|
|
|
commitBuilder.setAuthor(author); |
|
|
|
|
commitBuilder.setCommitter(committer); |
|
|
|
|
commitBuilder.setMessage("foo#1"); |
|
|
|
|
commitBuilder.setTreeId(insertId); |
|
|
|
|
ObjectId firstCommitId = newObjectInserter.insert(commitBuilder); |
|
|
|
|
|
|
|
|
|
newObjectInserter = git.getRepository().newObjectInserter(); |
|
|
|
|
mode = FileMode.REGULAR_FILE; |
|
|
|
|
insertId = blobId; |
|
|
|
|
for (int i = path.length - 1; i >= 0; --i) { |
|
|
|
|
TreeFormatter treeFormatter = new TreeFormatter(); |
|
|
|
|
treeFormatter.append(path[i], mode, insertId); |
|
|
|
|
insertId = newObjectInserter.insert(treeFormatter); |
|
|
|
|
mode = FileMode.TREE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create another commit
|
|
|
|
|
commitBuilder = new CommitBuilder(); |
|
|
|
|
commitBuilder.setAuthor(author); |
|
|
|
|
commitBuilder.setCommitter(committer); |
|
|
|
|
commitBuilder.setMessage("foo#2"); |
|
|
|
|
commitBuilder.setTreeId(insertId); |
|
|
|
|
commitBuilder.setParentId(firstCommitId); |
|
|
|
|
ObjectId commitId = newObjectInserter.insert(commitBuilder); |
|
|
|
|
|
|
|
|
|
RevWalk revWalk = new RevWalk(git.getRepository()); |
|
|
|
|
if (!secondCheckout) |
|
|
|
|
git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId)) |
|
|
|
|
.setName("refs/heads/master").setCreateBranch(true).call(); |
|
|
|
|
try { |
|
|
|
|
if (secondCheckout) { |
|
|
|
|
git.checkout().setStartPoint(revWalk.parseCommit(commitId)) |
|
|
|
|
.setName("refs/heads/master").setCreateBranch(true) |
|
|
|
|
.call(); |
|
|
|
|
} else { |
|
|
|
|
git.branchCreate().setName("refs/heads/next") |
|
|
|
|
.setStartPoint(commitId.name()).call(); |
|
|
|
|
git.checkout().setName("refs/heads/next") |
|
|
|
|
.call(); |
|
|
|
|
// Create another commit
|
|
|
|
|
commitBuilder = new CommitBuilder(); |
|
|
|
|
commitBuilder.setAuthor(author); |
|
|
|
|
commitBuilder.setCommitter(committer); |
|
|
|
|
commitBuilder.setMessage("foo#2"); |
|
|
|
|
commitBuilder.setTreeId(insertId); |
|
|
|
|
commitBuilder.setParentId(firstCommitId); |
|
|
|
|
ObjectId commitId = newObjectInserter.insert(commitBuilder); |
|
|
|
|
|
|
|
|
|
if (!secondCheckout) |
|
|
|
|
git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId)) |
|
|
|
|
.setName("refs/heads/master").setCreateBranch(true).call(); |
|
|
|
|
try { |
|
|
|
|
if (secondCheckout) { |
|
|
|
|
git.checkout().setStartPoint(revWalk.parseCommit(commitId)) |
|
|
|
|
.setName("refs/heads/master").setCreateBranch(true) |
|
|
|
|
.call(); |
|
|
|
|
} else { |
|
|
|
|
git.branchCreate().setName("refs/heads/next") |
|
|
|
|
.setStartPoint(commitId.name()).call(); |
|
|
|
|
git.checkout().setName("refs/heads/next") |
|
|
|
|
.call(); |
|
|
|
|
} |
|
|
|
|
if (!good) |
|
|
|
|
fail("Checkout of Tree " + Arrays.asList(path) + " should fail"); |
|
|
|
|
} catch (InvalidPathException e) { |
|
|
|
|
if (good) |
|
|
|
|
throw e; |
|
|
|
|
assertTrue(e.getMessage().startsWith("Invalid path")); |
|
|
|
|
} |
|
|
|
|
if (!good) |
|
|
|
|
fail("Checkout of Tree " + Arrays.asList(path) + " should fail"); |
|
|
|
|
} catch (InvalidPathException e) { |
|
|
|
|
if (good) |
|
|
|
|
throw e; |
|
|
|
|
assertTrue(e.getMessage().startsWith("Invalid path")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|