Skip to content

Update to docker guidence#117

Open
nikiwycherley wants to merge 3 commits intomainfrom
update-to-docker-guidence
Open

Update to docker guidence#117
nikiwycherley wants to merge 3 commits intomainfrom
update-to-docker-guidence

Conversation

@nikiwycherley
Copy link

@nikiwycherley nikiwycherley commented Jan 30, 2026

Description

Updates Docker guidance to address SonarCloud security vulnerability related to file permissions in COPY commands.

Problem

SonarCloud flags COPY --chown=node:node commands with the error: "Make sure no write permissions are assigned to the copied resource." This occurs because giving the running user ownership of files grants write permissions, creating an unnecessary security risk - see https://rules.sonarsource.com/docker/type/Security%20Hotspot/RSPEC-6504/

Solution

Changed all COPY commands from --chown=node:node to --chown=root:root in Dockerfile examples. This ensures:

  • Files are owned by root:root
  • Container runs as non-root user (node)
  • Running user has no write permissions to application files
  • Satisfies security scanning requirements without additional chmod commands

Changes Made

  • Added new "Security best practices" section explaining file ownership in COPY commands
  • Updated multi-stage build example to use --chown=root:root in both development and production stages
  • Documented why this approach is more secure than alternatives

Files Changed

  • docs/guides/docker_guidance.md

Security Impact

This change improves security posture by ensuring containerized applications follow the principle of least privilege - the running user cannot modify its own application files.

@nikiwycherley nikiwycherley self-assigned this Jan 30, 2026
@pmshaw15 pmshaw15 changed the base branch from master to main February 2, 2026 09:03
Copy link
Member

@johnwatson484 johnwatson484 left a comment

Choose a reason for hiding this comment

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

Looks good, I think this will help a lot of our teams as I often see this discussed with varying decisions being made at the end.

It may also be worth opening a PR is the https://github.com/DEFRA/defra-docker-node and https://github.com/DEFRA/defra-docker-dotnetcore repos as the example Dockerfiles contradict this guidance?

I've added a few suggested tweaks too.

**Why this works:**
- Files are owned by `root:root`
- The container runs as the `node` user (non-root) via `USER node`
- Since the `node` user doesn't own the files, it has no write permissions by default
Copy link
Member

@johnwatson484 johnwatson484 Feb 16, 2026

Choose a reason for hiding this comment

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

I think this part is misleading as COPY will copy the files with permissions as they are. So if they have write access to that group/owner or everyone already, the COPY command won't alter that.

Perhaps adding a step to set the file permissions explicitly after but before switching to node user?

eg.

USER root
COPY --chown=root:root --from=development /home/node/app/ ./app/
COPY --chown=root:root --from=development /home/node/package*.json ./
RUN npm ci
RUN chmod -R a-w /home/node
USER node

- This satisfies security scanning requirements in tools like Sonarqube without needing explicit `chmod` commands

**Approaches to avoid:**
- `COPY --chown=node:node` - gives write permissions to the running user
Copy link
Member

@johnwatson484 johnwatson484 Feb 16, 2026

Choose a reason for hiding this comment

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

Depending on what the Node process is doing there may be scenarios where it needs to write data. Typically this would be to a mounted tmp or perhaps within a node_modules .cache directory, but that depends on the hosting environment for how these permissions can be set.

I don't think we need to go into specific use cases for explicit answers and we should absolutely favour no write permissions. But I think we should at least acknowledge that there are scenarios where write access is needed and that readers need to consider whether they have that need when following this guide.

Comment on lines +65 to +67
COPY --chown=root:root package*.json ./
RUN npm install
COPY --chown=node:node app/ ./app/
COPY --chown=root:root app/ ./app/
Copy link
Member

@johnwatson484 johnwatson484 Feb 16, 2026

Choose a reason for hiding this comment

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

Need to consider the impact on tests, nodemon etc here. If the node process is unable to write files then local development and testing may be compromised.

Perhaps suggest this only applies to the production build stage?

It will mean Sonar will continue to flag for a check of course. But I think that friction is better than the friction of not being able to develop in a container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants