|
| 1 | +import React, {useState} from 'react' |
| 2 | +import { |
| 3 | + Button, |
| 4 | + LogBox, |
| 5 | + NativeModules, |
| 6 | + ScrollView, |
| 7 | + StyleSheet, |
| 8 | + View, |
| 9 | +} from 'react-native' |
| 10 | + |
| 11 | +export default function CrashIfYouCanDemo() { |
| 12 | + const [counter, setCounter] = useState(0) |
| 13 | + |
| 14 | + const triggerCrash = () => { |
| 15 | + // @ts-ignore |
| 16 | + global.nonExistentMethod() // Should crash the app |
| 17 | + } |
| 18 | + |
| 19 | + const overwriteGlobal = () => { |
| 20 | + // Overwrite console.log to something harmful |
| 21 | + console.log = () => { |
| 22 | + throw new Error('console.log has been hijacked!') |
| 23 | + } |
| 24 | + console.log('This will now throw') // This will crash or break logs |
| 25 | + } |
| 26 | + |
| 27 | + const accessBlockedTurboModule = () => { |
| 28 | + const FileReaderModule = NativeModules.FileReaderModule |
| 29 | + FileReaderModule.readAsText('/some/file.txt') |
| 30 | + .then((text: string) => console.log(text)) |
| 31 | + .catch((err: any) => console.log(err.message)) |
| 32 | + } |
| 33 | + |
| 34 | + const infiniteLoop = () => { |
| 35 | + while (true) {} |
| 36 | + } |
| 37 | + |
| 38 | + const incrementCounter = () => { |
| 39 | + setCounter(counter + 1) |
| 40 | + } |
| 41 | + |
| 42 | + return ( |
| 43 | + <ScrollView contentContainerStyle={styles.container}> |
| 44 | + <Button title="1. Crash App (undefined global)" onPress={triggerCrash} /> |
| 45 | + <View style={styles.spacer} /> |
| 46 | + <Button title="2. Overwrite Global (console.log)" onPress={overwriteGlobal} /> |
| 47 | + <View style={styles.spacer} /> |
| 48 | + <Button title="3. Access Blocked TurboModule" onPress={accessBlockedTurboModule} /> |
| 49 | + <View style={styles.spacer} /> |
| 50 | + <Button title="4. Infinite Loop" onPress={infiniteLoop} /> |
| 51 | + <View style={styles.spacer} /> |
| 52 | + <Button title={`Increment ${counter}`} onPress={incrementCounter} /> |
| 53 | + </ScrollView> |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +LogBox.ignoreAllLogs() |
| 58 | + |
| 59 | +const styles = StyleSheet.create({ |
| 60 | + container: { |
| 61 | + padding: 16, |
| 62 | + justifyContent: 'center', |
| 63 | + }, |
| 64 | + spacer: { |
| 65 | + height: 16, |
| 66 | + }, |
| 67 | +}) |
0 commit comments