Skip to content

Commit 27968d6

Browse files
committed
launder server name
1 parent 7b8ed55 commit 27968d6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Laundromat.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public static String launderInput(String value) {
5151
return replaceAll(value, ESC_N_R_T_F, "_");
5252
}
5353

54+
/**
55+
* Sanitize {@code value} where it will be used in subsequent OpenGrok
56+
* (non-logging) processing. Also allows for IPv6 address URIs with port number.
57+
* @return {@code null} if null or else {@code value} with invalid characters removed and leading dashes stripped
58+
*/
59+
public static String launderServerName(String value) {
60+
return replaceAll(value, "(^\\-*)|[^A-Za-z0-9\\-\\.: \\[\\]]", "");
61+
}
62+
5463
/**
5564
* Sanitize {@code value} where it will be used in subsequent OpenGrok
5665
* (non-logging) processing.

opengrok-indexer/src/test/java/org/opengrok/indexer/web/LaundromatTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@
1919

2020
/*
2121
* Copyright (c) 2020, Chris Fraire <[email protected]>.
22+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
2223
*/
2324
package org.opengrok.indexer.web;
2425

26+
import org.apache.commons.lang3.tuple.Pair;
2527
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.MethodSource;
2630

2731
import java.util.Arrays;
2832
import java.util.HashMap;
2933
import java.util.Map;
34+
import java.util.stream.Stream;
3035

3136
import static org.junit.jupiter.api.Assertions.assertEquals;
3237

@@ -54,6 +59,17 @@ void launderLog() {
5459
assertEquals(TEST_CONTENT_LOG_LAUNDRY, laundry);
5560
}
5661

62+
private static Stream<Pair<String, String>> getParamsForTestLaunderServerName() {
63+
return Stream.of(Pair.of("foo.example.com", Laundromat.launderServerName("--foo.example\n.com?=")),
64+
Pair.of("[2001:db8::1]:8080", Laundromat.launderServerName("[2001:db8::1]:8080")));
65+
}
66+
67+
@ParameterizedTest
68+
@MethodSource("getParamsForTestLaunderServerName")
69+
void testLaunderServerName(Pair<String, String> param) {
70+
assertEquals(param.getLeft(), param.getRight());
71+
}
72+
5773
@Test
5874
void launderLogMap() {
5975
HashMap<String, String[]> testMap = new HashMap<>();

opengrok-web/src/main/java/org/opengrok/web/PageConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ public String getServerName() {
14831483
if (env.getServerName() != null) {
14841484
return env.getServerName();
14851485
} else {
1486-
return req.getServerName();
1486+
return Laundromat.launderServerName(req.getServerName());
14871487
}
14881488
}
14891489

0 commit comments

Comments
 (0)