A React application that demonstrates iframe detection functionality with security guards and access control.
- π Iframe Detection: Automatically detects if the app is running inside an iframe
- π‘οΈ Security Guard: Restricts access based on iframe context
- π§ Development Mode: Bypasses restrictions in development for easier testing
- π Referrer Validation: Optional validation of allowed parent domains
- π¨ Modern UI: Clean, responsive design with real-time status indicators
react-iframe-secure-embed/
βββ src/
β βββ components/
β β βββ IframeGuard.tsx # Main guard component
β β βββ AccessBlocked.tsx # Access denied screen
β βββ hooks/
β β βββ useIframeDetection.ts # Iframe detection logic
β βββ App.tsx # Main application
β βββ App.css # Component styles
β βββ index.tsx # React entry point
β βββ index.css # Global styles
βββ public/
β βββ index.html # HTML template
βββ package.json # Dependencies
βββ tsconfig.json # TypeScript config
βββ README.md # This file
The custom hook uses multiple detection methods:
- Window comparison: Checks if
window !== window.top - Parent validation: Verifies
window.parent !== window - Referrer checking: Validates
document.referrer - Cross-origin detection: Attempts to access parent properties
The guard component provides:
- Loading state: Shows verification message during detection
- Error handling: Displays security errors if detection fails
- Development bypass: Allows access in development mode
- Referrer validation: Checks allowed parent domains
- Access blocking: Shows blocked message for unauthorized access
Professional access denied screen with:
- Clear warning icon and message
- Status code indication
- Responsive design
- Customizable messaging
-
Install dependencies:
cd react-iframe-secure-embed npm install -
Start development server:
npm start
-
Build for production:
npm run build
import { IframeGuard } from './components/IframeGuard';
function App() {
return (
<IframeGuard>
<YourProtectedContent />
</IframeGuard>
);
}<IframeGuard
bypassInDevelopment={true}
allowedReferrers={['example.com', 'trusted-domain.com']}
customBlockedMessage="Custom access denied message"
>
<YourProtectedContent />
</IframeGuard>import { useIframeDetection } from './hooks/useIframeDetection';
function MyComponent() {
const { isInIframe, isLoading, error } = useIframeDetection();
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
return (
<div>
{isInIframe ? 'Running in iframe' : 'Running standalone'}
</div>
);
}| Prop | Type | Default | Description |
|---|---|---|---|
children |
ReactNode |
- | Content to protect |
allowedReferrers |
string[] |
[] |
Allowed parent domains |
bypassInDevelopment |
boolean |
true |
Skip restrictions in dev mode |
customBlockedMessage |
string |
- | Custom access denied message |
| Property | Type | Description |
|---|---|---|
isInIframe |
boolean |
Whether app is in iframe |
isLoading |
boolean |
Detection in progress |
error |
string | null |
Error message if detection fails |
- Run
npm start- access is allowed by default - Check console for detection logs
- Build the app:
npm run build - Serve the build:
npx serve -s build - Direct access will be blocked
- Create an HTML file with iframe:
<!DOCTYPE html>
<html>
<head>
<title>Iframe Test</title>
</head>
<body>
<h1>Testing Iframe Detection</h1>
<iframe
src="http://localhost:3000"
width="800"
height="600"
style="border: 1px solid #ccc;">
</iframe>
</body>
</html>- Cross-origin safety: Handles cross-origin iframe restrictions
- Multiple detection methods: Reduces false positives/negatives
- Referrer validation: Optional additional security layer
- Development bypass: Configurable for different environments
- Error handling: Graceful fallbacks for detection failures
- β Chrome 60+
- β Firefox 55+
- β Safari 12+
- β Edge 79+
MIT License - feel free to use in your projects!
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
If you encounter any issues or have questions:
- Check the console for detection logs
- Verify your iframe setup
- Test in different browsers
- Check network/security settings
Note: This is a demo application showcasing iframe detection techniques. Adapt the security measures according to your specific requirements and threat model.