There is a risk if you answered yes to any of those questions.
== Recommended Secure Coding Practices
In the Dockerfile:
* Create a new default user and use it with the `USER` statement.
** Some container maintainers create a specific user to be used without explicitly setting it as default, such as `postgresql` or `zookeeper`. It is recommended to use these users instead of root.
** On Windows containers, the `ContainerUser` is available for this purpose.
Or, at launch time:
* Use the `user` argument when calling Docker or in the docker-compose file.
* Add fine-grained Linux capabilities to perform specific actions that require root privileges.
If this image is already explicitly set to launch with a non-privileged user, you can
add it to the safe images list rule property of your SonarQube instance, without the tag.
== Sensitive Code Example
For any image that does not provide a user by default, regardless of their
underlying operating system:
[source,docker]
----
# Sensitive
FROM alpine
ENTRYPOINT ["id"]
----
For multi-stage builds, the last stage is non-compliant if it does not contain
For images that use `scratch` as their base, it is not possible to add non-privileged
users by default. To do this, add an additional build stage to add the group
and user, and later copy `/etc/passwd`.
Here is an example that uses `adduser` in the first stage to generate a user and add it to the `/etc/passwd` file. In the next stage, this user is added by copying that file over from the previous stage:
* https://hub.docker.com/r/nginxinc/nginx-unprivileged[nginxinc/nginx-unprivileged: Example of a non-root container by default]
* https://learn.microsoft.com/en-us/virtualization/windowscontainers/manage-containers/container-security#when-to-use-containeradmin-and-containeruser-user-accounts[Microsoft docs, When to use ContainerAdmin and ContainerUser user accounts]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Noncompliant scratch images: "Scratch images run as root by default. Make sure it is safe here."
* Official noncompliant image: "The `image` image runs with root as the default user. Make sure it is safe here."
* Microsoft non-compliant images: "This image runs with root or containerAdministrator as the default user. Make sure it is safe here."
* Unofficial noncompliant image: "This image might run with root as the default user. Make sure it is safe here."
* The last USER is root or containerAdministrator: "Setting the default user as root might unnecessarily make the application unsafe. Make sure it is safe here."
=== Highlighting
* Unsafe FROM: "FROM name" instruction
* Last USER is root or containerAdministrator: "USER name" instruction