Browser4
Browser4 copied to clipboard
Correct URL blocking logic in WebDriver
Bugs in onRequestWillBeSent and isBlocked
Method isBlocked() has bugs
private fun onRequestWillBeSent(entry: NavigateEntry, event: RequestWillBeSent) {
if (!entry.url.startsWith("http")) {
// This can happen for the following cases:
// 1. non-http resources, for example, ftp, ws, etc.
// 2. chrome's internal page, for example, about:blank, chrome://settings/, chrome://settings/system, etc.
return
}
if (!URLUtils.isStandard(entry.url)) {
logger.warn("Not a valid url | {}", entry.url)
return
}
tracer?.trace("onRequestWillBeSent | driver | requestId: {}", event.requestId)
val chromeNavigateEntry = ChromeNavigateEntry(navigateEntry)
chromeNavigateEntry.updateStateBeforeRequestSent(event)
// perform blocking logic
val isMinor = chromeNavigateEntry.isMinorResource(event)
if (isMinor && isBlocked(event.request.url)) {
fetchAPI?.failRequest(event.requestId, ErrorReason.ABORTED)
}
}
Clarify the behavior of networkAPI?.setBlockedURLs(blockedURLs)
private fun navigateInvaded(entry: NavigateEntry) {
val url = entry.url
addScriptToEvaluateOnNewDocument()
if (blockedURLs.isNotEmpty()) {
// Blocks URLs from loading.
networkAPI?.setBlockedURLs(blockedURLs)
}
...
}