akkoma/lib/pleroma/web/plugs/csp_nonce_plug.ex

22 lines
353 B
Elixir
Raw Normal View History

2022-12-15 02:02:07 +00:00
defmodule Pleroma.Web.Plugs.CSPNoncePlug do
import Plug.Conn
def init(opts) do
opts
end
def call(conn, _opts) do
assign_csp_nonce(conn)
end
defp assign_csp_nonce(conn) do
nonce =
:crypto.strong_rand_bytes(128)
|> Base.url_encode64()
|> binary_part(0, 15)
conn
|> assign(:csp_nonce, nonce)
end
end