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

fix: avoid OOM and infinite loops in moduleInfo.load() #3769

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
64 changes: 38 additions & 26 deletions server/events/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,51 @@ func (t tfFs) ReadDir(dirname string) ([]os.FileInfo, error) {
var _ tfconfig.FS = tfFs{}

func (m moduleInfo) load(files fs.FS, dir string, projects ...string) (_ *module, diags tfconfig.Diagnostics) {
if _, set := m[dir]; !set {
tfFiles := tfFs{files}
var mod *tfconfig.Module
mod, diags = tfconfig.LoadModuleFromFilesystem(tfFiles, dir)

deps := make(map[string]bool)
if mod != nil {
for _, c := range mod.ModuleCalls {
mPath := path.Join(dir, c.Source)
if !tfconfig.IsModuleDirOnFilesystem(tfFiles, mPath) {
continue
dependenciesHandled := map[string]struct{}{dir: {}} // Only look at each dependency once
todo := []string{dir}

for len(todo) > 0 {
dir := todo[len(todo)-1] // order of processing list not important, pick last for efficiency
todo = todo[:len(todo)-1]

if _, set := m[dir]; !set {
tfFiles := tfFs{files}
var mod *tfconfig.Module
mod, diag := tfconfig.LoadModuleFromFilesystem(tfFiles, dir)
if diag != nil {
diags = append(diags, diag...)
}

deps := make(map[string]bool)
if mod != nil {
for _, c := range mod.ModuleCalls {
mPath := path.Join(dir, c.Source)
if !tfconfig.IsModuleDirOnFilesystem(tfFiles, mPath) {
continue
}
deps[mPath] = true
}
deps[mPath] = true
}

m[dir] = &module{
path: dir,
dependencies: deps,
projects: make(map[string]bool),
}
}

m[dir] = &module{
path: dir,
dependencies: deps,
projects: make(map[string]bool),
// set projects on my dependencies
for dep := range m[dir].dependencies {
if _, exists := dependenciesHandled[dep]; !exists {
dependenciesHandled[dep] = struct{}{}
todo = append(todo, dep)
}
}
}
// set projects on my dependencies
for dep := range m[dir].dependencies {
_, err := m.load(files, dep, projects...)
if err != nil {
diags = append(diags, err...)
// add projects to the list of dependant projects
for _, p := range projects {
m[dir].projects[p] = true
}
}
// add projects to the list of dependant projects
for _, p := range projects {
m[dir].projects[p] = true
}
return m[dir], diags
}

Expand Down