2020-07-22 19:29:38 -07:00
|
|
|
//
|
|
|
|
|
// SBRProcessPlugin.m
|
|
|
|
|
// SBrowserProcessBundle
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 7/22/20.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "SBRProcessPlugin.h"
|
|
|
|
|
|
|
|
|
|
#import "SBRWebProcessDelegate.h"
|
|
|
|
|
#import "SBRWebProcessProxy.h"
|
2021-10-21 13:24:58 -07:00
|
|
|
#import "SBRScriptPolicy.h"
|
2020-07-22 19:29:38 -07:00
|
|
|
|
|
|
|
|
#import <WebKit/_WKRemoteObjectInterface.h>
|
|
|
|
|
#import <WebKit/_WKRemoteObjectRegistry.h>
|
|
|
|
|
#import <WebKit/WKWebProcessPlugInBrowserContextController.h>
|
|
|
|
|
#import <WebKit/WKWebProcessPlugInBrowserContextControllerPrivate.h>
|
2021-10-21 13:24:58 -07:00
|
|
|
#import <WebKit/WKWebProcessPlugInFrame.h>
|
2020-07-22 19:29:38 -07:00
|
|
|
#import <WebKit/WKWebProcessPlugInLoadDelegate.h>
|
|
|
|
|
|
|
|
|
|
@interface SBRProcessPlugin () <WKWebProcessPlugInLoadDelegate, SBRWebProcessProxy>
|
2020-07-29 17:46:53 -07:00
|
|
|
|
2020-07-22 19:29:38 -07:00
|
|
|
@property (nonatomic, strong) id<SBRWebProcessDelegate> processDelegate;
|
2021-10-21 11:05:40 -07:00
|
|
|
|
2020-07-29 17:46:53 -07:00
|
|
|
@property (nonatomic, assign) BOOL allScriptsAllowed;
|
2021-10-21 11:05:40 -07:00
|
|
|
@property (nonatomic, strong) NSMutableSet<NSString *> *allowedResourceOrigins;
|
|
|
|
|
@property (nonatomic, strong) NSDictionary<NSString *, NSNumber *> *policyTypeByOrigin;
|
2020-07-22 19:29:38 -07:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation SBRProcessPlugin
|
|
|
|
|
|
|
|
|
|
+ (void)initialize
|
|
|
|
|
{
|
|
|
|
|
[super initialize];
|
|
|
|
|
NSLog(@"SBRProcessPlugin Initialized");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark <SBRWebProcessProxy>
|
|
|
|
|
|
|
|
|
|
- (void)hello
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"SBRProcessPlugin: Helloooooo");
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 19:26:35 -07:00
|
|
|
- (void)syncAllowedResourceOrigins:(NSArray<NSString *> *)allowedOrigins
|
2020-07-22 19:29:38 -07:00
|
|
|
{
|
2020-07-24 19:26:35 -07:00
|
|
|
_allowedResourceOrigins = [NSMutableSet setWithArray:allowedOrigins];
|
2020-07-22 19:29:38 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-21 11:05:40 -07:00
|
|
|
- (void)syncPolicyTypes:(NSDictionary<NSString *,NSNumber *> *)policyTypes
|
|
|
|
|
{
|
|
|
|
|
_policyTypeByOrigin = policyTypes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 17:46:53 -07:00
|
|
|
- (void)setAllScriptsAllowed:(BOOL)allScriptsAllowed
|
|
|
|
|
{
|
|
|
|
|
_allScriptsAllowed = allScriptsAllowed;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 19:29:38 -07:00
|
|
|
#pragma mark <WKWebProcessPlugIn>
|
|
|
|
|
|
2020-07-29 17:46:53 -07:00
|
|
|
- (void)webProcessPlugIn:(WKWebProcessPlugInController *)plugInController initializeWithObject:(id)initializationObject
|
|
|
|
|
{
|
|
|
|
|
_allowedResourceOrigins = [[plugInController parameters] valueForKey:SBRGetAllowedOriginsKey()];
|
|
|
|
|
_allScriptsAllowed = [[[plugInController parameters] valueForKey:SBRGetAllScriptsAllowedKey()] boolValue];
|
2021-10-21 13:24:58 -07:00
|
|
|
_policyTypeByOrigin = [[plugInController parameters] valueForKey:SBRGetPolicyTypeByOriginKey()];
|
2020-07-29 18:58:31 -07:00
|
|
|
NSLog(@"SBRProcessPlugin: %lu origins allowed, all scripts allowed: %@ ", (unsigned long)_allowedResourceOrigins.count, _allScriptsAllowed ? @"YES" : @"NO");
|
2020-07-29 17:46:53 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-22 19:29:38 -07:00
|
|
|
- (void)webProcessPlugIn:(WKWebProcessPlugInController *)plugInController didCreateBrowserContextController:(WKWebProcessPlugInBrowserContextController *)browserContextController
|
|
|
|
|
{
|
|
|
|
|
_WKRemoteObjectInterface *proxyInterface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(SBRWebProcessProxy)];
|
|
|
|
|
[browserContextController._remoteObjectRegistry registerExportedObject:self interface:proxyInterface];
|
|
|
|
|
|
|
|
|
|
_WKRemoteObjectInterface *eventListenerInterface = [_WKRemoteObjectInterface remoteObjectInterfaceWithProtocol:@protocol(SBRWebProcessDelegate)];
|
|
|
|
|
self.processDelegate = [browserContextController._remoteObjectRegistry remoteObjectProxyWithInterface:eventListenerInterface];
|
|
|
|
|
|
|
|
|
|
browserContextController.loadDelegate = self;
|
|
|
|
|
|
|
|
|
|
[[self processDelegate] webProcessDidConnect];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark <WKWebProcessPlugInLoadDelegate>
|
|
|
|
|
|
|
|
|
|
- (NSURLRequest *)webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller frame:(WKWebProcessPlugInFrame *)frame willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"SBRProcessPlugin: Sending request: %@", request);
|
2020-07-29 17:46:53 -07:00
|
|
|
if (_allowedResourceOrigins == nil) {
|
|
|
|
|
NSLog(@"Allowed resource origins should not be nil!!!!");
|
|
|
|
|
}
|
2020-07-22 19:29:38 -07:00
|
|
|
|
|
|
|
|
NSURL *requestURL = [request URL];
|
2021-10-21 13:24:58 -07:00
|
|
|
NSString *resourceOrigin = [requestURL host];
|
2020-07-22 19:29:38 -07:00
|
|
|
NSString *requestExtension = [requestURL pathExtension];
|
2021-10-21 13:24:58 -07:00
|
|
|
NSString *hostOrigin = [[[controller mainFrame] URL] host];
|
2020-07-22 19:29:38 -07:00
|
|
|
if (requestExtension.length > 0 && [requestExtension isEqualToString:@"js"]) {
|
2021-10-21 13:24:58 -07:00
|
|
|
NSNumber *policyType = [_policyTypeByOrigin objectForKey:hostOrigin];
|
|
|
|
|
NSLog(@"SBRProcessPlugin: Policy type for %@: %@", hostOrigin, policyType);
|
|
|
|
|
|
|
|
|
|
SBRScriptPolicy *policy = [[SBRScriptPolicy alloc] initWithSecurityOrigin:hostOrigin policyType:[policyType integerValue]];
|
|
|
|
|
if ([self allScriptsAllowed] || [policy allowsExternalJavaScriptResourceOrigin:resourceOrigin]) {
|
|
|
|
|
NSLog(@"SBRProcessPlugin: Policy allows script requestURL: %@", requestURL);
|
|
|
|
|
[[self processDelegate] webProcessDidAllowScriptWithOrigin:resourceOrigin];
|
2020-07-22 19:29:38 -07:00
|
|
|
} else {
|
2021-10-21 13:24:58 -07:00
|
|
|
NSLog(@"SBRProcessPlugin: Policy disallows script requestURL: %@", requestURL);
|
|
|
|
|
[[self processDelegate] webProcessDidBlockScriptWithOrigin:resourceOrigin];
|
2020-07-29 18:58:31 -07:00
|
|
|
|
2020-07-22 19:29:38 -07:00
|
|
|
request = nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|