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

Do only follow HTTP and HTTPS redirects #11032

Merged
merged 1 commit into from
Sep 23, 2014
Merged

Conversation

LukasReschke
Copy link
Member

We do not want to follow redirects to other protocols since they might allow an adversary to bypass network restrictions. (i.e. a redirect to ftp:// might be used to access files of a FTP server which might be in a secure zone and not be reachable from the net but from the ownCloud server)

LukasReschke added a commit to LukasReschke/sabre-http that referenced this pull request Sep 11, 2014
We do not want to follow redirects to other protocols since they might allow an adversary to bypass network restrictions. (i.e. a redirect to ftp:// might be used to access files of a FTP server which might be in a secure zone and not be reachable from the net but from the ownCloud server)

See owncloud/core#11032 for the change in ownCloud
@LukasReschke
Copy link
Member Author

Upstream change to SabreDAV: sabre-io/http#14

@LukasReschke
Copy link
Member Author

@LukasReschke
Copy link
Member Author

Todo:

  • Check the stream in newfile.php as well

LukasReschke added a commit to LukasReschke/sabre-http that referenced this pull request Sep 11, 2014
We do not want to follow redirects to other protocols since they might allow an adversary to bypass network restrictions. (i.e. a redirect to ftp:// might be used to access files of a FTP server which might be in a secure zone and not be reachable from the net but from the ownCloud server)

See owncloud/core#11032 for the change in ownCloud

Fix unit test

Add workaround for HHVM
'notification' => 'progress'
);
$ctx = stream_context_create(
$contextArray
Copy link

Choose a reason for hiding this comment

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

Are you sure this is correct? The manual says resource stream_context_create ([ array $options [, array $params ]] ) and the previous patch had null as the first parameter.

@LukasReschke LukasReschke force-pushed the harden-redirect branch 3 times, most recently from 92c9d11 to 3fccd44 Compare September 14, 2014 18:27
@ghost
Copy link

ghost commented Sep 15, 2014

🚀 Test Passed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org/job/pull-request-analyser/7346/

@LukasReschke
Copy link
Member Author

@craigpg @MTRichards Upgrading to gold. Critical one - needs a fix.

$ctx = stream_context_create(null, array('notification' =>'progress'));
$contextArray = array(
'http' => array(
'follow_location' => false, // Do not follow the location since we can't limit the protocol
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm worried about lost of functionality here, some websites might redirect their download links to the real file.
Not sure how often people use this feature though, and also how often such redirect cases might happen.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will take a look at how we can use getURLContent here - on it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a workaround around this PHP limitation, please review a870895 - thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

To clarify: Using getURLContent is here not easily possible since we have no hard dependency on cURL and maintaining the callback would be major pain.

@MTRichards
Copy link
Contributor

@karlitschek

@karlitschek
Copy link
Contributor

Yes. Change looks good to me. 👍 Please backport @LukasReschke

@ghost
Copy link

ghost commented Sep 18, 2014

🚀 Test Passed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org/job/pull-request-analyser/7454/

@icewind1991
Copy link
Contributor

Code looks good 👍

if($headerArray !== false && array_key_exists('Location', $headerArray)) {
$locationHeader = $headerArray['Location'];
if(substr($locationHeader, 0, 8) === 'https://' || substr($locationHeader, 0, 7) === 'http://') {
return self::getFinalLocationOfURL($headerArray['Location']);
Copy link

Choose a reason for hiding this comment

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

Hmm. Recursion. I don't like that.

Copy link

Choose a reason for hiding this comment

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

In general, calling a function has a certain overhead and as such recursion should be avoided when possible. Not that it matters here, but there also is a recursion limit of 100 in some environments. This can probably simply be replaced with a do while loop (untested):

$h = array('Location' => $location);
do
{
    $h = get_headers($headers['Location'], 1);
}
while (isset($h['Location']) && (strpos($h['Location'], 'http://') === 0 || strpos($h['Location'], 'https://') === 0));

Note that isset($h['Location']) implies that $h is not false.

$headerArray = $this->getHeaders($location, 1);

if($headerArray !== false && isset($headerArray['Location'])) {
while(true) {
Copy link

Choose a reason for hiding this comment

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

It is easier to understand when you have the break condition in the while loop instead of using an endless loop with break. Hence I made it a do-while loop in my version.

@bantu
Copy link

bantu commented Sep 22, 2014

Looks pretty much acceptable to me.

@LukasReschke
Copy link
Member Author

Thanks @bantu - I'll address your other comments later.

@ghost
Copy link

ghost commented Sep 22, 2014

🚀 Test PASSed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org//job/pull-request-analyser/7509/

@ghost
Copy link

ghost commented Sep 22, 2014

🚀 Test PASSed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org//job/pull-request-analyser/7510/

@LukasReschke LukasReschke force-pushed the harden-redirect branch 2 times, most recently from 26c5edf to ed2fd68 Compare September 22, 2014 17:44
@LukasReschke
Copy link
Member Author

Did the adjustments that I felt were reasonable.

We do not want to follow redirects to other protocols since they might allow an adversary to bypass network restrictions. (i.e. a redirect to ftp:// might be used to access files of a FTP server which might be in a secure zone and not be reachable from the net but from the ownCloud server)

Get final redirect manually using get_headers()

Migrate to HTTPHelper class and add unit tests
@bantu
Copy link

bantu commented Sep 22, 2014

👍

@ghost
Copy link

ghost commented Sep 23, 2014

🚀 Test PASSed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org//job/pull-request-analyser/7530/

@ghost
Copy link

ghost commented Sep 23, 2014

🚀 Test PASSed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org//job/pull-request-analyser/7532/

@ghost
Copy link

ghost commented Sep 23, 2014

🚀 Test PASSed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org//job/pull-request-analyser/7533/

LukasReschke added a commit that referenced this pull request Sep 23, 2014
Do only follow HTTP and HTTPS redirects
@LukasReschke LukasReschke merged commit 5d977f9 into master Sep 23, 2014
@LukasReschke LukasReschke deleted the harden-redirect branch September 23, 2014 09:34
@LukasReschke
Copy link
Member Author

Stable7: cb3bc5a

Stable6 and stable5 PR is upcoming...

@LukasReschke
Copy link
Member Author

Stable6: #11248

LukasReschke added a commit that referenced this pull request Sep 23, 2014
@LukasReschke
Copy link
Member Author

And the crippled stable5 one: #11249

@scrutinizer-notifier
Copy link

The inspection completed: No issues found

@lock lock bot locked as resolved and limited conversation to collaborators Aug 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants