Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape potential unintentional markdown formatting when bridging mails #272

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -70,7 +70,7 @@ private void postNewMessage(Email email) {
"*Mailing list message from [" + email.author().fullName().orElse(email.author().localPart()) +
"](mailto:" + email.author().address() + ") on [" + email.sender().localPart() +
"](mailto:" + email.sender().address() + "):*\n\n" +
email.body();
TextToMarkdown.escapeFormatting(email.body());
pr.addComment(body);
}

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bots.mlbridge;

import java.util.regex.Pattern;

public class TextToMarkdown {
private static final Pattern punctuationPattern = Pattern.compile("([!\"#$%&'()*+,\\-./:;<=?@\\[\\]^_`{|}~])", Pattern.MULTILINE);
private static final Pattern indentedPattern = Pattern.compile("^ {4}", Pattern.MULTILINE);

private static String escapeBackslashes(String text) {
return text.replace("\\", "\\\\");
}

private static String escapePunctuation(String text) {
var punctuationMatcher = punctuationPattern.matcher(text);
return punctuationMatcher.replaceAll(mr -> "\\\\" + mr.group(1));
}

private static String escapeIndention(String text) {
var indentedMatcher = indentedPattern.matcher(text);
return indentedMatcher.replaceAll("&#32; ");
}

static String escapeFormatting(String text) {
return escapeIndention(escapePunctuation(escapeBackslashes(text)));
}
}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bots.mlbridge;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class TextToMarkdownTests {
@Test
void punctuation() {
assertEquals("1\\. Not a list", TextToMarkdown.escapeFormatting("1. Not a list"));
assertEquals("\\*not emphasized\\*", TextToMarkdown.escapeFormatting("*not emphasized*"));
assertEquals("\\\\n", TextToMarkdown.escapeFormatting("\\n"));
}

@Test
void indented() {
assertEquals("&#32; hello", TextToMarkdown.escapeFormatting(" hello"));
}

@Test
void preserveQuoting() {
assertEquals("> quoted", TextToMarkdown.escapeFormatting("> quoted"));
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.