Text: add Markdown link/image destination resolver hook
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
This commit is contained in:
1 parent
549c7cfccb
commit
e6745ac8cb
2 files changed
+135
-10
No files matched your search
@@ -4,8 +4,10 @@
|
||||
#include <Void/Text/Markdown.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -21,6 +23,13 @@ std::string render(std::string_view input)
|
||||
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);
|
||||
@@ -275,6 +284,90 @@ Void::Testing::Test markdown_code_block_content_is_escaped()
|
||||
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()
|
||||
|
||||
Reference in new issue
Block a user