Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix(utils): prevent ReDoS in social email regex (#3845)
Anas Khand6a2b39252c0
diff --git a/packages/utils/src/internals/social.ts b/packages/utils/src/internals/social.tsindex 2c4a6a179..9ed4fd6c2 100644--- a/packages/utils/src/internals/social.ts+++ b/packages/utils/src/internals/social.ts@@ -3,8 +3,11 @@ import * as cheerio from 'cheerio';import { htmlToText } from './cheerio';// Regex inspired by https://zapier.com/blog/extract-links-email-phone-regex/+// The dot-atom local part and domain labels use RFC 5321 length bounds ({1,64}, {0,62})+// instead of unbounded quantifiers to avoid quadratic backtracking (ReDoS) on long+// dotted or hyphenated inputs, e.g. text scraped by parseHandlesFromHtml().const EMAIL_REGEX_STRING =- '(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])';+ '(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]{1,64}(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]{1,64}){0,32}|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])';/*** Regular expression to exactly match a single email address.diff --git a/test/utils/social.test.ts b/test/utils/social.test.tsindex ca91ef515..24260a72d 100644--- a/test/utils/social.test.ts+++ b/test/utils/social.test.ts@@ -77,6 +77,16 @@ describe('utils.social', () => {]);});++ test('does not hang on adversarial input (ReDoS)', () => {+ // A long dotted local part with no valid domain used to trigger quadratic+ // backtracking in EMAIL_REGEX_GLOBAL, freezing the event loop for seconds on+ // scraped text. A long hyphen run in a domain label had the same issue.+ const started = Date.now();+ expect(emailsFromText(`x${'.a'.repeat(50000)}@`)).toEqual([]);+ expect(emailsFromText(`a@${'a-'.repeat(50000)}!`)).toEqual([]);+ expect(Date.now() - started).toBeLessThan(1000);+ });});describe('emailsFromUrls()', () => {