Skip to content

Commit 614ec72

Browse files
petertrrsonartech
authored andcommitted
SONARIAC-2493 Variables in Docker should not start with a dash (#656)
GitOrigin-RevId: 0b2e6136176dcf5c25151a6c26d2f67c8ba0bb97
1 parent b4a9d56 commit 614ec72

File tree

8 files changed

+26
-6
lines changed

8 files changed

+26
-6
lines changed

iac-extensions/docker/src/main/java/org/sonar/iac/docker/parser/grammar/DockerLexicalConstant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public final class DockerLexicalConstant {
3030
public static final String RBRACKET_END_EXEC_FORM = "\\](?=[" + LexicalConstant.WHITESPACE + "]*+(?:[\r\n]|$))";
3131

3232
// ** IDENTIFIERS **
33-
private static final String VAR_IDENTIFIER_START = "[a-zA-Z_\\-0-9]";
33+
private static final String VAR_IDENTIFIER_START = "[a-zA-Z_0-9]";
3434
public static final String VAR_IDENTIFIER = VAR_IDENTIFIER_START + "++";
35-
public static final String ENCAPS_VAR_MODIFIER_SEPARATOR = ":(-|\\+)?";
35+
public static final String ENCAPS_VAR_MODIFIER_SEPARATOR = "(:[-+=?]?|[-+=?]|[#%/^,@]{1,2}|/#|/%)";
3636
public static final String ENCAPS_VAR_MODIFIER_GENERIC = "(\\\\}|[^}])+";
3737
public static final String FLAG_NAME = "[a-z][-a-z]*+";
3838

iac-extensions/docker/src/test/java/org/sonar/iac/docker/tree/impl/AddInstructionImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ void matchingSimple() {
4545
.matches("ADD \"src\" \"dest\"")
4646
.matches("ADD --option= src dest")
4747
.matches("ADD ${myadd:-test} dest")
48+
.matches("ADD ${myadd%%[a-z]+} dest")
4849

4950
.notMatches("ADD--option= src dest")
5051
.notMatches("ADDD --option= src dest")
5152
.notMatches("ADD")
5253
.notMatches("ADD ")
53-
.notMatches("ADD ${myadd%%[a-z]+} dest")
5454
.notMatches("ADD --option=value");
5555
}
5656

iac-extensions/docker/src/test/java/org/sonar/iac/docker/tree/impl/CopyInstructionImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ void matchingSimple() {
5959
.matches("COPY <<-EOT\n mkdir -p foo/bar\nEOT")
6060
.matches("COPY <<\"EOT\"\n mkdir -p foo/bar\nEOT")
6161
.matches("COPY ${mycopy:-test} dest")
62+
.matches("COPY ${mycopy%%[a-z]+} dest")
6263
.matches("COPY --chmod=755 <<'EOF' file.sh\necho \"Hello, World!\"\nEOF")
64+
6365
.notMatches("COPY <EOT\n mkdir -p foo/bar\nEOT")
6466
.notMatches("COPY <<EOT\n mkdir -p foo/bar\nEOT5")
6567
.notMatches("COPY--option= src dest")
6668
.notMatches("COPYY --option= src dest")
6769
.notMatches("COPY")
6870
.notMatches("COPY ")
69-
.notMatches("COPY ${mycopy%%[a-z]+} dest")
7071
.notMatches("COPY --option=value");
7172

7273
for (char c : FORBIDDEN_CHARACTERS_AFTER_KEYWORD) {

iac-extensions/docker/src/test/java/org/sonar/iac/docker/tree/impl/EncapsulatedVariableImplTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,33 @@ void shouldParseEncapsulatedVariable() {
3737
.matches("${F2}")
3838
.matches("${foo:-$bar}")
3939
.matches("${foo:-}")
40+
.matches("${foo-bar}")
4041
.matches("${foo:+$bar}")
4142
.matches("${foo:+'bar'}")
4243
.matches("${foo:+\"bar\"}")
4344
.matches("${foo:+bar}")
45+
.matches("${foo+bar}")
4446
.matches("${foo:+}")
4547
.matches("${foo:+bar$bar}")
4648
.matches("${foo:+${bar}}")
4749
.matches("${foo:+${bar:-'foobar'}}")
4850
.matches("${foo:+${bar:-${foobar:+'foobar'}}}")
4951
.matches("${23}")
5052
.matches("${foo:*$bar}")
53+
.matches("${foo?bar}")
54+
.matches("${foo:?bar}")
55+
.matches("${foo/bar/baz}")
56+
.matches("${foo//bar/baz}")
57+
.matches("${foo/#bar/baz}")
58+
.matches("${foo/%bar/baz}")
59+
.matches("${foo#bar}")
60+
.matches("${foo##bar}")
61+
.matches("${foo%bar}")
62+
.matches("${foo%%bar}")
63+
.matches("${foo^bar}")
64+
.matches("${foo^^bar}")
65+
.matches("${foo,bar}")
66+
.matches("${foo@U}")
5167

5268
.notMatches("$foo")
5369
.notMatches("${foo:+bar $bar}")

iac-extensions/docker/src/test/java/org/sonar/iac/docker/tree/impl/RegularVariableImplTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void shouldParseRegularVariable() {
3535
.matches("$1")
3636

3737
.notMatches("$foo=")
38+
.notMatches("$foo-bar")
3839
.notMatches("$foo.bar");
3940
}
4041

iac-extensions/docker/src/test/java/org/sonar/iac/docker/tree/impl/ShellFormImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ void shouldParseShellForm() {
4040
.matches(" $var")
4141
.matches(" ${var}")
4242
.matches(" ${var:-test}")
43+
.matches(" ${var%%[a-z+]}")
4344
.matches(" [ \"foo\", \"bar\" ] garbage")
4445
.matches(" [ \"foo\", \"bar\" ]garbage no space and multiple words")
4546

4647
.notMatches(" [ \"/bin/bash”, “-c” ]")
47-
.notMatches(" ${var%%[a-z+]}")
4848
.notMatches("ls -a")
4949
.notMatches("");
5050
}

iac-extensions/docker/src/test/java/org/sonar/iac/docker/tree/impl/VolumeInstructionImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ void matchingSimple() {
4141
.matches("VOLUME $myvolume")
4242
.matches("VOLUME ${myvolume}")
4343
.matches("VOLUME ${myvolume:-test}")
44-
.notMatches("VOLUME ${myvolume%%[a-z]+}")
44+
.matches("VOLUME ${myvolume%%[a-z]+}")
45+
4546
.notMatches("VOLUME")
4647
.notMatches("VOLUME ")
4748
.notMatches("VOLUMEE 80");

iac-extensions/docker/src/test/resources/checks/ShellFormOverExecFormCheck/dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ HEALTHCHECK CMD curl --fail http://localhost:8080/healthcheck || exit 1
2222
# FNs: exec form with explicit shell invocation are not supported
2323
CMD ["sh", "-c", "echo $message"]
2424
CMD ["sh", "-c", "echo ${message-default}"]
25+
CMD ["sh", "-c", "echo ${message:-default}"]
2526
CMD ["sh", "-c", "echo ${message:=default}"]
2627
CMD ["sh", "-c", "echo ${message:+var}"]
2728
CMD ["sh", "-c", "echo ${message:7}"]

0 commit comments

Comments
 (0)