Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1632e5fbd4 | ||
|
|
18b63cbd19 | ||
|
|
4371f051aa |
@@ -34,6 +34,8 @@
|
|||||||
</array>
|
</array>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||||
|
<key>ITSAppUsesNonExemptEncryption</key>
|
||||||
|
<false/>
|
||||||
<key>LSApplicationCategoryType</key>
|
<key>LSApplicationCategoryType</key>
|
||||||
<string>public.app-category.utilities</string>
|
<string>public.app-category.utilities</string>
|
||||||
<key>LSRequiresIPhoneOS</key>
|
<key>LSRequiresIPhoneOS</key>
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
<plist version="1.0">
|
<plist version="1.0">
|
||||||
<dict>
|
<dict>
|
||||||
|
<key>com.apple.developer.web-browser</key>
|
||||||
|
<true/>
|
||||||
<key>com.apple.security.app-sandbox</key>
|
<key>com.apple.security.app-sandbox</key>
|
||||||
<true/>
|
<true/>
|
||||||
<key>com.apple.security.network.client</key>
|
<key>com.apple.security.network.client</key>
|
||||||
|
|||||||
+10
-20
@@ -19,7 +19,6 @@ class Tab: NSObject, SBRProcessBundleBridgeDelegate
|
|||||||
|
|
||||||
public var tabInfo: TabInfo {
|
public var tabInfo: TabInfo {
|
||||||
get {
|
get {
|
||||||
updateMetadata()
|
|
||||||
return _tabInfo
|
return _tabInfo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,18 +38,14 @@ class Tab: NSObject, SBRProcessBundleBridgeDelegate
|
|||||||
}
|
}
|
||||||
public var policyManager: ResourcePolicyManager
|
public var policyManager: ResourcePolicyManager
|
||||||
|
|
||||||
private var _tabInfo: TabInfo = TabInfo()
|
// Persisted snapshot of visible tab metadata; do not recompute on read.
|
||||||
|
var _tabInfo: TabInfo = TabInfo()
|
||||||
|
|
||||||
private var loadedWebView: WKWebView? = nil
|
private var loadedWebView: WKWebView? = nil
|
||||||
public var title: String? { get { tabInfo.title } }
|
public var title: String? { get { tabInfo.title } }
|
||||||
public var url: URL? {
|
public var url: URL? {
|
||||||
get {
|
if let urlString = tabInfo.urlString { return URL(string: urlString) }
|
||||||
if let urlString = tabInfo.urlString {
|
return nil
|
||||||
return URL(string: urlString)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public var javaScriptEnabled: Bool = false {
|
public var javaScriptEnabled: Bool = false {
|
||||||
@@ -110,6 +105,10 @@ class Tab: NSObject, SBRProcessBundleBridgeDelegate
|
|||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
bridge.delegate = self
|
bridge.delegate = self
|
||||||
|
|
||||||
|
// Initialize snapshot metadata
|
||||||
|
_tabInfo.identifier = self.identifier
|
||||||
|
if let url { _tabInfo.urlString = url.absoluteString }
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
@@ -117,6 +116,8 @@ class Tab: NSObject, SBRProcessBundleBridgeDelegate
|
|||||||
}
|
}
|
||||||
|
|
||||||
func beginLoadingURL(_ url: URL) {
|
func beginLoadingURL(_ url: URL) {
|
||||||
|
// Update snapshot immediately so UI keeps URL even if process jettisons.
|
||||||
|
_tabInfo.urlString = url.absoluteString
|
||||||
let request = URLRequest(url: url)
|
let request = URLRequest(url: url)
|
||||||
webView.load(request)
|
webView.load(request)
|
||||||
}
|
}
|
||||||
@@ -150,15 +151,4 @@ class Tab: NSObject, SBRProcessBundleBridgeDelegate
|
|||||||
.assign(to: \.favicon, on: self)
|
.assign(to: \.favicon, on: self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateMetadata() {
|
|
||||||
guard contentProcessTerminated == false else { return }
|
|
||||||
|
|
||||||
_tabInfo = TabInfo(
|
|
||||||
title: loadedWebView?.title,
|
|
||||||
urlString: loadedWebView?.url?.absoluteString ?? self.homeURL?.absoluteString,
|
|
||||||
faviconData: self.favicon?.pngData(),
|
|
||||||
identifier: self.identifier
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,16 +49,28 @@ class TabController
|
|||||||
tabs.append(tab)
|
tabs.append(tab)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Title observation
|
// Title observation: update snapshot and notify delegate.
|
||||||
tab.titleObservation = tab.webView.observe(\.title, changeHandler: { [weak tab, weak self] (webView, change) in
|
tab.titleObservation = tab.webView.observe(\.title, changeHandler: { [weak tab, weak self] (webView, change) in
|
||||||
if let tab = tab, let self = self, let delegate = self.controllerDelegate {
|
guard let tab = tab else { return }
|
||||||
|
if let newTitle = webView.title, !newTitle.isEmpty {
|
||||||
|
tab._tabInfo.title = newTitle
|
||||||
|
}
|
||||||
|
if let self = self, let delegate = self.controllerDelegate {
|
||||||
delegate.tabController(self, didUpdateTitle: webView.title ?? "", forTab: tab)
|
delegate.tabController(self, didUpdateTitle: webView.title ?? "", forTab: tab)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// URL observation: persist the latest URL in the snapshot.
|
||||||
|
tab.urlObservation = tab.webView.observe(\.url, changeHandler: { [weak tab] (webView, change) in
|
||||||
|
guard let tab = tab else { return }
|
||||||
|
tab._tabInfo.urlString = webView.url?.absoluteString ?? tab._tabInfo.urlString
|
||||||
|
})
|
||||||
|
|
||||||
// Favicon Observation
|
// Favicon Observation
|
||||||
tab.faviconObservation = tab.$favicon.receive(on: RunLoop.main).sink { [weak tab, weak self] val in
|
tab.faviconObservation = tab.$favicon.receive(on: RunLoop.main).sink { [weak tab, weak self] val in
|
||||||
if let tab = tab, let self = self, let delegate = self.controllerDelegate {
|
guard let tab = tab else { return }
|
||||||
|
tab._tabInfo.faviconData = val?.pngData()
|
||||||
|
if let self = self, let delegate = self.controllerDelegate {
|
||||||
delegate.tabController(self, didUpdateFavicon: val, forTab: tab)
|
delegate.tabController(self, didUpdateFavicon: val, forTab: tab)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#import "Hacks.h"
|
#import "Hacks.h"
|
||||||
|
|
||||||
#import <OSLog/OSLog.h>
|
#import <OSLog/OSLog.h>
|
||||||
|
#import <stdatomic.h>
|
||||||
|
|
||||||
#import <WebKit/_WKRemoteObjectInterface.h>
|
#import <WebKit/_WKRemoteObjectInterface.h>
|
||||||
#import <WebKit/_WKRemoteObjectRegistry.h>
|
#import <WebKit/_WKRemoteObjectRegistry.h>
|
||||||
@@ -19,10 +20,28 @@
|
|||||||
#import <WebKit/WKProcessPoolPrivate.h>
|
#import <WebKit/WKProcessPoolPrivate.h>
|
||||||
#import <WebKit/WKWebViewPrivate.h>
|
#import <WebKit/WKWebViewPrivate.h>
|
||||||
#import <WebKit/WKWebViewConfigurationPrivate.h>
|
#import <WebKit/WKWebViewConfigurationPrivate.h>
|
||||||
|
#import <WebKit/WKContentRuleListStore.h>
|
||||||
|
|
||||||
#define WKUserStyleSheet id
|
#define WKUserStyleSheet id
|
||||||
#define WKUserStyleSheetEncodedClassName "X1dLVXNlclN0eWxlU2hlZXQ="
|
#define WKUserStyleSheetEncodedClassName "X1dLVXNlclN0eWxlU2hlZXQ="
|
||||||
|
|
||||||
|
NSArray<NSString *> *CommonCDNList(void) {
|
||||||
|
static dispatch_once_t onceToken;
|
||||||
|
static NSArray<NSString *> *commonCDNList = nil;
|
||||||
|
dispatch_once(&onceToken, ^{
|
||||||
|
commonCDNList = @[
|
||||||
|
@"cdn.jsdelivr.net",
|
||||||
|
@"fsdn.net",
|
||||||
|
@"cdnjs.com",
|
||||||
|
@"osscdn.com",
|
||||||
|
@"code.jquery.com",
|
||||||
|
@"bootstrapcdn.com"
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
return commonCDNList;
|
||||||
|
}
|
||||||
|
|
||||||
@interface StyleSheet : NSObject <NSCopying>
|
@interface StyleSheet : NSObject <NSCopying>
|
||||||
@property (nonatomic, readonly, copy) NSString *source;
|
@property (nonatomic, readonly, copy) NSString *source;
|
||||||
@property (nonatomic, readonly, copy) NSURL *baseURL;
|
@property (nonatomic, readonly, copy) NSURL *baseURL;
|
||||||
@@ -31,6 +50,9 @@
|
|||||||
- (instancetype)initWithSource:(NSString *)source forMainFrameOnly:(BOOL)forMainFrameOnly;
|
- (instancetype)initWithSource:(NSString *)source forMainFrameOnly:(BOOL)forMainFrameOnly;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
// Note: We no longer proxy http/https via WKURLSchemeHandler; we use
|
||||||
|
// WebKit content blocking rules instead.
|
||||||
|
|
||||||
@interface WKUserContentController (Private)
|
@interface WKUserContentController (Private)
|
||||||
- (void)__addUserStyleSheet:(WKUserStyleSheet)userStyleSheet;
|
- (void)__addUserStyleSheet:(WKUserStyleSheet)userStyleSheet;
|
||||||
- (void)__removeUserStyleSheet:(WKUserStyleSheet)userStyleSheet;
|
- (void)__removeUserStyleSheet:(WKUserStyleSheet)userStyleSheet;
|
||||||
@@ -68,6 +90,13 @@
|
|||||||
#define LOG_DEBUG(format, ...) os_log_debug(_log, format, ##__VA_ARGS__)
|
#define LOG_DEBUG(format, ...) os_log_debug(_log, format, ##__VA_ARGS__)
|
||||||
#define LOG_ERROR(format, ...) os_log_error(_log, format, ##__VA_ARGS__)
|
#define LOG_ERROR(format, ...) os_log_error(_log, format, ##__VA_ARGS__)
|
||||||
|
|
||||||
|
static os_log_t _log;
|
||||||
|
|
||||||
|
__attribute__((constructor))
|
||||||
|
static void initialize_log(void) {
|
||||||
|
_log = os_log_create("net.buzzert.attractor.webview", "bridge");
|
||||||
|
}
|
||||||
|
|
||||||
@interface NSURLResponse (BridgeAdditions)
|
@interface NSURLResponse (BridgeAdditions)
|
||||||
@property (nonatomic, readonly) BOOL isJavascriptResponse;
|
@property (nonatomic, readonly) BOOL isJavascriptResponse;
|
||||||
@end
|
@end
|
||||||
@@ -91,13 +120,11 @@
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface SBRProcessBundleBridge () <WKURLSchemeHandler>
|
@interface SBRProcessBundleBridge ()
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation SBRProcessBundleBridge {
|
@implementation SBRProcessBundleBridge {
|
||||||
os_log_t _log;
|
|
||||||
|
|
||||||
WKWebView *_webView;
|
WKWebView *_webView;
|
||||||
WKWebViewConfiguration *_webViewConfiguration;
|
WKWebViewConfiguration *_webViewConfiguration;
|
||||||
WKProcessPool *_processPool;
|
WKProcessPool *_processPool;
|
||||||
@@ -107,17 +134,21 @@
|
|||||||
|
|
||||||
NSArray<WKUserScript *> *_userScripts;
|
NSArray<WKUserScript *> *_userScripts;
|
||||||
|
|
||||||
dispatch_queue_t _dataTasksAccessQueue;
|
|
||||||
NSMutableDictionary<NSURLRequest *, NSURLSessionDataTask *> *_dataTasks;
|
|
||||||
|
|
||||||
// These come from settings.
|
// These come from settings.
|
||||||
WKUserStyleSheet _customizedUserStylesheet;
|
WKUserStyleSheet _customizedUserStylesheet;
|
||||||
WKUserScript *_customizedUserScript;
|
WKUserScript *_customizedUserScript;
|
||||||
|
|
||||||
|
// Content blocking
|
||||||
|
WKContentRuleList *_activeScriptRuleList;
|
||||||
|
void *_urlKVOContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)tearDown
|
- (void)tearDown
|
||||||
{
|
{
|
||||||
// This was used to unregister the delegate with the web process.
|
if (_webView) {
|
||||||
|
@try { [_webView removeObserver:self forKeyPath:@"URL" context:_urlKVOContext]; }
|
||||||
|
@catch (__unused NSException *ex) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (instancetype)initWithWebViewConfiguration:(WKWebViewConfiguration *)webViewConfiguration
|
- (instancetype)initWithWebViewConfiguration:(WKWebViewConfiguration *)webViewConfiguration
|
||||||
@@ -125,8 +156,6 @@
|
|||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
if (!webViewConfiguration) {
|
if (!webViewConfiguration) {
|
||||||
_log = os_log_create("net.buzzert.attractor.webview", "bridge");
|
|
||||||
|
|
||||||
webViewConfiguration = [[WKWebViewConfiguration alloc] init];
|
webViewConfiguration = [[WKWebViewConfiguration alloc] init];
|
||||||
|
|
||||||
// Set up process pool
|
// Set up process pool
|
||||||
@@ -135,12 +164,7 @@
|
|||||||
|
|
||||||
webViewConfiguration._waitsForPaintAfterViewDidMoveToWindow = NO;
|
webViewConfiguration._waitsForPaintAfterViewDidMoveToWindow = NO;
|
||||||
webViewConfiguration._applePayEnabled = YES;
|
webViewConfiguration._applePayEnabled = YES;
|
||||||
|
// No http/https interception — rely on content blocking rules instead.
|
||||||
_dataTasks = [NSMutableDictionary dictionary];
|
|
||||||
_dataTasksAccessQueue = dispatch_queue_create("net.buzzert.attractor.dataTasksAccess", DISPATCH_QUEUE_SERIAL);
|
|
||||||
|
|
||||||
[webViewConfiguration setURLSchemeHandler:self forURLScheme:@"http"];
|
|
||||||
[webViewConfiguration setURLSchemeHandler:self forURLScheme:@"https"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_webViewConfiguration = webViewConfiguration;
|
_webViewConfiguration = webViewConfiguration;
|
||||||
@@ -161,6 +185,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
_webView = webView;
|
_webView = webView;
|
||||||
|
_urlKVOContext = &_urlKVOContext; // unique context pointer
|
||||||
|
[_webView addObserver:self forKeyPath:@"URL" options:(NSKeyValueObservingOptionNew) context:_urlKVOContext];
|
||||||
|
|
||||||
|
// Initialize content blocking rules for current host
|
||||||
|
[self rebuildContentBlockingRulesForCurrentHost];
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
@@ -223,97 +252,18 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark <WKURLSchemeHandler>
|
|
||||||
|
|
||||||
- (void)webView:(WKWebView *)webView startURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask
|
|
||||||
{
|
|
||||||
NSString *hostOrigin = [[_webView URL] host];
|
|
||||||
NSURLRequest *request = [urlSchemeTask request];
|
|
||||||
|
|
||||||
LOG_DEBUG("Start URL scheme task: request: %@", request);
|
|
||||||
|
|
||||||
__weak __auto_type welf = self;
|
|
||||||
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
|
|
||||||
{
|
|
||||||
if (!welf) return;
|
|
||||||
__strong __auto_type sself = welf;
|
|
||||||
|
|
||||||
if (error != nil) {
|
|
||||||
[urlSchemeTask didFailWithError:error];
|
|
||||||
} else if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
||||||
NSURL *requestURL = [request URL];
|
|
||||||
NSString *resourceOrigin = [requestURL host];
|
|
||||||
const __auto_type allowResource = ^{
|
|
||||||
os_log_debug(sself->_log, "Allowing resource: %@", requestURL.lastPathComponent);
|
|
||||||
[urlSchemeTask didReceiveResponse:response];
|
|
||||||
[urlSchemeTask didReceiveData:data];
|
|
||||||
[urlSchemeTask didFinish];
|
|
||||||
|
|
||||||
[self webProcessDidAllowScriptWithOrigin:resourceOrigin];
|
|
||||||
};
|
|
||||||
|
|
||||||
const __auto_type denyResource = ^{
|
|
||||||
os_log_debug(sself->_log, "Blocking resource: %@", requestURL.lastPathComponent);
|
|
||||||
NSHTTPURLResponse *altResponse = [[NSHTTPURLResponse alloc] initWithURL:requestURL
|
|
||||||
MIMEType:@"application/javascript"
|
|
||||||
expectedContentLength:0 textEncodingName:@"utf8"];
|
|
||||||
[urlSchemeTask didReceiveResponse:altResponse];
|
|
||||||
[urlSchemeTask didReceiveData:[NSData data]];
|
|
||||||
[urlSchemeTask didFinish];
|
|
||||||
|
|
||||||
[self webProcessDidBlockScriptWithOrigin:resourceOrigin];
|
|
||||||
};
|
|
||||||
|
|
||||||
// Check MIME type for JavaScript responses.
|
|
||||||
if ([response isJavascriptResponse] && ![sself allowAllScripts]) {
|
|
||||||
dispatch_async(sself->_dataTasksAccessQueue, ^{
|
|
||||||
NSDictionary<NSString *, NSNumber *> *policyTypes = [sself->_policyDataSource scriptPolicyTypeByOrigin];
|
|
||||||
NSNumber *policyType = [policyTypes objectForKey:hostOrigin];
|
|
||||||
|
|
||||||
SBRScriptPolicy *policy = [[SBRScriptPolicy alloc] initWithSecurityOrigin:hostOrigin policyType:[policyType integerValue]];
|
|
||||||
if ([policy allowsExternalJavaScriptResourceOrigin:resourceOrigin]) {
|
|
||||||
allowResource();
|
|
||||||
} else {
|
|
||||||
denyResource();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
allowResource();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
[urlSchemeTask didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:nil]];
|
|
||||||
}
|
|
||||||
|
|
||||||
[sself->_dataTasks removeObjectForKey:request];
|
|
||||||
}];
|
|
||||||
|
|
||||||
[_dataTasks setObject:dataTask forKey:request];
|
|
||||||
[dataTask resume];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)webView:(WKWebView *)webView stopURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask
|
|
||||||
{
|
|
||||||
NSURLRequest *request = [urlSchemeTask request];
|
|
||||||
NSURLSessionDataTask *dataTask = [_dataTasks objectForKey:request];
|
|
||||||
if (dataTask) {
|
|
||||||
if ([dataTask state] != NSURLSessionTaskStateCanceling) {
|
|
||||||
[dataTask cancel];
|
|
||||||
}
|
|
||||||
|
|
||||||
[_dataTasks removeObjectForKey:request];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark Actions
|
#pragma mark Actions
|
||||||
|
|
||||||
- (void)policyDataSourceDidChange
|
- (void)policyDataSourceDidChange
|
||||||
{
|
{
|
||||||
// This was used when we had to signal the process bundle.
|
// Rebuild content blocking rules when policy changes.
|
||||||
|
[self rebuildContentBlockingRulesForCurrentHost];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setAllowAllScripts:(BOOL)allowAllScripts
|
- (void)setAllowAllScripts:(BOOL)allowAllScripts
|
||||||
{
|
{
|
||||||
_allowAllScripts = allowAllScripts;
|
_allowAllScripts = allowAllScripts;
|
||||||
|
[self rebuildContentBlockingRulesForCurrentHost];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setDarkModeEnabled:(BOOL)darkModeEnabled
|
- (void)setDarkModeEnabled:(BOOL)darkModeEnabled
|
||||||
@@ -360,4 +310,119 @@
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Content Blocking Rules
|
||||||
|
|
||||||
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
|
||||||
|
{
|
||||||
|
if (context == _urlKVOContext && [keyPath isEqualToString:@"URL"]) {
|
||||||
|
[self rebuildContentBlockingRulesForCurrentHost];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)rebuildContentBlockingRulesForCurrentHost
|
||||||
|
{
|
||||||
|
NSString *hostOrigin = _webView.URL.host;
|
||||||
|
if (!hostOrigin) {
|
||||||
|
// No page loaded yet; clear any existing rules
|
||||||
|
[self applyContentRuleListJSON:nil withName:nil];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine policy for this host
|
||||||
|
NSNumber *policyType = nil;
|
||||||
|
@synchronized (_policyDataSource) {
|
||||||
|
NSDictionary<NSString *, NSNumber *> *policyTypes = [_policyDataSource scriptPolicyTypeByOrigin];
|
||||||
|
policyType = [policyTypes objectForKey:hostOrigin] ?: @(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alpha=0 Bravo=1 Charlie=2 Delta=3 Echo=4
|
||||||
|
SBRScriptOriginPolicyType type = [policyType integerValue];
|
||||||
|
if (_allowAllScripts || type >= SBRScriptOriginPolicyTypeEcho) {
|
||||||
|
// Echo or shields down: no blocking
|
||||||
|
[self applyContentRuleListJSON:nil withName:nil];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Base trigger applies only to this page's domain
|
||||||
|
NSMutableArray *rules = [NSMutableArray array];
|
||||||
|
NSDictionary *baseScriptTrigger = @{
|
||||||
|
@"resource-type" : @[ @"script" ],
|
||||||
|
@"if-domain" : @[ hostOrigin ]
|
||||||
|
};
|
||||||
|
|
||||||
|
if (type <= SBRScriptOriginPolicyTypeBravo) {
|
||||||
|
// Alpha or Bravo: block all external script subresources (inline JS is controlled via preferences elsewhere)
|
||||||
|
[rules addObject:@{ @"trigger": baseScriptTrigger, @"action": @{ @"type": @"block" } }];
|
||||||
|
} else {
|
||||||
|
// Charlie/Delta: block third-party scripts
|
||||||
|
NSMutableDictionary *trigger = [baseScriptTrigger mutableCopy];
|
||||||
|
trigger[@"load-type"] = @[ @"third-party" ];
|
||||||
|
[rules addObject:@{ @"trigger": trigger, @"action": @{ @"type": @"block" } }];
|
||||||
|
|
||||||
|
if (type >= SBRScriptOriginPolicyTypeDelta) {
|
||||||
|
// Delta: add allowlist for common CDNs
|
||||||
|
for (NSString *cdn in CommonCDNList()) {
|
||||||
|
NSString *escaped = [NSRegularExpression escapedPatternForString:cdn];
|
||||||
|
NSString *pattern = [NSString stringWithFormat:@".*://([^.]*\\.)?%@/", escaped];
|
||||||
|
[rules addObject:@{
|
||||||
|
@"trigger": @{
|
||||||
|
@"url-filter": pattern,
|
||||||
|
@"if-domain": @[ hostOrigin ],
|
||||||
|
@"resource-type": @[ @"script" ]
|
||||||
|
},
|
||||||
|
@"action": @{ @"type": @"ignore-previous-rules" }
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Heuristic: allow cdn.<anything-with-family-name>/* where family name is the second-level label
|
||||||
|
NSArray<NSString *> *components = [hostOrigin componentsSeparatedByString:@"."];
|
||||||
|
if (components.count > 1) {
|
||||||
|
NSString *family = components[components.count - 2];
|
||||||
|
NSString *escapedFamily = [NSRegularExpression escapedPatternForString:family];
|
||||||
|
NSString *familyPattern = [NSString stringWithFormat:@".*://cdn\\.[^/]*%@[^/]*/", escapedFamily];
|
||||||
|
[rules addObject:@{
|
||||||
|
@"trigger": @{
|
||||||
|
@"url-filter": familyPattern,
|
||||||
|
@"if-domain": @[ hostOrigin ],
|
||||||
|
@"resource-type": @[ @"script" ]
|
||||||
|
},
|
||||||
|
@"action": @{ @"type": @"ignore-previous-rules" }
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:rules options:0 error:nil];
|
||||||
|
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
||||||
|
NSString *name = [NSString stringWithFormat:@"net.buzzert.attractor.rules.%@", hostOrigin];
|
||||||
|
[self applyContentRuleListJSON:json withName:name];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)applyContentRuleListJSON:(NSString *)json withName:(NSString *)name
|
||||||
|
{
|
||||||
|
WKUserContentController *controller = [_webViewConfiguration userContentController];
|
||||||
|
if (_activeScriptRuleList) {
|
||||||
|
[controller removeContentRuleList:_activeScriptRuleList];
|
||||||
|
_activeScriptRuleList = nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!json || !name) { return; }
|
||||||
|
|
||||||
|
WKContentRuleListStore *store = [WKContentRuleListStore defaultStore];
|
||||||
|
[store compileContentRuleListForIdentifier:name encodedContentRuleList:json completionHandler:^(WKContentRuleList * _Nullable ruleList, NSError * _Nullable error) {
|
||||||
|
if (error) {
|
||||||
|
LOG_ERROR("Failed to compile content rule list: %@", error.localizedDescription);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
self->_activeScriptRuleList = ruleList;
|
||||||
|
[controller addContentRuleList:ruleList];
|
||||||
|
LOG_DEBUG("Applied content rule list: %@", name);
|
||||||
|
});
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -790,7 +790,7 @@
|
|||||||
CODE_SIGN_ENTITLEMENTS = "App/Supporting Files/SBrowser.entitlements";
|
CODE_SIGN_ENTITLEMENTS = "App/Supporting Files/SBrowser.entitlements";
|
||||||
CODE_SIGN_IDENTITY = "Apple Development";
|
CODE_SIGN_IDENTITY = "Apple Development";
|
||||||
CODE_SIGN_STYLE = Automatic;
|
CODE_SIGN_STYLE = Automatic;
|
||||||
CURRENT_PROJECT_VERSION = 5;
|
CURRENT_PROJECT_VERSION = 6;
|
||||||
DEAD_CODE_STRIPPING = YES;
|
DEAD_CODE_STRIPPING = YES;
|
||||||
DEVELOPMENT_TEAM = 3SJALV9BQ7;
|
DEVELOPMENT_TEAM = 3SJALV9BQ7;
|
||||||
INFOPLIST_FILE = "App/Supporting Files/Info.plist";
|
INFOPLIST_FILE = "App/Supporting Files/Info.plist";
|
||||||
@@ -801,7 +801,7 @@
|
|||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
"@executable_path/Frameworks",
|
"@executable_path/Frameworks",
|
||||||
);
|
);
|
||||||
MARKETING_VERSION = 4.0;
|
MARKETING_VERSION = 4.1;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = net.buzzert.attractor;
|
PRODUCT_BUNDLE_IDENTIFIER = net.buzzert.attractor;
|
||||||
PRODUCT_NAME = Attractor;
|
PRODUCT_NAME = Attractor;
|
||||||
PROVISIONING_PROFILE_SPECIFIER = "";
|
PROVISIONING_PROFILE_SPECIFIER = "";
|
||||||
@@ -825,7 +825,7 @@
|
|||||||
CODE_SIGN_ENTITLEMENTS = "App/Supporting Files/SBrowser.entitlements";
|
CODE_SIGN_ENTITLEMENTS = "App/Supporting Files/SBrowser.entitlements";
|
||||||
CODE_SIGN_IDENTITY = "Apple Development";
|
CODE_SIGN_IDENTITY = "Apple Development";
|
||||||
CODE_SIGN_STYLE = Automatic;
|
CODE_SIGN_STYLE = Automatic;
|
||||||
CURRENT_PROJECT_VERSION = 5;
|
CURRENT_PROJECT_VERSION = 6;
|
||||||
DEAD_CODE_STRIPPING = YES;
|
DEAD_CODE_STRIPPING = YES;
|
||||||
DEVELOPMENT_TEAM = 3SJALV9BQ7;
|
DEVELOPMENT_TEAM = 3SJALV9BQ7;
|
||||||
INFOPLIST_FILE = "App/Supporting Files/Info.plist";
|
INFOPLIST_FILE = "App/Supporting Files/Info.plist";
|
||||||
@@ -836,7 +836,7 @@
|
|||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
"@executable_path/Frameworks",
|
"@executable_path/Frameworks",
|
||||||
);
|
);
|
||||||
MARKETING_VERSION = 4.0;
|
MARKETING_VERSION = 4.1;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = net.buzzert.attractor;
|
PRODUCT_BUNDLE_IDENTIFIER = net.buzzert.attractor;
|
||||||
PRODUCT_NAME = Attractor;
|
PRODUCT_NAME = Attractor;
|
||||||
PROVISIONING_PROFILE_SPECIFIER = "";
|
PROVISIONING_PROFILE_SPECIFIER = "";
|
||||||
|
|||||||
Reference in New Issue
Block a user