Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #7873: Skip old reddit redirect when loading new reddit media page #7895

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/Brave/WebFilters/WebsiteRedirects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
/// 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.
Expand All @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions Tests/ClientTests/Web Filters/WebsiteRedirectsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down