Skip to content

Commit

Permalink
disallow services starting with "-"
Browse files Browse the repository at this point in the history
- consolidate svc / -svc validation in CombineLayers
  • Loading branch information
barrettj12 committed Jun 30, 2023
1 parent 439165f commit 1d97914
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions internals/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,7 @@ func CombineLayers(layers ...*Layer) (*Layer, error) {
if serviceName == "all" || serviceName == "-all" {
continue
}
if _, ok := combined.Services[serviceName]; ok {
continue
}
// Check if this is `-svc`
// This could be `-svc` - try to trim a preceding
serviceName = strings.TrimPrefix(serviceName, "-")
if _, ok := combined.Services[serviceName]; ok {
continue
Expand Down Expand Up @@ -932,6 +929,11 @@ func ParseLayer(order int, label string, data []byte) (*Layer, error) {
if name == "all" || name == "default" || name == "none" {
logger.Noticef("WARNING: using %q as service name is deprecated", name)
}
if strings.HasPrefix(name, "-") {
return nil, &FormatError{
Message: fmt.Sprintf(`cannot use service name %q: starting with "-" not allowed`, name),
}
}
if service == nil {
return nil, &FormatError{
Message: fmt.Sprintf("service object cannot be null for service %q", name),
Expand Down
9 changes: 9 additions & 0 deletions internals/plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,15 @@ var planTests = []planTest{{
services: [nonexistent]
override: merge
`},
}, {
summary: `Service name can't start with "-"`,
error: `cannot use service name "-svc1": starting with "-" not allowed`,
input: []string{`
services:
-svc1:
command: foo
override: merge
`},
}}

func (s *S) TestParseLayer(c *C) {
Expand Down

0 comments on commit 1d97914

Please sign in to comment.