-
Notifications
You must be signed in to change notification settings - Fork 740
feature: add sandbox e2e test #7236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
janicduplessis
wants to merge
7
commits into
develop
Choose a base branch
from
@janic/sandbox-e2e-test
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
273790f
feature: add sandbox security e2e test
janicduplessis cb15b50
feature: add native module and WebView sandbox e2e tests
janicduplessis e3c16bf
refactor: split WebView test into separate SandboxWebViewTest component
janicduplessis a9d790f
feature: add native module and WebView sandbox e2e tests
janicduplessis cda041e
ci: set SANDBOX_BRANCH env var for react-native-sandbox iOS fixes
janicduplessis 4fd04a0
chore: run prettier on sandbox e2e files
janicduplessis d65dabe
feature: remove native module sandbox test
janicduplessis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
| appId: ${APP_ID} | ||
| tags: | ||
| - security | ||
| - sandbox | ||
| - parallel | ||
| --- | ||
| - launchApp: | ||
| clearState: true | ||
| clearKeychain: true | ||
| arguments: | ||
| isE2ETest: true | ||
| - runFlow: ../../utils/Prepare.yaml | ||
| - extendedWaitUntil: | ||
| visible: | ||
| id: welcome-screen | ||
| timeout: 60000 | ||
| - openLink: rainbow://e2e/sandbox-test | ||
| - runFlow: | ||
| when: | ||
| visible: 'Open in .*' | ||
| platform: iOS | ||
| commands: | ||
| - tapOn: 'Open' | ||
| - extendedWaitUntil: | ||
| visible: | ||
| id: sandbox-test-results | ||
| timeout: 15000 | ||
| - assertVisible: 'SANDBOX_TEST_PASSED' |
This file contains hidden or 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 hidden or 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,105 @@ | ||
| export interface SandboxTestResult { | ||
| name: string; | ||
| passed: boolean; | ||
| detail: string; | ||
| } | ||
|
|
||
| export interface WebViewTests { | ||
| initialLoad: { promise: Promise<SandboxTestResult>; onError: () => void }; | ||
| jsNavigation: { promise: Promise<SandboxTestResult>; onMessage: (event: { nativeEvent: { data: string } }) => void }; | ||
| } | ||
|
|
||
| async function testHttpBlocked(): Promise<SandboxTestResult> { | ||
| try { | ||
| const response = await fetch('https://example.com'); | ||
| if (response.status === 500) { | ||
| return { name: 'http_blocked', passed: true, detail: 'blocked with status 500' }; | ||
| } | ||
| return { name: 'http_blocked', passed: false, detail: `unexpected status ${response.status}` }; | ||
| } catch (e) { | ||
| return { name: 'http_blocked', passed: false, detail: `unexpected error: ${(e as Error).message}` }; | ||
| } | ||
| } | ||
|
|
||
| async function testHttpAllowed(): Promise<SandboxTestResult> { | ||
| try { | ||
| const response = await fetch('https://rainbow.me'); | ||
| if (response.ok) { | ||
| return { name: 'http_allowed', passed: true, detail: `allowed with status ${response.status}` }; | ||
| } | ||
| return { name: 'http_allowed', passed: false, detail: `unexpected status ${response.status}` }; | ||
| } catch (e) { | ||
| return { name: 'http_allowed', passed: false, detail: `blocked with error: ${(e as Error).message}` }; | ||
| } | ||
| } | ||
|
|
||
| function testWsBlocked(): Promise<SandboxTestResult> { | ||
| return new Promise(resolve => { | ||
| let errorFired = false; | ||
|
|
||
| try { | ||
| const ws = new WebSocket('wss://example.com'); | ||
| ws.onopen = () => { | ||
| ws.close(); | ||
| resolve({ name: 'ws_blocked', passed: false, detail: 'connection opened' }); | ||
| }; | ||
| ws.onerror = () => { | ||
| errorFired = true; | ||
| resolve({ name: 'ws_blocked', passed: false, detail: 'not blocked (server rejected upgrade)' }); | ||
| }; | ||
| ws.onclose = () => { | ||
| if (errorFired) return; | ||
| resolve({ name: 'ws_blocked', passed: true, detail: 'blocked (connection closed)' }); | ||
| }; | ||
| } catch (e) { | ||
| resolve({ name: 'ws_blocked', passed: false, detail: `unexpected error: ${(e as Error).message}` }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Creates two WebView tests: | ||
| * 1. initialLoad — loads a blocked URL directly, expects onError | ||
| * 2. jsNavigation — loads an allowed page, attempts JS navigation to blocked URL, | ||
| * then reports current URL via onMessage. If blocked, URL stays on allowed domain. | ||
| */ | ||
| export function createWebViewTests(): WebViewTests { | ||
| let resolveInitial: (result: SandboxTestResult) => void; | ||
| const initialPromise = new Promise<SandboxTestResult>(r => { | ||
| resolveInitial = r; | ||
| }); | ||
|
|
||
| let resolveJs: (result: SandboxTestResult) => void; | ||
| const jsPromise = new Promise<SandboxTestResult>(r => { | ||
| resolveJs = r; | ||
| }); | ||
|
|
||
| return { | ||
| initialLoad: { | ||
| promise: initialPromise, | ||
| onError: () => { | ||
| resolveInitial({ name: 'webview_initial_blocked', passed: true, detail: 'blocked by onError' }); | ||
| }, | ||
| }, | ||
| jsNavigation: { | ||
| promise: jsPromise, | ||
| onMessage: (event: { nativeEvent: { data: string } }) => { | ||
| const url = event.nativeEvent.data; | ||
| const blocked = !url.includes('example.com'); | ||
| resolveJs({ | ||
| name: 'webview_js_nav_blocked', | ||
| passed: blocked, | ||
| detail: blocked ? `blocked: still on ${url}` : `not blocked: navigated to ${url}`, | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export async function runSandboxTests(): Promise<SandboxTestResult[]> { | ||
| const results: SandboxTestResult[] = []; | ||
| results.push(await testHttpBlocked()); | ||
| results.push(await testHttpAllowed()); | ||
| results.push(await testWsBlocked()); | ||
| return results; | ||
| } | ||
This file contains hidden or 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,48 @@ | ||
| import { StyleSheet, Text, View } from 'react-native'; | ||
|
|
||
| import { type SandboxTestResult } from '../core/sandboxSecurityTest'; | ||
|
|
||
| interface Props { | ||
| results: SandboxTestResult[]; | ||
| allDone: boolean; | ||
| } | ||
|
|
||
| export function SandboxSecurityResults({ results, allDone }: Props) { | ||
| const allPassed = allDone && results.every(r => r.passed); | ||
|
|
||
| return ( | ||
| <View style={sx.overlay} testID={allDone ? 'sandbox-test-results' : undefined}> | ||
| <Text style={sx.status} testID="sandbox-test-status"> | ||
| {!allDone ? 'SANDBOX_TEST_RUNNING' : allPassed ? 'SANDBOX_TEST_PASSED' : 'SANDBOX_TEST_FAILED'} | ||
| </Text> | ||
| {results.map(r => ( | ||
| <Text key={r.name} style={sx.result} testID={`sandbox-${r.name}`}> | ||
| {`${r.name}: ${r.passed ? 'PASS' : 'FAIL'} - ${r.detail}`} | ||
| </Text> | ||
| ))} | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const sx = StyleSheet.create({ | ||
| overlay: { | ||
| backgroundColor: 'white', | ||
| borderRadius: 12, | ||
| elevation: 9999, | ||
| left: 20, | ||
| padding: 16, | ||
| position: 'absolute', | ||
| right: 20, | ||
| top: 120, | ||
| zIndex: 9999, | ||
| }, | ||
| result: { | ||
| color: '#333', | ||
| fontSize: 13, | ||
| marginTop: 6, | ||
| }, | ||
| status: { | ||
| fontSize: 16, | ||
| fontWeight: 'bold', | ||
| }, | ||
| }); |
This file contains hidden or 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,47 @@ | ||
| import { StyleSheet, View } from 'react-native'; | ||
|
|
||
| import WebView from 'react-native-webview'; | ||
|
|
||
| import { type WebViewTests } from '../core/sandboxSecurityTest'; | ||
|
|
||
| // Navigates to a blocked domain after the allowed page loads | ||
| const NAVIGATE_JS = ` | ||
| setTimeout(function() { | ||
| window.location.href = 'https://example.com'; | ||
| }, 2000); | ||
| setTimeout(function() { | ||
| window.ReactNativeWebView.postMessage(window.location.href); | ||
| }, 4000); | ||
| true; | ||
| `; | ||
|
|
||
| export function SandboxWebViewTest({ initialLoad, jsNavigation }: WebViewTests) { | ||
| return ( | ||
| <View style={sx.container}> | ||
| <WebView source={{ uri: 'https://example.com' }} sandbox style={sx.webview} onError={initialLoad.onError} /> | ||
| <WebView | ||
| source={{ uri: 'https://rainbow.me' }} | ||
| sandbox | ||
| style={sx.webview} | ||
| injectedJavaScript={NAVIGATE_JS} | ||
| onMessage={jsNavigation.onMessage} | ||
| /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const sx = StyleSheet.create({ | ||
| container: { | ||
| flexDirection: 'row', | ||
| left: 20, | ||
| position: 'absolute', | ||
| right: 20, | ||
| top: 300, | ||
| }, | ||
| webview: { | ||
| borderColor: 'red', | ||
| borderWidth: 2, | ||
| flex: 1, | ||
| height: 200, | ||
| }, | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.