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

Fix 3rd party check for request blocking #8260

Merged
merged 1 commit into from
Oct 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
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