From 4a0bcf9762a05d043f146ced2a7fe685e6777be2 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 27 Sep 2020 17:02:24 +0200 Subject: [PATCH] Sanitized Issue Proposals (#58) Fixes #53 Co-authored-by: kske Reviewed-on: https://git.kske.dev/zdm/envoy/pulls/58 Reviewed-by: kske Reviewed-by: DieGurke --- .../client/ui/settings/BugReportPane.java | 7 ++-- .../java/envoy/client/util/IssueUtil.java | 35 ----------------- .../main/java/envoy/event/IssueProposal.java | 39 ++++++++++++++++++- 3 files changed, 41 insertions(+), 40 deletions(-) delete mode 100644 client/src/main/java/envoy/client/util/IssueUtil.java 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 1f2e98f..0ac1d83 100644 --- a/client/src/main/java/envoy/client/ui/settings/BugReportPane.java +++ b/client/src/main/java/envoy/client/ui/settings/BugReportPane.java @@ -4,7 +4,6 @@ import javafx.event.EventHandler; import javafx.scene.control.*; import javafx.scene.input.InputEvent; -import envoy.client.util.IssueUtil; import envoy.event.IssueProposal; /** @@ -58,8 +57,10 @@ public final class BugReportPane extends OnlineOnlySettingsPane { // Displaying the submitReportButton submitReportButton.setDisable(true); - submitReportButton.setOnAction(e -> client.send(new IssueProposal(titleTextField.getText(), IssueUtil - .sanitizeIssueDescription(errorDetailArea.getText(), showUsernameInBugReport.isSelected() ? client.getSender().getName() : null), true))); + submitReportButton.setOnAction(e -> { + String title = titleTextField.getText(), description = errorDetailArea.getText(); + client.send(showUsernameInBugReport.isSelected() ? new IssueProposal(title, description, true) : new IssueProposal(title, description, client.getSender().getName(), 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 deleted file mode 100644 index 8933a67..0000000 --- a/client/src/main/java/envoy/client/util/IssueUtil.java +++ /dev/null @@ -1,35 +0,0 @@ -package envoy.client.util; - -/** - * Provides methods to handle outgoing issues. - * - * @author Leon Hofmeister - * @author Kai S. K. Engelbart - * @since Envoy Client v0.2-beta - */ -public final class IssueUtil { - - private IssueUtil() {} - - /** - * Normalizes line breaks 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 - */ - 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); - - return description; - } -} diff --git a/common/src/main/java/envoy/event/IssueProposal.java b/common/src/main/java/envoy/event/IssueProposal.java index 63c54f1..b12b6fd 100644 --- a/common/src/main/java/envoy/event/IssueProposal.java +++ b/common/src/main/java/envoy/event/IssueProposal.java @@ -23,11 +23,46 @@ public final class IssueProposal extends Event { * @since Envoy Common v0.2-beta */ public IssueProposal(String title, String description, boolean isBug) { - super(title); - this.description = description; + super(escape(title)); + this.description = sanitizeDescription(description); bug = isBug; } + /** + * @param title the title of the reported bug + * @param description the description of this bug + * @param user the name of the user creating the issue + * @param isBug determines whether this {@code IssueProposal} is + * supposed to be a + * feature or a bug (true = bug, false = feature) + * @since Envoy Common v0.2-beta + */ + public IssueProposal(String title, String description, String user, boolean isBug) { + super(escape(title)); + this.description = sanitizeDescription(description) + String.format("
Submitted by user %s.", user); + bug = isBug; + } + + /** + * Escapes an issue description and normalizes its line breaks. + * + * @param description the description to normalize + * @return the normalized description + * @since Envoy Common v0.2-beta + */ + private static String sanitizeDescription(String description) { + return escape(description).replaceAll("\r?\n", "
"); + } + + /** + * Escapes quotes and backslashes from a string. + * + * @param raw the string to escape + * @return the escaped string + * @since Envoy Client v0.2-beta + */ + private static String escape(String raw) { return raw.replace("\\", "\\\\").replace("\"", "\\\""); } + /** * @return the description * @since Envoy Common v0.2-beta