This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
uswds.js
62 lines (48 loc) · 1.77 KB
/
uswds.js
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
'use strict';
// TEST_LOCAL will turn on debug output.
// TODO: Allow --debug to turn on debug output from CLI/Python-land.
// TODO: Move logging functions into base.js where possible.
var debug = false;
if (process.env.TEST_LOCAL) debug = true;
// Default overall timeout, in seconds.
// TODO: make timeout calculation way more sophisticated. :)
// TODO: Move timeout management into base.js where possible.
var default_timeout = 20;
// JS entry point for third party scan.
module.exports = {
scan: async (domain, environment, options, browser, page) => {
const url = environment.url;
var data = {
url: url
};
// Override puppeteer default of 30, especially since that
// causes Lambda execution itself to timeout and halt.
page.setDefaultNavigationTimeout(default_timeout * 1000);
try {
await page.goto(url);
} catch (exc) {
// if it's a timeout, that's okay, send back what we got.
if (exc.message.includes("Navigation Timeout Exceeded"))
return data;
// otherwise, re-throw and handle higher up.
else throw exc;
}
// Do a quick text match on the HTML to find out of date USWDS
// banner text.
const html = await page.content();
data.banner_bad_text = hasBadText(html);
// Search DOM for evidence of USWDS being present at all.
data.present = await uswdsPresent(page);
return data;
}
}
// Simple text match on some old text.
var hasBadText = (html) => {
return (html.search("Federal government websites always use a .gov or .mil domain.") >= 0)
};
// Checks for any element with a class with "usa-" in it.
// TODO: Should "usa-" at least be at the start of the string?
var uswdsPresent = async (page) => {
var match = await page.$('[class*=usa-]');
return (match != null);
};