thojanssens1
Rendering html combined with unsafe data with Phoenix.HTML
Say I have some external (unsafe) data
unsafe_data = "some unsafe data"
I want to render this using Phoenix.HTML.Tag.content_tag/2
content_tag(:p, "<i>#{unsafe_data}</i>") |> safe_to_string
It will render:
"<p><i>some unsafe data</i></p>"
This will create html entities, so <i> will be literally printed on the screen.
So I can use Phoenix.HTML.raw/1:
content_tag(:p, raw "<i>#{unsafe_data}</i>") |> safe_to_string
However, this should not be secure.
Maybe I have to do the following?
unsafe_data = safe_to_string(html_escape(unsafe_data)) # <- does that make any sense?
content_tag(:p, raw "<i>#{unsafe_data}</i>") |> safe_to_string
Thank you for any help!
Marked As Solved
josevalim
For this particular example, the best would be:
content_tag(:p, content_tag(:i, unsafe_data))
Alternatively you can put it in a list marking the safe parts:
content_tag(:p, [{:safe, "<i>"}, "<omg>", {:safe, "</i>"}])
Also Liked
OvermindDL1
You can also just use the sigil too:
content_tag(:p, ~E"<i><%= unsafe_data %></i>
Or all of it:
~E"<p><i><%= unsafe_data %></i></p>"
derek-zhou
Phoenix defaults to escape the html to prevent XSS attacks (what if someone embed javascript in the unsafe data?) If you have sanitize it or you have reason to believe that it is clean, just raw/1 it. See the 2 links I cited above.
LostKobrakai
Last Post!
TwistingTwists
strange.
This exact thing was not working yesterday.
now it is working. ![]()
thanks. I feel almost foolish .
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









