fix(formatter): percent-encode quotes in Markdown URLs instead of cutting
Cutting the URL at the first quote dropped the rest of valid links such as ?q="cat"&page=2. Percent-encoding ", < and > keeps the URL complete and still keeps it inside the attribute.
This commit is contained in:
parent
de6ac2392c
commit
58d4f6aa2c
3 changed files with 19 additions and 11 deletions
|
|
@ -252,9 +252,13 @@ defmodule Pleroma.Formatter do
|
||||||
|
|
||||||
defp sanitize_markdown_url(value, name) do
|
defp sanitize_markdown_url(value, name) do
|
||||||
if String.downcase(to_string(name)) in ["href", "src"] do
|
if String.downcase(to_string(name)) in ["href", "src"] do
|
||||||
value
|
# Percent-encode instead of cutting, so the URL stays complete but can
|
||||||
|> String.split(~r/["<>]/, parts: 2)
|
# never close the attribute or open a tag.
|
||||||
|> hd()
|
String.replace(value, ~r/["<>]/, fn
|
||||||
|
"\"" -> "%22"
|
||||||
|
"<" -> "%3C"
|
||||||
|
">" -> "%3E"
|
||||||
|
end)
|
||||||
else
|
else
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -388,10 +388,9 @@ defmodule Pleroma.FormatterTest do
|
||||||
test "escapes link attribute values" do
|
test "escapes link attribute values" do
|
||||||
markdown = ~s|[click](http://example.com/?a=x" onerror="alert(1))|
|
markdown = ~s|[click](http://example.com/?a=x" onerror="alert(1))|
|
||||||
|
|
||||||
html = Formatter.markdown_to_html(markdown)
|
# The quotes are percent-encoded, so everything stays inside href.
|
||||||
|
assert Formatter.markdown_to_html(markdown) ==
|
||||||
assert html =~ ~s|href="http://example.com/?a=x"|
|
~s|<p><a href="http://example.com/?a=x%22 onerror=%22alert(1)">click</a></p>|
|
||||||
refute html =~ "onerror"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "preserves entities without rendering dangerous raw HTML" do
|
test "preserves entities without rendering dangerous raw HTML" do
|
||||||
|
|
@ -416,10 +415,15 @@ defmodule Pleroma.FormatterTest do
|
||||||
test "rejects entity-encoded URL delimiters" do
|
test "rejects entity-encoded URL delimiters" do
|
||||||
markdown = "[click](http://example.com/?a=x"onerror=alert(1))"
|
markdown = "[click](http://example.com/?a=x"onerror=alert(1))"
|
||||||
|
|
||||||
html = Formatter.markdown_to_html(markdown)
|
assert Formatter.markdown_to_html(markdown) ==
|
||||||
|
~s|<p><a href="http://example.com/?a=x%22onerror=alert(1)">click</a></p>|
|
||||||
|
end
|
||||||
|
|
||||||
assert html =~ ~s|href="http://example.com/?a=x"|
|
test "keeps URLs with encoded quotes complete" do
|
||||||
refute html =~ "onerror"
|
markdown = "[search](https://example.org/?q="cat"&page=2)"
|
||||||
|
|
||||||
|
assert Formatter.markdown_to_html(markdown) ==
|
||||||
|
~s(<p><a href="https://example.org/?q=%22cat%22&page=2">search</a></p>)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "strips email autolinks without changing explicit mailto links" do
|
test "strips email autolinks without changing explicit mailto links" do
|
||||||
|
|
|
||||||
|
|
@ -232,7 +232,7 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
|
||||||
code = ~s|[click](http://example.com/?a=x" onerror="alert(1))|
|
code = ~s|[click](http://example.com/?a=x" onerror="alert(1))|
|
||||||
{result, [], []} = Utils.format_input(code, "text/markdown")
|
{result, [], []} = Utils.format_input(code, "text/markdown")
|
||||||
|
|
||||||
assert result =~ ~s(<a href="http://example.com/?a=x">)
|
assert result =~ ~s|<a href="http://example.com/?a=x%22 onerror=%22alert(1)">|
|
||||||
refute result =~ ~s|onerror="alert(1)|
|
refute result =~ ~s|onerror="alert(1)|
|
||||||
refute result =~ ~s(<a href="http://example.com/?a=x" onerror=)
|
refute result =~ ~s(<a href="http://example.com/?a=x" onerror=)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue