Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
cypress-ntlm-auth
Advanced tools
NTLM authentication plugin for Cypress
If you want to perform end-to-end testing against deployed sites that require Windows Authentication, and you want to use Cypress, you will find that Cypress does not support Windows Authentication. Windows Authentication is quite widely used in corporate intranets. This plugin bridges the gap by providing NTLM authentication (and Negotiate when using SSO) for Cypress in a streamlined manner.
Never heard of Cypress?
Read the intro at their site and find out if it is the thing for you. (spoiler - it is!)
Want to use NTLM or Negotiate authentication for something else?
Parts of this library should be readily reusable, the ntlm-proxy is application agnostic and should be usable with Selenium or other solutions - you'll have to provide the streamlining into your application yourself though.
The HTTPS/SSL/TLS certificate validation has been refactored, and will now permit invalid certificates with a warning logged to the console. Previous versions would fail the test in this scenario. Most users won't need to update their test code, but if you require stricter validation, see HTTPS/SSL/TLS Certificates for details.
npm install --save-dev cypress-ntlm-auth
The --save-dev
flag stores cypress-ntlm-auth as a development dependency, which is suitable for a testing utility.
Follow these steps to configure Cypress to utilize this plugin:
In the file cypress/support/commands.js
(or .ts
) add this line
import "cypress-ntlm-auth/dist/commands";
The most convenient way to start Cypress with NTLM authentication is
npx cypress-ntlm open
This starts the ntlm-proxy and runs cypress in headed mode (like cypress open
). After Cypress exits, the ntlm-proxy is terminated. cypress-ntlm open
accepts the same command line arguments that cypress open
does.
npx cypress-ntlm run
This starts the ntlm-proxy and runs cypress in headless mode (like cypress run
), suitable for CI. After Cypress exits, the ntlm-proxy is terminated.cypress-ntlm run
accepts the same command line arguments that cypress run
does.
This binary is available in the node_modules/.bin
folder. Use it if you need to start the ntlm-proxy manually. To use specific ports you can set CYPRESS_NTLM_AUTH_API
and CYPRESS_NTLM_AUTH_PROXY
environment variables.
# Start NTLM proxy
npx ntlm-proxy
# Start NTLM proxy as a background process
npx ntlm-proxy &
# Start NTLM proxy with debug logging to console
DEBUG=cypress:plugin:ntlm-auth npx ntlm-proxy
# Start NTLM proxy
npx ntlm-proxy
# Start NTLM proxy as a background process, close window when ntlm-proxy terminates
start /min \"ntlm-proxy\" cmd /c npx ntlm-proxy
# Start NTLM proxy with debug logging to console
set DEBUG=cypress:plugin:ntlm-auth
npx ntlm-proxy
# Start NTLM proxy
npx ntlm-proxy
# Start NTLM proxy as a background process, close window when ntlm-proxy terminates
start /min \"ntlm-proxy\" cmd /c npx ntlm-proxy
# Start NTLM proxy with debug logging to console
$env:DEBUG='cypress:plugin:ntlm-auth'
npx ntlm-proxy
This binary is available in the node_modules/.bin
folder. Use it to send an exit command to a ntlm-proxy running in the background. CYPRESS_NTLM_AUTH_API
environment variable must be set for this command.
# Terminate NTLM proxy
npx ntlm-proxy-exit
# Stop NTLM proxy with debug logging to console
DEBUG=cypress:plugin:ntlm-auth npx ntlm-proxy-exit
# Terminate NTLM proxy
npx ntlm-proxy-exit
# Stop NTLM proxy with debug logging to console
set DEBUG=cypress:plugin:ntlm-auth
npx ntlm-proxy-exit
# Terminate NTLM proxy
npx ntlm-proxy-exit
# Stop NTLM proxy with debug logging to console
$env:DEBUG='cypress:plugin:ntlm-auth'
npx ntlm-proxy-exit
This binary is available in the node_modules/.bin
folder. Use it to start Cypress with NTLM authentication configured. Depending on environment variables, this command will use an existing ntlm-proxy or start its own. It is easier to just let it handle its own ntlm-proxy, but if you need to use a specific instance, you need to set the environment variable:
CYPRESS_NTLM_AUTH_API
- set this to the url the ntlm-proxy config API is listening to (example: http://localhost:54368)When this is set, cypress-ntlm will check if it can reach the proxy and see that it is alive. Otherwise the cypress-ntlm command will fail.
# Start Cypress with NTLM authentication
npx cypress-ntlm open
If your network environment enforces proxy usage for internet access (quite likely given that you are using NTLM) and the host you are testing uses resources on the internet (e.g. loading bootstrap or jQuery from a CDN), you need to make the ntlm-proxy aware of the internet proxy. This is done by setting the (standardized) environment variables below before starting the ntlm-proxy (with either the ntlm-proxy
binary or the cypress-ntlm
binary):
HTTP_PROXY
- The URL to the proxy for accessing external HTTP/HTTPS resources. Example: http://proxy.acme.com:8080
HTTPS_PROXY
- (optional) The URL to the proxy for accessing external HTTPS resources. Overrides HTTP_PROXY
for HTTPS resources. Example: http://proxy.acme.com:8080
NO_PROXY
- A comma separated list of internal hosts to exclude from proxying. Add the host you are testing, and other local network resources used from the browser when accessing the host you are testing. Note that hosts that are located on the internet (not your intranet) must not be added, they should pass through the upstream proxy.Include only the hostname (or IP), not the protocol or port. Wildcards are supported. Example: *.acme.com
Since the plugin requires traffic to localhost to be excluded from the corporate proxy, the plugin adds localhost
and 127.0.0.1
to the NO_PROXY
setting automatically unless they are already there. To disable this behavior (if you require an additional custom proxy), add <-loopback>
to NO_PROXY
.
Note that these environment variables must be specified as uppercase. Lowercase variants will be ignored.
The ntlm command is used to configure host/user mappings. After this command, all network communication from cypress to the specified hosts is monitored by the ntlm-proxy. If the server sends an authentication challenge, the ntlm-proxy will perform a NTLM login handshake with the configured user.
Note that "all network communication" includes calls to cy.visit(host)
, cy.request(host)
and indirect network communication (when the browser fetches additional resources after the cy.visit(host)
call).
If domain and workstation are not set, the ntlm-proxy will use the domain of the ntlmHost.
cy.ntlm(ntlmHosts, username, password, [domain, [workstation, [ntlmVersion]]]);
['localhost:4200', '*.acme.com']
os.hostname()
The ntlm command may be called multiple times to setup multiple ntlmHosts, also with different credentials. If the ntlm command is called with the same ntlmHost again, it overwrites the credentials for that ntlmHost. Existing connections are not terminated, but if the server requests reauthentication the new credentials will be used. To terminate existing connections when chaning credentials, use the cy.ntlmReset()
command.
If multiple configurations match a hosts, the most specific configuration is applied. The order of priority is:
Configuration set with the ntlm command persists until it is reset (see ntlmReset command) or when the proxy is terminated. Take note that it is not cleared when the current spec file is finished.
You want to test a IIS website on your intranet https://ntlm.acme.com
that requires Windows Authentication and allows NTLM. The test user is acme\bobby
(meaning domain acme
and username bobby
), and the password is brown
.
cy.ntlm(["ntlm.acme.com"], "bobby", "brown", "acme");
// Access the ntlm site with user bobby
cy.visit("https://ntlm.acme.com");
// Test actions and asserts here
cy.ntlm(["ntlm.acme.com"], "admin", "secret", "acme");
// Access the ntlm site with user admin
cy.visit("https://ntlm.acme.com");
// Test actions and asserts here
cy.ntlm(["ntlm-legacy.acme.com"], "admin", "secret", "acme", undefined, 1);
// Access the ntlm-legacy site with user admin using NTLMv1
cy.visit("https://ntlm-legacy.acme.com");
// Test actions and asserts here
Hard coding password into your test specs isn't a great idea, even though it may seem harmless. Test code will end up in a repository, which makes the full credentials for the accounts used in your tests searchable in the repository. Even if the repository is on an internal company hosted server, this is not good practice. The recommended way to handle credentials is to use config files / environment variables, and to have these populated by your release pipeline. Cypress has several options to provide custom configuration for different environments - pick one that makes sense in your pipeline.
You can then combine this with setting up multiple accounts to test your application using different levels of access (if needed by your application). Using this technique, you should end up with something like this:
// Read-only user access
cy.ntlm(
["ntlm.acme.com"],
Cypress.env.NTLM_READONLY_USERNAME,
Cypress.env.NTLM_READONLY_PASSWORD,
Cypress.env.NTLM_READONLY_DOMAIN
);
// tests ...
// Admin user access
cy.ntlm(
["ntlm.intranet.acme.com"],
Cypress.env.NTLM_ADMIN_USERNAME,
Cypress.env.NTLM_ADMIN_PASSWORD,
Cypress.env.NTLM_ADMIN_DOMAIN
);
// tests ...
If you are testing a single site, it is convenient to set the baseUrl parameter in Cypress to the hostname, so you don't have to provide it on every call to cy.visit()
or cy.request()
. Set it in cypress.json
or simply use:
Cypress.env.baseUrl = ntlmHost;
This will persist until the current spec file is finished.
The ntlmSso command is used to configure host for single sign on authentication. After this command, all network communication from cypress to the specified hosts is monitored by the ntlm-proxy. If the server sends an authentication challenge, the ntlm-proxy will perform a NTLM or Negotiate login handshake with the credentials of the user running the test client.
Note that "all network communication" includes calls to cy.visit(host)
, cy.request(host)
and indirect network communication (when the browser fetches additional resources after the cy.visit(host)
call).
cy.ntlmSso(ntlmHosts);
['localhost', '*.acme.com']
The ntlmSso command may be called multiple times, each call will overwrite the previous ntlmSso configuration.
The NTLM protocol version cannot be specified, it is negotiated automatically. The client will follow the settings in Windows (LMCompatibilityLevel), which could mean that a legacy host with NTLMv1 only cannot be accessed if the client settings don't allow NTLMv1.
Configuration set with the ntlmSso command persists until it is reset (see ntlmReset command) or when the proxy is terminated. Take note that it is not cleared when the current spec file is finished.
You want to test a IIS website on your intranet https://ntlm.acme.com
that requires Windows Authentication and allows NTLM.
// Enable single sign on all hosts within *.acme.com
cy.ntlmSso(["*.acme.com"]);
// Access the ntlm site with the user running the test client
cy.visit("https://ntlm.acme.com");
// Test actions and asserts here
// Enable single sign on for both ntlm.acme-legacy.com and all hosts within *.acme.com
cy.ntlmSso(["ntlm.acme-legacy.com", "*.acme.com"]);
// Access the legacy site with the user running the test client
cy.visit("https://ntlm.acme-legacy.com");
// Test actions and asserts here
The ntlmReset command is used to remove all connections and all configured ntlmHosts from previous ntlm command calls. Since the proxy configuration persists even when a test case or spec file is finished, a good practice is to call ntlmReset in the beforeEach method. This ensures that you have a clean setup at the start of each test.
cy.ntlmReset();
Using ntlmReset to clear configuration.
cy.ntlm(["ntlm.acme.com"], "bobby", "brown", "acme");
cy.visit("https://ntlm.acme.com"); // This succeeds
cy.ntlmReset();
cy.visit("https://ntlm.acme.com"); // This fails (401)
When reporting issues with this plugin, please collect debug logs for your scenario as described below and add them to the issue.
DEBUG=cypress:plugin:ntlm-auth npx cypress-ntlm open
set DEBUG=cypress:plugin:ntlm-auth
npx cypress-ntlm open
$env:DEBUG='cypress:plugin:ntlm-auth'
npx cypress-ntlm open
To write also the NTLM and Negotiate headers sent and received by ntlm-proxy, set the environment variable DEBUG_NTLM_HEADERS=1
. If you use this, take some care with the logs since access to the NTLM and Negotiate headers are an attack vector for the account, especially if you are using NTLMv1.
This plugin can also be called as a Node module.
The cypress-ntlm API mimics the behavior of the run and open methods in Cypress module API - accepting the same arguments, passing them on to Cypress and returning the same value. It will automatically start the ntlm-proxy before calling cypress.run()
, and it will shut down the ntlm-proxy after the tests have finished.
const cypressNtlmAuth = require("cypress-ntlm-auth");
cypressNtlmAuth
.run({
spec: "./cypress/integration/test.spec.js",
})
.then((result) => console.log(result))
.catch((err) => console.log(err));
It is also possible to launch and control the ntlm-proxy through the API, without using Cypress. Through this method, it is possible to launch multiple parallel ntlm-proxy instances within the same node process. Each process can operate independently with unique configuration, making it possible to act as multiple users towards the same site simultaneously. This may be useful for testing chat servers for instance.
startNtlmProxy()
: starts a ntlm-proxy and returns a NtlmProxy object. It contains a ports
property with the URLs to config API and to the proxy. The URL to the proxy should be used to configure your test object (likely a browser) to ensure the traffic will pass through the proxy.All these methods mimic the corresponding Cypress commands, see Usage for details about the arguments.
async NtlmProxy.alive()
: check if the proxy responds. Returns a resets configuration and connections in the proxyasync NtlmProxy.reset()
: resets configuration and connections in the proxyasync NtlmProxy.ntlm(NtlmConfig)
: adds an NTLM enabled site (or an array of sites) to the proxyasync NtlmProxy.ntlmSso(NtlmSsoConfig)
: sets which sites should perform SSO authentication. Only supported on Windows.async NtlmProxy.stop()
: closes all connections and stops the proxy.const cypressNtlmAuth = require("cypress-ntlm-auth");
async function run() {
let proxy = await cypressNtlmAuth.startNtlmProxy();
console.log(proxy);
await proxy.reset();
let ntlmConfig = {
ntlmHosts: ["localhost:5000"],
username: "bobby",
password: "brown",
domain: "acme",
};
await proxy.ntlm(ntlmConfig);
let ntlmSso = {
ntlmHosts: ["localhost:5006"],
};
// ntlmSso will throw on non-Windows OS
await proxy.ntlmSso(ntlmSso);
await proxy.alive();
await proxy.stop();
}
run();
When using the Docker containers provided by Cypress, Cypress is installed globally. Since this plugin requires the Node module API of Cypress, one cannot mix global Cypress with a local install of the plugin. There are two options:
cypress-ntlm
, not npx cypress-ntlm
).cypress/support/index.js
file to use the path of the global installation of the plugin. The path to use can be found by entering npm root -g
. Prefix the import statement of cypress-ntlm-auth with this path.
For instance, the typical path for a linux installation should be /usr/local/lib/node_modules
, meaning that for that environment the import statement in cypress/support/index.js
should beimport "/usr/local/lib/node_modules/cypress-ntlm-auth/dist/commands";
Which option to use is up to you. The first option is platform agnostic but requires the extra step of the local install (a Docker container can be prepared with all the global installs already in place). The second option removes the requirement of the local install, but the import path specified is not portable - it will vary between different OS variants, and also if you have multiple Node versions installed.
An example repository using method 1 above can be found here: cypress-ntlm-auth-docker
The http-mitm-proxy library will create a .http-mitm-proxy folder with generated certificates. This improves performance when re-running tests using the same sites. It is recommended to add this folder to your .gitignore so the certificates don't end up in your repo.
The NTLM proxy will accept self-signed certificates for sites that are served from localhost. This is convenient for testing local development builds without requiring a full CA chain for the certificates, while still requiring proper certificates from external servers.
By default, the plugin will validate certs (except for localhost targets) and log any validation errors as warnings. The plugin can also be configured to a stricter mode, preventing connections to targets that do not pass the validation. See HTTPS/SSL/TLS Certificates for details.
The plugin is written in TypeScript and the git repository does not include the transpiled files. Hence, if you need to build the plugin from source:
npm run build
This transpiles the sources into the dist
folder, copies additional files to dist
and sets permissions for the launchers.
To run the unit test suite:
npm test
FAQs
NTLM authentication plugin for Cypress
The npm package cypress-ntlm-auth receives a total of 3,110 weekly downloads. As such, cypress-ntlm-auth popularity was classified as popular.
We found that cypress-ntlm-auth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.