From 72b40613b8a1d92f67b64b7ae6922f046029c8a4 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 17 Aug 2024 21:37:45 +0200 Subject: [PATCH] Escape crash message and stacktrace placeholders This prevents warnings about '' "tags" found in call stacks, or because of unescaped messages. --- .../internal/placeholders/crashhandler/Stacktrace.java | 5 ++++- .../placeholders/crashhandler/StacktraceAll.java | 2 +- .../sosengine/base/logging/CrashHandler.java | 10 ++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/crashhandler/Stacktrace.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/crashhandler/Stacktrace.java index 86900b26..49a91bd3 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/crashhandler/Stacktrace.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/crashhandler/Stacktrace.java @@ -99,6 +99,9 @@ public final class Stacktrace implements Placeholder { if (throwable instanceof InvocationTargetException invocationTargetException) getFullStackTrace(invocationTargetException.getTargetException(), stacktrace); - return stacktrace.toString(); // Return stack trace + // Return stack trace + return stacktrace + .toString() + .replace("<", "\\<"); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/crashhandler/StacktraceAll.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/crashhandler/StacktraceAll.java index 408301de..f179f692 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/crashhandler/StacktraceAll.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/crashhandler/StacktraceAll.java @@ -66,6 +66,6 @@ public final class StacktraceAll implements Placeholder { .append(Miscellaneous.stringifyStackTrace(stacktraces.get(thread), false)); } - return text.replace("%stacktrace_all%", output.toString()); + return text.replace("%stacktrace_all%", output.toString().replace("<", "\\<")); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/logging/CrashHandler.java b/base/src/main/java/de/staropensource/sosengine/base/logging/CrashHandler.java index 096ea33e..5f1c6b7b 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/logging/CrashHandler.java +++ b/base/src/main/java/de/staropensource/sosengine/base/logging/CrashHandler.java @@ -117,12 +117,18 @@ Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party su * @since v1-alpha0 */ public static synchronized void handleCrash(@NotNull Class issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @Nullable Throwable throwable, boolean throwableHandled) { - Engine.getInstance().setState(EngineState.CRASHED); + Engine.getInstance().setState(EngineState.CRASHED); // Update engine state + // Prevent throwable handled warning if set to true but no throwable has been supplied if (throwable == null) throwableHandled = false; - // This is so simple we don't need the PlaceholderEngine to do it for us + // Escape message + message = message + .replace("\\", "\\\\") + .replace("<", "\\<"); + + // Replace %content% and %handled% String base = crashTemplate .replace("%content%", processCrashContent()) .replace("%handled%", throwableHandled ? "!!! This throwable is declared as handled and has been passed down the execution chain !!!" : "");