Skip to content

Commit abf225c

Browse files
author
wangkai168
committed
更新pod
1 parent 6132f64 commit abf225c

11 files changed

Lines changed: 162 additions & 102 deletions

‎.DS_Store‎

0 Bytes
Binary file not shown.

‎WKJavaScriptBridge/Dispatcher/WKJavaScriptMessageDispatcher.m‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,26 @@ - (void)fetchCommands:(NSString *)command {
5656
- (void)executePending {
5757
if (_commandDictionary.allKeys.count > 0) {
5858
WKMsgCommand *msgCommand = [WKMsgCommand commandFromJson:_commandDictionary];
59+
[_bridge pluginSecurityVerityWithCommand:msgCommand completionHandler:^(BOOL passed) {
60+
if (!passed) {
61+
NSLog(@"WKJavaScriptBridge Error : 模块: (%@) 方法:(%@) 权限验证未通过,如需使用请申请配置。",msgCommand.className, msgCommand.methodName);
62+
return ;
63+
}
5964
#ifdef DEBUG
60-
NSLog(@"WKJavaScriptBridge(%@): Calling %@.%@", msgCommand.callbackId, msgCommand.className, msgCommand.methodName);
65+
NSLog(@"PHJavaScriptBridge(%@): Calling %@.%@", msgCommand.callbackId, msgCommand.className, msgCommand.methodName);
6166
#endif
62-
if (![self execute:msgCommand]) {
67+
if (![self execute:msgCommand]) {
6368
#ifdef DEBUG
64-
NSString *commandJson = [NSString stringWithFormat:@"%@",_commandDictionary];
65-
static NSUInteger maxLogLength = 1024;
66-
NSString *commandString = ([commandJson length] > maxLogLength) ?
67-
[NSString stringWithFormat : @"%@[...]", [commandJson substringToIndex:maxLogLength]] :
68-
commandJson;
69-
70-
NSLog(@"WKJavaScriptBridge Error : FAILED pluginJSON = %@", commandString);
69+
NSString *commandJson = [NSString stringWithFormat:@"%@",self->_commandDictionary];
70+
static NSUInteger maxLogLength = 1024;
71+
NSString *commandString = ([commandJson length] > maxLogLength) ?
72+
[NSString stringWithFormat : @"%@[...]", [commandJson substringToIndex:maxLogLength]] :
73+
commandJson;
74+
75+
NSLog(@"PHJavaScriptBridge Error : FAILED pluginJSON = %@", commandString);
7176
#endif
72-
}
77+
}
78+
}];
7379
}
7480
}
7581

‎WKJavaScriptBridge/Plugin/WKBasePlugin.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "WKPluginProtocol.h"
1313
#import "WKMsgCommand.h"
1414
#import "WKJavaScriptBridgePluginAnnotation.h"
15+
#import "WKPluginSecurityProtocol.h"
1516

1617
NS_ASSUME_NONNULL_BEGIN
1718

‎WKJavaScriptBridge/Rigister/WKJavaScriptBridgePluginAnnotation.h‎

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// WKJavaScriptBridgePluginAnnotation.h
3+
// WKJavaScriptBridge-demo
4+
//
5+
// Created by Kevin on 2019/10/22.
6+
// Copyright © 2019 王凯. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
//@class WKJavaScriptBridge;
12+
//
13+
//#define WKWebPlugins "WKWebPlugins"
14+
//
15+
//#define WKWebPluginDATA(sectname) __attribute((used, section("__DATA,"#sectname" ")))
16+
//#define WKRegisterWhiteList(name) \
17+
//class WKJavaScriptBridge; char * k##name##_mod WKWebPluginDATA(WKWebPlugins) = ""#name"";
18+
19+
20+
21+
@class WKJavaScriptBridge;
22+
23+
#define SecurityConfig "SecurityConfig"
24+
25+
#define SecurityPlugin "SecurityPlugin"
26+
27+
#define WKWebPluginDATA(sectname) __attribute((used, section("__DATA,"#sectname" ")))
28+
29+
#define WKConfigPluginSafeLevel(service,action,level) \
30+
class WKJavaScriptBridge;char * k##service##_##action##_plu WKWebPluginDATA(SecurityConfig) = "[\""#service"\",\""#action"\",\""#level"\"]";
31+
32+
#define WKConfigSafePlugin(name) \
33+
class PHJavaScriptBridge;char * k##name##_plu WKWebPluginDATA(SecurityPlugin) = ""#name"";
34+
35+
NS_ASSUME_NONNULL_BEGIN
36+
37+
@interface WKJavaScriptBridgePluginAnnotation : NSObject
38+
- (instancetype)initWithBridge:(WKJavaScriptBridge *)bridge;
39+
- (void)getAllRegisterPluginName;
40+
@end
41+
42+
NS_ASSUME_NONNULL_END

WKJavaScriptBridge/Rigister/WKJavaScriptBridgePluginAnnotation.m renamed to WKJavaScriptBridge/Security/WKJavaScriptBridgePluginAnnotation.m

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,34 @@ - (void)getAllRegisterPluginName {
3636
}
3737

3838
- (void)registedAnnotationModules {
39-
NSArray<NSString *>*services = [WKJavaScriptBridgePluginAnnotation AnnotationModules];
40-
for (NSString *pluginName in services) {
41-
if (pluginName) {
42-
[self.bridge addWhiteList:pluginName];
39+
NSArray <NSString *>*whiteListPlugin = [WKJavaScriptBridgePluginAnnotation AnnotationWhiteListPlugin];
40+
if (whiteListPlugin.count > 0) {
41+
NSString *plugin = whiteListPlugin[0];
42+
if (plugin.length) {
43+
[self.bridge registerSecurityPlugin:plugin];
4344
}
4445
}
46+
47+
NSArray <NSString *>*securityInfo = [WKJavaScriptBridgePluginAnnotation AnnotationPlugins];
48+
[self.bridge registerPluginSecurityInfomation:securityInfo];
49+
}
50+
51+
+ (NSArray<NSString *> *)AnnotationPlugins {
52+
static NSArray<NSString *> *securityInfo = nil;
53+
static dispatch_once_t onceToken;
54+
dispatch_once(&onceToken, ^{
55+
securityInfo = BHReadConfiguration(SecurityConfig);
56+
});
57+
return securityInfo;
4558
}
4659

47-
+ (NSArray<NSString *> *)AnnotationModules {
48-
static NSArray<NSString *> *mods = nil;
60+
+ (NSArray<NSString *> *)AnnotationWhiteListPlugin {
61+
static NSArray<NSString *> *plugin = nil;
4962
static dispatch_once_t onceToken;
5063
dispatch_once(&onceToken, ^{
51-
mods = BHReadConfiguration(WKWebPlugins);
64+
plugin = BHReadConfiguration(SecurityPlugin);
5265
});
53-
return mods;
66+
return plugin;
5467
}
5568

5669
static NSArray<NSString *>* BHReadConfiguration(char *section) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// WKPluginSecurityProtocol.h
3+
// WKJavaScriptBridge-demo
4+
//
5+
// Created by Kevin on 2020/6/2.
6+
// Copyright © 2020 王凯. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
typedef void(^PHJudgeAuthorityComplete)(BOOL allowed);
12+
13+
NS_ASSUME_NONNULL_BEGIN
14+
15+
@protocol WKPluginSecurityProtocol <NSObject>
16+
17+
@required
18+
19+
/**
20+
插件是否符合使用权限
21+
22+
@param serviceName 类名
23+
@param actionName 方法名
24+
@param completionHandler 校验结果,异步返回
25+
*/
26+
- (void)applyAuthorityWithService:(NSString *)serviceName action:(NSString *)actionName securityInfomation:(NSArray<NSString *>*)secInfo complete:(PHJudgeAuthorityComplete)completionHandler;
27+
28+
@end
29+
30+
NS_ASSUME_NONNULL_END

‎WKJavaScriptBridge/WKJSBridge.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
*/
3838
WKJSBridge.prototype._handleMessageFromNative = function (messageString) {
3939
var callbackMessage = JSON.parse(messageString);
40-
var status = messageString.status;
40+
var status = callbackMessage.status;
4141
var callback = this.callbackCache[callbackMessage.callbackId];
4242
if (callback) { // 执行 callback 回调,并删除缓存的 callback
43-
if (status = '0') {//失败回调
43+
if (status == 1) {//失败回调
4444
callback.fail(callbackMessage.data);
4545
}else {//成功回调
4646
callback.success(callbackMessage.data);

‎WKJavaScriptBridge/WKJavaScriptBridge.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ NS_ASSUME_NONNULL_BEGIN
2525
*/
2626
+ (instancetype)bindBridgeWithWebView:(WKWebView *)webView;
2727

28-
/**
29-
是否开启插件白名单,默认关闭
30-
31-
@param openning 开关
32-
*/
33-
- (void)openWhiteList:(BOOL)openning;
34-
3528
@end
3629

3730
NS_ASSUME_NONNULL_END

‎WKJavaScriptBridge/WKJavaScriptBridge.m‎

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import "WKJavaScriptBridgePluginAnnotation.h"
1414
#import "WKBasePlugin.h"
1515
#import "WKCommandProtocol.h"
16+
#import "WKPluginSecurityProtocol.h"
1617

1718
static NSString * const WKJSBridge = @"WKJSBridgeMessageName";
1819

@@ -21,11 +22,11 @@ @interface WKJavaScriptBridge ()<WKScriptMessageHandler>
2122
@property (nonatomic, strong) WKJavaScriptBridgePluginAnnotation *annotation;//模块注册白名单
2223
@property (nonatomic, strong) WKJavaScriptMessageDispatcher *dispatcher;//插件模块派发
2324
@property (nonatomic, strong) WKCommandImpl *commandDelegate;//插件模块可执行函数实现
24-
@property (nonatomic, strong) NSMutableDictionary *whiteList;//白名单列表
25-
@property (nonatomic, assign, getter=isOpenWhiteList) BOOL openWhiteList;//是否开启白名单
2625
@property (nonatomic, strong) NSMutableDictionary *pluginsByName;//插件模块实例对象
2726
@property (nonatomic, weak, readwrite) WKWebView *webView;
28-
@property (nonatomic, strong) NSRecursiveLock *lock;
27+
//安全相关
28+
@property (nonatomic, strong) id <WKPluginSecurityProtocol>securityPlugin;//白名单插件
29+
@property (nonatomic, strong) NSArray *secInfo;//业务注册的安全信息
2930

3031
@end
3132

@@ -52,10 +53,6 @@ + (instancetype)bindBridgeWithWebView:(WKWebView *)webView {
5253
return nil;
5354
}
5455

55-
- (void)openWhiteList:(BOOL)openning {
56-
self.openWhiteList = openning;
57-
}
58-
5956
#pragma mark - Init
6057

6158
- (instancetype)initBridgeWithWebView:(WKWebView *)webView {
@@ -68,6 +65,7 @@ - (instancetype)initBridgeWithWebView:(WKWebView *)webView {
6865
}
6966

7067
- (void)commonInit {
68+
self.secInfo = [NSArray array];
7169
self.dispatcher = [[WKJavaScriptMessageDispatcher alloc] initWithBridge:self];
7270
self.annotation = [[WKJavaScriptBridgePluginAnnotation alloc] initWithBridge:self];
7371
self.commandDelegate = [[WKCommandImpl alloc] initWithBridge:self];
@@ -123,34 +121,36 @@ - (void)pluginInstall:(WKBasePlugin *)plugin {
123121

124122
#pragma mark - WKJavaScriptBridgeProtocol
125123

126-
- (void)addWhiteList:(NSString *)pluginName {
127-
[self.lock lock];
128-
[self.whiteList setValue:pluginName forKey:[pluginName lowercaseString]];
129-
[self.lock unlock];
124+
- (void)registerSecurityPlugin:(NSString *)name {
125+
id<WKPluginSecurityProtocol>securityPlugin = [self getCommandInstance:name];
126+
if ([securityPlugin isKindOfClass:[WKBasePlugin class]] &&
127+
[securityPlugin conformsToProtocol:@protocol(WKPluginSecurityProtocol)]) {
128+
self.securityPlugin = securityPlugin;
129+
}
130130
}
131131

132-
- (id)getCommandInstance:(NSString*)pluginName {
133-
NSString* className;
134-
if (self.isOpenWhiteList) {//校验白名单
135-
[self.lock lock];
136-
className = [self.whiteList objectForKey:[pluginName lowercaseString]];
137-
[self.lock unlock];
138-
if (className == nil) {
139-
#ifdef DEBUG
140-
NSLog(@"WKJavaScriptBridge Error : 模块: (%@) 没有注册白名单。请关闭白名单或者或者使用(@WKRegisterWhiteList)注册。", pluginName);
141-
#endif
142-
return nil;
143-
}
132+
- (void)registerPluginSecurityInfomation:(NSArray<NSString *>*)secInfo {
133+
self.secInfo = secInfo;
134+
}
135+
136+
- (void)pluginSecurityVerityWithCommand:(WKMsgCommand *)command completionHandler:(void (^)(BOOL passed))completionHandler {
137+
if (self.securityPlugin && [self.securityPlugin respondsToSelector:@selector(applyAuthorityWithService:action:securityInfomation:complete:)]) {
138+
[self.securityPlugin applyAuthorityWithService:command.className action:command.methodName securityInfomation:self.secInfo complete:^(BOOL allowed) {
139+
completionHandler(allowed);
140+
}];
144141
}else {
145-
className = pluginName;
142+
completionHandler(YES);
146143
}
144+
}
145+
146+
- (id)getCommandInstance:(NSString*)pluginName {
147147

148148
WKBasePlugin <WKPluginProtocol> *plugin = nil;
149149
Class implClass = [self pluginImplClass:pluginName];
150150

151151
BOOL shouldCache = [self pluginShouldCache:implClass];
152152
if (shouldCache) {
153-
plugin = [self.pluginsByName objectForKey:className];
153+
plugin = [self.pluginsByName objectForKey:pluginName];
154154
if (plugin) {
155155
return plugin;
156156
}
@@ -166,11 +166,11 @@ - (id)getCommandInstance:(NSString*)pluginName {
166166
if (plugin) {
167167
[self pluginInstall:plugin];
168168
if (shouldCache) {
169-
[self.pluginsByName setObject:plugin forKey:className];
169+
[self.pluginsByName setObject:plugin forKey:pluginName];
170170
}
171171
}else {
172172
#ifdef DEBUG
173-
NSLog(@"WKJavaScriptBridge Error : 模块: (%@) 不存在。", pluginName);
173+
NSLog(@"PHJavaScriptBridge Error : 模块: (%@) 不存在。", pluginName);
174174
#endif
175175
}
176176
return plugin;
@@ -181,11 +181,11 @@ - (id)getCommandInstance:(NSString*)pluginName {
181181
if (plugin) {
182182
[self pluginInstall:plugin];
183183
if (shouldCache) {
184-
[self.pluginsByName setObject:plugin forKey:className];
184+
[self.pluginsByName setObject:plugin forKey:pluginName];
185185
}
186186
}else {
187187
#ifdef DEBUG
188-
NSLog(@"WKJavaScriptBridge Error : 模块: (%@) 不存在。", pluginName);
188+
NSLog(@"PHJavaScriptBridge Error : 模块: (%@) 不存在。", pluginName);
189189
#endif
190190
}
191191
return plugin;
@@ -220,20 +220,6 @@ - (void)userContentController:(WKUserContentController *)userContentController d
220220

221221
#pragma mark - Set or Get
222222

223-
- (NSRecursiveLock *)lock {
224-
if (!_lock) {
225-
_lock = [[NSRecursiveLock alloc] init];
226-
}
227-
return _lock;
228-
}
229-
230-
- (NSMutableDictionary *)whiteList {
231-
if (!_whiteList) {
232-
_whiteList = [[NSMutableDictionary alloc] initWithCapacity:1];
233-
}
234-
return _whiteList;
235-
}
236-
237223
- (NSMutableDictionary *)pluginsByName {
238224
if (!_pluginsByName) {
239225
_pluginsByName = [[NSMutableDictionary alloc] initWithCapacity:1];

0 commit comments

Comments
 (0)