UNPKG

react-native-security-checker

Version:

A comprehensive React Native security checker that detects jailbreak, root, emulators, hooks, tampering, and other security threats

175 lines (155 loc) 5.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSecurityChecker = useSecurityChecker; var _react = require("react"); var _reactNative = require("react-native"); var _index = require("./index.js"); /** * Custom hook for continuous security monitoring * * @param onSecurityIssue - Callback function called when security issues are detected * @param interval - Interval in milliseconds for periodic checks (default: 5000) * @param options - Additional configuration options * @returns Security checker state and control functions */ function useSecurityChecker(onSecurityIssue, interval = 5000, options = {}) { const { config, checkOnBackground = true, checkOnForeground = true, startImmediately = true, enableContinuousMonitoring = true } = options; // State const [state, setState] = (0, _react.useState)({ result: null, isChecking: false, lastError: null, isMonitoring: false, checkCount: 0, lastCheckTime: null }); // Refs for cleanup const intervalRef = (0, _react.useRef)(null); const appStateRef = (0, _react.useRef)(_reactNative.AppState.currentState); const isCheckingRef = (0, _react.useRef)(false); const isMonitoringRef = (0, _react.useRef)(false); const startMonitoringRef = (0, _react.useRef)(() => {}); const stopMonitoringRef = (0, _react.useRef)(() => {}); const handleAppStateChangeRef = (0, _react.useRef)(() => {}); // Security check function const checkSecurity = (0, _react.useCallback)(async () => { if (isCheckingRef.current) return; // Prevent multiple simultaneous checks isCheckingRef.current = true; setState(prev => ({ ...prev, isChecking: true, lastError: null })); try { const result = await (0, _index.detectEnvironment)(config); setState(prev => ({ ...prev, result, isChecking: false, checkCount: prev.checkCount + 1, lastCheckTime: Date.now() })); // Call callback if security issues are detected if (result.isInsecureEnvironment) { onSecurityIssue(result); } } catch (error) { const errorObj = error instanceof Error ? error : new Error(String(error)); setState(prev => ({ ...prev, isChecking: false, lastError: errorObj })); } finally { isCheckingRef.current = false; } }, [config, onSecurityIssue]); // Start monitoring const startMonitoring = (0, _react.useCallback)(() => { if (isMonitoringRef.current) return; // Prevent multiple starts isMonitoringRef.current = true; setState(prev => ({ ...prev, isMonitoring: true })); // Start interval if continuous monitoring is enabled if (enableContinuousMonitoring && interval > 0) { intervalRef.current = setInterval(checkSecurity, interval); } }, [enableContinuousMonitoring, interval, checkSecurity]); // Store the latest function reference startMonitoringRef.current = startMonitoring; // Stop monitoring const stopMonitoring = (0, _react.useCallback)(() => { isMonitoringRef.current = false; setState(prev => ({ ...prev, isMonitoring: false })); if (intervalRef.current) { clearInterval(intervalRef.current); intervalRef.current = null; } }, []); // Store the latest function reference stopMonitoringRef.current = stopMonitoring; // Reset state const reset = (0, _react.useCallback)(() => { stopMonitoring(); isCheckingRef.current = false; setState({ result: null, isChecking: false, lastError: null, isMonitoring: false, checkCount: 0, lastCheckTime: null }); }, [stopMonitoring]); // App state change handler const handleAppStateChange = (0, _react.useCallback)(nextAppState => { const currentState = appStateRef.current; appStateRef.current = nextAppState; // Check on background if (checkOnBackground && currentState === 'active' && nextAppState === 'background') { checkSecurity(); } // Check on foreground if (checkOnForeground && currentState === 'background' && nextAppState === 'active') { checkSecurity(); } }, [checkOnBackground, checkOnForeground, checkSecurity]); // Store the latest function reference handleAppStateChangeRef.current = handleAppStateChange; // Setup effects - single useEffect with no dependencies to prevent loops (0, _react.useEffect)(() => { // Start monitoring if enabled if (startImmediately) { startMonitoringRef.current?.(); } // Setup app state listener const appStateSubscription = _reactNative.AppState.addEventListener('change', nextAppState => { handleAppStateChangeRef.current?.(nextAppState); }); return () => { appStateSubscription?.remove(); stopMonitoringRef.current?.(); }; }, []); // Empty dependency array - only run once return { state, checkSecurity, startMonitoring, stopMonitoring, reset }; } //# sourceMappingURL=useSecurityChecker.js.map