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 url validation errors #11835

Merged
merged 5 commits into from Nov 16, 2018

Conversation

Projects
None yet
2 participants
@talldan
Contributor

talldan commented Nov 14, 2018

Description

When adding href validation in #11286 I made a few errors, which this PR addresses

  1. Fragments in URLs were not being checked correctly, any existence of a fragment would make a URL fail the validation 🤦‍♂️ . This was happening due to the wrong value being passed to isValidFragment (the entire href was being passed instead of just the fragment).
  2. Because of the issue above, tests were also returning a false positive when validating incorrect fragments with two hashes like '#test#test'. This was happening because getFragment only captured from the last occurrence of a hash, now it captures from the first.
  3. Validation of HTTP urls didn't catch issues like the incorrect number of forward slashes following the protocol. I've added a special case for HTTP urls.
  4. isValidProtocol considered a protocol with trailing forward slashes as valid. I realised this didn't match what getProtocol considered a protocol (characters up to and including the colon, e.g. 'http:'), so I've made it stricter.

How has this been tested?

  • Added test cases to unit tests
  • Manual testing
  1. Try adding one of the following URLs as a link:
  1. Expect the validation to fail (the link should be displayed in red text).
  2. Try adding the following URL as a link:
  1. Expect the validation to succeed (the link should be displayed normally).

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@talldan talldan self-assigned this Nov 14, 2018

@talldan talldan added the [Type] Bug label Nov 14, 2018

@talldan talldan referenced this pull request Nov 14, 2018

Merged

Add validation to link format in RichText component #11286

4 of 4 tasks complete

@talldan talldan force-pushed the fix/url-validation-cases branch from d926d48 to e810f59 Nov 14, 2018

@talldan talldan requested a review from noisysocks Nov 14, 2018

@noisysocks noisysocks self-assigned this Nov 15, 2018

@noisysocks noisysocks added this to the 4.5 milestone Nov 16, 2018

@noisysocks

👍 looks good!

@@ -60,7 +66,7 @@ export function isValidHref( href ) {
}

const fragment = getFragment( trimmedHref );
if ( fragment && ! isValidFragment( trimmedHref ) ) {
if ( fragment && ! isValidFragment( fragment ) ) {

This comment has been minimized.

@noisysocks

@noisysocks noisysocks force-pushed the fix/url-validation-cases branch from e810f59 to 3a9d211 Nov 16, 2018

@noisysocks noisysocks merged commit 2a0b617 into master Nov 16, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@noisysocks noisysocks deleted the fix/url-validation-cases branch Nov 16, 2018

grey-rsi pushed a commit to OnTheGoSystems/gutenberg that referenced this pull request Nov 22, 2018

Fix url validation errors (WordPress#11835)
* Fix issue where href validation would incorrectly consider any URL with a fragment to be invalid

* Resolve issue where getFragment only matched to the last # encountered instead of the first

* Add extra checks for http urls to ensure the right number of forward slashes are used

* Update isValidProtocol to only consider the protocol as characters up to and including the colon

* Update changelogs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment