Skip to content

Commit e859d10

Browse files
authored
feat: allow root break for mapfs (#4094)
1 parent a6ef37f commit e859d10

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

pkg/fanal/artifact/local/fs.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ func (a Artifact) buildFS(dir, filePath string, info os.FileInfo, files *syncx.M
242242

243243
// Create fs.FS for each post-analyzer that wants to analyze the current file
244244
for _, at := range atypes {
245-
mfs, _ := files.LoadOrStore(at, mapfs.New())
245+
// Since filesystem scanning may require access outside the specified path, (e.g. Terraform modules)
246+
// it allows "../" access with "WithUnderlyingRoot".
247+
mfs, _ := files.LoadOrStore(at, mapfs.New(mapfs.WithUnderlyingRoot(dir)))
246248
if d := filepath.Dir(filePath); d != "." {
247249
if err := mfs.MkdirAll(d, os.ModePerm); err != nil && !errors.Is(err, fs.ErrExist) {
248250
return xerrors.Errorf("mapfs mkdir error: %w", err)

pkg/mapfs/fs.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,26 @@ var _ allFS = &FS{}
2828
// FS is an in-memory filesystem
2929
type FS struct {
3030
root *file
31+
32+
// When the underlyingRoot has a value, it allows access to the local filesystem outside of this in-memory filesystem.
33+
// The set path is used as the starting point when accessing the local filesystem.
34+
// In other words, although mapfs.Open("../foo") would normally result in an error, if this option is enabled,
35+
// it will be executed as os.Open(filepath.Join(underlyingRoot, "../foo")).
36+
underlyingRoot string
37+
}
38+
39+
type Option func(*FS)
40+
41+
// WithUnderlyingRoot returns an option to set the underlying root path for the in-memory filesystem.
42+
func WithUnderlyingRoot(root string) Option {
43+
return func(fsys *FS) {
44+
fsys.underlyingRoot = root
45+
}
3146
}
3247

3348
// New creates a new filesystem
34-
func New() *FS {
35-
return &FS{
49+
func New(opts ...Option) *FS {
50+
fsys := &FS{
3651
root: &file{
3752
stat: fileStat{
3853
name: ".",
@@ -43,6 +58,10 @@ func New() *FS {
4358
files: syncx.Map[string, *file]{},
4459
},
4560
}
61+
for _, opt := range opts {
62+
opt(fsys)
63+
}
64+
return fsys
4665
}
4766

4867
// Filter removes the specified skippedFiles and returns a new FS
@@ -57,7 +76,7 @@ func (m *FS) Filter(skippedFiles []string) (*FS, error) {
5776
}
5877

5978
func (m *FS) FilterFunc(fn func(path string, d fs.DirEntry) (bool, error)) (*FS, error) {
60-
newFS := New()
79+
newFS := New(WithUnderlyingRoot(m.underlyingRoot))
6180
err := fs.WalkDir(m, ".", func(path string, d fs.DirEntry, err error) error {
6281
if err != nil {
6382
return err
@@ -103,6 +122,10 @@ func (m *FS) CopyFilesUnder(dir string) error {
103122

104123
// Stat returns a FileInfo describing the file.
105124
func (m *FS) Stat(name string) (fs.FileInfo, error) {
125+
if strings.HasPrefix(name, "../") && m.underlyingRoot != "" {
126+
return os.Stat(filepath.Join(m.underlyingRoot, name))
127+
}
128+
106129
name = cleanPath(name)
107130
f, err := m.root.getFile(name)
108131
if err != nil {
@@ -121,11 +144,17 @@ func (m *FS) Stat(name string) (fs.FileInfo, error) {
121144
// ReadDir reads the named directory
122145
// and returns a list of directory entries sorted by filename.
123146
func (m *FS) ReadDir(name string) ([]fs.DirEntry, error) {
147+
if strings.HasPrefix(name, "../") && m.underlyingRoot != "" {
148+
return os.ReadDir(filepath.Join(m.underlyingRoot, name))
149+
}
124150
return m.root.ReadDir(cleanPath(name))
125151
}
126152

127153
// Open opens the named file for reading.
128154
func (m *FS) Open(name string) (fs.File, error) {
155+
if strings.HasPrefix(name, "../") && m.underlyingRoot != "" {
156+
return os.Open(filepath.Join(m.underlyingRoot, name))
157+
}
129158
return m.root.Open(cleanPath(name))
130159
}
131160

@@ -158,6 +187,10 @@ func (m *FS) MkdirAll(path string, perm fs.FileMode) error {
158187
// The caller is permitted to modify the returned byte slice.
159188
// This method should return a copy of the underlying data.
160189
func (m *FS) ReadFile(name string) ([]byte, error) {
190+
if strings.HasPrefix(name, "../") && m.underlyingRoot != "" {
191+
return os.ReadFile(filepath.Join(m.underlyingRoot, name))
192+
}
193+
161194
f, err := m.root.Open(cleanPath(name))
162195
if err != nil {
163196
return nil, err

0 commit comments

Comments
 (0)