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

fix: added protocols to the url along with correct port range #519

Merged
merged 7 commits into from
Apr 26, 2023

Conversation

allroundexperts
Copy link
Contributor

@allroundexperts allroundexperts commented Apr 18, 2023

Fixed Issues

$ Expensify/App#17205

Tests

This is to be tested with Expensify/App#17564

QA

No QA

@github-actions
Copy link

github-actions bot commented Apr 18, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@allroundexperts
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@allroundexperts
Copy link
Contributor Author

recheck

@puneetlath puneetlath self-requested a review April 18, 2023 15:45
@puneetlath
Copy link
Contributor

This makes sense to me. Let's add tests for it though.

@thesahindia
Copy link
Member

@allroundexperts, is this ready for the review?

@allroundexperts allroundexperts marked this pull request as ready for review April 18, 2023 18:00
@allroundexperts allroundexperts requested a review from a team as a code owner April 18, 2023 18:00
@allroundexperts
Copy link
Contributor Author

@allroundexperts, is this ready for the review?

Sorry, forgot to make this ready for review. Although I have to write tests as suggested by @puneetlath.

@melvin-bot melvin-bot bot requested review from MonilBhavsar and removed request for a team April 18, 2023 18:01
@allroundexperts
Copy link
Contributor Author

@puneetlath Added tests. Also exported URL_REGEX_WITH_REQUIRED_PROTOCOL that requires protocol to be present in the url as suggested by @MonilBhavsar

const addEscapedChar = reg => `(?:${reg}|&(?:amp|quot|#x27);)`;
const URL_PATH_REGEX = `(?:${addEscapedChar('[.,=(+$!*]')}?\\/${addEscapedChar('[-\\w$@.+!*:(),=%~]')}*${addEscapedChar('[-\\w~@:%)]')}|\\/)*`;
const URL_PARAM_REGEX = `(?:\\?${addEscapedChar('[-\\w$@.+!*()\\/,=%{}:;\\[\\]\\|_]')}*)?`;
const URL_FRAGMENT_REGEX = `(?:#${addEscapedChar('[-\\w$@.+!*()[\\],=%;\\/:~]')}*)?`;
const URL_REGEX = `(${URL_WEBSITE_REGEX}${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`;

const URL_REGEX_WITH_REQUIRED_PROTOCOL = URL_REGEX.replace(`${URL_PROTOCOL_REGEX}?`, URL_PROTOCOL_REGEX);
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't consider SMTP a valid protocol in bank account flow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MonilBhavsar Updated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also add this to automated test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

thesahindia
thesahindia previously approved these changes Apr 19, 2023
Copy link
Member

@thesahindia thesahindia left a comment

Choose a reason for hiding this comment

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

Looks good!

@allroundexperts
Copy link
Contributor Author

@MonilBhavsar @puneetlath Kind bump for review.

Comment on lines +23 to +24
expect(regexToTest.test('https://google.com:02')).toBeFalsy();
expect(regexToTest.test('https://google.com:65536')).toBeFalsy();
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure, if we want to add under this test suite. I think this goes better under invalid URL's?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I understand you here. All the failing cases are in its own it block. Do you want me to move these in there own describe block?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I think in this it block, we want to test URL's that doesn't have a protocol.
These examples are falsy because of invalid ports but not because they have invalid protocol.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I've written this differently. In the first describe block, I'm checking for both invalid ports and protocol because the regex used there expects a mandatory protocol. For the second describe block, I'm not check the protocol because it becomes optional. Does that make sense?

@@ -1,16 +1,22 @@
import TLD_REGEX from './tlds';

const URL_WEBSITE_REGEX = `(https?:\\/\\/)?((?:www\\.)?[-a-z0-9]+?\\.)+(?:${TLD_REGEX})(?:\\:\\d{2,4}|\\b|(?=_))`;
const ALLOWED_PORTS = '([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])';
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, could you please explain the range of ports covered here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The valid port range is from 1-65535

const addEscapedChar = reg => `(?:${reg}|&(?:amp|quot|#x27);)`;
const URL_PATH_REGEX = `(?:${addEscapedChar('[.,=(+$!*]')}?\\/${addEscapedChar('[-\\w$@.+!*:(),=%~]')}*${addEscapedChar('[-\\w~@:%)]')}|\\/)*`;
const URL_PARAM_REGEX = `(?:\\?${addEscapedChar('[-\\w$@.+!*()\\/,=%{}:;\\[\\]\\|_]')}*)?`;
const URL_FRAGMENT_REGEX = `(?:#${addEscapedChar('[-\\w$@.+!*()[\\],=%;\\/:~]')}*)?`;
const URL_REGEX = `(${URL_WEBSITE_REGEX}${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`;

const URL_REGEX_WITH_REQUIRED_PROTOCOL = URL_REGEX.replace(`${URL_PROTOCOL_REGEX}?`, URL_PROTOCOL_REGEX);
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

Updated spaces
@allroundexperts
Copy link
Contributor Author

@MonilBhavsar @puneetlath Can you please review this so that the ticket can move forward?

Copy link
Contributor

@MonilBhavsar MonilBhavsar left a comment

Choose a reason for hiding this comment

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

Code looks good to me! I want to test somethings. But my app build is down, so I'll test and approve it before EOD

puneetlath
puneetlath previously approved these changes Apr 24, 2023
Copy link
Contributor

@MonilBhavsar MonilBhavsar left a comment

Choose a reason for hiding this comment

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

I just did a quick testing. We should have identified the whole URL including protocol, no?

Screenshot 2023-04-25 at 6 09 50 PM

@allroundexperts
Copy link
Contributor Author

I just did a quick testing. We should have identified the whole URL including protocol, no?

Screenshot 2023-04-25 at 6 09 50 PM

@MonilBhavsar Are you sure that you tested this correctly? Here is how it looks on my side:

Screen.Recording.2023-04-26.at.12.50.24.AM.mov

Copy link
Contributor

@puneetlath puneetlath left a comment

Choose a reason for hiding this comment

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

It looks good to me but would like @MonilBhavsar to confirm as well. Thanks!

@MonilBhavsar
Copy link
Contributor

Hmm, Looks the lib is linked to project correctly but let me double check.

@MonilBhavsar
Copy link
Contributor

Sorry, i think I had issue linking because of conflicting node versions
Looks good and works well. Thanks for working on this!

Screenshot 2023-04-26 at 10 30 17 AM

@MonilBhavsar MonilBhavsar merged commit 3cdaa94 into Expensify:main Apr 26, 2023
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.

4 participants