|
25 | 25 | import org.jetbrains.annotations.NotNull; |
26 | 26 | import org.jetbrains.annotations.Nullable; |
27 | 27 |
|
28 | | -import java.nio.file.Path; |
29 | 28 | import java.util.ArrayList; |
30 | 29 | import java.util.Arrays; |
31 | 30 | import java.util.Collection; |
|
40 | 39 | */ |
41 | 40 | public class Laundromat { |
42 | 41 |
|
43 | | - private static final String ESC_N_R_T_F = "[\\n\\r\\t\\f]"; |
| 42 | + private static final String ESC_N_R_T_F = "[\\n\\r\\t\\f\\u0000]"; |
44 | 43 | private static final String ESG_N_R_T_F_1_N = ESC_N_R_T_F + "+"; |
45 | 44 |
|
46 | 45 | /** |
@@ -75,21 +74,22 @@ public static String launderRevision(String value) { |
75 | 74 |
|
76 | 75 | /** |
77 | 76 | * Sanitize {@code value} where it will be used in subsequent OpenGrok |
78 | | - * (non-logging) processing. The value is assumed to represent a file path, |
79 | | - * not necessarily existent on the file system. |
80 | | - * @return {@code null} if null or else {@code value} with path traversal |
81 | | - * path components {@code /../} removed. |
| 77 | + * (non-logging) processing. The value is assumed to represent URI path, |
| 78 | + * not necessarily existent on the file system. Further, it assumes that the URI path |
| 79 | + * is already decoded, e.g. {@code %2e%2e} turned into {@code ..}. |
| 80 | + * @return {@code value} with path traversal path components {@code /../} |
| 81 | + * and null characters replaced with {@code _}. |
82 | 82 | */ |
83 | | - public static String launderPath(@NotNull String value) { |
84 | | - Path path = Path.of(Laundromat.launderInput(value)); |
| 83 | + public static String launderUriPath(@NotNull String value) { |
85 | 84 | List<String> pathElements = new ArrayList<>(); |
86 | | - for (int i = 0; i < path.getNameCount(); i++) { |
87 | | - if (path.getName(i).toString().equals("..")) { |
| 85 | + String uriPath = Laundromat.launderInput(value); |
| 86 | + for (String pathElement : uriPath.split("/")) { |
| 87 | + if (pathElement.isEmpty() || pathElement.equals("..")) { |
88 | 88 | continue; |
89 | 89 | } |
90 | | - pathElements.add(path.getName(i).toString()); |
| 90 | + pathElements.add(pathElement); |
91 | 91 | } |
92 | | - return (path.isAbsolute() ? "/" : "") + String.join("/", pathElements); |
| 92 | + return (uriPath.startsWith("/") ? "/" : "") + String.join("/", pathElements); |
93 | 93 | } |
94 | 94 |
|
95 | 95 | /** |
|
0 commit comments