Skip to content

redirection to different domain must not keep previous port #221

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

Merged
merged 2 commits into from
Sep 29, 2022

Conversation

dbu
Copy link
Contributor

@dbu dbu commented Sep 27, 2022

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets fixes #219
Documentation -
License MIT

What's in this PR?

Correctly handle non-absolute URLs in the redirect

Why?

Conform with RFC 7231 Location header specification

Checklist

  • Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix

@dbu dbu force-pushed the fix-redirect-host branch from c00ff8e to ee39c1d Compare September 27, 2022 11:33
/**
* @testWith ["https://example.com/path?query=value", "https://example.com?query=value", "https://example.com?query=value"]
* ["https://example.com/path?query=value", "https://example.com/?query=value", "https://example.com/?query=value"]
* ["https://example.com", "https://example.com?query=value", "https://example.com?query=value"]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to provider method to have descriptions of the cases

@dbu dbu force-pushed the fix-redirect-host branch from ee39c1d to cf26336 Compare September 27, 2022 11:49
@dbu
Copy link
Contributor Author

dbu commented Sep 27, 2022

@ostrolucky can you review this one please? trying to get all of the redirection right. i discovered that besides the domain/scheme/port topic, RFC 7231 also allows relative paths with all of the path semantics...

@dbu dbu force-pushed the fix-redirect-host branch from cf26336 to baacbe9 Compare September 27, 2022 11:51
// If relative, we need to check if we have an absolute path or not

$path = array_key_exists('path', $parsedLocation) ? $parsedLocation['path'] : '';
if (!array_key_exists('host', $parsedLocation) && '/' !== $location[0]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what if $location has 0 length?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops. i assumed that parse_url('') would fail, but actually it gives an array with path => ''.

i add a check to fail if location is empty, as that would make no sense.


$path = array_key_exists('path', $parsedLocation) ? $parsedLocation['path'] : '';
if (!array_key_exists('host', $parsedLocation) && '/' !== $location[0]) {
// this is a relative-path reference
Copy link
Contributor

Choose a reason for hiding this comment

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

what is a relative path reference? this is unclear 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.

clarifying

}
/* replace '/./' or '/foo/../' with '/' */
$re = ['#(/\./)#', '#/(?!\.\.)[^/]+/\.\./#'];
for( $n=1; $n>0; $path=preg_replace( $re, '/', $path, -1, $n ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you have som weird code style here. Need to be a bit more careful because this project doesn't have cs check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh indeed. copy-pasted from stackoverflow 🙈
i tried to make it a bit better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also adding a cs-fixer now

@dbu dbu force-pushed the fix-redirect-host branch 3 times, most recently from 666fceb to 1c6a640 Compare September 28, 2022 15:24
@dbu dbu force-pushed the fix-redirect-host branch from 1c6a640 to 509e513 Compare September 28, 2022 15:33
@dbu
Copy link
Contributor Author

dbu commented Sep 28, 2022

thanks for the feedback. i updated all points you discovered

@dbu dbu merged commit 4a962c6 into master Sep 29, 2022
@dbu dbu deleted the fix-redirect-host branch September 29, 2022 06:17
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.

RedirectPlugin port handling
2 participants