Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 950d3a5

Browse files
theScrabiabelgardep
authored andcommitted
fix wrong handling of redirect to unsecure connection
1 parent 8eaa98a commit 950d3a5

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.owncloud.android.lib.common.OwnCloudClient
2828
import com.owncloud.android.lib.common.http.HttpConstants
2929
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
3030
import com.owncloud.android.lib.common.operations.RemoteOperationResult
31+
3132
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
3233
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME
3334
import org.json.JSONObject
@@ -36,14 +37,20 @@ import java.util.concurrent.TimeUnit
3637

3738
internal class StatusRequester {
3839

39-
private fun checkIfConnectionIsRedirectedToNoneSecure(
40-
isConnectionSecure: Boolean,
40+
/**
41+
* This function is ment to detect if a redirect from a secure to an unsecure connection
42+
* was made. If only connections from unsecure connections to unsecure connections were made
43+
* this function should not return true, because if the whole redirect chain was unsecure
44+
* we assume it was a debug setup.
45+
*/
46+
fun isRedirectedToNonSecureConnection(
47+
redirectedToUnsecureLocationBefore: Boolean,
4148
baseUrl: String,
4249
redirectedUrl: String
43-
): Boolean {
44-
return isConnectionSecure ||
45-
(baseUrl.startsWith(HTTPS_SCHEME) && redirectedUrl.startsWith(HTTP_SCHEME))
46-
}
50+
) = redirectedToUnsecureLocationBefore
51+
|| (baseUrl.startsWith(HTTPS_SCHEME)
52+
&& (!redirectedUrl.startsWith(HTTPS_SCHEME))
53+
&& redirectedUrl.startsWith(HTTP_SCHEME))
4754

4855
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
4956
if (!redirectedLocation.startsWith("/"))
@@ -84,7 +91,7 @@ internal class StatusRequester {
8491
} else {
8592
val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
8693
redirectedToUnsecureLocation =
87-
checkIfConnectionIsRedirectedToNoneSecure(
94+
isRedirectedToNonSecureConnection(
8895
redirectedToUnsecureLocation,
8996
currentLocation,
9097
nextLocation

owncloudComLibrary/src/test/java/com/owncloud/android/lib/StatusRequestorTest.kt

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,68 @@ package com.owncloud.android.lib
2626

2727
import com.owncloud.android.lib.resources.status.StatusRequester
2828
import org.junit.Assert.assertEquals
29+
import org.junit.Assert.assertFalse
30+
import org.junit.Assert.assertTrue
2931
import org.junit.Test
3032

3133
class StatusRequestorTest {
32-
private val requestor = StatusRequester()
34+
private val requester = StatusRequester()
3335

3436
@Test
3537
fun `update location - ok - absolute path`() {
36-
val newLocation = requestor.updateLocationWithRedirectPath(TEST_DOMAIN, "$TEST_DOMAIN$SUB_PATH")
38+
val newLocation = requester.updateLocationWithRedirectPath(TEST_DOMAIN, "$TEST_DOMAIN$SUB_PATH")
3739
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
3840
}
3941

4042
@Test
4143
fun `update location - ok - smaller absolute path`() {
42-
val newLocation = requestor.updateLocationWithRedirectPath("$TEST_DOMAIN$SUB_PATH", TEST_DOMAIN)
44+
val newLocation = requester.updateLocationWithRedirectPath("$TEST_DOMAIN$SUB_PATH", TEST_DOMAIN)
4345
assertEquals(TEST_DOMAIN, newLocation)
4446
}
4547

4648
@Test
4749
fun `update location - ok - relative path`() {
48-
val newLocation = requestor.updateLocationWithRedirectPath(TEST_DOMAIN, SUB_PATH)
50+
val newLocation = requester.updateLocationWithRedirectPath(TEST_DOMAIN, SUB_PATH)
4951
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
5052
}
5153

5254
@Test
5355
fun `update location - ok - replace relative path`() {
54-
val newLocation = requestor.updateLocationWithRedirectPath(
56+
val newLocation = requester.updateLocationWithRedirectPath(
5557
"$TEST_DOMAIN/some/other/subdir", SUB_PATH
5658
)
5759
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
5860
}
5961

62+
@Test
63+
fun `check redirect to unsecure connection - ok - redirect to http`() {
64+
assertTrue(requester.isRedirectedToNonSecureConnection(
65+
false, SECURE_DOMAIN, UNSECURE_DOMAIN))
66+
}
67+
68+
@Test
69+
fun `check redirect to unsecure connection - ko - redirect to https from http`() {
70+
assertFalse(requester.isRedirectedToNonSecureConnection(
71+
false, UNSECURE_DOMAIN, SECURE_DOMAIN))
72+
}
73+
74+
@Test
75+
fun `check redirect to unsecure connection - ko - from https to https`() {
76+
assertFalse(requester.isRedirectedToNonSecureConnection(
77+
false, SECURE_DOMAIN, SECURE_DOMAIN))
78+
}
79+
80+
@Test
81+
fun `check redirect to unsecure connection - ok - from https to https with previous http`() {
82+
assertTrue(requester.isRedirectedToNonSecureConnection(
83+
true, SECURE_DOMAIN, SECURE_DOMAIN))
84+
}
85+
6086
companion object {
6187
const val TEST_DOMAIN = "https://cloud.somewhere.com"
6288
const val SUB_PATH = "/subdir"
89+
90+
const val SECURE_DOMAIN = "https://cloud.somewhere.com"
91+
const val UNSECURE_DOMAIN = "http://somewhereelse.org"
6392
}
6493
}

0 commit comments

Comments
 (0)