Class MarkdownService

java.lang.Object
edu.ucsb.cs.scaffold.services.MarkdownService

@Service public class MarkdownService extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    clean(String markdown)
    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.
    cleanLabel(String markdown)
    Like clean(java.lang.String), but for short single-line-ish fields such as concept/subconcept labels, which have no legitimate use for embedded raw HTML.
    int
    Returns the length of the plain text this Markdown renders to, with runs of whitespace collapsed to a single space.
    toHtml(String markdown)
    Renders Markdown to HTML for direct display (e.g. via dangerouslySetInnerHTML on the frontend).
    toInlineHtml(String markdown)
    Like toHtml(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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MarkdownService

      public MarkdownService()
  • Method Details

    • clean

      public String clean(String markdown)
      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

      public String cleanLabel(String markdown)
      Like clean(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 as List<Integer> or Node<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 with toInlineHtml(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

      public String toHtml(String markdown)
      Renders Markdown to HTML for direct display (e.g. via dangerouslySetInnerHTML on 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

      public String toInlineHtml(String markdown)
      Like toHtml(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

      public int renderedLength(String markdown)
      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.