forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyarn-validate-workspace-deps.js
171 lines (147 loc) · 4.27 KB
/
yarn-validate-workspace-deps.js
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
#!/usr/bin/env node
/*
* Copyright (C) 2022 - present Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// yarn-validate-workspace-deps.js: ensure that any explicit dependency on a
// local workspace package is specified using the free-range specifier "*" so
// that yarn cannot be tricked into retrieving the package from a remote
// registry in case the package's local version can no longer satisfy the
// requested one
//
// USAGE:
//
// yarn --silent workspaces info --json |
// node script/yarn-validate-workspace-deps.js
//
// consider the following case:
//
// // file: package.json
// {
// "name": "canvas-lms",
// "workspaces": {
// "packages": [ "packages/*" ]
// },
// "dependencies": {
// "get-cookie": "^1"
// }
// }
//
// // file: packages/get-cookie/package.json
// {
// "name": "get-cookie",
// "version": "2.0"
// }
//
// since "get-cookie" got bumped up to 2 but consumer package.json is still
// requesting ^1, yarn will attempt to retrieve get-cookie@^1 from the remote
// registry, and we don't necessarily own that, which is a potential attack
// vector
//
// instead, we can guarantee that yarn will use whatever version the workspace
// package is providing by using a free specifier like "*"
//
// see SEC-4437
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const root = path.resolve(__dirname, '..')
async function main() {
const workspaces = await parseWorkspacesFromStdin()
let errors = []
for (const pkgfile of scanPkgfiles(require.resolve('../package.json'))) {
errors = errors.concat( validate({ pkgfile, workspaces, errors }) )
}
if (errors.length) {
process.exitCode = 1
console.log('dependencies listed below must have a specifier of "*"')
console.log('---')
for (const error of errors) {
console.log("%s:%s", path.relative(root, error.pkgfile), error.dep)
}
console.log('---')
}
}
function scanPkgfiles(rootpkgfile) {
let pkgfiles = [ rootpkgfile ]
for (const pattern of require(rootpkgfile).workspaces.packages || []) {
pkgfiles = pkgfiles.concat(
glob.sync(`${pattern}/package.json`, {
cwd: path.dirname(rootpkgfile),
absolute: true
})
)
}
return pkgfiles
}
function validate({ pkgfile, workspaces }) {
const { dependencies = {} } = require(pkgfile)
const errors = []
let depcount = 0
for (const [dep, version] of Object.entries(dependencies)) {
if (dep in workspaces) {
depcount += 1
if (version !== '*') {
errors.push({ pkgfile, dep })
}
}
}
if (depcount > 0) {
console.error('found %d workspace package dependencies and %d errors -- "%s"',
depcount,
errors.length,
path.relative(root, pkgfile),
)
}
else {
console.error('found no workspace package dependencies -- "%s"',
path.relative(root, pkgfile)
)
}
return errors
}
async function read(stream) {
const chunks = []
for await (const chunk of stream) {
chunks.push(chunk)
}
return Buffer.concat(chunks).toString('utf8')
}
async function parseWorkspacesFromStdin() {
const buffer = await read(process.stdin)
const parsed = JSON.parse(buffer)
// yarn 1.19.1, which is what jenkins is on, has a different output structure
// that looks like this:
//
// {
// "type": "log",
// "data": "{\"json\":\"blob\"}"
// }
//
// while yarn 1.22.1 directly emits the data with no metadata:
//
// {
// "json": "blob"
// }
//
if (parsed.type === 'log' && parsed.hasOwnProperty('data')) {
return JSON.parse(parsed.data)
}
else {
return parsed
}
}
main()