Sanitized issue proposals

Fixes #53
This commit is contained in:
Leon Hofmeister 2020-09-26 23:08:32 +02:00
parent f0e645c0ae
commit ecb792050a
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
2 changed files with 71 additions and 25 deletions

View File

@ -7,7 +7,6 @@ import javafx.scene.input.InputEvent;
import envoy.client.event.SendEvent; import envoy.client.event.SendEvent;
import envoy.client.util.IssueUtil; import envoy.client.util.IssueUtil;
import envoy.data.User; import envoy.data.User;
import envoy.event.IssueProposal;
import dev.kske.eventbus.EventBus; import dev.kske.eventbus.EventBus;
@ -70,8 +69,9 @@ public final class BugReportPane extends OnlyIfOnlineSettingsPane {
submitReportButton.setDisable(true); submitReportButton.setDisable(true);
submitReportButton.setOnAction(e -> { submitReportButton.setOnAction(e -> {
EventBus.getInstance() EventBus.getInstance()
.dispatch(new SendEvent(new IssueProposal(titleTextField.getText(), .dispatch(new SendEvent(IssueUtil.createIssueProposal(titleTextField.getText(),
IssueUtil.sanitizeIssueDescription(errorDetailArea.getText(), showUsernameInBugReport.isSelected() ? user.getName() : null), errorDetailArea.getText(),
showUsernameInBugReport.isSelected() ? user.getName() : null,
true))); true)));
}); });
getChildren().add(submitReportButton); getChildren().add(submitReportButton);

View File

@ -1,40 +1,86 @@
package envoy.client.util; package envoy.client.util;
import java.util.regex.Pattern;
import envoy.event.IssueProposal;
/** /**
* Provides methods to handle outgoing issues. * Provides methods to handle outgoing issues.
* <p>
* Project: <strong>client</strong><br>
* File: <strong>IssueUtil.java</strong><br>
* Created: <strong>20.08.2020</strong><br>
* *
* @author Leon Hofmeister * @author Leon Hofmeister
* @author Kai S. K. Engelbart
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public final class IssueUtil { public final class IssueUtil {
/** private static final Pattern removeBackslashes = Pattern.compile("\\\\");
* private static final Pattern escapeQuotes = Pattern.compile("\"");
* @since Envoy Client v0.2-beta
*/
private IssueUtil() {} private IssueUtil() {}
/** /**
* Performs actions to ensure the description of an issue will be displayed as * Creates a new {@code IssueProposal} from the given data.
* intended by the user.
* *
* @param rawDescription the description to sanitize * @param title the proposed title of the issue
* @param username the user who submitted the issue. Should be * @param description the proposed description of the issue
* {@code null} if he does not want to be named. * @param username the user who submitted the issue. Should be
* @return the sanitized description * {@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 * @since Envoy Client v0.2-beta
*/ */
public static String sanitizeIssueDescription(String rawDescription, String username) { public static IssueProposal createIssueProposal(String title, String description, String username, boolean isBug) {
// Appending the submitter name, if this option was enabled title = sanitizeIssueTitle(title);
rawDescription += username != null description = sanitizeIssueDescription(description, username);
? (rawDescription.endsWith("\n") || rawDescription.endsWith("<br>") ? "" : "<br>") + String.format("Submitted by user %s.", username) return new IssueProposal(title, description, isBug);
: "";
// Markdown does not support "normal" line breaks. It uses "<br>" }
rawDescription = rawDescription.replaceAll(System.getProperty("line.separator", "\r?\n"), "<br>");
return rawDescription; /**
* 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, <br>
* removes all backslashes, <br>
* escapes quotes and<br>
* 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 <br> tags
description = description.trim().replaceAll(System.getProperty("line.separator"), "<br>");
// Append user name if requested
if (username != null) description += String.format("<br>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;
} }
} }