From e206929e17a1bf5c86fa52d137fc191dea9483be Mon Sep 17 00:00:00 2001 From: Kyle Hickinson Date: Thu, 17 Aug 2023 18:56:23 -0400 Subject: [PATCH] Fix brave/brave-ios#7873: Skip old reddit redirect when loading new reddit media page (brave/brave-ios#7895) --- Sources/Brave/WebFilters/WebsiteRedirects.swift | 9 +++++++++ .../ClientTests/Web Filters/WebsiteRedirectsTests.swift | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/Sources/Brave/WebFilters/WebsiteRedirects.swift b/Sources/Brave/WebFilters/WebsiteRedirects.swift index 260dc0484a0d..858506b3312a 100644 --- a/Sources/Brave/WebFilters/WebsiteRedirects.swift +++ b/Sources/Brave/WebFilters/WebsiteRedirects.swift @@ -13,6 +13,10 @@ struct WebsiteRedirects { let hostToRedirectTo: String /// What hosts should be redirected. Due to compat reasons not every host may be easily replaced. let eligibleHosts: Set + /// Any additional predicates on whether or not the URL should redirect (requires all to be satified) + /// + /// It can be assumed that any URL passed into these predicates will be a eligible host + var additionalPredicates: [(URL) -> Bool] = [] /// What hosts should not be redirected. It's either due to web compat reasons or to let user explicitely type a url to not override it. /// Reddit is good example, regular reddit.com and new.reddit.com point to the same new user interface. /// So we redirect all regular reddit.com link, but the user may explicitely go to new.reddit.com without having to disable the reddit redirect toggle. @@ -22,6 +26,10 @@ struct WebsiteRedirects { private static let reddit = Rule( hostToRedirectTo: "old.reddit.com", eligibleHosts: ["reddit.com", "www.reddit.com", "np.reddit.com", "amp.reddit.com", "i.reddit.com"], + additionalPredicates: [ { url in + !url.path.hasPrefix("/media") + } + ], excludedHosts: ["new.reddit.com"]) private static let npr = Rule( @@ -53,6 +61,7 @@ struct WebsiteRedirects { .first(where: { $0.eligibleHosts.contains(host) }) guard let redirect = foundMatch, + redirect.additionalPredicates.allSatisfy({ $0(url) }), var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return nil } // For privacy reasons we do not redirect websites if username or password are present. diff --git a/Tests/ClientTests/Web Filters/WebsiteRedirectsTests.swift b/Tests/ClientTests/Web Filters/WebsiteRedirectsTests.swift index 5da48caba2ba..8478c4523db7 100644 --- a/Tests/ClientTests/Web Filters/WebsiteRedirectsTests.swift +++ b/Tests/ClientTests/Web Filters/WebsiteRedirectsTests.swift @@ -74,6 +74,11 @@ class WebsiteRedirectsTests: XCTestCase { XCTAssertEqual(WebsiteRedirects.redirect( for: try url("https://www.reddit.com/r/brave")), try url("https://old.reddit.com/r/brave")) + + // Skip media redirect + + XCTAssertNil(WebsiteRedirects.redirect( + for: try url("https://reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fimageid.jpg"))) } func testNpr() throws {