Skip to content
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
9 changes: 8 additions & 1 deletion internal/initwd/module_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/apparentlymart/go-versions/versions"
version "github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"

"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/configs/configload"
Expand Down Expand Up @@ -162,6 +162,13 @@ func (i *ModuleInstaller) moduleInstallWalker(ctx context.Context, manifest mods
return nil, nil, diags
}

if !hclsyntax.ValidIdentifier(req.Name) {
// A module with an invalid name shouldn't be installed at all. This is
// mostly a concern for remote modules, since we need to be able to convert
// the name to a valid path.
return nil, nil, diags
}

key := manifest.ModuleKey(req.Path)
instPath := i.packageInstallPath(req.Path)

Expand Down
21 changes: 21 additions & 0 deletions internal/initwd/module_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ func TestModuleInstaller_emptyModuleName(t *testing.T) {
}
}

func TestModuleInstaller_invalidModuleName(t *testing.T) {
fixtureDir := filepath.Clean("testdata/invalid-module-name")
dir, done := tempChdir(t, fixtureDir)
defer done()

hooks := &testInstallHooks{}

modulesDir := filepath.Join(dir, ".terraform/modules")

loader, close := configload.NewLoaderForTests(t)
defer close()
inst := NewModuleInstaller(modulesDir, loader, registry.NewClient(nil, nil))
_, diags := inst.InstallModules(context.Background(), dir, "tests", false, false, hooks)

if !diags.HasErrors() {
t.Fatal("expected error")
} else {
assertDiagnosticSummary(t, diags, "Invalid module instance name")
}
}

func TestModuleInstaller_packageEscapeError(t *testing.T) {
fixtureDir := filepath.Clean("testdata/load-module-package-escape")
dir, done := tempChdir(t, fixtureDir)
Expand Down
3 changes: 3 additions & 0 deletions internal/initwd/testdata/invalid-module-name/child/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "boop" {
value = "beep"
}
3 changes: 3 additions & 0 deletions internal/initwd/testdata/invalid-module-name/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "../invalid" {
source = "./child"
}