Skip to content

Commit

Permalink
Check invalid environment replacement form like ${VAR:%}
Browse files Browse the repository at this point in the history
Environment replacement form like below are invalid, so they should
cause error.

- `${VAR:#pattern}`
- `${VAR:##pattern}`
- `${VAR:%pattern}`
- `${VAR:%%pattern}`

Signed-off-by: Mitsuru Kariya <[email protected]>
  • Loading branch information
kariya-mitsuru committed Jul 10, 2024
1 parent 5b5afdb commit 9749c52
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frontend/dockerfile/shell/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ func (sw *shellWord) processDollar() (string, error) {
fallthrough
case '+', '-', '?', '#', '%':
rawEscapes := ch == '#' || ch == '%'
if nullIsUnset && rawEscapes {
return "", errors.Errorf("unsupported modifier (%s) in substitution", chs)
}
word, _, err := sw.processStopOn('}', rawEscapes)
if err != nil {
if sw.scanner.Peek() == scanner.EOF {
Expand Down
20 changes: 20 additions & 0 deletions frontend/dockerfile/shell/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,26 @@ func TestProcessWithMatches(t *testing.T) {
expected: "xxyy",
matches: map[string]struct{}{"FOO": {}, "BAR": {}},
},
{
input: "${FOO:#}",
envs: map[string]string{},
expectedErr: true,
},
{
input: "${FOO:##}",
envs: map[string]string{},
expectedErr: true,
},
{
input: "${FOO:%}",
envs: map[string]string{},
expectedErr: true,
},
{
input: "${FOO:%%}",
envs: map[string]string{},
expectedErr: true,
},
{
// test: wildcards
input: "${FOO/$NEEDLE/.} - ${FOO//$NEEDLE/.}",
Expand Down

0 comments on commit 9749c52

Please sign in to comment.