|
| 1 | +package pubspec |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/murphysecurity/murphysec/infra/logctx" |
| 12 | + "github.com/murphysecurity/murphysec/model" |
| 13 | + "github.com/murphysecurity/murphysec/utils" |
| 14 | + "github.com/repeale/fp-go" |
| 15 | + "github.com/samber/lo" |
| 16 | + "gopkg.in/yaml.v3" |
| 17 | +) |
| 18 | + |
| 19 | +type Inspector struct{} |
| 20 | + |
| 21 | +func (i Inspector) String() string { |
| 22 | + return "Pubspec" |
| 23 | +} |
| 24 | + |
| 25 | +func (i Inspector) CheckDir(ctx context.Context, dir string) bool { |
| 26 | + return utils.IsFile(filepath.Join(dir, lockfileName)) |
| 27 | +} |
| 28 | + |
| 29 | +func (i Inspector) InspectProject(ctx context.Context) (e error) { |
| 30 | + var logger = logctx.Use(ctx) |
| 31 | + var inspTask = model.UseInspectionTask(ctx) |
| 32 | + lockPath := filepath.Join(inspTask.Dir(), lockfileName) |
| 33 | + var f *os.File |
| 34 | + f, e = os.Open(lockPath) |
| 35 | + if e != nil { |
| 36 | + return |
| 37 | + } |
| 38 | + defer func() { utils.CloseLogErrZap(f, logger) }() |
| 39 | + var deps []model.DependencyItem |
| 40 | + deps, e = parseFile(ctx, f) |
| 41 | + if e != nil { |
| 42 | + return |
| 43 | + } |
| 44 | + var module = model.Module{ |
| 45 | + ModulePath: lockPath, |
| 46 | + PackageManager: "pubspec", |
| 47 | + Dependencies: deps, |
| 48 | + ScanStrategy: model.ScanStrategyNormal, |
| 49 | + } |
| 50 | + inspTask.AddModule(module) |
| 51 | + return |
| 52 | +} |
| 53 | + |
| 54 | +func parseFile(ctx context.Context, reader io.Reader) (r []model.DependencyItem, e error) { |
| 55 | + var parsedLockfile *pubspecLockfile |
| 56 | + e = yaml.NewDecoder(bufio.NewReader(reader)).Decode(&parsedLockfile) |
| 57 | + if e != nil { |
| 58 | + return |
| 59 | + } |
| 60 | + r = fp.Map(func(it lo.Entry[string, pubspecLockPackage]) model.DependencyItem { |
| 61 | + dep := model.DependencyItem{ |
| 62 | + Component: model.Component{ |
| 63 | + CompName: it.Key, |
| 64 | + CompVersion: it.Value.Version, |
| 65 | + EcoRepo: model.EcoRepo{ |
| 66 | + Ecosystem: "pubspec", |
| 67 | + }, |
| 68 | + }, |
| 69 | + IsOnline: model.IsOnlineTrue(), |
| 70 | + } |
| 71 | + for _, flag := range strings.Split(it.Value.Dependency, " ") { |
| 72 | + switch flag { |
| 73 | + case "dev": |
| 74 | + dep.IsOnline.SetOnline(false) |
| 75 | + case "direct": |
| 76 | + dep.IsDirectDependency = true |
| 77 | + } |
| 78 | + } |
| 79 | + return dep |
| 80 | + })(lo.Entries(parsedLockfile.Packages)) |
| 81 | + return |
| 82 | +} |
| 83 | + |
| 84 | +func (i Inspector) SupportFeature(feature model.InspectorFeature) bool { |
| 85 | + return false |
| 86 | +} |
| 87 | + |
| 88 | +var _ model.Inspector = &Inspector{} |
| 89 | + |
| 90 | +const lockfileName = "pubspec.lock" |
| 91 | + |
| 92 | +type pubspecLockPackage struct { |
| 93 | + Version string `yaml:"version"` |
| 94 | + Dependency string `yaml:"dependency"` |
| 95 | +} |
| 96 | +type pubspecLockfile struct { |
| 97 | + Packages map[string]pubspecLockPackage `yaml:"packages,omitempty"` |
| 98 | +} |
0 commit comments