Skip to content

Fix malformed conversion of relative action URLs for forms#4250

Merged
westonruter merged 7 commits intodevelopfrom
fix/4208-malformed-action-urls
Feb 9, 2020
Merged

Fix malformed conversion of relative action URLs for forms#4250
westonruter merged 7 commits intodevelopfrom
fix/4208-malformed-action-urls

Conversation

@pierlon
Copy link
Copy Markdown
Contributor

@pierlon pierlon commented Feb 6, 2020

Summary

Fixes #4208.

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@googlebot googlebot added the cla: yes Signed the Google CLA label Feb 6, 2020
} else {
// Otherwise, when the action URL includes an absolute path, just append it to the schemeless-host.
$action_url = $schemeless_host . $action_url;
$parsed_url = wp_parse_url( $action_url );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This conditional is getting big. Please move it into a helper method as I've done in #4212. This will make the condition needing less nesting, as you can just return at more points.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 1c9ab53.

// Ignore a malformed URL - it will be later sanitized.
if ( false !== $parsed_url ) {
// Handle relative URLs.
if ( ! isset( $parsed_url['scheme'] ) || 'https' !== $parsed_url['scheme'] ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why the 'https' !== $parsed_url['scheme'] condition here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To ignore URLs with the https scheme, otherwise it becomes schemeless which is unnecessary.

I've updated the comment there to reflect this:

// If there is no URL scheme or the scheme is not 'https', make it a schemeless URL.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@pierlon I think I simplified this further, and also added short-circuiting when it is a scheme-less URL (in which case nothing is left to do either). Please see e99eade.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! And its more readable 👍 .

Comment on lines +82 to +88
if ( ! isset( $parsed_url['path'] ) ) {
// If there is action URL path, use the one from the request.
$parsed_url['path'] = trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) );
} elseif ( '' !== $parsed_url['path'] && '/' !== $parsed_url['path'][0] ) {
// If the path is relative, append it to the current request path.
$parsed_url['path'] = trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . trailingslashit( $parsed_url['path'] );
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If no path is supplied and yet the host is present, then the path should assume to be empty. The current REQUEST_URI should not be supplied. I added a failing test case to show the proper behavior: f27f5a2.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 16b7fa8.

@pierlon pierlon requested a review from westonruter February 7, 2020 22:33
if ( '?' === $action_url[0] || '#' === $action_url[0] ) {
// For actions consisting of only a query or URL fragment, include the schemeless-host and the REQUEST URI of the current page.
$action_url = $schemeless_host . wp_unslash( $_SERVER['REQUEST_URI'] ) . $action_url;
} elseif ( '.' === $action_url[0] ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In your changes, you correctly changed the '.' === $action_url[0] to '/' !== $action_url[0]. This I believe fixes the case where there is an action URL like foo/ as opposed to ../. I added a test to confirm this: 585be89.

@westonruter westonruter added this to the v1.4.3 milestone Feb 9, 2020
@westonruter westonruter merged commit 91cea86 into develop Feb 9, 2020
@westonruter westonruter deleted the fix/4208-malformed-action-urls branch February 9, 2020 02:37
westonruter added a commit that referenced this pull request Feb 9, 2020
* Fix malformed conversion of relative action URLs for forms

* Simplify URL reconstruction by eliminating ternary expressions

* Add failing case for pathless URL

* Refactor determining action url to separate function

* Set an empty path if none is defined but there is a host

* Short-circuit when get_action_url has nothing to do

* Add test for dotless relative path action URL

Co-authored-by: Weston Ruter <westonruter@google.com>
westonruter added a commit that referenced this pull request Feb 13, 2020
* tag '1.4.3': (22 commits)
  Update readme and screenshots for Stories removal (#4259)
  Open story export instructions in a new window (#4258)
  Bump version to 1.4.3-RC1
  Hide Stories options and add deprecation notice (#4219)
  Fix malformed conversion of relative action URLs for forms (#4250)
  Limit Stories experience to WP 5.3 & Gutenberg 7.1.0 (#4217)
  Prevent errors in admin bar filters from non-array arguments (#4207)
  Update @wordpress/e2e-test-utils dependency
  Revert update of mustache/mustache dependency
  Update composer.lock
  Update WP CLI to 2.4.0
  For WordPress.tv embed, Use an oembed filter instead of block filter (#4164)
  Update readme to add FAQs section (#4159)
  Apply workaround to fix test__multiple_valid_image_files (#4034)
  Ignore Story editor tests (#4043)
  Update amp-video embed regex pattern to include other Vimeo URL formats (#4051)
  Update amp-instagram embed regex (#4053)
  Update wp-dev-lib package (#4029)
  Fix conversion of forms with relative action URLs (#4003)
  Improve release instructions (#3995)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Signed the Google CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed conversion of forms with relative action URLs

3 participants