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

Commit

Permalink
Fix 3rd party check for request blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
cuba committed Oct 16, 2023
1 parent 8d4490f commit cb91250
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Sources/Brave/WebFilters/AdblockRustEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ extension AdblockEngine {
return false
}

guard let requestDomain = requestURL.baseDomain, let sourceDomain = sourceURL.baseDomain else {
return false
}

guard let requestHost = requestURL.host, let sourceHost = sourceURL.host else {
return false
}

// Normally we should check the etld+1.
// However for network filtering we use content blockers
// which unfortunately uses a host comparison.
// So this is what we simulate here
let isThirdParty = requestHost != sourceHost
// The content blocker rule for third party is the following:
// "third-party triggers if the resource isn’t from the same domain as the main page resource"
let isThirdParty = requestDomain != sourceDomain

if !isAggressive {
// If we have standard mode for this engine,
Expand Down
41 changes: 41 additions & 0 deletions Tests/ClientTests/Web Filters/CachedAdBlockEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,47 @@ import Data
@testable import Brave

final class CachedAdBlockEngineTests: XCTestCase {
func test3rdPartyCheck() throws {
let engine = try AdblockEngine(rules: [
"||brave.com/path",
"||brave.com/other-path",
"@@||brave.com/path$~third-party"
].joined(separator: "\n"))

XCTAssertFalse(
engine.shouldBlock(
requestURL: URL(string: "https://subdomain.brave.com/path")!,
sourceURL: URL(string: "https://brave.com")!, resourceType: .xmlhttprequest,
isAggressive: false
),
"We should not be blocking requests in the same domain on standard mode, regardless of the rules"
)
XCTAssertFalse(
engine.shouldBlock(
requestURL: URL(string: "https://subdomain.brave.com/path")!,
sourceURL: URL(string: "https://brave.com")!, resourceType: .xmlhttprequest,
isAggressive: true
),
"We should not be blocking this request even on aggressive mode because of the exception rule"
)
XCTAssertFalse(
engine.shouldBlock(
requestURL: URL(string: "https://subdomain.brave.com/other-path")!,
sourceURL: URL(string: "https://brave.com")!, resourceType: .xmlhttprequest,
isAggressive: false
),
"We should not be blocking requests in the same domain on standard mode, regardless of the rules"
)
XCTAssertTrue(
engine.shouldBlock(
requestURL: URL(string: "https://subdomain.brave.com/other-path")!,
sourceURL: URL(string: "https://brave.com")!, resourceType: .xmlhttprequest,
isAggressive: true
),
"We should be blocking this request on aggressive mode because of the blocking rule without an exception"
)
}

func testEngineMemoryManagment() throws {
AdblockEngine.setDomainResolver()
var engine: AdblockEngine? = AdblockEngine()
Expand Down

0 comments on commit cb91250

Please sign in to comment.