{"id":18561245,"url":"https://github.com/apostrophecms/time-limited-regular-expressions","last_synced_at":"2025-06-11T09:03:10.886Z","repository":{"id":43838457,"uuid":"278455250","full_name":"apostrophecms/time-limited-regular-expressions","owner":"apostrophecms","description":"Evaluates regular expressions with a time limit to mitigate DOS attacks based on catastrophic backtracking.","archived":false,"fork":false,"pushed_at":"2022-02-16T17:16:16.000Z","size":15,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-03T05:30:04.333Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apostrophecms.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-09T19:39:58.000Z","updated_at":"2024-06-11T20:53:23.000Z","dependencies_parsed_at":"2022-09-25T09:34:00.507Z","dependency_job_id":null,"html_url":"https://github.com/apostrophecms/time-limited-regular-expressions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Ftime-limited-regular-expressions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Ftime-limited-regular-expressions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Ftime-limited-regular-expressions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Ftime-limited-regular-expressions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apostrophecms","download_url":"https://codeload.github.com/apostrophecms/time-limited-regular-expressions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248150677,"owners_count":21055965,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T22:06:20.166Z","updated_at":"2025-04-10T03:30:34.723Z","avatar_url":"https://github.com/apostrophecms.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# time-limited-regular-expressions\n\n[![CircleCI](https://circleci.com/gh/apostrophecms/time-limited-regular-expressions/tree/main.svg?style=svg)](https://circleci.com/gh/apostrophecms/time-limited-regular-expressions/tree/main)\n\n## Why?\n\nYou want to let end users enter their own regular expressions. But regular expressions can lead to [catastrophic backtracking](https://medium.com/@nitinpatel_20236/what-are-evil-regexes-7b21058c747e). This can take up hours of CPU time. In Node.js this means no other code can execute. It is a Denial of Service (DOS) attack vector, whether intentionally or by accident.\n\nThis module lets you test regular expressions with a time limit to mitigate the pain.\n\n## Usage\n\n```javascript\n// Set a 1-second limit. Default is 0.25 seconds\nconst regExp = require('time-limited-regular-expressions')({ limit: 1 });\n\n// A common email address validator with potentially evil characteristics\n// (catastrophic backtracking)\nconst evil = /^([a-zA-Z0-9])(([\\-.]|[_]+)?([a-zA-Z0-9]+))*(@){1}[a-z0-9]+[.]{1}(([a-z]{2,3})|([a-z]{2,3}[.]{1}[a-z]{2,3}))$/;\n\n(async () =\u003e {\n  // Run a potentially slow regular expression on short, matching input\n  const realEmail = 'test@test.com';\n  const realEmailResult = await regExp.match(evil, realEmail);\n  // Normal behavior, may be truthy or falsy according to match,\n  // returns the same array result as regular regexp match() calls\n  console.log(realEmailResult);\n  // This input is long enough to trigger catastrophic backtracking and\n  // could take hours to evaluate\n  const evilEmail = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';\n  try {\n    const evilEmailResult = await regExp.match(evil, evilEmail);\n    // We will not get here, exception will be thrown\n  } catch (e) {\n    console.log(e.name); // Will be 'timeout'\n  }\n})();\n```\n\n## Notes\n\n\"Why is `match` an async function?\" It runs in a separate process because that is the only way to avoid starving the Node.js application and implement a portable timeout on the regular expression.\n\n\"How bad is the performance overhead?\" Communication with a separate worker process makes it slower of course, but the process is reused by later calls, so the hit is not serious.\n\nFlags, for instance the `g` flag, are supported.\n\nYou can pass the regular expression as a string, but regular expression literals (what you are used to typing) are easier to get right because you don't have to double-escape anything.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapostrophecms%2Ftime-limited-regular-expressions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapostrophecms%2Ftime-limited-regular-expressions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapostrophecms%2Ftime-limited-regular-expressions/lists"}