Skip to content

Commit

Permalink
feat(lookup): add protection against infinite recursion for Cloudflare (
Browse files Browse the repository at this point in the history
#1505)

Fixes #1459
Fixes #1490
  • Loading branch information
neatchee committed Dec 24, 2020
1 parent 28d1cf9 commit 1cf618c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/logger.ts
Expand Up @@ -212,6 +212,21 @@ export const Print = {
}

return `✖ ${buildProductString(link, store)} :: RATE LIMIT EXCEEDED`;
},
recursionLimit(link: Link, store: Store, color?: boolean): string {
if (color) {
return (
'✖ ' +
buildProductString(link, store, true) +
' :: ' +
chalk.yellow('CLOUDFLARE RETRY LIMIT REACHED, ABORT')
);
}

return `✖ ${buildProductString(
link,
store
)} :: CLOUDFLARE RETRY LIMIT REACHED, ABORT`;
}
};

Expand Down
31 changes: 20 additions & 11 deletions src/store/lookup.ts
Expand Up @@ -328,7 +328,8 @@ async function handleResponse(
store: Store,
page: Page,
link: Link,
response?: Response | null
response?: Response | null,
recursionDepth = 0
) {
if (!response) {
logger.debug(Print.noResponse(link, store, true));
Expand All @@ -341,16 +342,24 @@ async function handleResponse(
logger.warn(Print.rateLimit(link, store, true));
} else if (statusCode === 503) {
if (await checkIsCloudflare(store, page, link)) {
const response: Response | null = await page.waitForNavigation({
waitUntil: 'networkidle0'
});
statusCode = await handleResponse(
browser,
store,
page,
link,
response
);
if (recursionDepth > 4) {
logger.warn(Print.recursionLimit(link, store, true));
} else {
const response: Response | null = await page.waitForNavigation(
{
waitUntil: 'networkidle0'
}
);
recursionDepth++;
statusCode = await handleResponse(
browser,
store,
page,
link,
response,
recursionDepth
);
}
} else {
logger.warn(Print.badStatusCode(link, store, statusCode, true));
}
Expand Down

0 comments on commit 1cf618c

Please sign in to comment.