Dynamically add websocket endpoint to CSP
pull/436/head
Jimmy Lauzau 6 years ago committed by GitHub
commit 0d8cc39bd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      apps/explorer_web/.sobelow-conf
  2. 28
      apps/explorer_web/lib/explorer_web/csp_header.ex
  3. 12
      apps/explorer_web/lib/explorer_web/router.ex

@ -5,5 +5,5 @@
router: "lib/explorer_web/router.ex",
exit: "low",
format: "compact",
ignore: ["Config.CSRF"]
ignore: ["Config.Headers"]
]

@ -0,0 +1,28 @@
defmodule ExplorerWeb.CSPHeader do
@moduledoc """
Plug to set content-security-policy with websocket endpoints
"""
alias Phoenix.Controller
alias Plug.Conn
def init(opts), do: opts
def call(conn, _opts) do
Controller.put_secure_browser_headers(conn, %{
"content-security-policy" => "\
connect-src 'self' #{websocket_endpoints(conn)}; \
default-src 'self';\
script-src 'self' 'unsafe-inline' 'unsafe-eval';\
style-src 'self' 'unsafe-inline' 'unsafe-eval' https://fonts.googleapis.com;\
img-src 'self' 'unsafe-inline' 'unsafe-eval' data:;\
font-src 'self' 'unsafe-inline' 'unsafe-eval' https://fonts.gstatic.com data:;\
"
})
end
defp websocket_endpoints(conn) do
host = Conn.get_req_header(conn, "host")
"ws://#{host} wss://#{host}"
end
end

@ -6,17 +6,7 @@ defmodule ExplorerWeb.Router do
plug(:fetch_session)
plug(:fetch_flash)
plug(:protect_from_forgery)
plug(:put_secure_browser_headers, %{
"content-security-policy" => "\
connect-src 'self' ws://localhost:*;\
default-src 'self';\
script-src 'self' 'unsafe-inline' 'unsafe-eval';\
style-src 'self' 'unsafe-inline' 'unsafe-eval' https://fonts.googleapis.com;\
img-src 'self' 'unsafe-inline' 'unsafe-eval' data:;\
font-src 'self' 'unsafe-inline' 'unsafe-eval' https://fonts.gstatic.com data:;\
"
})
plug(ExplorerWeb.CSPHeader)
end
pipeline :api do

Loading…
Cancel
Save