November 24, 2026 路 #69
The token in the image history
The image is pushed to a public registry. A stranger runs docker history and finds the private npm token. Which line leaked it?
FROM node:22-slim
WORKDIR /app
ARG NPM_TOKEN
COPY package*.json ./
RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc \
&& npm ci \
&& rm .npmrc
COPY . .
CMD ["node", "server.js"]Next puzzle in UTC
One try. The first answer sticks.
How to fix it
Docker records the expanded RUN command, including the ARG value, in the image history and build cache. Deleting the file does not help. Use RUN --mount=type=secret so the token is never written to a layer.
Build arguments are not secret. When a RUN instruction uses ${NPM_TOKEN}, BuildKit stores the command with the value substituted, and docker history --no-trunc <image> prints it. Anyone who can pull the image can read the token, even though .npmrc was removed in the same step. The same goes for ENV, and for files you COPY in and delete in a later layer.
Fix
Use a BuildKit secret mount. The secret exists only for the duration of that one RUN and is never part of a layer or the history.
# syntax=docker/dockerfile:1
FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
npm ci
COPY . .
CMD ["node", "server.js"]
Build with docker build --secret id=npmrc,src=$HOME/.npmrc .. In CI, pass the secret from your secret store rather than a build argument. Then rotate the token that already leaked, since deleting the image tag does not remove copies that were already pulled.