MarkdownOptions::resolver rewrites a validated link/image destination before it is escaped and emitted, once per destination in document order. Unsafe schemes (javascript:, data:, ...) are still dropped and never reach the resolver. A throwing resolver falls back to the original destination; an overlong result is bounded by MarkdownLimits::max_output. Closes #59
404 lines
15 KiB
C++
404 lines
15 KiB
C++
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
#include <Void/Text/Html.hpp>
|
|
#include <Void/Text/Markdown.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
using Void::Text::MarkdownError;
|
|
using Void::Text::MarkdownLimits;
|
|
using Void::Text::MarkdownOptions;
|
|
using Void::Text::markdown_to_html;
|
|
|
|
std::string render(std::string_view input)
|
|
{
|
|
const auto r = markdown_to_html(input);
|
|
Void::Testing::expect(r.has_value());
|
|
return r ? *r : std::string{};
|
|
}
|
|
|
|
std::string render_with(std::string_view input, MarkdownOptions options)
|
|
{
|
|
const auto r = markdown_to_html(input, options);
|
|
Void::Testing::expect(r.has_value());
|
|
return r ? *r : std::string{};
|
|
}
|
|
|
|
MarkdownError render_error(std::string_view input, MarkdownOptions options)
|
|
{
|
|
const auto r = markdown_to_html(input, options);
|
|
Void::Testing::expect(!r.has_value());
|
|
return r ? MarkdownError{} : r.error();
|
|
}
|
|
} // namespace
|
|
|
|
// ── HTML escaping ────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test html_escape_special_characters()
|
|
{
|
|
Void::Testing::expect_eq(Void::Text::html_escape("<a href=\"x\">&'</a>"),
|
|
std::string{"<a href="x">&'</a>"});
|
|
}
|
|
|
|
Void::Testing::Test html_escape_passthrough()
|
|
{
|
|
Void::Testing::expect_eq(Void::Text::html_escape("plain text 123"),
|
|
std::string{"plain text 123"});
|
|
Void::Testing::expect(Void::Text::html_escape("").empty());
|
|
}
|
|
|
|
// ── Headings and paragraphs ──────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test markdown_atx_headings()
|
|
{
|
|
Void::Testing::expect_eq(render("# one"), std::string{"<h1>one</h1>\n"});
|
|
Void::Testing::expect_eq(render("### three"), std::string{"<h3>three</h3>\n"});
|
|
Void::Testing::expect_eq(render("###### six"), std::string{"<h6>six</h6>\n"});
|
|
// Seven hashes is not a heading.
|
|
Void::Testing::expect_eq(render("####### seven"),
|
|
std::string{"<p>####### seven</p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_atx_closing_sequence()
|
|
{
|
|
Void::Testing::expect_eq(render("## title ##"), std::string{"<h2>title</h2>\n"});
|
|
Void::Testing::expect_eq(render("## title#"), std::string{"<h2>title#</h2>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_setext_headings()
|
|
{
|
|
Void::Testing::expect_eq(render("Title\n====="), std::string{"<h1>Title</h1>\n"});
|
|
Void::Testing::expect_eq(render("Title\n-----"), std::string{"<h2>Title</h2>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_paragraphs_and_soft_break()
|
|
{
|
|
Void::Testing::expect_eq(render("one\ntwo\n\nthree"),
|
|
std::string{"<p>one\ntwo</p>\n<p>three</p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_empty_input()
|
|
{
|
|
Void::Testing::expect_eq(render(""), std::string{});
|
|
Void::Testing::expect_eq(render("\n\n \n"), std::string{});
|
|
}
|
|
|
|
// ── Inline constructs ───────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test markdown_emphasis_and_strong()
|
|
{
|
|
Void::Testing::expect_eq(render("*em*"), std::string{"<p><em>em</em></p>\n"});
|
|
Void::Testing::expect_eq(render("_em_"), std::string{"<p><em>em</em></p>\n"});
|
|
Void::Testing::expect_eq(render("**strong**"),
|
|
std::string{"<p><strong>strong</strong></p>\n"});
|
|
Void::Testing::expect_eq(render("__strong__"),
|
|
std::string{"<p><strong>strong</strong></p>\n"});
|
|
Void::Testing::expect_eq(render("**bold *and em***"),
|
|
std::string{"<p><strong>bold <em>and em</em></strong></p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_underscore_intraword_is_literal()
|
|
{
|
|
Void::Testing::expect_eq(render("snake_case_name"),
|
|
std::string{"<p>snake_case_name</p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_unmatched_delimiters_are_literal()
|
|
{
|
|
Void::Testing::expect_eq(render("*unclosed"), std::string{"<p>*unclosed</p>\n"});
|
|
Void::Testing::expect_eq(render("a * b"), std::string{"<p>a * b</p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_code_spans()
|
|
{
|
|
Void::Testing::expect_eq(render("`a < b`"), std::string{"<p><code>a < b</code></p>\n"});
|
|
// A run of two backticks can contain a single backtick.
|
|
Void::Testing::expect_eq(render("``a ` b``"),
|
|
std::string{"<p><code>a ` b</code></p>\n"});
|
|
// One space is trimmed from each side when both sides have one.
|
|
Void::Testing::expect_eq(render("` code `"), std::string{"<p><code>code</code></p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_backslash_escapes()
|
|
{
|
|
Void::Testing::expect_eq(render("\\*not em\\*"), std::string{"<p>*not em*</p>\n"});
|
|
Void::Testing::expect_eq(render("\\[not a link\\]"),
|
|
std::string{"<p>[not a link]</p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_hard_breaks()
|
|
{
|
|
Void::Testing::expect_eq(render("one \ntwo"),
|
|
std::string{"<p>one<br />\ntwo</p>\n"});
|
|
Void::Testing::expect_eq(render("one\\\ntwo"),
|
|
std::string{"<p>one<br />\ntwo</p>\n"});
|
|
}
|
|
|
|
// ── Links, images, autolinks ────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test markdown_inline_link()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
render("[text](https://example.com)"),
|
|
std::string{"<p><a href=\"https://example.com\">text</a></p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_inline_link_with_title()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
render("[text](https://example.com \"a title\")"),
|
|
std::string{"<p><a href=\"https://example.com\" title=\"a title\">text</a></p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_relative_and_mailto_links_allowed()
|
|
{
|
|
Void::Testing::expect(render("[/x](/x)").find("href=\"/x\"") != std::string::npos);
|
|
Void::Testing::expect(render("[#a](#a)").find("href=\"#a\"") != std::string::npos);
|
|
Void::Testing::expect(render("[m](mailto:a@b.c)").find("href=\"mailto:a@b.c\"") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test markdown_image()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
render(""),
|
|
std::string{"<p><img src=\"https://example.com/x.png\" alt=\"alt text\" /></p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_image_disallowed_renders_alt()
|
|
{
|
|
MarkdownOptions options;
|
|
options.allow_images = false;
|
|
const auto r = markdown_to_html("", options);
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(*r, std::string{"<p>alt text</p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_autolink()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
render("<https://example.com/a?b=1>"),
|
|
std::string{"<p><a href=\"https://example.com/a?b=1\">https://example.com/a?b=1</a></p>\n"});
|
|
}
|
|
|
|
// ── Blocks ──────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test markdown_thematic_break()
|
|
{
|
|
Void::Testing::expect_eq(render("---"), std::string{"<hr />\n"});
|
|
Void::Testing::expect_eq(render("***"), std::string{"<hr />\n"});
|
|
Void::Testing::expect_eq(render("___"), std::string{"<hr />\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_fenced_code_block()
|
|
{
|
|
Void::Testing::expect_eq(render("```\nint x = 1;\n```"),
|
|
std::string{"<pre><code>int x = 1;\n</code></pre>\n"});
|
|
Void::Testing::expect_eq(
|
|
render("```cpp\nif (a < b) {}\n```"),
|
|
std::string{"<pre><code class=\"language-cpp\">if (a < b) {}\n</code></pre>\n"});
|
|
// Tilde fences work too.
|
|
Void::Testing::expect_eq(render("~~~\nx\n~~~"),
|
|
std::string{"<pre><code>x\n</code></pre>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_unclosed_fence_renders_to_end()
|
|
{
|
|
Void::Testing::expect_eq(render("```\nabc"),
|
|
std::string{"<pre><code>abc\n</code></pre>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_unordered_list()
|
|
{
|
|
Void::Testing::expect_eq(render("- one\n- two"),
|
|
std::string{"<ul>\n<li>one</li>\n<li>two</li>\n</ul>\n"});
|
|
Void::Testing::expect_eq(render("* a\n* b"),
|
|
std::string{"<ul>\n<li>a</li>\n<li>b</li>\n</ul>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_ordered_list_and_start()
|
|
{
|
|
Void::Testing::expect_eq(render("1. one\n2. two"),
|
|
std::string{"<ol>\n<li>one</li>\n<li>two</li>\n</ol>\n"});
|
|
Void::Testing::expect_eq(render("3. three\n4. four"),
|
|
std::string{"<ol start=\"3\">\n<li>three</li>\n<li>four</li>\n</ol>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_blockquote()
|
|
{
|
|
Void::Testing::expect_eq(render("> quoted"),
|
|
std::string{"<blockquote>\n<p>quoted</p>\n</blockquote>\n"});
|
|
Void::Testing::expect_eq(render("> a\n> b"),
|
|
std::string{"<blockquote>\n<p>a\nb</p>\n</blockquote>\n"});
|
|
}
|
|
|
|
// ── Hostile input ──────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test markdown_raw_html_is_escaped()
|
|
{
|
|
Void::Testing::expect_eq(render("<script>alert(1)</script>"),
|
|
std::string{"<p><script>alert(1)</script></p>\n"});
|
|
Void::Testing::expect_eq(
|
|
render("<img src=x onerror=alert(1)>"),
|
|
std::string{"<p><img src=x onerror=alert(1)></p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_javascript_links_dropped()
|
|
{
|
|
Void::Testing::expect_eq(render("[x](javascript:alert(1))"),
|
|
std::string{"<p>x</p>\n"});
|
|
Void::Testing::expect_eq(render("[x](JaVaScRiPt:alert(1))"),
|
|
std::string{"<p>x</p>\n"});
|
|
Void::Testing::expect_eq(render("[x](data:text/html;base64,AAAA)"),
|
|
std::string{"<p>x</p>\n"});
|
|
Void::Testing::expect_eq(render("[x](vbscript:msgbox(1))"),
|
|
std::string{"<p>x</p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_entity_smuggled_scheme_dropped()
|
|
{
|
|
Void::Testing::expect_eq(render("[x](javascript:alert(1))"),
|
|
std::string{"<p>x</p>\n"});
|
|
Void::Testing::expect_eq(render("[x](javascript:alert(1))"),
|
|
std::string{"<p>x</p>\n"});
|
|
Void::Testing::expect_eq(render("[x](javascript:alert(1))"),
|
|
std::string{"<p>x</p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_attribute_injection_is_escaped()
|
|
{
|
|
const std::string out = render("[x](https://e.com/\" onmouseover=\"alert(1))");
|
|
Void::Testing::expect(out.find("onmouseover=\"") == std::string::npos);
|
|
Void::Testing::expect(out.find(""") != std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test markdown_code_block_content_is_escaped()
|
|
{
|
|
Void::Testing::expect_eq(render("```\n</code><script>x</script>\n```"),
|
|
std::string{"<pre><code></code><script>x</script>\n</code></pre>\n"});
|
|
}
|
|
|
|
// ── Destination resolver ───────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test markdown_resolver_called_once_per_destination_in_order()
|
|
{
|
|
std::vector<std::string> seen;
|
|
MarkdownOptions options;
|
|
options.resolver = [&seen](std::string_view dest, bool image) {
|
|
seen.push_back((image ? std::string{"img:"} : std::string{"link:"}) +
|
|
std::string{dest});
|
|
return "https://wiki.local" + std::string{dest};
|
|
};
|
|
const auto r = markdown_to_html("[a](/one)  [b](/three)", options);
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(
|
|
*r,
|
|
std::string{"<p><a href=\"https://wiki.local/one\">a</a> "
|
|
"<img src=\"https://wiki.local/two.png\" alt=\"i\" /> "
|
|
"<a href=\"https://wiki.local/three\">b</a></p>\n"});
|
|
Void::Testing::expect_eq(seen.size(), std::size_t{3});
|
|
Void::Testing::expect_eq(seen[0], std::string{"link:/one"});
|
|
Void::Testing::expect_eq(seen[1], std::string{"img:/two.png"});
|
|
Void::Testing::expect_eq(seen[2], std::string{"link:/three"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_resolver_output_is_escaped()
|
|
{
|
|
MarkdownOptions options;
|
|
options.resolver = [](std::string_view, bool) {
|
|
return std::string{"\"><script>alert(1)</script>"};
|
|
};
|
|
const std::string out = render_with("[x](/safe)", options);
|
|
Void::Testing::expect(out.find("<script>") == std::string::npos);
|
|
Void::Testing::expect(out.find(""><script>") != std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test markdown_resolver_throw_falls_back_to_original()
|
|
{
|
|
MarkdownOptions options;
|
|
options.resolver = [](std::string_view, bool) -> std::string {
|
|
throw std::runtime_error{"boom"};
|
|
};
|
|
const auto r = markdown_to_html("[x](/safe)", options);
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(*r, std::string{"<p><a href=\"/safe\">x</a></p>\n"});
|
|
}
|
|
|
|
Void::Testing::Test markdown_resolver_overlong_result_is_bounded()
|
|
{
|
|
MarkdownOptions options;
|
|
options.limits.max_output = 16;
|
|
options.resolver = [](std::string_view, bool) { return std::string(64, 'a'); };
|
|
const auto err = render_error("[x](/safe)", options);
|
|
Void::Testing::expect(err.code == MarkdownError::Code::output_limit);
|
|
}
|
|
|
|
Void::Testing::Test markdown_resolver_never_sees_unsafe_destinations()
|
|
{
|
|
std::size_t calls = 0;
|
|
MarkdownOptions options;
|
|
options.resolver = [&calls](std::string_view, bool) {
|
|
++calls;
|
|
return std::string{"https://ok.example"};
|
|
};
|
|
Void::Testing::expect_eq(render_with("[x](javascript:alert(1))", options),
|
|
std::string{"<p>x</p>\n"});
|
|
Void::Testing::expect_eq(render_with("[x](data:text/html,AAAA)", options),
|
|
std::string{"<p>x</p>\n"});
|
|
Void::Testing::expect_eq(calls, std::size_t{0});
|
|
}
|
|
|
|
Void::Testing::Test markdown_resolver_skipped_for_disallowed_images()
|
|
{
|
|
std::size_t calls = 0;
|
|
MarkdownOptions options;
|
|
options.allow_images = false;
|
|
options.resolver = [&calls](std::string_view, bool) {
|
|
++calls;
|
|
return std::string{"https://ok.example"};
|
|
};
|
|
Void::Testing::expect_eq(render_with("", options),
|
|
std::string{"<p>alt</p>\n"});
|
|
Void::Testing::expect_eq(calls, std::size_t{0});
|
|
}
|
|
|
|
// ── Bounds ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test markdown_input_limit()
|
|
{
|
|
MarkdownOptions options;
|
|
options.limits.max_input = 4;
|
|
const auto err = render_error("12345", options);
|
|
Void::Testing::expect(err.code == MarkdownError::Code::input_limit);
|
|
}
|
|
|
|
Void::Testing::Test markdown_output_limit()
|
|
{
|
|
MarkdownOptions options;
|
|
options.limits.max_output = 8;
|
|
const auto err = render_error("# a long heading", options);
|
|
Void::Testing::expect(err.code == MarkdownError::Code::output_limit);
|
|
}
|
|
|
|
Void::Testing::Test markdown_block_limit()
|
|
{
|
|
MarkdownOptions options;
|
|
options.limits.max_blocks = 1;
|
|
const auto err = render_error("one\n\ntwo", options);
|
|
Void::Testing::expect(err.code == MarkdownError::Code::block_limit);
|
|
}
|
|
|
|
Void::Testing::Test markdown_depth_limit()
|
|
{
|
|
MarkdownOptions options;
|
|
options.limits.max_depth = 1;
|
|
const auto err = render_error("**a *b* c**", options);
|
|
Void::Testing::expect(err.code == MarkdownError::Code::depth_limit);
|
|
}
|