Skip to content

Commit ee1fba5

Browse files
Vladimir Kotalahornace
authored andcommitted
strip whitespace from whitelist entries
1 parent 725ab9f commit ee1fba5

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

plugins/src/main/java/opengrok/auth/plugin/UserWhiteListPlugin.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package opengrok.auth.plugin;
@@ -70,7 +70,7 @@ public void load(Map<String, Object> parameters) {
7070

7171
// Load whitelist from file to memory.
7272
try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
73-
stream.forEach(whitelist::add);
73+
stream.map(String::strip).forEach(whitelist::add);
7474
} catch (IOException e) {
7575
throw new IllegalArgumentException(String.format("Unable to read the file \"%s\"", filePath), e);
7676
}
@@ -79,6 +79,11 @@ public void load(Map<String, Object> parameters) {
7979
new Object[]{filePath, whitelist.size(), fieldName});
8080
}
8181

82+
// for testing
83+
Set<String> getWhitelist() {
84+
return whitelist;
85+
}
86+
8287
@Override
8388
public void unload() {
8489
whitelist.clear();
@@ -88,7 +93,7 @@ private boolean checkWhitelist(HttpServletRequest request) {
8893
User user;
8994
String attrName = UserPlugin.REQUEST_ATTR;
9095
if ((user = (User) request.getAttribute(attrName)) == null) {
91-
LOGGER.log(Level.WARNING, "cannot get {0} attribute", attrName);
96+
LOGGER.log(Level.WARNING, "cannot get {0} attribute from {1}", attrName);
9297
return false;
9398
}
9499

plugins/src/test/java/opengrok/auth/plugin/UserWhiteListPluginTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,20 @@
3636
import java.io.BufferedWriter;
3737
import java.io.File;
3838
import java.io.FileOutputStream;
39+
import java.io.IOException;
3940
import java.io.OutputStreamWriter;
41+
import java.io.PrintWriter;
4042
import java.nio.charset.StandardCharsets;
43+
import java.nio.file.Files;
4144
import java.util.Arrays;
4245
import java.util.Collection;
4346
import java.util.HashMap;
4447
import java.util.Map;
48+
import java.util.Set;
49+
import java.util.stream.Collectors;
50+
import java.util.stream.Stream;
4551

52+
import static org.junit.jupiter.api.Assertions.assertEquals;
4653
import static org.junit.jupiter.api.Assertions.assertFalse;
4754
import static org.junit.jupiter.api.Assertions.assertNotNull;
4855
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -164,6 +171,35 @@ public void shouldThrowOnLoadIfNoFileSpecified(String param) {
164171
"caughtException should mention 'Missing parameter'");
165172
}
166173

174+
@ParameterizedTest
175+
@MethodSource("parameters")
176+
public void shouldStripWhitespaceFromWhitelists(String param) throws IOException {
177+
plugin = new UserWhiteListPlugin();
178+
HashMap<String, Object> pluginParameters = new HashMap<>();
179+
pluginParameters.put(UserWhiteListPlugin.FIELD_PARAM, param);
180+
Set<String> entries = Set.of("Moomin", " Fillyjonk", " Snuffkin", "Snork Maiden ", "Groke ");
181+
182+
File tmpFile = File.createTempFile("UserWhiteListPluginTestId", "txt");
183+
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(
184+
new FileOutputStream(tmpFile), StandardCharsets.UTF_8))) {
185+
for (String entity : entries) {
186+
writer.println(entity);
187+
}
188+
}
189+
190+
// Make sure there as some entries with trailing spaces in the file.
191+
Stream<String> stream = Files.lines(tmpFile.toPath());
192+
assertTrue(stream.filter(s -> s.startsWith(" ") || s.endsWith(" ")).
193+
collect(Collectors.toSet()).size() > 0);
194+
195+
pluginParameters.put(UserWhiteListPlugin.FILE_PARAM, tmpFile.toString());
196+
plugin.load(pluginParameters);
197+
tmpFile.delete();
198+
199+
Set<String> expected = entries.stream().map(String::trim).collect(Collectors.toSet());
200+
assertEquals(expected, plugin.getWhitelist());
201+
}
202+
167203
@ParameterizedTest
168204
@MethodSource("parameters")
169205
public void shouldUnload(String param) {

0 commit comments

Comments
 (0)