Skip to content

Commit

Permalink
feat(cookie): add support for cookies and tokens (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
UlisesGascon authored Jan 31, 2020
1 parent 3e4e5b5 commit d0c8cfe
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Finds publicly known security vulnerabilities in a website's frontend JavaScript
Using Node.js's `npx` to run a one-off scan of a website:

```bash
npx is-website-vulnerable https://example.com [--json] [--js-lib] [--mobile|--desktop] [--chromePath]
npx is-website-vulnerable https://example.com [--json] [--js-lib] [--mobile|--desktop] [--chromePath] [--cookie] [--token]
```

The CLI will gracefully handle cases where the URL to scan is missing by prompting you to enter it:
Expand Down
34 changes: 34 additions & 0 deletions __tests__/Utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,38 @@ describe('Utils', () => {
expect(Utils.parseDevice(argv)).toEqual(device)
expect(Utils.hasDevice(argv)).toEqual(true)
})

test('hasAutentication & parseAutentication with none flag', async () => {
const argv = {}
expect(Utils.parseAutentication(argv)).toEqual({})
expect(Utils.hasAutentication(argv)).toEqual(false)
})

test('hasAutentication & parseAutentication with cookie flag', async () => {
const argv = { cookie: 'This is the COOKIE content' }
expect(Utils.parseAutentication(argv)).toEqual({
Cookie: 'This is the COOKIE content'
})
expect(Utils.hasAutentication(argv)).toEqual(true)
})

test('hasAutentication & parseAutentication with token flag', async () => {
const argv = { token: 'This is the TOKEN content' }
expect(Utils.parseAutentication(argv)).toEqual({
Authorization: 'Bearer This is the TOKEN content'
})
expect(Utils.hasAutentication(argv)).toEqual(true)
})

test('hasAutentication & parseAutentication with token and cookie flags', async () => {
const argv = {
cookie: 'This is the COOKIE content',
token: 'This is the TOKEN content'
}
expect(Utils.parseAutentication(argv)).toEqual({
Cookie: 'This is the COOKIE content',
Authorization: 'Bearer This is the TOKEN content'
})
expect(Utils.hasAutentication(argv)).toEqual(true)
})
})
15 changes: 11 additions & 4 deletions bin/is-website-vulnerable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const os = require('os')
const debug = require('debug')('is-website-vulnerable')
const argv = require('yargs').argv
const { argv } = require('yargs')
const { Audit, RenderConsole, RenderJson, Utils } = require('../index')
const promptUrlInput = require('./prompt-url-input')

Expand All @@ -17,9 +17,16 @@ function detectEnvironment() {
}

function getLighthouseOptions() {
const lighthouseOpts = Utils.hasDevice(argv)
? { emulatedFormFactor: Utils.parseDevice(argv) }
: {}
const lighthouseOpts = {}

if (Utils.hasDevice(argv)) {
lighthouseOpts.emulatedFormFactor = Utils.parseDevice(argv)
}

if (Utils.hasAutentication(argv)) {
lighthouseOpts.extraHeaders = Utils.parseAutentication(argv)
}

const { chromePath } = argv
const chromeOpts = chromePath ? { chromePath } : {}

Expand Down
15 changes: 15 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,20 @@ module.exports = {
},
hasDevice: function(argv) {
return argv.mobile || argv.desktop || argv.none || false
},
parseAutentication: function(argv) {
const extraHeaders = {}
if (argv.cookie) {
extraHeaders.Cookie = argv.cookie
}

if (argv.token) {
extraHeaders.Authorization = `Bearer ${argv.token}`
}

return extraHeaders
},
hasAutentication: function(argv) {
return ['cookie', 'token'].some(prop => Object.hasOwnProperty.call(argv, prop))
}
}

0 comments on commit d0c8cfe

Please sign in to comment.