Skip to content

Commit

Permalink
Return nil unless santitised string matches original.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkachowski committed Oct 29, 2024
1 parent 6fb1a2b commit aa559a3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ defmodule BlockScoutWeb.Tokens.Instance.OverviewView do

def external_url(nil), do: nil

def external_url("http" <> _rest = external_url), do: external_url
def external_url("http" <> _rest = external_url) do
sanitised = external_url |> html_escape() |> safe_to_string()
if sanitised != external_url do
nil
else
external_url
end
end

def external_url(string) when is_binary(string), do: external_url(nil)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,9 @@ defmodule BlockScoutWeb.Tokens.Instance.OverviewViewTest do
end

test "does not return html escape" do
json = """
{
"name": "CELO XSS",
"image": "https://0-a.nl/nft/nft.jpg",
"description": "CELO XSS",
"external_url": "https\" id=x tabindex=1 onfocusin=eval(atob('KGZ1bmN0aW9uKCl7d2luZG93LmV0aG'))"
}
"""

data = Jason.decode!(json)
data = %{
"external_url" => "https\" id=x tabindex=1 onfocusin=eval(atob('KGZ1bmN0aW9uKCl7d2luZG93LmV0aG'))"
}

result = OverviewView.external_url(%{metadata: data})

Expand All @@ -170,6 +163,7 @@ defmodule BlockScoutWeb.Tokens.Instance.OverviewViewTest do
result = OverviewView.external_url(%{metadata: data})

assert String.starts_with?(result, "http"), "Valid url should be returned"
assert result == "https://happyland.nft"
end
end
end

0 comments on commit aa559a3

Please sign in to comment.