1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2026-01-08 13:19:03 +00:00

Handle more of Windows's weird paths

AFAICT, any sequence of ".[ .]*" (except from "") is equivalent to "."
on Windows. Let's handle that in our path sanitisation code.

Fixes #2151.
This commit is contained in:
Jonathan Coates
2025-12-15 23:46:18 +00:00
parent 60bcb9d4d3
commit f31d8febbf
3 changed files with 15 additions and 17 deletions

View File

@@ -352,7 +352,7 @@ public class FileSystem {
return sanitizePath(path, false);
}
private static final Pattern threeDotsPattern = Pattern.compile("^\\.{3,}$");
private static final Pattern manyDotsPattern = Pattern.compile("^[. ]+$");
// IMPORTANT: Both arrays are sorted by ASCII value.
private static final char[] specialChars = new char[]{ '"', '*', ':', '<', '>', '?', '|' };
@@ -376,27 +376,19 @@ public class FileSystem {
for (var fullPart : Splitter.on('/').split(path)) {
var part = fullPart.strip();
if (part.isEmpty() || part.equals(".") || threeDotsPattern.matcher(part).matches()) {
// . is redundant
// ... and more are treated as .
continue;
}
// Limit part length to 255.
if (part.length() > 255) part = part.substring(0, 255).strip();
if (part.equals("..")) {
// .. can cancel out the last folder entered
if (!outputParts.isEmpty()) {
var top = outputParts.peekLast();
if (!top.equals("..")) {
outputParts.removeLast();
} else {
outputParts.addLast("..");
}
} else {
if (outputParts.isEmpty() || outputParts.peekLast().equals("..")) {
outputParts.addLast("..");
} else {
outputParts.removeLast();
}
} else if (part.length() >= 255) {
// If part length > 255 and it is the last part
outputParts.addLast(part.substring(0, 255).strip());
} else if (part.isEmpty() || (part.startsWith(".") && manyDotsPattern.matcher(part).matches())) {
// Skip empty paths, ".", or any other sequence of "[. ]+" (as this is also treated as "." on Windows).
continue;
} else {
// Anything else we add to the stack
outputParts.addLast(part);

View File

@@ -89,6 +89,7 @@ public class FileSystemTest {
new String[]{ "a/./b", "a/b" },
new String[]{ "a/../b", "b" },
new String[]{ "a/.../b", "a/b" },
new String[]{ "a/. ./b", "a/b" },
new String[]{ " a ", "a" },
new String[]{ "a b c", "a b c" },
};

View File

@@ -156,6 +156,11 @@ describe("The fs library", function()
expect(fs.combine("a", "../../c")):eq("../c")
end)
it("handles weird Windows paths", function()
expect(fs.combine("a", "...")):eq("a")
expect(fs.combine("a", ". .")):eq("a")
end)
it("combines empty paths", function()
expect(fs.combine("a")):eq("a")
expect(fs.combine("a", "")):eq("a")