|
1 | 1 | package lockfile |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "log" |
6 | | - "os" |
7 | | - "strings" |
8 | | - |
9 | | - "github.com/g-rath/osv-detector/internal/cachedregexp" |
| 4 | + "github.com/google/osv-scalibr/extractor/filesystem/language/ruby/gemfilelock" |
10 | 5 | ) |
11 | 6 |
|
12 | 7 | const BundlerEcosystem Ecosystem = "RubyGems" |
13 | 8 |
|
14 | | -const lockfileSectionBUNDLED = "BUNDLED WITH" |
15 | | -const lockfileSectionDEPENDENCIES = "DEPENDENCIES" |
16 | | -const lockfileSectionPLATFORMS = "PLATFORMS" |
17 | | -const lockfileSectionRUBY = "RUBY VERSION" |
18 | | -const lockfileSectionGIT = "GIT" |
19 | | -const lockfileSectionGEM = "GEM" |
20 | | -const lockfileSectionPATH = "PATH" |
21 | | -const lockfileSectionPLUGIN = "PLUGIN SOURCE" |
22 | | - |
23 | | -type parserState string |
24 | | - |
25 | | -const parserStateSource parserState = "source" |
26 | | -const parserStateDependency parserState = "dependency" |
27 | | -const parserStatePlatform parserState = "platform" |
28 | | -const parserStateRuby parserState = "ruby" |
29 | | -const parserStateBundledWith parserState = "bundled_with" |
30 | | - |
31 | | -func isSourceSection(line string) bool { |
32 | | - return strings.Contains(line, lockfileSectionGIT) || |
33 | | - strings.Contains(line, lockfileSectionGEM) || |
34 | | - strings.Contains(line, lockfileSectionPATH) || |
35 | | - strings.Contains(line, lockfileSectionPLUGIN) |
36 | | -} |
37 | | - |
38 | | -type gemfileLockfileParser struct { |
39 | | - state parserState |
40 | | - dependencies []PackageDetails |
41 | | - bundlerVersion string |
42 | | - rubyVersion string |
43 | | - |
44 | | - // holds the commit of the gem that is currently being parsed, if found |
45 | | - currentGemCommit string |
46 | | -} |
47 | | - |
48 | | -func (parser *gemfileLockfileParser) addDependency(name string, version string) { |
49 | | - parser.dependencies = append(parser.dependencies, PackageDetails{ |
50 | | - Name: name, |
51 | | - Version: version, |
52 | | - Ecosystem: BundlerEcosystem, |
53 | | - CompareAs: BundlerEcosystem, |
54 | | - Commit: parser.currentGemCommit, |
55 | | - }) |
56 | | -} |
57 | | - |
58 | | -func (parser *gemfileLockfileParser) parseSpec(line string) { |
59 | | - // nameVersionReg := cachedregexp.MustCompile(`^( {2}| {4}| {6})(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?(!)?$`) |
60 | | - nameVersionReg := cachedregexp.MustCompile(`^( +)(.*?)(?: \(([^-]*)(?:-(.*))?\))?(!)?$`) |
61 | | - |
62 | | - results := nameVersionReg.FindStringSubmatch(line) |
63 | | - |
64 | | - if results == nil { |
65 | | - return |
66 | | - } |
67 | | - |
68 | | - spaces := results[1] |
69 | | - |
70 | | - if spaces == "" { |
71 | | - log.Fatal("Weird error when parsing spec in Gemfile.lock (unexpectedly had no spaces) - please report this") |
72 | | - } |
73 | | - |
74 | | - if len(spaces) == 4 { |
75 | | - parser.addDependency(results[2], results[3]) |
76 | | - } |
77 | | -} |
78 | | - |
79 | | -func (parser *gemfileLockfileParser) parseSource(line string) { |
80 | | - if line == " specs" { |
81 | | - // todo: skip for now |
82 | | - return |
83 | | - } |
84 | | - |
85 | | - // OPTIONS = /^ ([a-z]+): (.*)$/i.freeze |
86 | | - optionsRegexp := cachedregexp.MustCompile(`(?i)^ {2}([a-z]+): (.*)$`) |
87 | | - |
88 | | - // todo: support |
89 | | - options := optionsRegexp.FindStringSubmatch(line) |
90 | | - |
91 | | - if options != nil { |
92 | | - commit := strings.TrimPrefix(options[0], " revision: ") |
93 | | - |
94 | | - // if the prefix was removed then the gem being parsed is git based, so |
95 | | - // we store the commit to be included later |
96 | | - if commit != options[0] { |
97 | | - parser.currentGemCommit = commit |
98 | | - } |
99 | | - |
100 | | - return |
101 | | - } |
102 | | - |
103 | | - // todo: source check |
104 | | - |
105 | | - parser.parseSpec(line) |
106 | | -} |
107 | | - |
108 | | -func isNotIndented(line string) bool { |
109 | | - re := cachedregexp.MustCompile(`^\S`) |
110 | | - |
111 | | - return re.MatchString(line) |
112 | | -} |
113 | | - |
114 | | -func (parser *gemfileLockfileParser) parseLineBasedOnState(line string) { |
115 | | - switch parser.state { |
116 | | - case parserStateDependency: |
117 | | - case parserStatePlatform: |
118 | | - break |
119 | | - case parserStateRuby: |
120 | | - parser.rubyVersion = strings.TrimSpace(line) |
121 | | - case parserStateBundledWith: |
122 | | - parser.bundlerVersion = strings.TrimSpace(line) |
123 | | - case parserStateSource: |
124 | | - parser.parseSource(line) |
125 | | - default: |
126 | | - log.Fatalf("Unknown supported '%s'\n", parser.state) |
127 | | - } |
128 | | -} |
129 | | - |
130 | | -func (parser *gemfileLockfileParser) parse(contents string) { |
131 | | - lineMatcher := cachedregexp.MustCompile(`(?:\r?\n)+`) |
132 | | - |
133 | | - lines := lineMatcher.Split(contents, -1) |
134 | | - |
135 | | - for _, line := range lines { |
136 | | - if isSourceSection(line) { |
137 | | - // clear the stateful package details, |
138 | | - // since we're now parsing a new group |
139 | | - parser.currentGemCommit = "" |
140 | | - parser.state = parserStateSource |
141 | | - parser.parseSource(line) |
142 | | - |
143 | | - continue |
144 | | - } |
145 | | - |
146 | | - switch line { |
147 | | - case lockfileSectionDEPENDENCIES: |
148 | | - parser.state = parserStateDependency |
149 | | - case lockfileSectionPLATFORMS: |
150 | | - parser.state = parserStatePlatform |
151 | | - case lockfileSectionRUBY: |
152 | | - parser.state = parserStateRuby |
153 | | - case lockfileSectionBUNDLED: |
154 | | - parser.state = parserStateBundledWith |
155 | | - default: |
156 | | - if isNotIndented(line) { |
157 | | - parser.state = "" |
158 | | - } |
159 | | - |
160 | | - if parser.state != "" { |
161 | | - parser.parseLineBasedOnState(line) |
162 | | - } |
163 | | - } |
164 | | - } |
165 | | -} |
166 | | - |
167 | 9 | func ParseGemfileLock(pathToLockfile string) ([]PackageDetails, error) { |
168 | | - var parser gemfileLockfileParser |
169 | | - |
170 | | - bytes, err := os.ReadFile(pathToLockfile) |
171 | | - |
172 | | - if err != nil { |
173 | | - return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err) |
174 | | - } |
175 | | - |
176 | | - parser.parse(string(bytes)) |
177 | | - |
178 | | - return parser.dependencies, nil |
| 10 | + return extract(pathToLockfile, gemfilelock.New(), BundlerEcosystem) |
179 | 11 | } |
0 commit comments