Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

Recent projects: a path holding a tab or a line break stays in the list

scottsturrock8e164d5710c0
diff --git a/src/opensuite/ui/RecentProjects.java b/src/opensuite/ui/RecentProjects.java
index d19c156..1999cd2 100644
--- a/src/opensuite/ui/RecentProjects.java
+++ b/src/opensuite/ui/RecentProjects.java
@@ -11,6 +11,8 @@ import java.util.List;
* The list of recently used project files, shown in the startup screen's "Open
* existing project" table. Persisted as a small tab-separated file under
* {@code ~/.opensuite}, most-recent first, so reopening a project is one click.
+ * A backslash, tab, carriage return or newline inside a name or a path is
+ * written escaped, so a path holding one stays one field on one line.
*/
public final class RecentProjects {
@@ -34,7 +36,7 @@ public final class RecentProjects {
String[] f = line.split("\t", 3);
if (f.length < 3) continue;
try {
- File file = new File(f[1]);
+ File file = new File(unescape(f[1]));
// Skip projects whose file has been deleted or moved. The
// name shown is always the file's own name, whatever an
// older entry recorded.
@@ -63,14 +65,46 @@ public final class RecentProjects {
Files.createDirectories(p.getParent());
StringBuilder b = new StringBuilder();
for (Entry e : list) {
- b.append(e.name()).append('\t')
- .append(e.file().getAbsolutePath()).append('\t')
+ b.append(escape(e.name())).append('\t')
+ .append(escape(e.file().getAbsolutePath())).append('\t')
.append(e.lastAccessed()).append('\n');
}
Files.writeString(p, b.toString(), StandardCharsets.UTF_8);
} catch (Exception ignored) {}
}
+ static String escape(String s) {
+ StringBuilder b = new StringBuilder(s.length() + 8);
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ switch (c) {
+ case '\\' -> b.append("\\\\");
+ case '\t' -> b.append("\\t");
+ case '\r' -> b.append("\\r");
+ case '\n' -> b.append("\\n");
+ default -> b.append(c);
+ }
+ }
+ return b.toString();
+ }
+
+ static String unescape(String s) {
+ StringBuilder b = new StringBuilder(s.length());
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ if (c != '\\' || i + 1 == s.length()) { b.append(c); continue; }
+ char n = s.charAt(++i);
+ switch (n) {
+ case '\\' -> b.append('\\');
+ case 't' -> b.append('\t');
+ case 'r' -> b.append('\r');
+ case 'n' -> b.append('\n');
+ default -> b.append(c).append(n);
+ }
+ }
+ return b.toString();
+ }
+
public static String baseName(File f) {
String n = f.getName();
int dot = n.lastIndexOf('.');
diff --git a/tests/RunTests.java b/tests/RunTests.java
index be19d5b..ef9d31e 100644
--- a/tests/RunTests.java
+++ b/tests/RunTests.java
@@ -38,6 +38,7 @@ public final class RunTests {
new opensuite.decoder.DecoderTransformsTest().run();
new opensuite.ui.MessageEditorJsonTest().run();
new opensuite.ui.EditorMenuTest().run();
+ new opensuite.ui.RecentProjectsTest().run();
new opensuite.core.HttpMessageTitleTest().run();
new opensuite.csrf.CsrfPocTest().run();
new opensuite.sequencer.SequencerStatsTest().run();
diff --git a/tests/opensuite/ui/RecentProjectsTest.java b/tests/opensuite/ui/RecentProjectsTest.java
new file mode 100644
index 0000000..f9badbf
--- /dev/null
+++ b/tests/opensuite/ui/RecentProjectsTest.java
@@ -0,0 +1,53 @@
+package opensuite.ui;
+
+import opensuite.testkit.Expect;
+import opensuite.testkit.Harness;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+/** The recent projects list keeps a project whose path holds a tab, a newline or a backslash. */
+public final class RecentProjectsTest {
+
+ public void run() {
+ Harness.suite("recent projects");
+
+ Harness.test("escaping a field and reading it back gives the same text", () -> {
+ for (String s : List.of("plain", "a\tb", "a\nb", "a\r\nb", "C:\\temp\\new", "a\\tb", "ends\\", ""))
+ Expect.equal(RecentProjects.unescape(RecentProjects.escape(s)), s, "round trip");
+ Expect.isTrue(!RecentProjects.escape("a\tb\nc\rd").matches("(?s).*[\t\n\r].*"), "no raw tab or line break is written");
+ });
+
+ // Windows does not allow these characters in a file name.
+ if (File.separatorChar == '/') Harness.test("projects whose paths hold a tab and a newline come back from the list", () -> {
+ String home = System.getProperty("user.home");
+ Path tmp = null;
+ try {
+ tmp = Files.createTempDirectory("recent");
+ System.setProperty("user.home", tmp.toString());
+ File tab = tmp.resolve("with\ttab.osuite").toFile(), line = tmp.resolve("with\nline.osuite").toFile(),
+ plain = tmp.resolve("plain.osuite").toFile();
+ for (File f : List.of(tab, line, plain)) Files.writeString(f.toPath(), "x");
+ RecentProjects.record("a", tab);
+ RecentProjects.record("b", line);
+ RecentProjects.record("c", plain);
+ List<RecentProjects.Entry> got = RecentProjects.load();
+ Expect.equal(got.size(), 3, "all three are listed");
+ Expect.equal(got.get(0).file(), plain, "newest first");
+ Expect.equal(got.get(1).file(), line, "the path with a newline");
+ Expect.equal(got.get(2).file(), tab, "the path with a tab");
+ Expect.equal(got.get(2).name(), "with\ttab.osuite", "its name");
+ } catch (java.io.IOException e) {
+ throw new RuntimeException(e);
+ } finally {
+ System.setProperty("user.home", home);
+ if (tmp != null) {
+ try (var w = Files.walk(tmp)) { w.sorted(java.util.Comparator.reverseOrder()).forEach(q -> q.toFile().delete()); }
+ catch (java.io.IOException ignored) { }
+ }
+ }
+ });
+ }
+}