|
6 | 6 | modulesWithKeyword, |
7 | 7 | checkForNewServerVersion, |
8 | 8 | getLatestServerVersion, |
9 | | - importOrRequire |
| 9 | + importOrRequire, |
| 10 | + runNpm |
10 | 11 | } = require('../dist/modules') |
11 | 12 |
|
12 | 13 | describe('modulesWithKeyword', () => { |
@@ -212,3 +213,98 @@ describe('importOrRequire', () => { |
212 | 213 | chai.expect(mod).to.be.a('function') |
213 | 214 | }) |
214 | 215 | }) |
| 216 | + |
| 217 | +describe('runNpm version validation', () => { |
| 218 | + const config = { |
| 219 | + configPath: '/tmp', |
| 220 | + name: 'signalk-server' |
| 221 | + } |
| 222 | + |
| 223 | + const testVersion = (version, shouldPass) => { |
| 224 | + return new Promise((resolve, reject) => { |
| 225 | + let errCalled = false |
| 226 | + const onErr = (err) => { |
| 227 | + errCalled = true |
| 228 | + if (shouldPass) { |
| 229 | + reject( |
| 230 | + new Error(`Should have passed but failed with: ${err.message}`) |
| 231 | + ) |
| 232 | + } else { |
| 233 | + chai.expect(err.message).to.contain('Invalid version') |
| 234 | + resolve() |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + const onClose = (code) => { |
| 239 | + if (shouldPass && !errCalled) { |
| 240 | + resolve() |
| 241 | + } else if (!shouldPass && !errCalled) { |
| 242 | + reject(new Error(`Should have failed but passed (code ${code})`)) |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + // We mock spawn to do nothing if validation passes |
| 247 | + const originalSpawn = require('child_process').spawn |
| 248 | + require('child_process').spawn = () => ({ |
| 249 | + stdout: { on: () => {} }, |
| 250 | + stderr: { on: () => {} }, |
| 251 | + on: (event, cb) => { |
| 252 | + if (event === 'close') cb(0) |
| 253 | + } |
| 254 | + }) |
| 255 | + |
| 256 | + try { |
| 257 | + runNpm( |
| 258 | + config, |
| 259 | + 'some-package', |
| 260 | + version, |
| 261 | + 'install', |
| 262 | + () => {}, |
| 263 | + onErr, |
| 264 | + onClose |
| 265 | + ) |
| 266 | + } finally { |
| 267 | + require('child_process').spawn = originalSpawn |
| 268 | + } |
| 269 | + }) |
| 270 | + } |
| 271 | + |
| 272 | + it('should accept valid semantic versions', () => { |
| 273 | + return testVersion('1.0.0', true) |
| 274 | + }) |
| 275 | + |
| 276 | + it('should accept valid prerelease versions', () => { |
| 277 | + return testVersion('1.0.0-alpha.1', true) |
| 278 | + }) |
| 279 | + |
| 280 | + it('should accept empty version', () => { |
| 281 | + return testVersion('', true) |
| 282 | + }) |
| 283 | + |
| 284 | + it('should reject URL encoded http URL', () => { |
| 285 | + return testVersion('http:%2F%2Fattacker.com%2Fpkg.tgz', false) |
| 286 | + }) |
| 287 | + |
| 288 | + it('should reject URL encoded git URL', () => { |
| 289 | + return testVersion( |
| 290 | + 'git%2Bhttps:%2F%2Fattacker.com%2Fmalicious-plugin.git', |
| 291 | + false |
| 292 | + ) |
| 293 | + }) |
| 294 | + |
| 295 | + it('should reject scoped package path', () => { |
| 296 | + return testVersion('attacker%2Fmalicious-plugin', false) |
| 297 | + }) |
| 298 | + |
| 299 | + it('should reject npm alias', () => { |
| 300 | + return testVersion('npm:malicious-package@1.0.0', false) |
| 301 | + }) |
| 302 | + |
| 303 | + it('should reject plain http URL', () => { |
| 304 | + return testVersion('http://attacker.com/pkg.tgz', false) |
| 305 | + }) |
| 306 | + |
| 307 | + it('should reject plain git URL', () => { |
| 308 | + return testVersion('git+https://attacker.com/malicious-plugin.git', false) |
| 309 | + }) |
| 310 | +}) |
0 commit comments