@contrast/test-bench-utils
Shared code for use in Contrast's Node.js test apps.
Adding a shared sink
Under lib/routes.js, create a sink definition with the following form:
[ruleName: string]: {
base: string,
name: string,
link: string,
products: string[],
inputs: string[],
params: string[],
sinks: Object<string, Function>,
}
Then create a file under lib/sinks/ that exports functions with a consistent
signature:
module.exports['sinkName'] = async function sink({ input }, { safe = false, noop = false } = {}) { };
The sink function will be called by the endpoint handler appropriately by each
framework. By default, for the /unsafe
endpoint the function is called with
user input, and for the /safe
and /noop
endpoints it is called with the
safe
and noop
options set to true, respectively.
If providing more than one "safe" or "unsafe" pattern, you can export an object
rather than a single function. This is done using the following pattern:
module.exports['sinkName'] = {
async safeOne({ input }) { },
async safeTwo({ input }) { },
async unsafeOne({ input }) { },
async unsafeTwo({ input }) { },
async noop() { return 'NOOP' },
}
There's nothing tying you to the existing safe
and unsafe
method names when
providing an object, so please provide meaningful function names. Routes will
be generated with the function name as the last part of the path. Existing views
assume the methods are safe
, unsafe
, and noop
, but accounting for
additional endpoints will require changes to views for the time being.
Front-end content
If there is any custom data you want to provide to the test bench front end, you
can export it from lib/content/. For example, we export the following XML
string as a potential attack for the xpath injection rule:
lib/content/xpathInjection.js
module.exports.xml = `
<?xml version="1.0"?>
<users>
<user>
<username>admin</username>
<password>admin</password>
</user>
<user>
<username>user1</username>
<password>123456</password>
</user>
<user>
<username>tony</username>
<password>ynot</password>
</user>
</users>
`;
This string is then used by the xpathInjection.ejs
view in @contrast/test-bench-utils
to render an input prepopulated with the attack value.
Adding a shared view
Once you have configured a sink you're ready to add a shared view. Shared view
templates are rendered with the following locals provided:
name
: the name of the vulnerability being testedlink
: a link to OWASP or another reference describing the vulnerabilitysinkData
: an array of objects describing the sinks exercising a rule,
containing (at least) the following keys:
method
: the HTTP method being used to submit the attackname
: the name of the particular sink being exercisedurl
: the api endpoint url to hit
_csrf
for Kraken apps, we provide the csrf token to be included as a hidden
field within a form
An endpoint may also be configured to provide additional locals to the template
to render additional context for a rule. For example, we provide an XML string
to the xpathInjection endpoint as a potential attack value.
Test Bench Applications
Once you have configured the shared sink and view, consult the following
instructions for including the shared functionality in each test bench app: