Skip to content
12 changes: 12 additions & 0 deletions .devcontainer/Dockerfile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built the image like:

vscode ➜ /workspaces/workspaces/gitpod (clu/npm-security-hardening) $ docker build -f .devcontainer/Dockerfile .

Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ RUN curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh |
&& nvm alias default v${NODE_VERSION} \
&& npm install -g typescript yarn pnpm node-gyp @anthropic-ai/claude-code"

# Disable npm/yarn lifecycle scripts by default (security hardening)
# To allow specific packages, use: npm rebuild <package> or yarn rebuild <package>
RUN npm config set ignore-scripts true --location=user && \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is set for global:

npm config get ignore-scripts --location=global
true

But get an error at the user (default) level:

pm config get ignore-scripts --location=user
npm error code ENOWORKSPACES
npm error This command does not support workspaces.
npm error A complete log of this run can be found in: /root/.npm/_logs/2025-12-03T22_23_34_233Z-debug-0.log

What are we trying to accomplish here? Global makes sense to me, if we're talking a Dockerfile.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter part of the command seems okay:

cat $HOME/.yarnrc 
ignore-scripts true

But, I'm unsure of the former.

echo 'ignore-scripts true' >> ~/.yarnrc

# Disable npx (security hardening - prevents arbitrary package execution)
RUN rm -f /usr/bin/npx /usr/local/bin/npx && \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npx is also located as part of the nvm installation, we should remove it from here too:

Evidence:

vscode ➜ /workspaces/workspaces/gitpod (clu/npm-security-hardening) $ docker run -it abe5f93ace01
root / $ nvm which node
/root/.nvm/versions/node/v22.17.0/bin/node
root / $ which npx
/root/.nvm/versions/node/v22.17.0/bin/npx

echo '#!/bin/sh' > /usr/local/bin/npx && \
echo 'echo "npx is disabled for security reasons. Use explicit package installation instead." >&2' >> /usr/local/bin/npx && \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works:

root / $ /usr/local/bin/npx
npx is disabled for security reasons. Use explicit package installation instead.

echo 'exit 1' >> /usr/local/bin/npx && \
chmod +x /usr/local/bin/npx

ENV PATH=$PATH:/root/.aws-iam:/root/.terraform:/workspace/bin

### Telepresence ###
Expand Down
Loading