summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/legacy-no-auth-leak.js
blob: f837239250222b6fa0d9fb4853fc5be3404ece27 (plain)
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
'use strict'
var test = require('tap').test
var common = require('../common-tap.js')
var path = require('path')
var basepath = path.resolve(__dirname, path.basename(__filename, '.js'))
var Tacks = require('tacks')
var File = Tacks.File
var Dir = Tacks.Dir

var fixture = new Tacks(
  Dir({
    README: File(
      'just an npm test\n'
    ),
    'package.json': File({
      name: 'npm-test-no-auth-leak',
      version: '0.0.0',
      scripts: {
        test: 'node test.js'
      }
    }),
    '.npmrc': File(
      'auth=abc',
      'authCrypt=def',
      'password=xyz',
      '//registry.npmjs.org/:_authToken=nopenope'
    ),
    'test.js': File(
      'var authTokenKeys = Object.keys(process.env)\n' +
      '  .filter(function (key) { return /authToken/.test(key) })\n' +
      'console.log(JSON.stringify({\n' +
      '  password: process.env.npm_config__password || null,\n' +
      '  auth: process.env.npm_config__auth || null,\n' +
      '  authCrypt: process.env.npm_config__authCrypt || null ,\n' +
      '  authToken: authTokenKeys && process.env[authTokenKeys[0]] || null\n' +
      '}))'
    )
  })
)

test('setup', function (t) {
  setup()
  t.done()
})

test('no-auth-leak', function (t) {
  common.npm(['test'], {cwd: basepath}, function (err, code, stdout, stderr) {
    if (err) throw err
    t.is(code, 0, 'test ran ok')
    if (stderr) console.log(stderr)
    var matchResult = /^[^{]*(\{(?:.|\n)*\})[^}]*$/
    t.like(stdout, matchResult, 'got results with a JSON chunk in them')
    var stripped = stdout.replace(matchResult, '$1')
    var result = JSON.parse(stripped)
    t.is(result.password, null, 'password')
    t.is(result.auth, null, 'auth')
    t.is(result.authCrypt, null, 'authCrypt')
    t.is(result.authToken, null, 'authToken')
    t.end()
  })
})

test('cleanup', function (t) {
  cleanup()
  t.done()
})

function setup () {
  cleanup()
  fixture.create(basepath)
}

function cleanup () {
  fixture.remove(basepath)
}