Package edu.ucsb.cs.scaffold.services
Class MarkdownService
java.lang.Object
edu.ucsb.cs.scaffold.services.MarkdownService
Cleans user-supplied Markdown before it is stored. Markdown itself has no invalid syntax (every
string is a valid CommonMark document), so "linting" takes the form of reformatting to canonical
CommonMark. The security-relevant part is sanitizing raw HTML embedded in the Markdown: only the
HTML nodes of the parsed document are run through the OWASP sanitizer, so code samples such as
if a < b: in text or code blocks are left untouched.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSanitizes raw HTML embedded in the Markdown (dropping anything the OWASP policy rejects, including scripts and javascript: URLs), removes unsafe link/image destinations, and reformats the result to canonical CommonMark.cleanLabel(String markdown) Likeclean(java.lang.String), but for short single-line-ish fields such as concept/subconcept labels, which have no legitimate use for embedded raw HTML.intrenderedLength(String markdown) Returns the length of the plain text this Markdown renders to, with runs of whitespace collapsed to a single space.Renders Markdown to HTML for direct display (e.g. viadangerouslySetInnerHTMLon the frontend).toInlineHtml(String markdown) LiketoHtml(java.lang.String), but for short, single-line Markdown meant to be displayed inline (e.g. a concept label shown inside a<span>): the block-level<p>wrapper CommonMark would normally add around a single paragraph is stripped, since nesting a block element inside an inline container is invalid HTML.
-
Constructor Details
-
MarkdownService
public MarkdownService()
-
-
Method Details
-
clean
Sanitizes raw HTML embedded in the Markdown (dropping anything the OWASP policy rejects, including scripts and javascript: URLs), removes unsafe link/image destinations, and reformats the result to canonical CommonMark.- Parameters:
markdown- the raw Markdown, may be null- Returns:
- the cleaned Markdown, or null if the input was null
-
cleanLabel
Likeclean(java.lang.String), but for short single-line-ish fields such as concept/subconcept labels, which have no legitimate use for embedded raw HTML. Rather than sanitizing (and possibly dropping) HTML-looking fragments, any raw HTML is turned into literal text, so that CS notation such asList<Integer>orNode<T>round-trips intact instead of being silently stripped by the HTML sanitizer (which can otherwise reduce a label to nothing and make it fail the "label may not be empty" check). The result is still safe to render withtoInlineHtml(java.lang.String), since that runs its own HTML sanitization pass at display time.- Parameters:
markdown- the raw Markdown, may be null- Returns:
- the cleaned Markdown, or null if the input was null
-
toHtml
Renders Markdown to HTML for direct display (e.g. viadangerouslySetInnerHTMLon the frontend). The rendered HTML is itself run through the OWASP sanitizer before being returned, so the result is safe to inject even if the stored Markdown was never cleaned (e.g. seed data inserted directly via SQL).- Parameters:
markdown- the raw Markdown, may be null- Returns:
- sanitized HTML, or null if the input was null
-
toInlineHtml
LiketoHtml(java.lang.String), but for short, single-line Markdown meant to be displayed inline (e.g. a concept label shown inside a<span>): the block-level<p>wrapper CommonMark would normally add around a single paragraph is stripped, since nesting a block element inside an inline container is invalid HTML.- Parameters:
markdown- the raw Markdown, may be null- Returns:
- sanitized inline HTML, or null if the input was null
-
renderedLength
Returns the length of the plain text this Markdown renders to, with runs of whitespace collapsed to a single space. Used to enforce limits on the rendered length of a field independent of how verbose the Markdown markup is.
-