forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPM.qll
More file actions
333 lines (268 loc) · 11 KB
/
NPM.qll
File metadata and controls
333 lines (268 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Provides classes for working with NPM module definitions and dependencies.
*/
import javascript
private import NodeModuleResolutionImpl
/** A `package.json` configuration object. */
class PackageJSON extends JSONObject {
PackageJSON() {
getJsonFile().getBaseName() = "package.json" and
isTopLevel()
}
/** Gets the name of this package. */
string getPackageName() { result = getPropStringValue("name") }
/** Gets the version of this package. */
string getVersion() { result = getPropStringValue("version") }
/** Gets the description of this package. */
string getDescription() { result = getPropStringValue("description") }
/** Gets the array of keywords for this package. */
JSONArray getKeywords() { result = getPropValue("keywords") }
/** Gets a keyword for this package. */
string getAKeyword() { result = getKeywords().getElementStringValue(_) }
/** Gets the homepage URL of this package. */
string getHomepage() { result = getPropStringValue("homepage") }
/** Gets the bug tracker information of this package. */
BugTrackerInfo getBugs() { result = getPropValue("bugs") }
/** Gets the license information of this package. */
string getLicense() { result = getPropStringValue("license") }
/** Gets the author information of this package. */
ContributorInfo getAuthor() { result = getPropValue("author") }
/** Gets information for a contributor to this package. */
ContributorInfo getAContributor() {
result = getPropValue("contributors").(JSONArray).getElementValue(_)
}
/** Gets the array of files for this package. */
JSONArray getFiles() { result = getPropValue("files") }
/** Gets a file for this package. */
string getAFile() { result = getFiles().getElementStringValue(_) }
/** Gets the main module of this package. */
string getMain() { result = getPropStringValue("main") }
/** Gets the path of a command defined for this package. */
string getBin(string cmd) {
cmd = getPackageName() and result = getPropStringValue("bin")
or
result = getPropValue("bin").(JSONObject).getPropStringValue(cmd)
}
/** Gets a manual page for this package. */
string getAManFile() {
result = getPropStringValue("man") or
result = getPropValue("man").(JSONArray).getElementStringValue(_)
}
/** Gets information about the directories of this package. */
JSONObject getDirectories() { result = getPropValue("directories") }
/** Gets repository information for this package. */
RepositoryInfo getRepository() { result = getPropValue("repository") }
/** Gets information about the scripts of this package. */
JSONObject getScripts() { result = getPropValue("scripts") }
/** Gets configuration information for this package. */
JSONObject getConfig() { result = getPropValue("config") }
/** Gets the dependencies of this package. */
PackageDependencies getDependencies() { result = getPropValue("dependencies") }
/** Gets the development dependencies of this package. */
PackageDependencies getDevDependencies() { result = getPropValue("devDependencies") }
/** Gets the peer dependencies of this package. */
PackageDependencies getPeerDependencies() { result = getPropValue("peerDependencies") }
/** Gets the bundled dependencies of this package. */
PackageDependencies getBundledDependencies() {
result = getPropValue("bundledDependencies") or
result = getPropValue("bundleDependencies")
}
/** Gets the optional dependencies of this package. */
PackageDependencies getOptionalDependencies() { result = getPropValue("optionalDependencies") }
/**
* Gets a JSON object describing a group of dependencies of
* this package of the kind specified by `depkind`:
* `""` for normal dependencies, `"dev"` for `devDependencies`,
* `"bundled"` for `bundledDependencies` and `"opt"` for
* `optionalDependencies`.
*/
PackageDependencies getADependenciesObject(string depkind) {
result = getDependencies() and depkind = ""
or
result = getDevDependencies() and depkind = "dev"
or
result = getBundledDependencies() and depkind = "bundled"
or
result = getOptionalDependencies() and depkind = "opt"
}
/**
* Holds if this package declares a dependency (including
* optional, development and bundled dependencies) on the given version
* of the given package.
*
* This does _not_ consider peer dependencies, which are semantically
* different from the other dependency types.
*/
predicate declaresDependency(string pkg, string version) {
getADependenciesObject(_).getADependency(pkg, version)
}
/** Gets the engine dependencies of this package. */
PackageDependencies getEngines() { result = getPropValue("engines") }
/** Holds if this package has strict engine requirements. */
predicate isEngineStrict() { getPropValue("engineStrict").(JSONBoolean).getValue() = "true" }
/** Gets information about operating systems supported by this package. */
JSONArray getOSs() { result = getPropValue("os") }
/** Gets an operating system supported by this package. */
string getWhitelistedOS() {
result = getOSs().getElementStringValue(_) and
not result.matches("!%")
}
/** Gets an operating system not supported by this package. */
string getBlacklistedOS() {
exists(string str | str = getOSs().getElementStringValue(_) |
result = str.regexpCapture("!(.*)", 1)
)
}
/** Gets information about platforms supported by this package. */
JSONArray getCPUs() { result = getPropValue("cpu") }
/** Gets a platform supported by this package. */
string getWhitelistedCPU() {
result = getCPUs().getElementStringValue(_) and
not result.matches("!%")
}
/** Gets a platform not supported by this package. */
string getBlacklistedCPU() {
exists(string str | str = getCPUs().getElementStringValue(_) |
result = str.regexpCapture("!(.*)", 1)
)
}
/** Holds if this package prefers to be installed globally. */
predicate isPreferGlobal() { getPropValue("preferGlobal").(JSONBoolean).getValue() = "true" }
/** Holds if this is a private package. */
predicate isPrivate() { getPropValue("private").(JSONBoolean).getValue() = "true" }
/** Gets publishing configuration information about this package. */
JSONValue getPublishConfig() { result = getPropValue("publishConfig") }
/**
* Gets the main module of this package.
*/
Module getMainModule() {
result = min(Module m, int prio | m.getFile() = resolveMainModule(this, prio) | m order by prio)
}
}
/**
* A representation of bug tracker information for an NPM package.
*/
class BugTrackerInfo extends JSONValue {
BugTrackerInfo() {
exists(PackageJSON pkg | pkg.getPropValue("bugs") = this) and
(this instanceof JSONObject or this instanceof JSONString)
}
/** Gets the bug tracker URL. */
string getUrl() {
result = this.(JSONObject).getPropStringValue("url") or
result = this.(JSONString).getValue()
}
/** Gets the bug reporting email address. */
string getEmail() { result = this.(JSONObject).getPropStringValue("email") }
}
/**
* A representation of contributor information for an NPM package.
*/
class ContributorInfo extends JSONValue {
ContributorInfo() {
exists(PackageJSON pkg |
this = pkg.getPropValue("author") or
this = pkg.getPropValue("contributors").(JSONArray).getElementValue(_)
) and
(this instanceof JSONObject or this instanceof JSONString)
}
/**
* Gets the `i`th item of information about a contributor, where the first
* item is their name, the second their email address, and the third their
* homepage URL.
*/
private string parseInfo(int group) {
result = this.(JSONString).getValue().regexpCapture("(.*?)(?: <(.*?)>)?(?: \\((.*)?\\))", group)
}
/** Gets the contributor's name. */
string getName() {
result = this.(JSONObject).getPropStringValue("name") or
result = parseInfo(1)
}
/** Gets the contributor's email address. */
string getEmail() {
result = this.(JSONObject).getPropStringValue("email") or
result = parseInfo(2)
}
/** Gets the contributor's homepage URL. */
string getUrl() {
result = this.(JSONObject).getPropStringValue("url") or
result = parseInfo(3)
}
}
/**
* A representation of repository information for an NPM package.
*/
class RepositoryInfo extends JSONObject {
RepositoryInfo() { exists(PackageJSON pkg | this = pkg.getPropValue("repository")) }
/** Gets the repository type. */
string getType() { result = getPropStringValue("type") }
/** Gets the repository URL. */
string getUrl() { result = getPropStringValue("url") }
}
/**
* A representation of package dependencies for an NPM package.
*/
class PackageDependencies extends JSONObject {
PackageDependencies() {
exists(PackageJSON pkg, string name |
name.regexpMatch("(.+D|d)ependencies|engines") and
this = pkg.getPropValue(name)
)
}
/** Holds if this package depends on version 'version' of package 'pkg'. */
predicate getADependency(string pkg, string version) { version = getPropStringValue(pkg) }
}
/**
* An NPM package.
*/
class NPMPackage extends @folder {
/** The `package.json` file of this package. */
PackageJSON pkg;
NPMPackage() { pkg.getJsonFile().getParentContainer() = this }
/** Gets a textual representation of this package. */
string toString() { result = this.(Folder).toString() }
/** Gets the full file system path of this package. */
string getPath() { result = this.(Folder).getAbsolutePath() }
/** Gets the `package.json` object of this package. */
PackageJSON getPackageJSON() { result = pkg }
/** Gets the name of this package. */
string getPackageName() { result = getPackageJSON().getPackageName() }
/** Gets the `node_modules` folder of this package. */
Folder getNodeModulesFolder() {
result.getBaseName() = "node_modules" and
result.getParentContainer() = this
}
/**
* Gets a file belonging to this package.
*
* We only consider files to belong to the nearest enclosing package,
* and files inside the `node_modules` folder of a package are not
* considered to belong to that package.
*/
File getAFile() { this = packageInternalParent*(result.getParentContainer()) }
/**
* Gets a Node.js module belonging to this package.
*
* We only consider modules to belong to the nearest enclosing package,
* and modules inside the `node_modules` folder of a package are not
* considered to belong to that package.
*/
Module getAModule() { result.getFile() = getAFile() }
/**
* Gets the main module of this package.
*/
Module getMainModule() { result = pkg.getMainModule() }
/**
* Holds if this package declares a dependency on version `v` of package `p`.
*/
predicate declaresDependency(string p, string v) { pkg.declaresDependency(p, v) }
}
/**
* Gets the parent folder of `c`, provided that they belong to the same NPM
* package; that is, `c` must not be a `node_modules` folder.
*/
private Folder packageInternalParent(Container c) {
result = c.getParentContainer() and
not c.(Folder).getBaseName() = "node_modules"
}