This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #5461: Add more robust previousUrl check for de-amping
- Loading branch information
Showing
7 changed files
with
140 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2022 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import Shared | ||
import WebKit | ||
|
||
public class DeAmpHelper: TabContentScript { | ||
private struct DeAmpDTO: Decodable { | ||
enum CodingKeys: String, CodingKey { | ||
case destURL, securityToken | ||
} | ||
|
||
let securityToken: String | ||
let destURL: URL | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.securityToken = try container.decode(String.self, forKey: .securityToken) | ||
let destURLString = try container.decode(String.self, forKey: .destURL) | ||
|
||
guard let destURL = URL(string: destURLString) else { | ||
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "`resourceURL` is not a valid URL. Fix the `RequestBlocking.js` script")) | ||
} | ||
|
||
self.destURL = destURL | ||
} | ||
} | ||
|
||
private weak var tab: Tab? | ||
|
||
init(tab: Tab) { | ||
self.tab = tab | ||
} | ||
|
||
static func name() -> String { | ||
return "DeAmpHelper" | ||
} | ||
|
||
static func scriptMessageHandlerName() -> String { | ||
return ["deAmpHelper", UserScriptManager.messageHandlerTokenString].joined(separator: "_") | ||
} | ||
|
||
func scriptMessageHandlerName() -> String? { | ||
return Self.scriptMessageHandlerName() | ||
} | ||
|
||
func userContentController(_ userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) { | ||
do { | ||
let data = try JSONSerialization.data(withJSONObject: message.body) | ||
let dto = try JSONDecoder().decode(DeAmpDTO.self, from: data) | ||
|
||
guard dto.securityToken == UserScriptManager.securityTokenString else { | ||
assertionFailure("Invalid security token. Fix the `RequestBlocking.js` script") | ||
replyHandler(false, nil) | ||
return | ||
} | ||
|
||
// Check that the destination is not the same as the previousURL | ||
// or that previousURL is nil | ||
let shouldRedirect = dto.destURL != tab?.previousComittedURL | ||
replyHandler(shouldRedirect, nil) | ||
} catch { | ||
assertionFailure("Invalid type of message. Fix the `RequestBlocking.js` script") | ||
replyHandler(false, nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters