Backport from 1.5.x: Require valid module name (#373)

This commit is contained in:
Elbaz 2023-09-10 12:29:40 +03:00 committed by GitHub
parent c45a8b48ca
commit 065ca8fb3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 0 deletions

View File

@ -21,6 +21,7 @@ BUG FIXES:
* Transitive dependencies were lost during apply when the referenced resource expanded into zero instances ([#33403](https://github.com/hashicorp/terraform/issues/33403))
* OpenTF will no longer override SSH settings in local git configuration when installing modules. ([#33592](https://github.com/hashicorp/terraform/issues/33592))
* Handle file-operation errors in `internal/states/statemgr`. ([#278](https://github.com/opentffoundation/opentf/issues/278))
* `opentf init`: OpenTF will no longer allow downloading remote modules to invalid paths. ([#356](https://github.com/opentffoundation/opentf/issues/356))
## Previous Releases

View File

@ -16,6 +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/placeholderplaceholderplaceholder/opentf/internal/addrs"
"github.com/placeholderplaceholderplaceholder/opentf/internal/configs"
@ -162,6 +163,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)

View File

@ -139,6 +139,26 @@ 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)

View File

@ -0,0 +1,3 @@
output "boop" {
value = "beep"
}

View File

@ -0,0 +1,3 @@
module "../invalid" {
source = "./child"
}