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

v1.5 backport of initwd: require valid module name #33769

Merged
merged 3 commits into from
Aug 30, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.5.7 (Unreleased)

BUG FIXES:

* `terraform init`: Terraform will no longer allow downloading remote modules to invalid paths. [#33745]

## 1.5.6 (August 23, 2023)

BUG FIXES:
Expand Down
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 @@ -156,6 +156,13 @@ func (i *ModuleInstaller) installDescendentModules(ctx context.Context, rootMod
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 @@ -139,6 +139,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, 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"
}
Loading