Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix decoding internal blocked page #22138

Merged
merged 1 commit into from
Mar 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ extension BrowserViewController {
@objc private func addToFavouritesCommand() {
guard let selectedTab = tabManager.selectedTab,
let selectedUrl = selectedTab.url,
!(selectedUrl.isLocal || selectedUrl.isReaderModeURL)
!(selectedUrl.isLocal || selectedUrl.isInternalURL(for: .readermode))
else {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import BraveShared
import Shared
import Storage
import WebKit
Expand Down Expand Up @@ -149,9 +150,7 @@ extension BrowserViewController {
let forwardList = webView.backForwardList.forwardList

guard let currentURL = webView.backForwardList.currentItem?.url,
let readerModeURL = currentURL.encodeReaderModeURL(
"\(InternalURL.baseUrl)/\(InternalURL.Path.readermode.rawValue)"
)
let readerModeURL = currentURL.encodeEmbeddedInternalURL(for: .readermode)
else { return }

recordTimeBasedNumberReaderModeUsedP3A(activated: true)
Expand Down Expand Up @@ -194,7 +193,7 @@ extension BrowserViewController {
let forwardList = webView.backForwardList.forwardList

if let currentURL = webView.backForwardList.currentItem?.url {
if let originalURL = currentURL.decodeReaderModeURL {
if let originalURL = currentURL.decodeEmbeddedInternalURL(for: .readermode) {
if backList.count > 1 && backList.last?.url == originalURL {
let playlistItem = tab.playlistItem
webView.go(to: backList.last!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension BrowserViewController {
var activities = [UIActivity]()

// Copy Clean URL Activity
if !url.isLocal, !InternalURL.isValid(url: url), !url.isReaderModeURL {
if !url.isLocal, !InternalURL.isValid(url: url), !url.isInternalURL(for: .readermode) {
let cleanedURL =
URLSanitizerServiceFactory.get(
privateMode: tab?.isPrivate ?? true
Expand All @@ -42,7 +42,7 @@ extension BrowserViewController {

// Send Tab To Self Activity - Show device selection screen
if braveCore.syncAPI.isSendTabToSelfVisible, !privateBrowsingManager.isPrivateBrowsing,
!url.isLocal, !InternalURL.isValid(url: url), !url.isReaderModeURL
!url.isLocal, !InternalURL.isValid(url: url), !url.isInternalURL(for: .readermode)
{
activities.append(
BasicMenuActivity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ extension BrowserViewController: TopToolbarDelegate {
}

func topToolbarDidLongPressReloadButton(_ topToolbar: TopToolbarView, from button: UIButton) {
guard let tab = tabManager.selectedTab, let url = tab.url, !url.isLocal, !url.isReaderModeURL
guard let tab = tabManager.selectedTab, let url = tab.url, !url.isLocal,
!url.isInternalURL(for: .readermode)
else { return }

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
Expand Down Expand Up @@ -841,12 +842,12 @@ extension BrowserViewController: TopToolbarDelegate {
func openAddBookmark() {
guard let selectedTab = tabManager.selectedTab,
let selectedUrl = selectedTab.url,
!(selectedUrl.isLocal || selectedUrl.isReaderModeURL)
!(selectedUrl.isLocal || selectedUrl.isInternalURL(for: .readermode))
else {
return
}

let bookmarkUrl = selectedUrl.decodeReaderModeURL ?? selectedUrl
let bookmarkUrl = selectedUrl.decodeEmbeddedInternalURL(for: .readermode) ?? selectedUrl

let mode = BookmarkEditMode.addBookmark(
title: selectedTab.displayTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ extension BrowserViewController: WKNavigationDelegate {
// are going to a about:reader page. Then we keep it on screen: it will change status
// (orange color) as soon as the page has loaded.
if let url = webView.url {
if !url.isReaderModeURL {
if !url.isInternalURL(for: .readermode) {
topToolbar.updateReaderModeState(ReaderModeState.unavailable)
hideReaderModeBar(animated: false)
}
Expand Down Expand Up @@ -147,7 +147,6 @@ extension BrowserViewController: WKNavigationDelegate {
decidePolicyFor navigationAction: WKNavigationAction,
preferences: WKWebpagePreferences
) async -> (WKNavigationActionPolicy, WKWebpagePreferences) {

guard var requestURL = navigationAction.request.url else {
return (.cancel, preferences)
}
Expand Down Expand Up @@ -432,16 +431,10 @@ extension BrowserViewController: WKNavigationDelegate {
isAggressiveMode: domain.blockAdsAndTrackingLevel.isAggressive
)

if shouldBlock, let escapingURL = requestURL.absoluteString.escape() {
var components = URLComponents(string: InternalURL.baseUrl)
components?.path = "/\(InternalURL.Path.blocked.rawValue)"
components?.queryItems = [URLQueryItem(name: "url", value: escapingURL)]

if let url = components?.url {
let request = PrivilegedRequest(url: url) as URLRequest
tab?.loadRequest(request)
return (.cancel, preferences)
}
if shouldBlock, let url = requestURL.encodeEmbeddedInternalURL(for: .blocked) {
let request = PrivilegedRequest(url: url) as URLRequest
tab?.loadRequest(request)
return (.cancel, preferences)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ public class BrowserViewController: UIViewController {
} else if !url.absoluteString.hasPrefix(
"\(InternalURL.baseUrl)/\(SessionRestoreHandler.path)"
) {
hideActiveNewTabPageController(url.isReaderModeURL)
hideActiveNewTabPageController(url.isInternalURL(for: .readermode))
}
} else if isAboutHomeURL {
showNewTabPageController()
Expand Down Expand Up @@ -2010,7 +2010,7 @@ public class BrowserViewController: UIViewController {
if tab.secureContentState == .secure, !webView.hasOnlySecureContent,
tab.url?.origin == tab.webView?.url?.origin
{
if let url = tab.webView?.url, url.isReaderModeURL {
if let url = tab.webView?.url, url.isInternalURL(for: .readermode) {
break
}

Expand Down Expand Up @@ -2054,7 +2054,7 @@ public class BrowserViewController: UIViewController {
details: "Certificate is missing (no serverTrust)"
)
}
} else if url.isReaderModeURL || InternalURL.isValid(url: url) {
} else if url.isInternalURL(for: .readermode) || InternalURL.isValid(url: url) {
tab.secureContentState = .localhost
logSecureContentState(tab: tab, path: path, details: "Reader Mode or Internal URL")
}
Expand Down Expand Up @@ -2135,7 +2135,7 @@ public class BrowserViewController: UIViewController {
break
}

if url.isReaderModeURL || InternalURL.isValid(url: url) {
if url.isInternalURL(for: .readermode) || InternalURL.isValid(url: url) {
tab.secureContentState = .localhost
logSecureContentState(tab: tab, path: path, details: "Reader Mode or Internal URL")

Expand Down Expand Up @@ -2243,7 +2243,7 @@ public class BrowserViewController: UIViewController {

func updateForwardStatusIfNeeded(webView: WKWebView) {
if let forwardListItem = webView.backForwardList.forwardList.first,
forwardListItem.url.isReaderModeURL
forwardListItem.url.isInternalURL(for: .readermode)
{
navigationToolbar.updateForwardStatus(false)
} else {
Expand All @@ -2256,7 +2256,7 @@ public class BrowserViewController: UIViewController {
toolbarVisibilityViewModel.toolbarState = .expanded

if let url = tab.url {
if url.isReaderModeURL {
if url.isInternalURL(for: .readermode) {
showReaderModeBar(animated: false)
NotificationCenter.default.addObserver(
self,
Expand Down Expand Up @@ -2562,7 +2562,7 @@ public class BrowserViewController: UIViewController {
// Whether to show search icon or + icon
toolbar?.setSearchButtonState(url: url)

if !InternalURL.isValid(url: url) || url.isReaderModeURL, !url.isFileURL {
if !InternalURL.isValid(url: url) || url.isInternalURL(for: .readermode), !url.isFileURL {
// Fire the readability check. This is here and not in the pageShow event handler in ReaderMode.js anymore
// because that event will not always fire due to unreliable page caching. This will either let us know that
// the currently loaded page can be turned into reading mode or if the page already is in reading mode. We
Expand All @@ -2573,7 +2573,7 @@ public class BrowserViewController: UIViewController {
)

// Only add history of a url which is not a localhost url
if !url.isReaderModeURL {
if !url.isInternalURL(for: .readermode) {
if !tab.isPrivate {
braveCore.historyAPI.add(url: url, title: tab.title, dateAdded: Date())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import BraveShared
import BraveVPN
import Foundation
import Shared
Expand Down Expand Up @@ -110,7 +111,7 @@ class BrowserNavigationHelper {
} else {
share(url: url)
}
} else if url.isReaderModeURL, let readerSourceURL = url.decodeReaderModeURL {
} else if let readerSourceURL = url.self.decodeEmbeddedInternalURL(for: .readermode) {
// We want to decode the underlying url that generated the reader mode file and share that instead
// This way we avoid sharing a url of a local file
share(url: readerSourceURL)
Expand Down
2 changes: 1 addition & 1 deletion ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class Tab: NSObject {

// Setting URL in SyncTab is adding pending item to navigation manager on brave-core side
if let url = url, !isPrivate, !url.isLocal, !InternalURL.isValid(url: url),
!url.isReaderModeURL
!url.isInternalURL(for: .readermode)
{
syncTab?.setURL(url)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class TabManager: NSObject {

for tab in regularTabs {
if let url = tab.fetchedURL, !tab.type.isPrivate, !url.isLocal,
!InternalURL.isValid(url: url), !url.isReaderModeURL
!InternalURL.isValid(url: url), !url.isInternalURL(for: .readermode)
{
tab.addTabInfoToSyncedSessions(url: url, displayTitle: tab.displayTitle)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ShieldsViewController: UIViewController, PopoverContentComponent {
private lazy var url: URL? = {
guard let _url = tab.url else { return nil }

if let tabURL = _url.stippedInternalURL {
if let tabURL = _url.strippedInternalURL {
return tabURL
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BlockedDomainScriptHandler: TabContentScript {
}

private func blockedDomainDidProceed() {
guard let url = tab?.url?.stippedInternalURL, let etldP1 = url.baseDomain else {
guard let url = tab?.url?.strippedInternalURL, let etldP1 = url.baseDomain else {
assertionFailure(
"There should be no way this method can be triggered if the tab is not on an internal url"
)
Expand All @@ -63,7 +63,7 @@ class BlockedDomainScriptHandler: TabContentScript {
}

private func blockedDomainDidGoBack() {
guard let url = tab?.url?.stippedInternalURL else {
guard let url = tab?.url?.strippedInternalURL else {
assertionFailure(
"There should be no way this method can be triggered if the tab is not on an internal url"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,96 @@ extension URL {
/// Obtains a clean stripped url from the current Internal URL
///
/// Returns the original url without internal parameters
public var stippedInternalURL: URL? {
public var strippedInternalURL: URL? {
if InternalURL.isValid(url: self),
let internalURL = InternalURL(self)
{

switch internalURL.urlType {
case .errorPage:
return internalURL.originalURLFromErrorPage
case .web3Page, .sessionRestorePage, .readerModePage, .aboutHomePage, .blockedPage:
case .web3Page, .sessionRestorePage, .aboutHomePage:
return internalURL.extractedUrlParam
case .blockedPage:
return decodeEmbeddedInternalURL(for: .blocked)
case .readerModePage:
return decodeEmbeddedInternalURL(for: .readermode)
default:
return nil
}
}

return nil
}

/// URL returned for display purposes
public var displayURL: URL? {
if self.absoluteString.starts(with: "blob:") {
return self.havingRemovedAuthorisationComponents()
}

if self.isFileURL {
return URL(string: "file://\(self.lastPathComponent)")
}

if self.isInternalURL(for: .readermode) {
return self.decodeEmbeddedInternalURL(for: .readermode)?
.havingRemovedAuthorisationComponents()
}

if let internalUrl = InternalURL(self), internalUrl.isErrorPage {
return internalUrl.originalURLFromErrorPage?.displayURL
}

if let internalUrl = InternalURL(self),
internalUrl.isSessionRestore || internalUrl.isWeb3URL || internalUrl.isBlockedPage
{
return internalUrl.extractedUrlParam?.displayURL
}

if !InternalURL.isValid(url: self) {
let url = self.havingRemovedAuthorisationComponents()
if let internalUrl = InternalURL(url), internalUrl.isErrorPage {
return internalUrl.originalURLFromErrorPage?.displayURL
}
return url
}

return nil
}

/// Returns true if this is an embedded url for the given `InternalURL.Path`
public func isInternalURL(for internalPath: InternalURL.Path) -> Bool {
let scheme = self.scheme
let host = self.host
let path = self.path
return scheme == InternalURL.scheme && host == InternalURL.host
&& path == "/\(internalPath.rawValue)"
}

/// Extract an embedded url given by the `url` query param from an interna urll (i.e. `internal://local`)
public func decodeEmbeddedInternalURL(for internalPath: InternalURL.Path) -> URL? {
guard self.isInternalURL(for: internalPath) else { return nil }
let components = URLComponents(url: self, resolvingAgainstBaseURL: false)
let queryItem = components?.queryItems?.first(where: {
$0.name == InternalURL.Param.url.rawValue
})
guard let value = queryItem?.value else { return nil }
return URL(string: value)
}

/// Embed a url into an internal URL for the given path. The url will be placed in a `url` querey param
public func encodeEmbeddedInternalURL(for path: InternalURL.Path) -> URL? {
let baseURL = "\(InternalURL.baseUrl)/\(path.rawValue)"

guard
let encodedURL = absoluteString.addingPercentEncoding(withAllowedCharacters: .alphanumerics)
else {
return nil
}

return URL(string: "\(baseURL)?\(InternalURL.Param.url.rawValue)=\(encodedURL)")
}
}

extension InternalURL {
Expand Down
Loading
Loading