Giter Club home page Giter Club logo

url-regex-safe's Introduction

url-regex-safe

build status code style styled with prettier made with lass license npm downloads

Regular expression matching for URL's. Maintained, safe, and browser-friendly version of url-regex. Resolves CVE-2020-7661 for Node.js servers. Works in Node v14+ and browsers. Maintained for Spam Scanner and Forward Email.

Table of Contents

Foreword

After discovering CVE-2020-7661 and disclosing it publicly (through my work on Spam Scanner and Forward Email) – I used an implementation of url-regex with some extra glue on top to filter out bad URL matches.

However after using it on Forward Email in production (which processes hundreds of thousands of emails per week), I found and documented several more core issues with url-regex.

Realizing that url-regex is no longer actively maintained, has 9 open pull requests as of this writing, and also lacked browser support – I decided to write this package for everyone and merge all the open pull requests.

This package should hopefully more closely resemble real-world intended usage of a URL regular expression, and also allowing the user to configure it as they wish. Please check out Forward Email if this package helped you, and explore our source code on GitHub which shows how we use this package.

Install

NOTE: The default behavior of this package will attempt to load re2 (it is an optional peer dependency used to prevent regular expression denial of service attacks and more). If you wish to use this behavior, you must have re2 installed via npm install re2 – otherwise it will fallback to using normal RegExp instances. As of v4.0.0 we added an option if you wish to force this package to not even attempt to load re2 (e.g. it's in your node_modules but you don't want to use it) – simply pass re2: false as an option.

npm:

npm install url-regex-safe

Usage

Node

We've resolved CVE-2020-7661 by including RE2 for Node.js usage. You will not have to manually wrap your URL regular expressions with new RE2(urlRegex()) anymore through url-regex-safe (we do it automatically for you).

const urlRegexSafe = require('url-regex-safe');

const str = 'some long string with url.com in it';
const matches = str.match(urlRegexSafe());

for (const match of matches) {
  console.log('match', match);
}

console.log(urlRegexSafe({ exact: true }).test('github.com'));

Browser

Since RE2 is not made for the browser, it will not be used, and therefore CVE-2020-7661 is still an issue on the client-side. However it is not severe since the most it would do is crash the browser tab (as on the Node.js side it would have crashed the entire process and thrown an out of memory exception).

VanillaJS

This is the solution for you if you're just using <script> tags everywhere!

<script src="https://unpkg.com/url-regex-safe"></script>
<script type="text/javascript">
  (function() {
    var str = 'some long string with url.com in it';
    var matches = str.match(urlRegexSafe());

    for (var i=0; i<matches.length; i++) {
      console.log('match', matches[i]);
    }

    console.log(urlRegexSafe({ exact: true }).test('github.com'));
  })();
</script>

Bundler

Assuming you are using browserify, webpack, rollup, or another bundler, you can simply follow Node usage above.

TypeScript

To use this package with TypeScript, you can install the @types/url-regex-safe package for type definitions.

npm install --save-dev @types/url-regex-safe

Options

Property Type Default Value Description
re2 Boolean true Attempt to load re2 to use instead of RegExp for creating new regular expression instances. If you pass re2: false, then re2 will not even be attempted to be loaded.
exact Boolean false Only match an exact String. Useful with regex.test(str) to check if a String is a URL. We set this to false by default in order to match String values such as github.com (as opposed to requiring a protocol or www subdomain). We feel this closely more resembles real-world intended usage of this package.
strict Boolean false Force URL's to start with a valid protocol or www if set to true. If true, then it will allow any TLD as long as it is a minimum of 2 valid characters. If it is false, then it will match the TLD against the list of valid TLD's using tlds.
auth Boolean false Match against Basic Authentication headers. We set this to false by default since it was deprecated in Chromium, and otherwise it leaves the user with unwanted URL matches (more closely resembles real-world intended usage of this package by having it set to false by default too).
localhost Boolean true Allows localhost in the URL hostname portion. See the test/test.js for more insight into the localhost test and how it will return a value which may be unwanted. A pull request would be considered to resolve the "pic.jp" vs. "pic.jpg" issue.
parens Boolean false Match against Markdown-style trailing parenthesis. We set this to false because it should be up to the user to parse for Markdown URL's.
apostrophes Boolean false Match against apostrophes. We set this to false because we don't want the String background: url('http://example.com/pic.jpg'); to result in http://example.com/pic.jpg'. See this issue for more information.
trailingPeriod Boolean false Match against trailing periods. We set this to false by default since real-world behavior would want example.com versus example.com. as the match (this is different than url-regex where it matches the trailing period in that package).
ipv4 Boolean true Match against IPv4 URL's.
ipv6 Boolean true Match against IPv6 URL's.
tlds Array tlds Match against a specific list of tlds, or the default list provided by tlds.
returnString Boolean false Return the RegExp as a String instead of a RegExp (useful for custom logic, such as we did with Spam Scanner).

Quick tips and migration from url-regex

You must override the default and set strict: true if you do not wish to match github.com by itself (though www.github.com will work if strict: false).

Unlike the deprecated and unmaintained package url-regex, we do a few things differently:

  • We set strict to false by default (url-regex had this set to true)
  • We added an auth option, which is set to false by default (url-regex matches against Basic Authentication; had this set to true - however this is a deprecated behavior in Chromium).
  • We added parens and ipv6 options, which are set to false and true by default (url-regex had parens set to true and ipv6 was non-existent or set to false rather).
  • We added an apostrophe option, which is set to false by default (url-regex had this set to true).
  • We added a trailingPeriod option, which is set to false by default (which means matches won't contain trailing periods, whereas url-regex had this set to true).

Limitations

This limitation only applies if you are using re2: Since we cannot use regular expression's "negative lookbehinds" functionality (due to RE2 limitations), we could not merge the logic from this pull request. This would have allowed us to make it so example.jpeg would match only if it was example.jp, however if you pass example.jpeg right now it will extract example.jp from it (since .jp is a TLD). An alternative solution may exist, and we welcome community contributions regarding this issue.

Contributors

Name Website
Forward Email LLC https://forwardemail.net
Kevin Mårtensson
Diego Perini

License

MIT © Forward Email LLC

url-regex-safe's People

Contributors

birtles avatar kevinnovak avatar niftylettuce avatar pjotrsavitski avatar titanism avatar vicary avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

url-regex-safe's Issues

browser friendly

Hello,

I'm opening this issue because, although I adopted url-regex-safe instead of url-regex, recently discovered url-regex-safe depends on re2 and it isn't a suitable browser dependency:

uhop/node-re2#77 (comment)

There is a proposition to make re2 browser friendly

uhop/node-re2#18

but until that happens, this is making me not possible to adopt url-regex-safe

not sure if you have an opinion about that, just I wanted to warn you 🙂

Not working with TypeScript

Would it be possible to make this package TypeScript friendly? This seems to be one thing that url-regex module had.

Support closing parentheses in URLs do not match

Describe the bug

Node.js version: v20.9.0

OS version: FreeBSD freebsd 13.2-RELEASE-p4

Description: URL with ) characters are not matched.

Actual behavior

Testing with an exact match against a url like http://foo.com/blah_blah_(wikipedia) returns false.

Expected behavior

Testing with an exact match against a URL like http://foo.com/blah_blah_(wikipedia) should return true.

Code to reproduce

const urlRegex = require( 'url-regex-safe' );

const s0 = 'http://foo.com/blah_blah_(wikipedia';
const s1 = 'http://foo.com/blah_blah_(wikipedia)';

// Returns true as expected
console.log(urlRegex({exact: true}).test(s0));

// Should be true, but return false
console.log(urlRegex({exact: true}).test(s1));

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Add option to not use `re2` even if it's in the dependency tree

Describe the feature

I want to use this package, but I never want to use the re2 package. However, the re2 package may be in node_modules even if I don't depend on it. So whether it's used, is out of my control.

I have my own way of ensuring safety, and re2 is problematic, as the regex it returns is not a proper RegExp instance.

It would be useful with an option to force using normal RegExp.

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.

Issue with Email Addresses Getting pulled In

So we allow our end users to send test strings out and those strings can contain a mix of urls and email addresses. We are using a email regex function to pull out all email addresses and validate those emails against a white list that we have. We also do the same for urls using url-regex-safe to pull out URLs from the string and validate against a different white list.

The issue I am encountering is that url-regex-safe is pulling in portions of the email or the email domain.

For example: This is a test of our notification system, for any questions please go to www.test.com/info for further details. To get further information on the process please email [email protected], or [email protected].

What url-regex-safe will do is get www.test.com/info, test.com, and Bob.Sm to be evaluated.

Can anything be done to exclude email addresses from the urlRegexSafe function?

Regex not matching URLS

Hi,

<img class="central-featured-logo" src="portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png" srcset="portal/wikipedia.org/assets/img/[email protected] 1.5x, portal/wikipedia.org/assets/img/[email protected] 2x" width="200" height="183" alt="Wikipedia">

(Wikipedia Homepage)
Here the URL that is matched is wikipedia.org/assets/img/Wikipedia-logo-v2.png instead of portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png

        var matches = StringToMatch.match(urlRegexSafe({
            localhost: false,
            ipv4: false,
            ipv6: false
        }));

Am I doing something wrong or it's url-regex-safe that isn't doing proper match?
Thanks for the help

jest malloc errors

When I import const urlRegexSafe = require("url-regex-safe");

and run jest --runInBand, the following error shows up:

node(30943,0x10a4d3600) malloc: *** error for object 0x600000590010: pointer being freed was not allocated
node(30943,0x10a4d3600) malloc: *** set a breakpoint in malloc_error_break to debug

And when running in github actions, it shows

Ran all test suites.
malloc_consolidate(): invalid chunk size
Aborted (core dumped)
error Command failed with exit code 134.

At first I thought it was jsdom, then I thought htmlparser2 or even parser5, but in the end was this library that seems to make the error.

Any thoughts why?

High CPU usage compared to 'url-regex'

Hi,

We replaced 'url-regex' with 'url-regex-safe' and noticed an unusually high CPU usage. When running the following example code on my machine, 'url-regex' consumes around 1.5% CPU while 'url-regex-safe' consumes 99%. Is this expected?

const urlRegex = require("url-regex");
const urlRegexSafe = require("url-regex-safe");

setInterval(()=> {
    const s = "Test123 http://example.com test..."
    s.match(urlRegex()); // Replace with urlRegexSafe for comparison
}, 10);

Capture group support?

In order to do things like:

escapeHTML(text).replace(
  urlRegexSafe(),
  '<a href="$1" target="_blank" rel="noreferrer noopener">$1</a>'
)

Extra characters at the end

Hey

So I used this code to extract some urls from a html file.

const urlRegex = require("url-regex-safe");
function urlsFromText(text) {
    if (!text) {
        return [];
    }

    const matchingUrls = text.match(urlRegex({
        localhost: true,
    }));
    return matchingUrls || [];
}

When I run it, I get the following matches (the html does have the same links across several places) and as you can see, there are some places where it is found ok, and others are not removing the last characters. NOTE: I changed the hostname, but is ok, that is not the part that is failing.

[
"http://thisdomain.com/c/eJwtjsEKwyAQRL8muUU2qzF68NBLf6NsNGkEbYJryO_XQmHgwTDMTHBLmLzEPjoERBhBgVYGjcAwWw12lAq8DOg7BZliEr7kclAQ_sj97qzXejVWBW2lwcnONM4G1DLJAITr1ie313pyJx8dPpvu-xb3mhJvVN7Hr6WZZ1mZG9u--gMQphdnSmlYLo6fFhiOs8YcOffF8U47lfZpScS1UIgXC4pf7dg_Yw",
"http://thisdomain.com/c/eJwtjsEKwyAQRL8muUU2qzF68NBLf6NsNGkEbYJryO_XQmHgwTDMTHBLmLzEPjoERBhBgVYGjcAwWw12lAq8DOg7BZliEr7kclAQ_sj97qzXejVWBW2lwcnONM4G1DLJAITr1ie313pyJx8dPpvu-xb3mhJvVN7Hr6WZZ1mZG9u--gMQphdnSmlYLo6fFhiOs8YcOffF8U47lfZpScS1UIgXC4pf7dg_Yw>.",
"https://thisdomain.com/ls/click?upn=Edw9Mjq0OQ4hwVYZdSS19DmzTkQO2hOfz77XO47T2bIMc3fVT4uKWvVpzZJxLCO-2BnMr4_n1llmOec-2BgJkFgpT9Du8t95rbeVygh6Lk33ithME8pCC9rzKG6j6Ja37TSev7QnwrTdkQhfH80qgFSxfMHmNaXGZNOk-2Fah53KlwQ7jgpJujJwj8MSytOl1hYAwh9wbU6yCqiOm0BH8MT1C606xPjKSfXMcQhi6XbDKFSpeCfAX2BSplJFosHqoO-2B47y56WQ-2BMAjh5TPyYzCTBsVurHpCTeYNo17KesLVQSfiE4yBkMNN-2BlStPCGUbntKRMrf-2BnL0cbPriBj1FSi86bbTY6q6vT2wXwB-2BognImKofq803zMLG2JNz6lR1-2Bo7ms72uVRfaNP2xuG3hM2hDzfXDhcTuJXCMrdnKreeZEhSHuS77-2FYXZ1IP35IVzKn6H8MD05V758Ig6FB5GALPf6RS7g7aV-2Fw7U-2FxFGrxjg6QgEWdYh1Jg-3D",
"https://thisdomain.com/ls/click?upn=Edw9Mjq0OQ4hwVYZdSS19DmzTkQO2hOfz77XO47T2bIMc3fVT4uKWvVpzZJxLCO-2BnMr4_n1llmOec-2BgJkFgpT9Du8t95rbeVygh6Lk33ithME8pCC9rzKG6j6Ja37TSev7QnwrTdkQhfH80qgFSxfMHmNaXGZNOk-2Fah53KlwQ7jgpJujJwj8MSytOl1hYAwh9wbU6yCqiOm0BH8MT1C606xPjKSfXMcQhi6XbDKFSpeCfAX2BSplJFosHqoO-2B47y56WQ-2BMAjh5TPyYzCTBsVurHpCTeYNo17KesLVQSfiE4yBkMNN-2BlStPCGUbntKRMrf-2BnL0cbPriBj1FSi86bbTY6q6vT2wXwB-2BognImKofq803zMLG2JNz6lR1-2Bo7ms72uVRfaNP2xuG3hM2hDzfXDhcTuJXCMrdnKreeZEhSHuS77-2FYXZ1IP35IVzKn6H8MD05V758Ig6FB5GALPf6RS7g7aV-2Fw7U-2FxFGrxjg6QgEWdYh1Jg-3D>;",
"https://thisdomain.com/ls/click?upn=Edw9Mjq0OQ4hwVYZdSS19LQ-2FdstCwdNG97aq-2BoKXcUNnhvG3KpLkcq0oeyJNtaudeZ0V_n1llmOec-2BgJkFgpT9Du8t95rbeVygh6Lk33ithME8pCC9rzKG6j6Ja37TSev7QnwrTdkQhfH80qgFSxfMHmNaXGZNOk-2Fah53KlwQ7jgpJujJwj8MSytOl1hYAwh9wbU6yCqiOm0BH8MT1C606xPjKY4AYzg-2BbyFJle44p2Nwr3WIWW3AiLXnesEuTNuz17FZAbx6h2oWpO8I-2FbW4LJl88L6h6QCn5mnYgDikeWl-2FKWL-2BrgosEqEoH-2FskquLIQktySB1kz6M-2FT-2BhXu8C2DdXlfI3ahSRNjQIvkwp-2FzFbTdlxJ32vRnbdSrmTJS97orQlk0q2wr9jr9QMYq4hKUIrjNuyEO7AFhK7N8pzPq-2FNbR4BJEauwBP33v7NWzR-2BQ4VFbdI-2B7E4t04555TlbB0ndkLaGJ2hyI1o1YECwmiqWkceI-3D>[image:",
"https://thisdomain.com/ls/click?upn=Edw9Mjq0OQ4hwVYZdSS19PFHnXZ1cLWsRvhx9RaY-2BQAS5Vos-2BHGFwfuQwfhpbU-2FZ7JjEkayk2WmqvPwVmk2DWQ-3D-3DI3QJ_n1llmOec-2BgJkFgpT9Du8t95rbeVygh6Lk33ithME8pCC9rzKG6j6Ja37TSev7QnwrTdkQhfH80qgFSxfMHmNaXGZNOk-2Fah53KlwQ7jgpJujJwj8MSytOl1hYAwh9wbU6yCqiOm0BH8MT1C606xPjKcCDbuMBMgW5oVfHk-2BCaODfQayFCp9YHYhzQAPKJJbSmYqbtbTZ98nj0XgwxLBsj8NSfuVuXc1KqTFvvMKzlByWqJSDk7JWWOhJEoG3D9NphMRpU69JGqsu-2BDnC8c4XQxC-2BeSx-2FvQ1J0C0dEMt1kAQilciDJK926NIxxyok4LZSp-2FVoIe4H3LLTYGv1H8MSN1R4REVk8n6uCvjmox0-2Blq-2FOUFtwLCOQF-2BkqqM9gbAPhWSBnVBZcwHalHIktdK2pN-2BmXznQQ4R0yYRELFY2-2BcMrI-3D>[image:",
]

Is this a bug? or is it something we can improve somehow without changing the library?

Thanks!

url regex unsafe

Well. safe-Regex complains that the regex for url is considered "unsafe".

How can we implement it "safe" without deep digging in the regex?

something like this?

(url) => {
	try {
		const parsedUrl = URL(url).toString();
		return url === parsedUrl;
	} catch (e) {
		return false;
	}
}

Matches email addresses (within a larger string) with default options

If you take this string: "Please send an email to [email protected], providing ONLY your first name", this module will return "test.com" as a match.

Is this intended behavior and I am just misunderstanding how it's supposed to work?

I see that the "auth" option (if set to true) may result in undesired matches to email addresses, but it defaults to false.
I don't see a test case in the tests for this scenario. (see comment below)

What's interesting is that you commented on this exact problem in a relevant url-regex issue:

We're using this library to pull urls out of a user input string. We're currently running into a problem where it returns email addresses.

'This is an [email protected]'.match(urlRegex({ exact: false, strict: false }));
// returns ['[email protected]']

I think it should return no matches in this case.

...

This issue is fixed in my maintained and modern version of this package at https://github.com/niftylettuce/url-regex-safe. You should be able to switch from url-regex to url-regex-safe now. See the updated list of options as I added some new ones, and changed a few defaults to more sensible ones (since not everyone is parsing Markdown for instance).

Here are the relevant options that might impact this test, although they are all just their default values so wouldn't need to be explicitly set in a test:
exact: false
strict: false
auth: false

Install fails because "re2" dependency can't find Python

When I try to install url-regex-safe with the following command:

npm install [email protected]

I get the following error:

npm ERR! code 1
npm ERR! path MyPackage\node_modules\re2
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c install-from-cache --artifact build/Release/re2.node --host-var RE2_DOWNLOAD_MIRROR || npm run rebuild
npm ERR! Trying https://github.com/uhop/node-re2/releases/download/1.16.0/win32-x64-102.br ...
npm ERR! Trying https://github.com/uhop/node-re2/releases/download/1.16.0/win32-x64-102.gz ...
npm ERR! Building locally ...
npm ERR! 
npm ERR! > [email protected] rebuild
npm ERR! > node-gyp rebuild
npm ERR! 
npm ERR! 
npm ERR! > [email protected] rebuild
npm ERR! > node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | win32 | x64
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration
npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
npm ERR! gyp ERR! find Python checking if "python3" can be used
npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if "python" can be used
npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python39\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python39\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python39\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python39\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python38\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python38\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python38\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python38\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python37\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python37\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python37\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python37\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python36\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python36\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python36\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python36\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if the py launcher can be used to find Python 3
npm ERR! gyp ERR! find Python - "py.exe" is not in PATH or produced an error
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python You need to install the latest version of Python.
npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
npm ERR! gyp ERR! find Python you can try one of the following options:
npm ERR! gyp ERR! find Python - Use the switch --python="C:\Path\To\python.exe"
npm ERR! gyp ERR! find Python   (accepted by both node-gyp and npm)
npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
npm ERR! gyp ERR! find Python   npm config set python "C:\Path\To\python.exe"
npm ERR! gyp ERR! find Python For more information consult the documentation at:
npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
npm ERR! gyp ERR! stack     at PythonFinder.fail (MyPackage\node_modules\node-gyp\lib\find-python.js:330:47)
npm ERR! gyp ERR! stack     at PythonFinder.runChecks (MyPackage\node_modules\node-gyp\lib\find-python.js:159:21)
npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (MyPackage\node_modules\node-gyp\lib\find-python.js:228:18)
npm ERR! gyp ERR! stack     at PythonFinder.execFileCallback (MyPackage\node_modules\node-gyp\lib\find-python.js:294:16)
npm ERR! gyp ERR! stack     at exithandler (node:child_process:404:5)
npm ERR! gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:416:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
npm ERR! gyp ERR! stack     at onErrorNT (node:internal/child_process:475:16)
npm ERR! gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
npm ERR! gyp ERR! System Windows_NT 10.0.22000
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "MyPackage\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd MyPackage\node_modules\re2
npm ERR! gyp ERR! node -v v17.1.0
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok
npm ERR! node:internal/process/promises:246
npm ERR!           triggerUncaughtException(err, true /* fromPromise */);
npm ERR!           ^
npm ERR!
npm ERR! [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "1".] {
npm ERR!   code: 'ERR_UNHANDLED_REJECTION'
npm ERR! }
npm ERR!
npm ERR! Node.js v17.1.0
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | win32 | x64
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration
npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
npm ERR! gyp ERR! find Python checking if "python3" can be used
npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if "python" can be used
npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python39\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python39\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python39\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python39\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python38\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python38\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python38\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python38\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python37\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python37\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python37\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python37\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python36\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python36\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python36\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python36\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is AppData\Local\Programs\Python\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "AppData\Local\Programs\Python\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if the py launcher can be used to find Python 3
npm ERR! gyp ERR! find Python - "py.exe" is not in PATH or produced an error
npm ERR! gyp ERR! find Python 
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python You need to install the latest version of Python.
npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
npm ERR! gyp ERR! find Python you can try one of the following options:
npm ERR! gyp ERR! find Python - Use the switch --python="C:\Path\To\python.exe"
npm ERR! gyp ERR! find Python   (accepted by both node-gyp and npm)
npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
npm ERR! gyp ERR! find Python   npm config set python "C:\Path\To\python.exe"
npm ERR! gyp ERR! find Python For more information consult the documentation at:
npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
npm ERR! gyp ERR! stack     at PythonFinder.fail (MyPackage\node_modules\node-gyp\lib\find-python.js:330:47)
npm ERR! gyp ERR! stack     at PythonFinder.runChecks (MyPackage\node_modules\node-gyp\lib\find-python.js:159:21)
npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (MyPackage\node_modules\node-gyp\lib\find-python.js:228:18)
npm ERR! gyp ERR! stack     at PythonFinder.execFileCallback (MyPackage\node_modules\node-gyp\lib\find-python.js:294:16)
npm ERR! gyp ERR! stack     at exithandler (node:child_process:404:5)
npm ERR! gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:416:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
npm ERR! gyp ERR! stack     at onErrorNT (node:internal/child_process:475:16)
npm ERR! gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
npm ERR! gyp ERR! System Windows_NT 10.0.22000
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "MyPackage\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd MyPackage\node_modules\re2
npm ERR! gyp ERR! node -v v17.1.0
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     AppData\Local\npm-cache\_logs\2021-12-04T17_28_58_533Z-debug.log

This happens even with a fresh node project (no files, just barebones package.json). I do not have Python installed. Is Python really a dependency for this package? Can something be changed to avoid a dependency on Python?

Seems related to Issue #18.

[fix] Invalid protocols are matched

Describe the bug

Node.js version: v18.18.2

OS version: macOS 14.2.1

Description: If a valid protocol has extra characters preceding it, the extra characters are included in the match.

Actual behavior

urlRegexSafe({ strict: true }).exec("gaewggwhttp://localhost:3000/derp")

Produces a match that contains the entire string, including "gaewgg".

This also happens when strict is set to false.

Expected behavior

The match that is produced does not include "gaewgg"

Code to reproduce

const urlRegexSafe = require('url-regex-safe')
const match = urlRegexSafe({ strict: true }).exec("gaewggwhttp://localhost:3000/derp")
console.log(match)

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

re2 breaks builds

This originates from uhop/node-re2#92, affecting builds via webpack and esbuild, browser or TypeScript for servers all alike.

Option 1

Change re2 into a peer dependency, such that users can choose to install re2 or not, depending on their situation.

Option 2

If the existence of url-regex-safe over url-regex means re2 must be kept in dependencies, at least change the actual code to allow fallback to plain RegExp, so webpack/esbuild users can mark re2 as external in build time until the issue above is fixed.

Wrong match in html

the matching results for this string are wrong

const text = "<div><a href="https://www.test.com/">https://www.test.com/</a></div>"
text.match(urlRegexSafe());

["https://www.test.com/", "https://www.test.com/</a></div>"]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.