From b6010806c7624ddc505e7095ac51d9880ec7045c Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 13 Jun 2024 08:53:29 +1000 Subject: [PATCH] fix: `AbsModuleDirs()` wasn't correctly computing directories (#1755) --- common/projectconfig/projectconfig.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/common/projectconfig/projectconfig.go b/common/projectconfig/projectconfig.go index 95880206af..a0cde56aee 100644 --- a/common/projectconfig/projectconfig.go +++ b/common/projectconfig/projectconfig.go @@ -37,15 +37,28 @@ type Config struct { FTLMinVersion string `toml:"ftl-min-version"` } +// Root directory of the project. +func (c Config) Root() string { + if !filepath.IsAbs(c.Path) { + panic(fmt.Errorf("project config path must be absolute: %s", c.Path)) + } + return filepath.Dir(c.Path) +} + // AbsModuleDirs returns the absolute path for the module-dirs field from the ftl-project.toml, unless // that is not defined, in which case it defaults to the root directory. func (c Config) AbsModuleDirs() []string { if len(c.ModuleDirs) == 0 { return []string{filepath.Dir(c.Path)} } + root := c.Root() absDirs := make([]string, len(c.ModuleDirs)) for i, dir := range c.ModuleDirs { - absDirs[i], _ = filepath.Abs(dir) + cleaned := filepath.Clean(filepath.Join(root, dir)) + if !strings.HasPrefix(cleaned, root) { + panic(fmt.Errorf("module-dirs path %q is not within the project root %q", dir, root)) + } + absDirs[i] = cleaned } return absDirs } @@ -132,8 +145,15 @@ func Load(ctx context.Context, path string) (Config, error) { if config.FTLMinVersion != "" && !ftl.IsVersionAtLeastMin(ftl.Version, config.FTLMinVersion) { return config, fmt.Errorf("FTL version %q predates the minimum version %q", ftl.Version, config.FTLMinVersion) } - config.Path = path + + for _, dir := range config.ModuleDirs { + absDir := filepath.Clean(filepath.Join(config.Root(), dir)) + if !strings.HasPrefix(absDir, config.Root()) { + return Config{}, fmt.Errorf("module-dirs path %q is not within the project root %q", dir, config.Root()) + } + } + return config, nil }