Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Website breakage triage tools upgraded for efficient mitigation #1787

Merged
merged 8 commits into from
Nov 8, 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
28 changes: 27 additions & 1 deletion DuckDuckGo/Application/AppConfigurationURLProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,40 @@ import Configuration

struct AppConfigurationURLProvider: ConfigurationURLProviding {

// MARK: - Debug

internal init(customPrivacyConfiguration: URL? = nil) {
if let customPrivacyConfiguration {
// Overwrite custom privacy configuration if provided
self.customPrivacyConfiguration = customPrivacyConfiguration.absoluteString
}
// Otherwise use the default or already stored custom configuration
}

@UserDefaultsWrapper(key: .customConfigurationUrl, defaultValue: nil)
private var customPrivacyConfiguration: String?

private var customPrivacyConfigurationUrl: URL? {
if let customPrivacyConfiguration {
return URL(string: customPrivacyConfiguration)
}
return nil
}

mutating func resetToDefaultConfigurationUrl() {
self.customPrivacyConfiguration = nil
}

// MARK: - Main

func url(for configuration: Configuration) -> URL {
// URLs for privacyConfiguration and trackerDataSet shall match the ones in update_embedded.sh.
// Danger checks that the URLs match on every PR. If the code changes, the regex that Danger uses may need an update.
switch configuration {
case .bloomFilterBinary: return URL(string: "https://staticcdn.duckduckgo.com/https/https-mobile-v2-bloom.bin")!
case .bloomFilterSpec: return URL(string: "https://staticcdn.duckduckgo.com/https/https-mobile-v2-bloom-spec.json")!
case .bloomFilterExcludedDomains: return URL(string: "https://staticcdn.duckduckgo.com/https/https-mobile-v2-false-positives.json")!
case .privacyConfiguration: return URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/config/v4/macos-config.json")!
case .privacyConfiguration: return customPrivacyConfigurationUrl ?? URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/config/v4/macos-config.json")!
case .surrogates: return URL(string: "https://staticcdn.duckduckgo.com/surrogates.txt")!
case .trackerDataSet: return URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/v5/current/macos-tds.json")!
// In archived repo, to be refactored shortly (https://staticcdn.duckduckgo.com/useragents/social_ctp_configuration.json)
Expand Down
15 changes: 15 additions & 0 deletions DuckDuckGo/Common/Extensions/NSAlertExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ extension NSAlert {
return alert
}

static func customConfigurationAlert(configurationUrl: String) -> NSAlert {
let alert = NSAlert()
alert.messageText = "Set custom configuration URL:"
alert.addButton(withTitle: UserText.ok)
alert.addButton(withTitle: UserText.cancel)
let textField = NSTextField(frame: NSRect(x: 0, y: 0, width: 300, height: 24))
textField.maximumNumberOfLines = 1
textField.lineBreakMode = .byTruncatingTail
textField.stringValue = configurationUrl
alert.accessoryView = textField
alert.window.initialFirstResponder = alert.accessoryView
textField.currentEditor()?.selectAll(nil)
return alert
}

@discardableResult
func runModal() async -> NSApplication.ModalResponse {
await withCheckedContinuation { continuation in
Expand Down
1 change: 1 addition & 0 deletions DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public struct UserDefaultsWrapper<T> {
case loggingCategories = "logging.categories"

case firstLaunchDate = "first.app.launch.date"
case customConfigurationUrl = "custom.configuration.url"

// Data Broker Protection

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Configuration/ConfigurationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class ConfigurationManager {
static let queue: DispatchQueue = DispatchQueue(label: "Configuration Manager")

@UserDefaultsWrapper(key: .configLastUpdated, defaultValue: .distantPast)
private var lastUpdateTime: Date
private(set) var lastUpdateTime: Date

private var timerCancellable: AnyCancellable?
private var lastRefreshCheckTime: Date = Date()
Expand Down
21 changes: 20 additions & 1 deletion DuckDuckGo/Menus/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Combine
import OSLog // swiftlint:disable:this enforce_os_log_wrapper
import SwiftUI
import WebKit
import Configuration

#if NETWORK_PROTECTION
import NetworkProtection
Expand Down Expand Up @@ -93,6 +94,8 @@ import Subscription
// MARK: - Debug

private var loggingMenu: NSMenu?
let customConfigurationUrlMenuItem = NSMenuItem(title: "Last Update Time", action: nil)
let configurationDateAndTimeMenuItem = NSMenuItem(title: "Configuration URL", action: nil)

// MARK: - Help

Expand Down Expand Up @@ -380,6 +383,7 @@ import Subscription
updateBookmarksBarMenuItem()
updateShortcutMenuItems()
updateLoggingMenuItems()
updateRemoteConfigurationInfo()
}

// MARK: - Bookmarks
Expand Down Expand Up @@ -530,6 +534,7 @@ import Subscription

// MARK: - Debug

// swiftlint:disable:next function_body_length
private func setupDebugMenu() -> NSMenu {
let debugMenu = NSMenu(title: "Debug") {
NSMenuItem(title: "Reset Data") {
Expand All @@ -555,7 +560,15 @@ import Subscription
NSMenuItem(title: "Show Pop Up Window", action: #selector(MainViewController.showPopUpWindow))
}
NSMenuItem(title: "Remote Configuration") {
NSMenuItem(title: "Fetch Configuration Now", action: #selector(MainViewController.fetchConfigurationNow))
customConfigurationUrlMenuItem
configurationDateAndTimeMenuItem
NSMenuItem.separator()
NSMenuItem(title: "Reload Configuration Now", action: #selector(MainViewController.reloadConfigurationNow))
NSMenuItem(title: "Set custom configuration URL…", action: #selector(MainViewController.setCustomConfigurationURL))
NSMenuItem(title: "Reset configuration to default", action: #selector(MainViewController.resetConfigurationToDefault))
}
NSMenuItem(title: "User Scripts") {
NSMenuItem(title: "Remove user scripts from selected tab", action: #selector(MainViewController.removeUserScripts))
}
NSMenuItem(title: "Sync")
.submenu(SyncDebugMenu())
Expand Down Expand Up @@ -621,6 +634,12 @@ import Subscription
}
}

private func updateRemoteConfigurationInfo() {
let dateString = DateFormatter.localizedString(from: ConfigurationManager.shared.lastUpdateTime, dateStyle: .short, timeStyle: .medium)
configurationDateAndTimeMenuItem.title = "Last Update Time: \(dateString)"
customConfigurationUrlMenuItem.title = "Configuration URL: \(AppConfigurationURLProvider().url(for: .privacyConfiguration).absoluteString)"
}

@objc private func loggingMenuItemAction(_ sender: NSMenuItem) {
guard let category = sender.identifier?.rawValue else { return }

Expand Down
45 changes: 44 additions & 1 deletion DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import BrowserServicesKit
import Cocoa
import Common
import WebKit
import Configuration

// Actions are sent to objects of responder chain

Expand Down Expand Up @@ -724,10 +725,52 @@ extension MainViewController {
EmailManager().resetEmailProtectionInContextPrompt()
}

@objc func fetchConfigurationNow(_ sender: Any?) {
@objc func removeUserScripts(_ sender: Any?) {
tabCollectionViewModel.selectedTab?.userContentController?.cleanUpBeforeClosing()
tabCollectionViewModel.selectedTab?.reload()
os_log("User scripts removed from the current tab", type: .info)
}

@objc func reloadConfigurationNow(_ sender: Any?) {
OSLog.loggingCategories.insert(OSLog.AppCategories.config.rawValue)

ConfigurationManager.shared.forceRefresh()
}

private func setConfigurationUrl(_ configurationUrl: URL?) {
OSLog.loggingCategories.insert(OSLog.AppCategories.config.rawValue)

var configurationProvider = AppConfigurationURLProvider(customPrivacyConfiguration: configurationUrl)
if configurationUrl == nil {
configurationProvider.resetToDefaultConfigurationUrl()
}
Configuration.setURLProvider(configurationProvider)
ConfigurationManager.shared.forceRefresh()
if let configurationUrl {
os_log("New configuration URL set to \(configurationUrl.absoluteString)", type: .info)
} else {
os_log("New configuration URL reset to default", type: .info)
}
}

@objc func setCustomConfigurationURL(_ sender: Any?) {
let currentConfigurationURL = AppConfigurationURLProvider().url(for: .privacyConfiguration).absoluteString
let alert = NSAlert.customConfigurationAlert(configurationUrl: currentConfigurationURL)
if alert.runModal() != .cancel {
guard let textField = alert.accessoryView as? NSTextField,
let newConfigurationUrl = URL(string: textField.stringValue) else {
os_log("Failed to set custom configuration URL", type: .error)
return
}

setConfigurationUrl(newConfigurationUrl)
}
}

@objc func resetConfigurationToDefault(_ sender: Any?) {
setConfigurationUrl(nil)
}

// MARK: - Developer Tools

@objc func toggleDeveloperTools(_ sender: Any?) {
Expand Down