Skip to content

Commit

Permalink
Use Smarter Encryption feature from BSK (duckduckgo#1069)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/72649045549333/1200350063882004/f

Description:
Smarter Encryption has been moved to external BSK module.

Steps to test this PR:

Run tests.
Perform series of manual tests to validate some websites are upgraded to https and others aren't.
https://duckduckgo-my.sharepoint.com/❌/p/chuston/ESDZG35bOvZHh1Z_ZIXgJ40BUJQ4pse2KSxb8BYIs2mTdA?e=Aj6n3c
  • Loading branch information
jaceklyp authored Mar 7, 2022
1 parent 81f2c5b commit 9e5d8fd
Show file tree
Hide file tree
Showing 32 changed files with 199 additions and 534 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[submodule "submodules/ios-js-support"]
path = submodules/ios-js-support
url = https://github.com/duckduckgo/ios-js-support.git
[submodule "submodules/bloom_cpp"]
path = submodules/bloom_cpp
url = https://github.com/duckduckgo/bloom_cpp
[submodule "submodules/privacy-grade"]
path = submodules/privacy-grade
url = https://github.com/duckduckgo/privacy-grade
Expand Down
55 changes: 19 additions & 36 deletions Core/HTTPSUpgradeStore.swift → Core/AppHTTPSUpgradeStore.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// HTTPSUpgradeStore.swift
// AppHTTPSUpgradeStore.swift
// DuckDuckGo
//
// Copyright © 2018 DuckDuckGo. All rights reserved.
Expand All @@ -20,21 +20,12 @@
import Foundation
import CoreData
import os.log
import BrowserServicesKit

public protocol HTTPSUpgradeStore {

func bloomFilter() -> BloomFilterWrapper?

func bloomFilterSpecification() -> HTTPSBloomFilterSpecification?

@discardableResult func persistBloomFilter(specification: HTTPSBloomFilterSpecification, data: Data) -> Bool

func shouldExcludeDomain(_ domain: String) -> Bool

@discardableResult func persistExcludedDomains(_ domains: [String]) -> Bool
}
extension HTTPSStoredBloomFilterSpecification: Managed {}
extension HTTPSExcludedDomain: Managed {}

public class HTTPSUpgradePersistence: HTTPSUpgradeStore {
public final class AppHTTPSUpgradeStore: HTTPSUpgradeStore {

private struct Resource {
static var bloomFilter: URL {
Expand All @@ -61,15 +52,15 @@ public class HTTPSUpgradePersistence: HTTPSUpgradeStore {
return (try? Resource.bloomFilter.checkResourceIsReachable()) ?? false
}

public func bloomFilter() -> BloomFilterWrapper? {
let storedSpecification = hasBloomFilterData ? bloomFilterSpecification() : loadEmbeddedData()?.specification
public var bloomFilter: BloomFilterWrapper? {
let storedSpecification = hasBloomFilterData ? bloomFilterSpecification : loadEmbeddedData()?.specification
guard let specification = storedSpecification else { return nil }
return BloomFilterWrapper(fromPath: Resource.bloomFilter.path,
withBitCount: Int32(specification.bitCount),
andTotalItems: Int32(specification.totalEntries))
}

public func bloomFilterSpecification() -> HTTPSBloomFilterSpecification? {
public var bloomFilterSpecification: HTTPSBloomFilterSpecification? {
var specification: HTTPSBloomFilterSpecification?
context.performAndWait {
let request: NSFetchRequest<HTTPSStoredBloomFilterSpecification> = HTTPSStoredBloomFilterSpecification.fetchRequest()
Expand All @@ -96,7 +87,7 @@ public class HTTPSUpgradePersistence: HTTPSUpgradeStore {
return EmbeddedBloomData(specification: specification, bloomFilter: bloomData, excludedDomains: excludedDomains.data)
}

@discardableResult public func persistBloomFilter(specification: HTTPSBloomFilterSpecification, data: Data) -> Bool {
@discardableResult func persistBloomFilter(specification: HTTPSBloomFilterSpecification, data: Data) -> Bool {
os_log("HTTPS Bloom Filter %s", log: generalLog, type: .debug, Resource.bloomFilter.absoluteString)
guard data.sha256 == specification.sha256 else { return false }
guard persistBloomFilter(data: data) else { return false }
Expand All @@ -121,18 +112,12 @@ public class HTTPSUpgradePersistence: HTTPSUpgradeStore {

context.performAndWait {
deleteBloomFilterSpecification()

let entityName = String(describing: HTTPSStoredBloomFilterSpecification.self)

if let storedEntity = NSEntityDescription.insertNewObject(
forEntityName: entityName,
into: context) as? HTTPSStoredBloomFilterSpecification {

storedEntity.bitCount = Int64(specification.bitCount)
storedEntity.totalEntries = Int64(specification.totalEntries)
storedEntity.errorRate = specification.errorRate
storedEntity.sha256 = specification.sha256
}

let storedEntity: HTTPSStoredBloomFilterSpecification = context.insertObject()
storedEntity.bitCount = Int64(specification.bitCount)
storedEntity.totalEntries = Int64(specification.totalEntries)
storedEntity.errorRate = specification.errorRate
storedEntity.sha256 = specification.sha256

do {
try context.save()
Expand All @@ -148,7 +133,7 @@ public class HTTPSUpgradePersistence: HTTPSUpgradeStore {
}
}

public func shouldExcludeDomain(_ domain: String) -> Bool {
public func hasExcludedDomain(_ domain: String) -> Bool {
var result = false
context.performAndWait {
let request: NSFetchRequest<HTTPSExcludedDomain> = HTTPSExcludedDomain.fetchRequest()
Expand All @@ -159,16 +144,14 @@ public class HTTPSUpgradePersistence: HTTPSUpgradeStore {
return result
}

@discardableResult public func persistExcludedDomains(_ domains: [String]) -> Bool {
@discardableResult func persistExcludedDomains(_ domains: [String]) -> Bool {
var result = true
context.performAndWait {
deleteExcludedDomains()

for domain in domains {
let entityName = String(describing: HTTPSExcludedDomain.self)
if let storedDomain = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context) as? HTTPSExcludedDomain {
storedDomain.domain = domain.lowercased()
}
let storedDomain: HTTPSExcludedDomain = context.insertObject()
storedDomain.domain = domain.lowercased()
}
do {
try context.save()
Expand Down
1 change: 1 addition & 0 deletions Core/AtbParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//

import Foundation
import BrowserServicesKit

public struct AtbParser {
func convert(fromJsonData data: Data) throws -> Atb {
Expand Down
27 changes: 0 additions & 27 deletions Core/BloomFilterWrapper.h

This file was deleted.

64 changes: 0 additions & 64 deletions Core/BloomFilterWrapper.mm

This file was deleted.

5 changes: 3 additions & 2 deletions Core/ContentBlockerLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

import Foundation
import os.log
import BrowserServicesKit

public class ContentBlockerLoader {
typealias ContentBlockerLoaderProgress = (ContentBlockerRequest.Configuration) -> Void

private typealias DataDict = [ContentBlockerRequest.Configuration: Any]
private typealias EtagDict = [ContentBlockerRequest.Configuration: String]

private let httpsUpgradeStore: HTTPSUpgradeStore = HTTPSUpgradePersistence()
private let httpsUpgradeStore: HTTPSUpgradeStore = AppHTTPSUpgradeStore()
private let etagStorage: BlockerListETagStorage
private let fileStore: FileStore

Expand Down Expand Up @@ -120,7 +121,7 @@ public class ContentBlockerLoader {
return
}

if let storedSpecification = self.httpsUpgradeStore.bloomFilterSpecification(), storedSpecification == specification {
if let storedSpecification = self.httpsUpgradeStore.bloomFilterSpecification, storedSpecification == specification {
os_log("Bloom filter already downloaded", log: generalLog, type: .debug)
progress?(.httpsBloomFilter)
semaphore.signal()
Expand Down
13 changes: 3 additions & 10 deletions Core/HTTPSBloomFilterSpecification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,9 @@
// limitations under the License.
//

import Foundation
import BrowserServicesKit

public struct HTTPSBloomFilterSpecification: Equatable, Decodable {
let bitCount: Int
let errorRate: Double
let totalEntries: Int
let sha256: String

static public func == (lhs: HTTPSBloomFilterSpecification, rhs: HTTPSBloomFilterSpecification) -> Bool {
return lhs.bitCount == rhs.bitCount && lhs.errorRate == rhs.errorRate && lhs.totalEntries == rhs.totalEntries && lhs.sha256 == rhs.sha256
}
extension HTTPSBloomFilterSpecification {

static func copy(storedSpecification specification: HTTPSStoredBloomFilterSpecification?) -> HTTPSBloomFilterSpecification? {
guard let specification = specification else { return nil }
Expand All @@ -36,4 +28,5 @@ public struct HTTPSBloomFilterSpecification: Equatable, Decodable {
totalEntries: Int(specification.totalEntries),
sha256: specification.sha256!)
}

}
24 changes: 0 additions & 24 deletions Core/HTTPSExcludedDomains.swift

This file was deleted.

96 changes: 0 additions & 96 deletions Core/HTTPSUpgrade.swift

This file was deleted.

Loading

0 comments on commit 9e5d8fd

Please sign in to comment.