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

limit identifiers to word chars, dashes, and dots #43

Merged
merged 1 commit into from
Jun 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions load/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ package load

import (
"fmt"
"regexp"

"github.com/asteris-llc/converge/resource"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
)

var (
nameRe = regexp.MustCompile(`^[\w\-\.]+$`)
)

// Parse parses a module and returns it
func Parse(content []byte) (*resource.Module, error) {
f, err := hcl.ParseBytes(content)
Expand Down Expand Up @@ -77,6 +82,12 @@ func parseModule(node ast.Node) (*resource.Module, error) {
return n, false
}

// validate the name
if !nameRe.MatchString(resource.Name()) {
errs = append(errs, &ParseError{item.Pos(), fmt.Sprintf("invalid name %q", resource.Name())})
return n, false
}

// check if the name is already present, error if so
dupCheckName := token + "." + resource.Name()
if present := names[dupCheckName]; present {
Expand Down
13 changes: 13 additions & 0 deletions load/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ func TestParseDuplicateTask(t *testing.T) {
}
}

func TestBadName(t *testing.T) {
t.Parallel()

_, err := load.Parse([]byte(badName))
if assert.Error(t, err) {
assert.EqualError(t, err, `At 2:1: invalid name "a b"`)
}
}

var (
basicModule = `
param "filename" { }
Expand Down Expand Up @@ -107,5 +116,9 @@ module "x" "y" {
duplicateTask = `
task "x" { }
task "x" { }
`

badName = `
task "a b" { }
`
)