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 conversion of forms with relative action URLs #4003

Merged
merged 2 commits into from
Dec 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions includes/sanitizers/class-amp-form-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,21 @@ public function sanitize() {
$action_url = esc_url_raw( '//' . $_SERVER['HTTP_HOST'] . wp_unslash( $_SERVER['REQUEST_URI'] ) );
} else {
$action_url = $node->getAttribute( 'action' );
// Check if action_url is a relative path and add the host to it.

// Handle relative URLs.
if ( ! preg_match( '#^(https?:)?//#', $action_url ) ) {
$action_url = esc_url_raw( '//' . $_SERVER['HTTP_HOST'] . $action_url );
$schemeless_host = '//' . $_SERVER['HTTP_HOST'];
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 = '//' . $_SERVER['HTTP_HOST'] . wp_unslash( $_SERVER['REQUEST_URI'] ) . $action_url;
westonruter marked this conversation as resolved.
Show resolved Hide resolved
} elseif ( '.' === $action_url[0] ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: the '.' check was not correct here. See #4250 (review)

// For actions consisting of relative paths (e.g. '../'), prepend the schemeless-host and a trailing-slashed REQUEST URI.
$action_url = '//' . $_SERVER['HTTP_HOST'] . trailingslashit( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . $action_url;
westonruter marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the trailing slash should always be added. Taking the action .foo for example, depending on the server configuration .foo could be a file while .foo/ could be a directory.

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, but the trailing slash is being added after the path in order to append the relative segment. For example, if the user is currently on /about/team and there is a form that has an action of ../ the resulting action URL path needs to be /about/team/../ not /about/team../. Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, thanks for the clarification.

} else {
// Otherwise, when the action URL includes an absolute path, just append it to the schemeless-host.
$action_url = $schemeless_host . $action_url;
}
$action_url = esc_url_raw( $action_url );
}
}
$xhr_action = $node->getAttribute( 'action-xhr' );
Expand Down
13 changes: 12 additions & 1 deletion tests/php/test-amp-form-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ public function get_data() {
'<form method="post" action="/login/"></form>',
'#' . preg_quote( '<form method="post" action-xhr="//example.org/login/?_wp_amp_action_xhr_converted=1" target="_top">', '#' ) . $form_template_pattern . '</form>#s',
],

'form_with_relative_path_action_url' => [
'<form method="post" action="../"></form>',
'#' . preg_quote( '<form method="post" action-xhr="//example.org/current-page/../?_wp_amp_action_xhr_converted=1" target="_top">', '#' ) . $form_template_pattern . '</form>#s',
],
'form_with_relative_query_action_url' => [
'<form method="post" action="?foo=bar"></form>',
'#' . preg_quote( '<form method="post" action-xhr="//example.org/current-page/?foo=bar&amp;_wp_amp_action_xhr_converted=1" target="_top">', '#' ) . $form_template_pattern . '</form>#s',
],
'form_with_relative_fragment_action_url' => [
'<form method="post" action="#foo"></form>',
'#' . preg_quote( '<form method="post" action-xhr="//example.org/current-page/?_wp_amp_action_xhr_converted=1#foo" target="_top">', '#' ) . $form_template_pattern . '</form>#s',
],
'test_with_dev_mode' => [
'<form data-ampdevmode="" action="javascript:"></form>',
null, // No change.
Expand Down