From 10f79e13070b6e858b92a0bcfd55b32240e13197 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Dec 2020 16:32:03 +0100 Subject: [PATCH] Remove pbj=1 parameter from YouYube urls in recaptcha activity --- .../org/schabi/newpipe/ReCaptchaActivity.java | 17 +++++++----- .../schabi/newpipe/ReCaptchaActivityTest.kt | 27 +++++++++++++++++++ .../ktx/OffsetDateTimeToCalendarTest.kt | 3 +-- 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 app/src/test/java/org/schabi/newpipe/ReCaptchaActivityTest.kt diff --git a/app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java b/app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java index c962ed99d..03c9ae4af 100644 --- a/app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java +++ b/app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java @@ -53,6 +53,16 @@ public class ReCaptchaActivity extends AppCompatActivity { public static final String YT_URL = "https://www.youtube.com"; public static final String RECAPTCHA_COOKIES_KEY = "recaptcha_cookies"; + public static String sanitizeRecaptchaUrl(@Nullable final String url) { + if (url == null || url.trim().isEmpty()) { + return YT_URL; // YouTube is the most likely service to have thrown a recaptcha + } else { + // remove "pbj=1" parameter from YouYube urls, as it makes the page JSON and not HTML + return url.replace("&pbj=1", "").replace("pbj=1&", "").replace("?pbj=1", ""); + } + } + + private WebView webView; private String foundCookies = ""; @@ -64,15 +74,10 @@ public class ReCaptchaActivity extends AppCompatActivity { final Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - String url = getIntent().getStringExtra(RECAPTCHA_URL_EXTRA); - if (url == null || url.isEmpty()) { - url = YT_URL; - } - + final String url = sanitizeRecaptchaUrl(getIntent().getStringExtra(RECAPTCHA_URL_EXTRA)); // set return to Cancel by default setResult(RESULT_CANCELED); - webView = findViewById(R.id.reCaptchaWebView); // enable Javascript diff --git a/app/src/test/java/org/schabi/newpipe/ReCaptchaActivityTest.kt b/app/src/test/java/org/schabi/newpipe/ReCaptchaActivityTest.kt new file mode 100644 index 000000000..b9f6887f9 --- /dev/null +++ b/app/src/test/java/org/schabi/newpipe/ReCaptchaActivityTest.kt @@ -0,0 +1,27 @@ +package org.schabi.newpipe + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.schabi.newpipe.ReCaptchaActivity.YT_URL + +class ReCaptchaActivityTest { + private fun assertSanitized(expected: String, actual: String?) { + assertEquals(expected, ReCaptchaActivity.sanitizeRecaptchaUrl(actual)) + } + + @Test fun `null, empty or blank url is sanitized correctly`() { + assertSanitized(YT_URL, null) + assertSanitized(YT_URL, "") + assertSanitized(YT_URL, " \n \t ") + } + + @Test fun `YouTube url containing pbj=1 is sanitized correctly`() { + val sanitizedUrl = "https://m.youtube.com/results?search_query=test" + assertSanitized(sanitizedUrl, "https://m.youtube.com/results?search_query=test") + assertSanitized(sanitizedUrl, "https://m.youtube.com/results?search_query=test&pbj=1&pbj=1") + assertSanitized(sanitizedUrl, "https://m.youtube.com/results?pbj=1&search_query=test") + assertSanitized("pbj://pbj.pbj.pbj/pbj", "pbj://pbj.pbj.pbj/pbj?pbj=1") + assertSanitized("http://www.host.com/b?p1=7&p2=9", "http://www.host.com/b?p1=7&pbj=1&p2=9") + assertSanitized("http://www.host.com/a?pbj=0", "http://www.host.com/a?pbj=0") + } +} diff --git a/app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt b/app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt index c93d36eb5..74da07998 100644 --- a/app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt +++ b/app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt @@ -4,7 +4,6 @@ import org.junit.Assert.assertEquals import org.junit.Test import java.time.LocalDate import java.time.OffsetDateTime -import java.time.ZoneId import java.time.ZoneOffset import java.util.Calendar import java.util.TimeZone @@ -13,7 +12,7 @@ class OffsetDateTimeToCalendarTest { @Test fun testRelativeTimeWithCurrentOffsetDateTime() { val calendar = LocalDate.of(2020, 1, 1).atStartOfDay().atOffset(ZoneOffset.UTC) - .toCalendar() + .toCalendar() assertEquals(2020, calendar[Calendar.YEAR]) assertEquals(0, calendar[Calendar.MONTH])