Skip to content

Commit 00ff1db

Browse files
committed
Fix security and favicon
1 parent 3bdf485 commit 00ff1db

File tree

9 files changed

+53
-30
lines changed

9 files changed

+53
-30
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ _build/
33
assets/node_modules/
44
deps/
55
test/
6+
priv/plts
67
priv/static/assets/
78

89
.*

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ npm-debug.log
1616
/assets/node_modules
1717

1818
# Ignore assets that are produced by build tools
19-
/priv/static/assets
20-
21-
# Ignore digested assets cache.
22-
/priv/static/cache_manifest.json
19+
/priv/static/*
20+
!/priv/static/favicon.svg
2321

2422
# Local environment variable files
2523
.env.local

Dockerfile

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
ARG NODEJS_VERSION=22-bookworm-slim
2+
ARG ELIXIR_VERSION=1.18.1
3+
ARG OTP_VERSION=27.2
4+
ARG DEBIAN_VERSION=bookworm-20241223-slim
5+
6+
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
7+
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
8+
19
# -----------------------------------------------
210
# Stage: npm dependencies
311
# -----------------------------------------------
4-
FROM node:20.5-bookworm-slim AS npm-builder
12+
FROM node:${NODEJS_VERSION} AS npm-builder
513

614
# Install Debian dependencies
715
RUN apt-get update -y && \
@@ -16,44 +24,51 @@ COPY assets assets
1624
RUN npm ci --prefix assets
1725

1826
# -----------------------------------------------
19-
# Stage: hex dependencies
27+
# Stage: hex dependencies + OTP release
2028
# -----------------------------------------------
21-
FROM hexpm/elixir:1.15.5-erlang-26.0.2-debian-bookworm-20230612-slim AS otp-builder
29+
FROM ${BUILDER_IMAGE} AS hex-builder
2230

23-
# Install Debian dependencies
31+
# install build dependencies
2432
RUN apt-get update -y && \
2533
apt-get install -y build-essential git && \
2634
apt-get clean && \
2735
rm -f /var/lib/apt/lists/*_*
2836

37+
# prepare build dir
2938
WORKDIR /app
3039

31-
# Install Erlang dependencies
32-
RUN mix local.rebar --force && \
33-
mix local.hex --force
40+
ENV MIX_ENV=prod
41+
ENV ERL_FLAGS="+JPperf true"
42+
43+
# install hex + rebar
44+
RUN mix local.hex --force && \
45+
mix local.rebar --force
3446

47+
# set build ENV
3548
ENV MIX_ENV="prod"
3649

37-
# Install mix dependencies
50+
# install mix dependencies
3851
COPY mix.exs mix.lock ./
3952
RUN mix deps.get --only $MIX_ENV
4053

41-
# Copy compile-time config files before we compile dependencies
54+
# copy compile-time config files before we compile dependencies
4255
# to ensure any relevant config change will trigger the dependencies
4356
# to be re-compiled.
4457
RUN mkdir config
4558
COPY config/config.exs config/${MIX_ENV}.exs config/
46-
47-
# Compile mix dependencies
4859
RUN mix deps.compile
4960

50-
# Compile assets
61+
# install Esbuild so it is cached
62+
RUN mix esbuild.install --if-missing
63+
64+
COPY lib lib
5165
COPY --from=npm-builder /app/assets assets
5266
COPY priv priv
67+
68+
# Compile assets
5369
RUN mix assets.deploy
5470

55-
# Compile code
56-
COPY lib lib
71+
# Compile the release
5772
RUN mix compile
5873

5974
# Changes to config/runtime.exs don't require recompiling the code
@@ -65,25 +80,27 @@ RUN mix release
6580
# -----------------------------------------------
6681
# Stage: Bundle release in a docker image
6782
# -----------------------------------------------
68-
FROM debian:bookworm-20230612-slim
83+
FROM ${RUNNER_IMAGE}
6984

7085
RUN apt-get update -y && \
71-
apt-get install -y libstdc++6 openssl libncurses5 locales && \
72-
apt-get clean && \
73-
rm -f /var/lib/apt/lists/*_*
86+
apt-get install -y curl jq libstdc++6 openssl libncurses5 locales && \
87+
apt-get clean && \
88+
rm -f /var/lib/apt/lists/*_*
7489

7590
# Set the locale
7691
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
7792

78-
ENV LANG en_US.UTF-8
79-
ENV LANGUAGE en_US:en
80-
ENV LC_ALL en_US.UTF-8
81-
8293
WORKDIR "/app"
8394
RUN chown nobody /app
8495

96+
# set runner ENV
97+
ENV LANG=en_US.UTF-8
98+
ENV LANGUAGE=en_US:en
99+
ENV LC_ALL=en_US.UTF-8
100+
ENV MIX_ENV="prod"
101+
85102
# Only copy the final release from the build stage
86-
COPY --from=otp-builder --chown=nobody:root /app/_build/prod/rel/elixir_boilerplate ./
103+
COPY --from=hex-builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/elixir_boilerplate ./
87104

88105
USER nobody
89106

lib/elixir_boilerplate_web/endpoint.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule ElixirBoilerplateWeb.Endpoint do
2424
at: "/",
2525
from: :elixir_boilerplate,
2626
gzip: true,
27-
only: ~w(assets fonts images favicon.ico robots.txt)
27+
only: ~w(assets fonts images favicon.svg robots.txt)
2828
)
2929

3030
# Code reloading can be explicitly enabled under the

lib/elixir_boilerplate_web/home/live.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule ElixirBoilerplateWeb.Home.Live do
77
def mount(_, _, socket) do
88
socket = assign(socket, :message, "Hello, world!")
99
socket = assign(socket, :counter, 0)
10+
socket = assign(socket, :page_title, "Home")
1011

1112
{:ok, socket}
1213
end

lib/elixir_boilerplate_web/layouts/templates/root.html.heex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
<meta charset="utf-8" />
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<Phoenix.Component.live_title>
8+
<%= assigns[:page_title] || "ElixirBoilerplate" %>
9+
</Phoenix.Component.live_title>
10+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
711
<link rel="stylesheet" href={Routes.static_url(@conn, "/assets/app.css")} />
812
<meta name="csrf-token" content={Plug.CSRFProtection.get_csrf_token()} />
9-
<script type="text/javascript" src={Routes.static_url(@conn, "/assets/app.js")}></script>
13+
<script defer type="text/javascript" src={Routes.static_url(@conn, "/assets/app.js")}></script>
1014
</head>
1115

1216
<body>

lib/elixir_boilerplate_web/plugs/security.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule ElixirBoilerplateWeb.Plugs.Security do
4141
if Application.get_env(:elixir_boilerplate, __MODULE__)[:allow_unsafe_scripts] do
4242
"'self' 'unsafe-eval' 'unsafe-inline'"
4343
else
44-
"'self'"
44+
"'self' 'unsafe-inline'"
4545
end
4646
end
4747
end

lib/elixir_boilerplate_web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule ElixirBoilerplateWeb.Router do
1010
plug(:fetch_session)
1111

1212
plug(:protect_from_forgery)
13+
plug(:put_secure_browser_headers)
1314
plug(:fetch_live_flash)
1415

1516
plug(:put_layout, {ElixirBoilerplateWeb.Layouts, :app})

priv/static/favicon.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)