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
+131
-6
No files matched your search
@@ -5,6 +5,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
@@ -26,6 +27,14 @@ namespace Void::Text
|
||||
// https, mailto, or a relative URL); unsafe schemes such as javascript: and
|
||||
// data: are dropped, keeping only the label text.
|
||||
//
|
||||
// An optional MarkdownOptions::resolver can rewrite a destination (e.g. a
|
||||
// relative wiki link to a bounded server route) after the allow-list check and
|
||||
// before escaping. It is called once per emitted link/image destination, in
|
||||
// document order; its result is escaped like any other destination. Unsafe
|
||||
// destinations are dropped regardless of the resolver, which is never called
|
||||
// for them. If the resolver throws, the original validated destination is
|
||||
// used; an overlong result is reported as MarkdownError::Code::output_limit.
|
||||
//
|
||||
// All work is bounded by MarkdownLimits: input size, output size, block count
|
||||
// and nesting depth.
|
||||
|
||||
@@ -53,10 +62,15 @@ struct MarkdownError
|
||||
[[nodiscard]] constexpr bool operator==(const MarkdownError&) const noexcept = default;
|
||||
};
|
||||
|
||||
// Rewrites a validated link/image destination. `image` is true for images.
|
||||
// The returned string is escaped before being emitted.
|
||||
using MarkdownResolver = std::function<std::string(std::string_view destination, bool image)>;
|
||||
|
||||
struct MarkdownOptions
|
||||
{
|
||||
MarkdownLimits limits{};
|
||||
bool allow_images = true;
|
||||
MarkdownResolver resolver{};
|
||||
};
|
||||
|
||||
namespace Detail
|
||||
@@ -713,6 +727,23 @@ inline void render_inline(std::string_view s,
|
||||
return false;
|
||||
}
|
||||
|
||||
// Apply the optional resolver to an already-validated destination. A throwing
|
||||
// resolver falls back to the original destination; the writer bounds the size
|
||||
// of whatever comes back.
|
||||
[[nodiscard]] inline std::string resolve_destination(const MarkdownOptions& opt,
|
||||
std::string_view destination,
|
||||
bool image)
|
||||
{
|
||||
if (!opt.resolver) {
|
||||
return std::string{destination};
|
||||
}
|
||||
try {
|
||||
return opt.resolver(destination, image);
|
||||
} catch (...) {
|
||||
return std::string{destination};
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::size_t try_link(std::string_view s,
|
||||
std::size_t open,
|
||||
MarkdownWriter& w,
|
||||
@@ -738,19 +769,20 @@ inline void render_inline(std::string_view s,
|
||||
render_inline(label, w, opt, depth + 1);
|
||||
return paren + 1;
|
||||
}
|
||||
if (image && !opt.allow_images) {
|
||||
render_inline(label, w, opt, depth + 1);
|
||||
return paren + 1;
|
||||
}
|
||||
const std::string resolved = resolve_destination(opt, dest, image);
|
||||
if (image) {
|
||||
if (opt.allow_images) {
|
||||
w.append("<img src=\"");
|
||||
append_escaped(w, dest);
|
||||
append_escaped(w, resolved);
|
||||
w.append("\" alt=\"");
|
||||
append_escaped(w, label);
|
||||
w.append("\" />");
|
||||
} else {
|
||||
render_inline(label, w, opt, depth + 1);
|
||||
}
|
||||
} else {
|
||||
w.append("<a href=\"");
|
||||
append_escaped(w, dest);
|
||||
append_escaped(w, resolved);
|
||||
w.append("\"");
|
||||
if (!title.empty()) {
|
||||
w.append(" title=\"");
|
||||
|
||||
@@ -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