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

Add HTTP URLs verification by default #14

Closed
wants to merge 20 commits into from
Closed

Add HTTP URLs verification by default #14

wants to merge 20 commits into from

Conversation

JoaoOliveiraRocha
Copy link

@JoaoOliveiraRocha JoaoOliveiraRocha commented Apr 28, 2021

Attempt to cover #11. Possible breaking changes.

It now verifies if it's an HTTP URL using a RegEx based on this

You can now use isAbsolutePath('http://sindresorhus.com'). It will behave as isAbsolutePath('http://sindresorhus.com', true).

There is possible to isAbsolutePath('http://sindresorhus.com', false) and get the same results as before this PR (it even uses the original RegEx to evaluate the string).

I've decided to K.I.S.S. and keep the PR as you mentioned here, in #11 and #12. Since there are no requests for extra features there's no need to "prepare" for expansion right now.

Note: this package returns true on situations like http://sindr esorhus.com (white-space in between). I kept its original behavior. If you think it would be helpful for this package to check for whitespaces (and warn/return false) please inform me!

This last object, called Options, can have some parameters (so it's easier to add extra features later). Right now I've only done 'httpOnly'.

It has 3 levels of 'strictness': 'strictest', 'strict' and 'loose' / 'true. Basically strictest is the rigid http://www.lorem.ipsum.com/path, the strict is something your browser could understand, and loose only checks for the HTTP protocol header.

I have wrote some examples on the /readme.md file.

I've also copied the current array of tests to test against the 'levels of strictness'. If you have any doubt about each link I suggest open the test files and see what is the result of each "URL" against each parameter.


Fixes #11

readme.md Outdated
```



Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do unrelated changes. Applies everywhere.

index.d.ts Outdated
@@ -17,6 +17,6 @@ isAbsoluteUrl('foo/bar');
//=> false
```
*/
declare function isAbsoluteUrl(url: string): boolean;
declare function isAbsoluteUrl(url: string, options: object): boolean;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus
Copy link
Owner

There are a lot of typos and formatting problems. Try to look over the diff and improve it: https://github.com/sindresorhus/is-absolute-url/pull/14/files

@sindresorhus
Copy link
Owner

Why 3 states instead of just a httpOnly option as mentioned in #11?

@JoaoOliveiraRocha
Copy link
Author

Why 3 states instead of just a httpOnly option as mentioned in #11?

It's possible to simply set httpOnly as true and it will behave the way #11 intended.

I've added the 3 states because 1) according to the RFC: URLs should be case-sensitive. And 2) it could be expanded in the future to check other conditions p.e. if is an absolute ftp address, contains the port or to check for secure HTTPS only ( p.e. pass { httpOnly: true, portOnly: true } for validating a web link with a defined port ).

Or do you think it's better to keep it simple and go in a way #12 tried to archive?

Copy link

@satazor satazor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of unrelated changes in the docs.

index.js Outdated
@@ -1,16 +1,35 @@
'use strict';

module.exports = url => {
// Options - httpOnly
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Options - httpOnly
// Options
// - httpOnly

readme.md Outdated
@@ -26,19 +38,90 @@ isAbsoluteUrl('foo/bar');
```


## Related
## Options Parameters
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Options Parameters
## Options

readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
readme.md Outdated

See [is-relative-url](https://github.com/sindresorhus/is-relative-url) for the inverse.
### httpOnly

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep the docs simple like how it's done in the Usage section. A simple js block with lines and their result as comments would be better for readability.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. Can you provide feedback now please?

@sindresorhus
Copy link
Owner

I think it should be a single httpOnly boolean option.

@sindresorhus
Copy link
Owner

  • strictest > Everything accordingly to RFC 1738 - Case sensititive

This is incorrect. The URL scheme is case-insensitive.

@sindresorhus
Copy link
Owner

You missed this from the original issue:

How about an option called httpOnly which defaults to true

@@ -17,6 +17,10 @@ isAbsoluteUrl('foo/bar');
//=> false
```
*/
declare function isAbsoluteUrl(url: string): boolean;
interface Options {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index.d.ts Outdated
@@ -17,6 +17,10 @@ isAbsoluteUrl('foo/bar');
//=> false
```
*/
declare function isAbsoluteUrl(url: string): boolean;
interface Options {
httpOnly?: boolean | string;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tab-indentation

index.d.ts Outdated
@param url - The URL to check.
@param options - Object with different parameters to check.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't add much. I would remove it. People know what "options" means.

index.d.ts Outdated
@@ -1,7 +1,7 @@
/**
Check if a URL is absolute.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do unrelated changes.

readme.md Outdated
@@ -41,4 +56,4 @@ See [is-relative-url](https://github.com/sindresorhus/is-relative-url) for the i
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
</div>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do unrelated changes.

index.test-d.ts Outdated
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import isAbsoluteUrl = require('.');

expectType<boolean>(isAbsoluteUrl('https://sindresorhus.com/foo/bar'));
expectType<boolean>(isAbsoluteUrl('https://sindresorhus.com/foo/bar', { httpOnly: true }));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expectType<boolean>(isAbsoluteUrl('https://sindresorhus.com/foo/bar', { httpOnly: true }));
expectType<boolean>(isAbsoluteUrl('https://sindresorhus.com/foo/bar', {httpOnly: true}));

readme.md Outdated
isAbsoluteUrl('https://si', { httpOnly: true });
//=> true

is absoluteUrl('mailto:[email protected]', { httpOnly: true });
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

readme.md Outdated
@@ -25,6 +25,21 @@ isAbsoluteUrl('foo/bar');
//=> false
```

## Options
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the same format for readme docs as used here: https://github.com/sindresorhus/boxen#api

test.js Outdated
t.false(isAbsoluteUrl('C:\\Dev\\test-broken', {}));
t.false(isAbsoluteUrl('ht,tp://sindresorhus.com', {}));
});

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@sindresorhus
Copy link
Owner

The pull request needs a proper descriptive title.

@JoaoOliveiraRocha JoaoOliveiraRocha changed the title Attempt to solve #11 while maintaining the current usage Add HTTP URLs verification by default May 2, 2021
@JoaoOliveiraRocha
Copy link
Author

You missed this from the original issue:

How about an option called httpOnly which defaults to true

Done. I've also changed the title and description of this PR.

index.js Outdated
// - true : checks http(s) header and correct formatting or URL.
// - false: checks only accordance with rfc3986

module.exports = (url, httpOnly = true) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

httpOnly should be an options-object, not a parameter.

https://ariya.io/2011/08/hall-of-api-shame-boolean-trap

index.d.ts Show resolved Hide resolved
index.js Outdated
if (typeof url !== 'string') {
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``);
}

// Optional Chaining not supported by XO? please confirm
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code comments is not the place to discuss PR stuff. You can just comment on your own PR if you have questions.

index.js Outdated
if (typeof url !== 'string') {
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``);
}

// Optional Chaining not supported by XO? please confirm
if (httpOnly) {
return /^(((([hH][tT]{2}[pP]([sS])?)?:\/\/)(www\.)?)([-a-zA-Z0-9()@:%_.~#?&//=]*))/g.test(url);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex can be simplified.

index.js Outdated
module.exports = url => {
// Param: httpOnly
// - true : checks http(s) header and correct formatting or URL.
// - false: checks only accordance with rfc3986
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these comments. They don't add anything.

readme.md Outdated
Type: `boolean`
Default: `true`

Check if a URL is a http URL. Defaults to `true`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http => HTTP

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the sentence could be clearer about what it actually does.

readme.md Outdated
isAbsoluteUrl('https://si', true);
//=> true

isabsoluteUrl('mailto:[email protected]', false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

readme.md Outdated

isabsoluteUrl('mailto:[email protected]', false);
//=> true

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace

test.js Outdated
@@ -17,3 +17,61 @@ test('main', t => {
t.false(isAbsoluteUrl('C:\\Dev\\test-broken'));
t.false(isAbsoluteUrl('ht,tp://sindresorhus.com'));
});

test('HttpOnly set as true (same as default behavior). Assert TRUE tests', t => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too verbose test title. Formatting needs work too.

test.js Outdated
// eslint-disable-line no-script-url
t.true(isAbsoluteUrl('javascript:throw%20document.cookie', false));

t.true(isAbsoluteUrl('https://sindresorhus', false)); // Should this behavior exist? missing TLD?...
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of asking questions, try to research and figure it out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt it should at least check for the TLD, but it would return false to URLs as http://localhost:7700 and they are absolute. So it will not check for it.

@@ -1,10 +1,14 @@
'use strict';

module.exports = url => {
module.exports = (url, options = {httpOnly: true}) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct way to do default values for options objects. If another option is added, it will silently not do the right thing as both options will be overwritten if one is set.

Suggested change
module.exports = (url, options = {httpOnly: true}) => {
module.exports = (url, {httpOnly = true} = {}) => {

if (typeof url !== 'string') {
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``);
}

if (options.httpOnly) {
return /^((http[s]?)?:\/\/([-a-z0-9()@:%_.~#?&//=]*))/gi.test(url);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex could still be simplified.

readme.md Outdated Show resolved Hide resolved
Co-authored-by: Sindre Sorhus <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Only allow HTTP URLs by default
3 participants