Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARIAC-1856 Modify S7019: add EXEC alternatives and exceptions #4597

Merged
merged 9 commits into from
Jan 8, 2025
34 changes: 34 additions & 0 deletions rules/S7019/docker/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ This can cause problems when trying to gracefully stop containers because the ma
Moreover, the exec form provides more control and predictability over the execution of the command.
It does not invoke a command shell, which means it does not have the potential side effects of shell processing.


=== Exceptions

The exec form does not allow shell features such as variable expansion, piping (`|`) and command chaining (`&&`, `||`, `;`).
In case you need to use these features, there are a few alternatives:
* Creation of a wrapper script
* Explicitly specify the shell to use with the `SHELL` instruction before `CMD` or `ENTRYPOINT`


This rule will not raise an issue if the `SHELL` instruction is used before the `CMD` or `ENTRYPOINT` instruction, as we consider this a conscious decision.

== How to fix it

=== Code examples
Expand All @@ -22,6 +33,14 @@ FROM scratch
ENTRYPOINT echo "Welcome!"
----

[source,docker,diff-id=2,diff-type=noncompliant]
----
FROM scratch
ENTRYPOINT echo "Long script with chaining commands" \
&& echo "Welcome!" \
&& echo "Goodbye"
----

==== Compliant solution

[source,docker,diff-id=1,diff-type=compliant]
Expand All @@ -30,6 +49,21 @@ FROM scratch
ENTRYPOINT ["echo", "Welcome!"]
----

[source,docker,diff-id=2,diff-type=compliant]
----
FROM scratch
SHELL ["/bin/bash", "-c"]
ENTRYPOINT echo "Long script with chaining commands" \
&& echo "Welcome!" \
&& echo "Goodbye"
----

[source,docker,diff-id=2,diff-type=compliant]
----
FROM scratch
ENTRYPOINT ["/entrypoint.sh"]
----

== Resources
=== Documentation

Expand Down
Loading