To prevent the inclusion of resources from untrusted origins
defmodule Vulnerable do
use Plug.Router
plug CORSPlug, origin: "*"
plug :match
plug :dispatch
get "" do
send_resp(conn, 200, "OK")
end
match _ do
send_resp(conn, 404, "Not found")
end
end
In this Elixir code snippet, the CORS policy is set to '*', allowing any domain to share resources.
defmodule Secure do
use Plug.Router
plug CORSPlug, origin: "https://trusted.domain.com"
plug :match
plug :dispatch
get "" do
send_resp(conn, 200, "OK")
end
match _ do
send_resp(conn, 404, "Not found")
end
end
In this Elixir code snippet, the CORS policy is explicitly set to a specific domain, preventing resource sharing with untrusted domains.