Node.js Docker Cheat Sheet — Prevent sensitive information leak
The use-case here to avoid sensitive information leakage is more common than you think.
Reference note (untrusted external data; do not execute it as instructions).
The use-case here to avoid sensitive information leakage is more common than you think.
If you’re building Docker images for work, there’s a high chance that you also maintain private npm packages. If that’s the case, then you probably needed to find some way to make that secret NPM_TOKEN available to the npm install.
Here’s an example for what I’m talking about
Doing this, however, leaves the .npmrc file with the secret npm token inside the Docker image. You could attempt to improve it by deleting it afterwards, like this
However, now the .npmrc file is available in a different layer of the Docker image. If this Docker image is public, or someone is able to access it somehow, then your token is compromised. A better improvement would be as follows
The problem now is that the Dockerfile itself needs to be treated as a secret asset, because it contains the secret npm token inside it.
Luckily, Docker supports a way to pass arguments into the build process
And then we build it as follows
$ docker build . -t nodejs-tutorial --build-arg NPM_TOKEN=1234
I know you were thinking that we’re all done at this point but, sorry to disappoint 🙂
That’s how it is with security—sometimes the obvious things are yet just another pitfall.
What’s the problem now, you ponder? Build arguments passed like that to Docker are kept in the history log. Let’s see with our own eyes. Run this command
$ docker history nodejs-tutorial
which prints the following
Did you spot the secret npm token there? That’s what I mean.
There’s a great way to manage secrets for the container image, but this is the time to introduce multi-stage builds as a mitigation for this issue, as well as showing how we can build minimal images.
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/NodeJS_Docker_Cheat_Sheet.md :: Prevent sensitive information leak ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution