-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3420 from AtlasOfLivingAustralia/feature/issue3419
Replaced grails markdown plugin with commonmark #3419
- Loading branch information
Showing
6 changed files
with
99 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/groovy/au/org/ala/merit/util/MarkdownUtils.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package au.org.ala.merit.util | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Slf4j | ||
import org.commonmark.parser.Parser | ||
import org.commonmark.renderer.html.HtmlRenderer | ||
import org.owasp.html.HtmlChangeListener | ||
import org.owasp.html.HtmlPolicyBuilder | ||
import org.owasp.html.PolicyFactory | ||
import org.owasp.html.Sanitizers | ||
|
||
@CompileStatic | ||
@Slf4j | ||
class MarkdownUtils { | ||
|
||
/** Allow simple formatting, links and text within p and divs by default */ | ||
static PolicyFactory policy = (Sanitizers.FORMATTING & Sanitizers.LINKS & Sanitizers.BLOCKS) & new HtmlPolicyBuilder().allowTextIn("p", "div").toFactory() | ||
|
||
static String markdownToHtmlAndSanitise(String text) { | ||
Parser parser = Parser.builder().build() | ||
org.commonmark.node.Node document = parser.parse(text) | ||
HtmlRenderer renderer = HtmlRenderer.builder().build() | ||
String html = renderer.render(document) | ||
|
||
internalSanitise(policy, html) | ||
} | ||
|
||
private static String internalSanitise(PolicyFactory policyFactory, String input, String imageId = '', String metadataName = '') { | ||
policyFactory.sanitize(input, new HtmlChangeListener<Object>() { | ||
void discardedTag(Object context, String elementName) { | ||
log.warn("Dropping element $elementName in $imageId.$metadataName") | ||
} | ||
void discardedAttributes(Object context, String tagName, String... attributeNames) { | ||
log.warn("Dropping attributes $attributeNames from $tagName in $imageId.$metadataName") | ||
} | ||
}, null) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,6 @@ class EmailServiceSpec extends Specification implements AutowiredTest{ | |
List usersAndRoles = [admin1, grantManager1, editor] | ||
EmailTemplate emailTemplate = EmailTemplate.DEFAULT_PLAN_SUBMITTED_EMAIL_TEMPLATE | ||
String body = "body" | ||
body.metaClass.markdownToHtml = { "Body" } | ||
EmailParams email | ||
|
||
when: | ||
|
@@ -69,7 +68,7 @@ class EmailServiceSpec extends Specification implements AutowiredTest{ | |
email.params.from == "[email protected]" | ||
email.params.replyTo == "[email protected]" | ||
email.params.subject == "Subject" | ||
email.params.html == "Body" | ||
email.params.html == "<p>body</p>\n" | ||
} | ||
|
||
|
||
|
@@ -88,7 +87,6 @@ class EmailServiceSpec extends Specification implements AutowiredTest{ | |
List usersAndRoles = [admin1, admin2, editor] | ||
EmailTemplate emailTemplate = EmailTemplate.DEFAULT_PLAN_APPROVED_EMAIL_TEMPLATE | ||
String body = "body" | ||
body.metaClass.markdownToHtml = { "Body" } | ||
EmailParams email | ||
|
||
when: | ||
|
@@ -102,7 +100,7 @@ class EmailServiceSpec extends Specification implements AutowiredTest{ | |
email.params.from == "[email protected]" | ||
email.params.replyTo == "[email protected]" | ||
email.params.subject == "Subject" | ||
email.params.html == "Body" | ||
email.params.html == "<p>body</p>\n" | ||
|
||
} | ||
|
||
|
50 changes: 50 additions & 0 deletions
50
src/test/groovy/au/org/ala/merit/util/MarkdownUtilsSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package au.org.ala.merit.util | ||
|
||
import spock.lang.Specification | ||
|
||
class MarkdownUtilsSpec extends Specification { | ||
|
||
def "markdownToHtmlAndSanitise should convert markdown to HTML and sanitize it"() { | ||
given: | ||
String markdown = "# Heading\n\nThis is a [link](http://example.com)." | ||
|
||
when: | ||
String result = MarkdownUtils.markdownToHtmlAndSanitise(markdown) | ||
|
||
then: | ||
result == "<h1>Heading</h1>\n<p>This is a <a href=\"http://example.com\" rel=\"nofollow\">link</a>.</p>\n" | ||
} | ||
|
||
def "markdownToHtmlAndSanitise should remove disallowed tags"() { | ||
given: | ||
String markdown = "<script>alert('XSS');</script>" | ||
|
||
when: | ||
String result = MarkdownUtils.markdownToHtmlAndSanitise(markdown) | ||
|
||
then: | ||
result == "\n" | ||
} | ||
|
||
def "markdownToHtmlAndSanitise should allow simple formatting"() { | ||
given: | ||
String markdown = "**bold** *italic*" | ||
|
||
when: | ||
String result = MarkdownUtils.markdownToHtmlAndSanitise(markdown) | ||
|
||
then: | ||
result == "<p><strong>bold</strong> <em>italic</em></p>\n" | ||
} | ||
|
||
def "markdownToHtmlAndSanitise should allow text within p and div tags"() { | ||
given: | ||
String markdown = "<p>Paragraph</p><div>Division</div>" | ||
|
||
when: | ||
String result = MarkdownUtils.markdownToHtmlAndSanitise(markdown) | ||
|
||
then: | ||
result == "<p>Paragraph</p><div>Division</div>\n" | ||
} | ||
} |