Thibault Malbranche 5 years ago
parent
commit
5017ba3a39
4 changed files with 17 additions and 68 deletions
  1. 7
    11
      src/WebView.android.tsx
  2. 1
    6
      src/WebView.ios.tsx
  3. 8
    41
      src/WebViewShared.ts
  4. 1
    10
      src/WebViewTypes.ts

+ 7
- 11
src/WebView.android.tsx View File

14
 import invariant from 'invariant';
14
 import invariant from 'invariant';
15
 
15
 
16
 import {
16
 import {
17
-  defaultOriginWhitelist,
18
   createOnShouldStartLoadWithRequest,
17
   createOnShouldStartLoadWithRequest,
19
   getViewManagerConfig,
18
   getViewManagerConfig,
20
 } from './WebViewShared';
19
 } from './WebViewShared';
57
     saveFormDataDisabled: false,
56
     saveFormDataDisabled: false,
58
     cacheEnabled: true,
57
     cacheEnabled: true,
59
     androidHardwareAccelerationDisabled: false,
58
     androidHardwareAccelerationDisabled: false,
60
-    originWhitelist: defaultOriginWhitelist,
61
   };
59
   };
62
 
60
 
63
   static isFileUploadSupported = async () => {
61
   static isFileUploadSupported = async () => {
207
     shouldStart: boolean,
205
     shouldStart: boolean,
208
     url: string,
206
     url: string,
209
   ) => {
207
   ) => {
210
-    if (shouldStart) {
211
-      UIManager.dispatchViewManagerCommand(
212
-        this.getWebViewHandle(),
213
-        this.getCommands().loadUrl,
214
-        [String(url)],
215
-      );
208
+    if (!shouldStart) {
209
+      return;
216
     }
210
     }
211
+    UIManager.dispatchViewManagerCommand(
212
+      this.getWebViewHandle(),
213
+      this.getCommands().loadUrl,
214
+      [String(url)],
215
+    );
217
   };
216
   };
218
 
217
 
219
   render() {
218
   render() {
220
     const {
219
     const {
221
       onMessage,
220
       onMessage,
222
       onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
221
       onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
223
-      originWhitelist,
224
       renderError,
222
       renderError,
225
       renderLoading,
223
       renderLoading,
226
       source,
224
       source,
268
 
266
 
269
     const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
267
     const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
270
       this.onShouldStartLoadWithRequestCallback,
268
       this.onShouldStartLoadWithRequestCallback,
271
-      // casting cause it's in the default props
272
-      originWhitelist as ReadonlyArray<string>,
273
       onShouldStartLoadWithRequestProp,
269
       onShouldStartLoadWithRequestProp,
274
     );
270
     );
275
 
271
 

+ 1
- 6
src/WebView.ios.tsx View File

13
 import invariant from 'invariant';
13
 import invariant from 'invariant';
14
 
14
 
15
 import {
15
 import {
16
-  defaultOriginWhitelist,
17
   createOnShouldStartLoadWithRequest,
16
   createOnShouldStartLoadWithRequest,
18
   getViewManagerConfig,
17
   getViewManagerConfig,
19
 } from './WebViewShared';
18
 } from './WebViewShared';
82
   static defaultProps = {
81
   static defaultProps = {
83
     useWebKit: true,
82
     useWebKit: true,
84
     cacheEnabled: true,
83
     cacheEnabled: true,
85
-    originWhitelist: defaultOriginWhitelist,
86
     useSharedProcessPool: true,
84
     useSharedProcessPool: true,
87
   };
85
   };
88
 
86
 
299
       viewManager = viewManager || RNCUIWebViewManager;
297
       viewManager = viewManager || RNCUIWebViewManager;
300
     }
298
     }
301
     invariant(viewManager != null, 'viewManager expected to be non-null');
299
     invariant(viewManager != null, 'viewManager expected to be non-null');
302
-    viewManager.startLoadWithResult(!!shouldStart, lockIdentifier);
300
+    viewManager.startLoadWithResult(shouldStart, lockIdentifier);
303
   };
301
   };
304
 
302
 
305
   componentDidUpdate(prevProps: IOSWebViewProps) {
303
   componentDidUpdate(prevProps: IOSWebViewProps) {
336
       nativeConfig = {},
334
       nativeConfig = {},
337
       onMessage,
335
       onMessage,
338
       onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
336
       onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
339
-      originWhitelist,
340
       renderError,
337
       renderError,
341
       renderLoading,
338
       renderLoading,
342
       scalesPageToFit = this.props.useWebKit ? undefined : true,
339
       scalesPageToFit = this.props.useWebKit ? undefined : true,
374
 
371
 
375
     const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
372
     const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
376
       this.onShouldStartLoadWithRequestCallback,
373
       this.onShouldStartLoadWithRequestCallback,
377
-      // casting cause it's in the default props
378
-      originWhitelist as ReadonlyArray<string>,
379
       onShouldStartLoadWithRequestProp,
374
       onShouldStartLoadWithRequestProp,
380
     );
375
     );
381
 
376
 

+ 8
- 41
src/WebViewShared.ts View File

1
-import escapeStringRegexp from 'escape-string-regexp';
2
-import { Linking, UIManager as NotTypedUIManager } from 'react-native';
1
+import { UIManager as NotTypedUIManager } from 'react-native';
3
 import {
2
 import {
4
   WebViewNavigationEvent,
3
   WebViewNavigationEvent,
5
   OnShouldStartLoadWithRequest,
4
   OnShouldStartLoadWithRequest,
8
 
7
 
9
 const UIManager = NotTypedUIManager as CustomUIManager;
8
 const UIManager = NotTypedUIManager as CustomUIManager;
10
 
9
 
11
-const defaultOriginWhitelist = ['http://*', 'https://*'];
12
-
13
-const extractOrigin = (url: string): string => {
14
-  const result = /^[A-Za-z][A-Za-z0-9+\-.]+:(\/\/)?[^/]*/.exec(url);
15
-  return result === null ? '' : result[0];
16
-};
17
-
18
-const originWhitelistToRegex = (originWhitelist: string): string =>
19
-  `^${escapeStringRegexp(originWhitelist).replace(/\\\*/g, '.*')}`;
20
-
21
-const passesWhitelist = (
22
-  compiledWhitelist: ReadonlyArray<string>,
23
-  url: string,
24
-) => {
25
-  const origin = extractOrigin(url);
26
-  return compiledWhitelist.some(x => new RegExp(x).test(origin));
27
-};
28
-
29
-const compileWhitelist = (
30
-  originWhitelist: ReadonlyArray<string>,
31
-): ReadonlyArray<string> =>
32
-  ['about:blank', ...(originWhitelist || [])].map(originWhitelistToRegex);
33
-
34
 const createOnShouldStartLoadWithRequest = (
10
 const createOnShouldStartLoadWithRequest = (
35
   loadRequest: (
11
   loadRequest: (
36
     shouldStart: boolean,
12
     shouldStart: boolean,
37
     url: string,
13
     url: string,
38
     lockIdentifier: number,
14
     lockIdentifier: number,
39
   ) => void,
15
   ) => void,
40
-  originWhitelist: ReadonlyArray<string>,
41
   onShouldStartLoadWithRequest?: OnShouldStartLoadWithRequest,
16
   onShouldStartLoadWithRequest?: OnShouldStartLoadWithRequest,
42
 ) => {
17
 ) => {
43
   return ({ nativeEvent }: WebViewNavigationEvent) => {
18
   return ({ nativeEvent }: WebViewNavigationEvent) => {
44
-    let shouldStart = true;
45
     const { url, lockIdentifier } = nativeEvent;
19
     const { url, lockIdentifier } = nativeEvent;
46
 
20
 
47
-    if (!passesWhitelist(compileWhitelist(originWhitelist), url)) {
48
-      Linking.openURL(url);
49
-      shouldStart = false;
50
-    }
51
-
52
-    if (onShouldStartLoadWithRequest) {
53
-      shouldStart = onShouldStartLoadWithRequest(nativeEvent);
21
+    if (
22
+      onShouldStartLoadWithRequest
23
+      && !onShouldStartLoadWithRequest(nativeEvent)
24
+    ) {
25
+      loadRequest(false, url, lockIdentifier);
54
     }
26
     }
55
-
56
-    loadRequest(shouldStart, url, lockIdentifier);
27
+    loadRequest(true, url, lockIdentifier);
57
   };
28
   };
58
 };
29
 };
59
 
30
 
66
   return UIManager.getViewManagerConfig(viewManagerName);
37
   return UIManager.getViewManagerConfig(viewManagerName);
67
 };
38
 };
68
 
39
 
69
-export {
70
-  defaultOriginWhitelist,
71
-  createOnShouldStartLoadWithRequest,
72
-  getViewManagerConfig,
73
-};
40
+export { createOnShouldStartLoadWithRequest, getViewManagerConfig };

+ 1
- 10
src/WebViewTypes.ts View File

182
 export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
182
 export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
183
 
183
 
184
 export interface ViewManager {
184
 export interface ViewManager {
185
-  startLoadWithResult: Function;
185
+  startLoadWithResult: (shouldStart: boolean, lockIdentifier: number) => void;
186
 }
186
 }
187
 
187
 
188
 export interface WebViewNativeConfig {
188
 export interface WebViewNativeConfig {
591
    */
591
    */
592
   mediaPlaybackRequiresUserAction?: boolean;
592
   mediaPlaybackRequiresUserAction?: boolean;
593
 
593
 
594
-  /**
595
-   * List of origin strings to allow being navigated to. The strings allow
596
-   * wildcards and get matched against *just* the origin (not the full URL).
597
-   * If the user taps to navigate to a new page but the new page is not in
598
-   * this whitelist, we will open the URL in Safari.
599
-   * The default whitelisted origins are "http://*" and "https://*".
600
-   */
601
-  originWhitelist?: ReadonlyArray<string>;
602
-
603
   /**
594
   /**
604
    * Function that allows custom handling of any web view requests. Return
595
    * Function that allows custom handling of any web view requests. Return
605
    * `true` from the function to continue loading the request and `false`
596
    * `true` from the function to continue loading the request and `false`