1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.api;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48
49 import java.io.File;
50 import java.io.FilePermission;
51 import java.io.IOException;
52 import java.lang.reflect.ReflectPermission;
53 import java.nio.file.Files;
54 import java.security.Permission;
55 import java.security.SecurityPermission;
56 import java.util.ArrayList;
57 import java.util.List;
58 import java.util.PropertyPermission;
59 import java.util.logging.LoggingPermission;
60
61 import javax.security.auth.AuthPermission;
62
63 import org.eclipse.jgit.api.errors.GitAPIException;
64 import org.eclipse.jgit.junit.JGitTestUtil;
65 import org.eclipse.jgit.junit.MockSystemReader;
66 import org.eclipse.jgit.junit.SeparateClassloaderTestRunner;
67 import org.eclipse.jgit.revwalk.RevCommit;
68 import org.eclipse.jgit.treewalk.TreeWalk;
69 import org.eclipse.jgit.util.FileUtils;
70 import org.eclipse.jgit.util.SystemReader;
71 import org.junit.After;
72 import org.junit.Before;
73 import org.junit.Test;
74 import org.junit.runner.RunWith;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 @RunWith(SeparateClassloaderTestRunner.class)
107 public class SecurityManagerTest {
108 private File root;
109
110 private SecurityManager originalSecurityManager;
111
112 private List<Permission> permissions = new ArrayList<>();
113
114 @Before
115 public void setUp() throws Exception {
116
117 SystemReader.setInstance(new MockSystemReader());
118 root = Files.createTempDirectory("jgit-security").toFile();
119
120
121 permissions.add(new RuntimePermission("*"));
122 permissions.add(new SecurityPermission("*"));
123 permissions.add(new AuthPermission("*"));
124 permissions.add(new ReflectPermission("*"));
125 permissions.add(new PropertyPermission("*", "read,write"));
126 permissions.add(new LoggingPermission("control", null));
127
128 permissions.add(new FilePermission(
129 System.getProperty("java.home") + "/-", "read"));
130
131 String tempDir = System.getProperty("java.io.tmpdir");
132 permissions.add(new FilePermission(tempDir, "read,write,delete"));
133 permissions
134 .add(new FilePermission(tempDir + "/-", "read,write,delete"));
135
136
137 String classPath = System.getProperty("java.class.path");
138 if (classPath != null) {
139 for (String path : classPath.split(File.pathSeparator)) {
140 permissions.add(new FilePermission(path, "read"));
141 }
142 }
143
144 String jgitSourcesRoot = new File(System.getProperty("user.dir"))
145 .getParent();
146 permissions.add(new FilePermission(jgitSourcesRoot + "/-", "read"));
147
148
149
150 permissions.add(new FilePermission(root.getPath() + "/-",
151 "read,write,delete,execute"));
152
153
154 originalSecurityManager = System.getSecurityManager();
155 System.setSecurityManager(new SecurityManager() {
156
157 @Override
158 public void checkPermission(Permission requested) {
159 for (Permission permission : permissions) {
160 if (permission.implies(requested)) {
161 return;
162 }
163 }
164
165 super.checkPermission(requested);
166 }
167 });
168 }
169
170 @After
171 public void tearDown() throws Exception {
172 System.setSecurityManager(originalSecurityManager);
173
174
175
176
177 FileUtils.delete(root, FileUtils.RECURSIVE | FileUtils.RETRY);
178 }
179
180 @Test
181 public void testInitAndClone() throws IOException, GitAPIException {
182 File remote = new File(root, "remote");
183 File local = new File(root, "local");
184
185 try (Git git = Git.init().setDirectory(remote).call()) {
186 JGitTestUtil.write(new File(remote, "hello.txt"), "Hello world!");
187 git.add().addFilepattern(".").call();
188 git.commit().setMessage("Initial commit").call();
189 }
190
191 try (Git git = Git.cloneRepository().setURI(remote.toURI().toString())
192 .setDirectory(local).call()) {
193 assertTrue(new File(local, ".git").exists());
194
195 JGitTestUtil.write(new File(local, "hi.txt"), "Hi!");
196 git.add().addFilepattern(".").call();
197 RevCommit commit1 = git.commit().setMessage("Commit on local repo")
198 .call();
199 assertEquals("Commit on local repo", commit1.getFullMessage());
200 assertNotNull(TreeWalk.forPath(git.getRepository(), "hello.txt",
201 commit1.getTree()));
202 }
203
204 }
205
206 }