Skip to content

Commit ca9a887

Browse files
tulinkryVladimir Kotal
authored andcommitted
filtering the xref of '/' based on authorization (#1618)
fixes #1610
1 parent 6fb3bf4 commit ca9a887

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

src/org/opensolaris/opengrok/web/PageConfig.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ public String canProcess() {
378378
* @return an empty list, if the resource does not exist, is not a directory
379379
* or an error occurred when reading it, otherwise a list of filenames in
380380
* that directory, sorted alphabetically
381+
*
382+
* <p>
383+
* For the root directory (/xref/) an authorization is performed for each
384+
* project in case that projects are used.</p>
385+
*
381386
* @see #getResourceFile()
382387
* @see #isDir()
383388
*/
@@ -391,8 +396,29 @@ public List<String> getResourceFileList() {
391396
dirFileList = Collections.emptyList();
392397
} else {
393398
Arrays.sort(files, String.CASE_INSENSITIVE_ORDER);
394-
dirFileList
395-
= Collections.unmodifiableList(Arrays.asList(files));
399+
List<String> listOfFiles = Arrays.asList(files);
400+
if (env.hasProjects() && getPath().isEmpty()) {
401+
/**
402+
* This denotes the source root directory, we need to filter
403+
* projects which aren't allowed by the authorization
404+
* because otherwise the main xref page expose the names of
405+
* all projects in OpenGrok even those which aren't allowed
406+
* for the particular user. E. g. remove all which aren't
407+
* among the filtered set of projects.
408+
*
409+
* The authorization check is made in
410+
* {@link ProjectHelper#getAllProjects()} as a part of all
411+
* projects filtering.
412+
*/
413+
List<String> modifiableListOfFiles = new ArrayList<>(listOfFiles);
414+
modifiableListOfFiles.removeIf((t) -> {
415+
return !getProjectHelper().getAllProjects().stream().anyMatch((p) -> {
416+
return p.getName().equalsIgnoreCase(t);
417+
});
418+
});
419+
return dirFileList = Collections.unmodifiableList(modifiableListOfFiles);
420+
}
421+
dirFileList = Collections.unmodifiableList(listOfFiles);
396422
}
397423
}
398424
return dirFileList;

test/org/opensolaris/opengrok/web/PageConfigTest.java

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
20+
/*
2121
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.web;
@@ -26,15 +26,23 @@
2626
import java.io.FileNotFoundException;
2727
import java.io.IOException;
2828
import java.nio.file.Files;
29+
import java.util.ArrayList;
2930
import java.util.Arrays;
31+
import java.util.List;
32+
import java.util.Map;
3033
import javax.servlet.http.HttpServletRequest;
3134
import org.junit.AfterClass;
3235
import org.junit.BeforeClass;
3336
import org.junit.Rule;
3437
import org.junit.Test;
38+
import org.opensolaris.opengrok.authorization.AuthControlFlag;
39+
import org.opensolaris.opengrok.authorization.AuthorizationFramework;
40+
import org.opensolaris.opengrok.authorization.AuthorizationPlugin;
41+
import org.opensolaris.opengrok.authorization.TestPlugin;
3542
import org.opensolaris.opengrok.condition.ConditionalRun;
3643
import org.opensolaris.opengrok.condition.ConditionalRunRule;
3744
import org.opensolaris.opengrok.condition.RepositoryInstalled;
45+
import org.opensolaris.opengrok.configuration.Project;
3846
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
3947
import org.opensolaris.opengrok.history.Annotation;
4048
import org.opensolaris.opengrok.history.HistoryGuru;
@@ -135,6 +143,73 @@ public void canProcessXref() {
135143
assertCanProcess(null, "/source", "/xref", "/mercurial/xyz/");
136144
}
137145

146+
/**
147+
* Testing the root of /xref for authorization filtering.
148+
*/
149+
@Test
150+
public void testGetResourceFileList() {
151+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
152+
153+
// backup original values
154+
String oldSourceRootPath = env.getSourceRootPath();
155+
AuthorizationFramework oldAuthorizationFramework = env.getAuthorizationFramework();
156+
Map<String, Project> oldProjects = env.getProjects();
157+
158+
// set up the source root directory containing some projects
159+
env.setSourceRoot(repository.getSourceRoot());
160+
161+
// enable projects
162+
for (String file : new File(repository.getSourceRoot()).list()) {
163+
env.getProjects().put(file, new Project(file));
164+
}
165+
166+
HttpServletRequest req = createRequest("/source", "/xref", "");
167+
PageConfig cfg = PageConfig.get(req);
168+
List<String> allFiles = new ArrayList<>(cfg.getResourceFileList());
169+
170+
/**
171+
* Check if there are some files (the "5" here is just a sufficient
172+
* value for now which won't break any future repository tests) without
173+
* any authorization.
174+
*/
175+
assertTrue(allFiles.size() > 5);
176+
assertTrue(allFiles.contains("git"));
177+
assertTrue(allFiles.contains("mercurial"));
178+
179+
/**
180+
* Now set up the same projects with authorization plugin enabling only
181+
* some of them.
182+
* <pre>
183+
* - disabling "git"
184+
* - disabling "mercurial"
185+
* </pre>
186+
*/
187+
env.setAuthorizationFramework(new AuthorizationFramework(null));
188+
env.getAuthorizationFramework().getStack()
189+
.add(new AuthorizationPlugin(AuthControlFlag.REQUIRED, new TestPlugin() {
190+
@Override
191+
public boolean isAllowed(HttpServletRequest request, Project project) {
192+
return !project.getName().startsWith("git")
193+
&& !project.getName().startsWith("mercurial");
194+
}
195+
}));
196+
197+
req = createRequest("/source", "/xref", "");
198+
cfg = PageConfig.get(req);
199+
List<String> filteredFiles = new ArrayList<>(cfg.getResourceFileList());
200+
// list subtraction - retains only disabled files
201+
allFiles.removeAll(filteredFiles);
202+
203+
assertEquals(2, allFiles.size());
204+
assertTrue(allFiles.contains("git"));
205+
assertTrue(allFiles.contains("mercurial"));
206+
207+
// restore original values
208+
env.setAuthorizationFramework(oldAuthorizationFramework);
209+
env.setSourceRoot(oldSourceRootPath);
210+
env.setProjects(oldProjects);
211+
}
212+
138213
@Test
139214
public void testGetIntParam() {
140215
String[] attrs = {"a", "b", "c", "d", "e", "f", "g", "h"};
@@ -193,7 +268,6 @@ public String getParameter(String name) {
193268
}
194269
}
195270

196-
197271
@Test
198272
@ConditionalRun(condition = RepositoryInstalled.GitInstalled.class)
199273
public void testGetAnnotation() {

0 commit comments

Comments
 (0)