From ecb792050ac548099d5826c284b56137658df45f Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 26 Sep 2020 23:08:32 +0200 Subject: [PATCH] Sanitized issue proposals Fixes #53 --- .../client/ui/settings/BugReportPane.java | 6 +- .../java/envoy/client/util/IssueUtil.java | 90 ++++++++++++++----- 2 files changed, 71 insertions(+), 25 deletions(-) diff --git a/client/src/main/java/envoy/client/ui/settings/BugReportPane.java b/client/src/main/java/envoy/client/ui/settings/BugReportPane.java index 88b0397..5fab593 100644 --- a/client/src/main/java/envoy/client/ui/settings/BugReportPane.java +++ b/client/src/main/java/envoy/client/ui/settings/BugReportPane.java @@ -7,7 +7,6 @@ import javafx.scene.input.InputEvent; import envoy.client.event.SendEvent; import envoy.client.util.IssueUtil; import envoy.data.User; -import envoy.event.IssueProposal; import dev.kske.eventbus.EventBus; @@ -70,8 +69,9 @@ public final class BugReportPane extends OnlyIfOnlineSettingsPane { submitReportButton.setDisable(true); submitReportButton.setOnAction(e -> { EventBus.getInstance() - .dispatch(new SendEvent(new IssueProposal(titleTextField.getText(), - IssueUtil.sanitizeIssueDescription(errorDetailArea.getText(), showUsernameInBugReport.isSelected() ? user.getName() : null), + .dispatch(new SendEvent(IssueUtil.createIssueProposal(titleTextField.getText(), + errorDetailArea.getText(), + showUsernameInBugReport.isSelected() ? user.getName() : null, true))); }); getChildren().add(submitReportButton); diff --git a/client/src/main/java/envoy/client/util/IssueUtil.java b/client/src/main/java/envoy/client/util/IssueUtil.java index 6c6e165..08fd1f8 100644 --- a/client/src/main/java/envoy/client/util/IssueUtil.java +++ b/client/src/main/java/envoy/client/util/IssueUtil.java @@ -1,40 +1,86 @@ package envoy.client.util; +import java.util.regex.Pattern; + +import envoy.event.IssueProposal; + /** * Provides methods to handle outgoing issues. - *

- * Project: client
- * File: IssueUtil.java
- * Created: 20.08.2020
* * @author Leon Hofmeister + * @author Kai S. K. Engelbart * @since Envoy Client v0.2-beta */ public final class IssueUtil { - /** - * - * @since Envoy Client v0.2-beta - */ + private static final Pattern removeBackslashes = Pattern.compile("\\\\"); + private static final Pattern escapeQuotes = Pattern.compile("\""); + private IssueUtil() {} /** - * Performs actions to ensure the description of an issue will be displayed as - * intended by the user. + * Creates a new {@code IssueProposal} from the given data. * - * @param rawDescription the description to sanitize - * @param username the user who submitted the issue. Should be - * {@code null} if he does not want to be named. - * @return the sanitized description + * @param title the proposed title of the issue + * @param description the proposed description of the issue + * @param username the user who submitted the issue. Should be + * {@code null} if he does not want to be named. * @param + * isBug + * @param isBug whether this issue is a bug or a feature + * @return a sanitized IssueProposal that should not fail to be sent * @since Envoy Client v0.2-beta */ - public static String sanitizeIssueDescription(String rawDescription, String username) { - // Appending the submitter name, if this option was enabled - rawDescription += username != null - ? (rawDescription.endsWith("\n") || rawDescription.endsWith("
") ? "" : "
") + String.format("Submitted by user %s.", username) - : ""; - // Markdown does not support "normal" line breaks. It uses "
" - rawDescription = rawDescription.replaceAll(System.getProperty("line.separator", "\r?\n"), "
"); - return rawDescription; + public static IssueProposal createIssueProposal(String title, String description, String username, boolean isBug) { + title = sanitizeIssueTitle(title); + description = sanitizeIssueDescription(description, username); + return new IssueProposal(title, description, isBug); + + } + + /** + * Escapes quotes and removes backslashes for a suggested issue title. + * + * @param title the title to sanitize + * @return the sanitized title + * @since Envoy Client v0.2-beta + */ + private static String sanitizeIssueTitle(String title) { + + // Remove ALL backslashes as they are only error prone + title = removeBackslashes.matcher(title).replaceAll(""); + + // Escape quotes + title = escapeQuotes.matcher(title).replaceAll("\\\\\""); + return title; + } + + /** + * Normalizes line breaks,
+ * removes all backslashes,
+ * escapes quotes and
+ * appends the user name to the issue description if requested. + * + * @param description the description to sanitize + * @param username the user who submitted the issue. Should be + * {@code null} if he does not want to be named. + * @return the sanitized description + * @since Envoy Client v0.2-beta + * @apiNote the String returned might not be sanitized in case multiple + * backslashes are preceding a quote. + */ + public static String sanitizeIssueDescription(String description, String username) { + + // Trim and replace line breaks by
tags + description = description.trim().replaceAll(System.getProperty("line.separator"), "
"); + + // Append user name if requested + if (username != null) description += String.format("
Submitted by user %s.", username); + + // Remove ALL backslashes as they are only error prone + description = removeBackslashes.matcher(description).replaceAll(""); + + // Escape all quotes to avoid prematurely ending the string + description = escapeQuotes.matcher(description).replaceAll("\\\\\""); + return description; } }