Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Primary Scan crashes #397

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/cli/src/queue.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class QueueController {
this.logger.log(`queueing site: ${url}`);

try {
let website = await this.websiteService.findByUrl(url);
const website = await this.websiteService.findByUrl(url);

const coreInput: CoreInputDto = {
websiteId: website.id,
Expand Down
17 changes: 12 additions & 5 deletions libs/core-scanner/src/scans/cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { CmsScan } from 'entities/scan-data.entity';
import { Logger } from 'pino';

export async function buildCmsResult( parentLogger: Logger, mainResponse: HTTPResponse ): Promise<CmsScan> {
const htmlMatches = await getHtmlMatches(mainResponse);
const headerMatches = await getHeaderMatches(mainResponse);
parentLogger.info(`Building CMS scan result for: ${mainResponse.url()}`);
const htmlMatches = await getHtmlMatches(parentLogger, mainResponse);
const headerMatches = await getHeaderMatches(parentLogger, mainResponse);

let cms = null;

Expand All @@ -20,8 +21,14 @@ export async function buildCmsResult( parentLogger: Logger, mainResponse: HTTPRe
return { cms };
};

const getHtmlMatches = async (response: HTTPResponse) => {
const actualHtml = await response.text();
const getHtmlMatches = async (logger: Logger, response: HTTPResponse) => {
logger.info(`Getting HTML matches for: ${response.url()}`);
let actualHtml = '';
try {
actualHtml = await response.text();
} catch (error) {
logger.error({ error }, `Error getting HTML from response: ${error.message}`);
}

return cmsData.filter((obj) => {
if (obj.html) {
Expand All @@ -42,7 +49,7 @@ const getHtmlMatches = async (response: HTTPResponse) => {
});
};

const getHeaderMatches = async (response: HTTPResponse) => {
const getHeaderMatches = async (logger: Logger, response: HTTPResponse) => {
const actualHeaders = await response.headers();
const formattedActualHeaders = _.transform(
actualHeaders,
Expand Down
25 changes: 17 additions & 8 deletions libs/core-scanner/src/scans/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,25 @@ const loginProviderStrings = [
];

export async function buildLoginResult ( parentLogger: Logger, mainResponse: HTTPResponse ): Promise<LoginScan> {
const html = await mainResponse.text();
const htmlLower = html.toLowerCase();
parentLogger.info(`Building login scan result for: ${mainResponse.url()}`);
try {
const html = await mainResponse.text();
const htmlLower = html.toLowerCase();

const loginDetected = getLoginDetectedResults(htmlLower);
const loginProvider = getLoginProviderResults(htmlLower);
const loginDetected = getLoginDetectedResults(htmlLower);
const loginProvider = getLoginProviderResults(htmlLower);

return {
loginDetected,
loginProvider,
};
return {
loginDetected,
loginProvider,
};
} catch (error) {
parentLogger.error({ error }, `Error scanning for login: ${error.message}`);
return {
loginDetected: null,
loginProvider: null,
};
}
};

const getLoginDetectedResults = (html: string): string | null => {
Expand Down
23 changes: 14 additions & 9 deletions libs/core-scanner/src/scans/mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ export const buildMobileResult = async (
logger: Logger,
page: Page,
): Promise<MobileScan> => {
const viewportMetaTag = await getHasViewportMetaTag(page);
const viewportMetaTag = await getHasViewportMetaTag(logger, page);

return {
viewportMetaTag,
};
};

async function getHasViewportMetaTag(page: Page): Promise<boolean> {
const result = await page.evaluate(() => {
const el = document.querySelector(
"head > meta[name='viewport'][content*='width=']",
);

return el === null ? false : true;
});
async function getHasViewportMetaTag(logger: Logger, page: Page): Promise<boolean> {
let result = false;
try {
result = await page.evaluate(() => {
const el = document.querySelector(
"head > meta[name='viewport'][content*='width=']",
);

return el === null ? false : true;
});
} catch(error) {
logger.error({error}, `Error getting viewport meta tag: ${error}`);
};

return result;
}
138 changes: 73 additions & 65 deletions libs/core-scanner/src/scans/required-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,79 @@ import { RequiredLinksScan } from 'entities/scan-data.entity';
import { Logger } from 'pino';

export async function buildRequiredLinksResult( parentLogger: Logger, page: Page ): Promise<RequiredLinksScan> {
const requiredLinksResults = await page.evaluate(() => {
const requiredLinksUrlContents = [
'about',
'fear',
'foia',
'inspector',
'privacy',
'usa.gov',
'spanish',
'espanol',
'español',
'/es',
];

const requiredLinksUrl = requiredLinksUrlContents
.filter((string) => {
let stringDetected = false;

document.querySelectorAll('a').forEach((el) => {
const href = el.getAttribute('href');
if (href && href.toLowerCase().includes(string)) {
stringDetected = true;
}
});

return stringDetected;
})
.join(',');

const requiredLinksTextContents = [
'about us',
'accessibility',
'budget and performance',
'no fear act',
'foia',
'freedom of information act',
'inspector general',
'privacy policy',
'vulnerability disclosure',
'usa.gov',
'espanol',
'español',
'espa&ntilde;ol',
'spanish',
];

const requiredLinksText = requiredLinksTextContents
.filter((string) => {
let stringDetected = false;

document.querySelectorAll('a').forEach((el) => {
if (el.textContent.toLowerCase().includes(string)) {
stringDetected = true;
}
});

return stringDetected;
})
.join(',');

return {
requiredLinksUrl,
requiredLinksText,
};
});
let requiredLinksResults = {
requiredLinksUrl: '',
requiredLinksText: '',
};
try {
requiredLinksResults = await page.evaluate(() => {
const requiredLinksUrlContents = [
'about',
'fear',
'foia',
'inspector',
'privacy',
'usa.gov',
'spanish',
'espanol',
'español',
'/es',
];

const requiredLinksUrl = requiredLinksUrlContents
.filter((string) => {
let stringDetected = false;

document.querySelectorAll('a').forEach((el) => {
const href = el.getAttribute('href');
if (href && href.toLowerCase().includes(string)) {
stringDetected = true;
}
});

return stringDetected;
})
.join(',');

const requiredLinksTextContents = [
'about us',
'accessibility',
'budget and performance',
'no fear act',
'foia',
'freedom of information act',
'inspector general',
'privacy policy',
'vulnerability disclosure',
'usa.gov',
'espanol',
'español',
'espa&ntilde;ol',
'spanish',
];

const requiredLinksText = requiredLinksTextContents
.filter((string) => {
let stringDetected = false;

document.querySelectorAll('a').forEach((el) => {
if (el.textContent.toLowerCase().includes(string)) {
stringDetected = true;
}
});

return stringDetected;
})
.join(',');

return {
requiredLinksUrl,
requiredLinksText,
};
});
} catch (error) {
parentLogger.error({ error }, 'Error building required links scan');
};

return requiredLinksResults;
};
Loading
Loading