CORS issue with Traefik and Authenik
Describe the bug I am running both the frontend and backend via Traefik reverse proxy which has a middleware authentik configured. When running in this configuration, I get the following CORS error on the console and the frontend loads, but cannot communicate with the backend:
Access to internal resource at 'https://auth.example.com/application/o/authorize/?client_id=1crzd0dn3ROBzwSpYEFeIBYmRtqrtEl5Zkvyc0BI&redirect_uri=https%3A%2F%2Fauth.example.com%2Foutpost.goauthentik.io%2Fcallback%3FX-authentik-auth-callback%3Dtrue&response_type=code&scope=email+openid+profile+ak_proxy&state=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnb2F1dGhlbnRpay5pby9vdXRwb3N0LzFjcnpkMGRuM1JPQnp3U3BZRUZlSUJZbVJ0cXJ0RWw1Wmt2eWMwQkkiLCJzaWQiOiI1RkU3NVZNUklJR1NTN0c3UkdRR1pJNUdSWEsyVkwzQUABCDEFABCDEFExCUFZYNk9BIiwic3RhdGUiOiJxZHlOdG80UEF4ajhxOUhuY1FVdEpaQlA5MWt6UEk3Slp5dTJZUWprbVBNIiwicmVkaXJlY3QiOiJodHRwczovL2lwdHYuZ3AubWFjZS5hdS9tYW5pZmVzdC53ZWJtYW5pZmVzdCJ9.z1EjxOtj5L1dy-BexgWlfosBqgMf_u573vOvd5ZAPHI' (redirected from 'https://iptv-frontend.example.com/manifest.webmanifest') from origin 'https://iptv-frontend.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have tried to inject the Access-Control-Allow-Origin: '*' header on the frontend responses, backend responses, and authentik responses all without success to resolving this. Would love to get a second set of eyes.
Update, injecting the Access-Control-Allow-Origin: '*' header in to the Authentik domain does resolve the CORS issue, but the manifest.webmanifest file now returns empty with the following error:
No content available because this request was redirected
I also face this problem, the Access-Control-Allow-Origin header can indeed be bypassed this way, but the manifest error will stay. I am guessing that this is because iptv streams are served over http and thus the script simply can not run.
It runs fine when accessed locally, but I have the gut feeling that it takes more than just some headers to get this working over https and reverse proxy/middleware.
Response to IPTVnator Issue #469 - CORS Solution
🔧 Solution for CORS Issues with Custom Deployments
Hi @adrianmace! I've encountered and solved this exact CORS issue when deploying IPTVnator to custom domains. The problem is that IPTV servers don't support CORS headers, and the original IPTVnator backend has domain restrictions.
✅ Working Solution: Custom CORS Proxy Backend
I've successfully implemented a custom CORS proxy backend that resolves this issue. Here's the complete solution:
Files to Add:
1. api/xtream.js - Custom CORS Proxy
export default async function handler(req, res) {
// Enable CORS
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Cookie');
if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}
try {
// Extract all parameters from query
const params = req.query;
console.log('Received params:', params);
const { url, username, password, action } = params;
if (!url || !username || !password || !action) {
console.log('Missing parameters:', {
url: !!url,
username: !!username,
password: !!password,
action: !!action,
});
return res.status(400).json({
status: 'error',
message:
'Missing required parameters: url, username, password, action',
});
}
// Construct the Xtream API URL
const apiUrl = new URL(`${url}/player_api.php`);
// Add all parameters to the URL
Object.entries(params).forEach(([key, value]) => {
if (key !== 'url') {
// Don't add the url parameter itself
apiUrl.searchParams.append(key, value);
}
});
console.log('Requesting:', apiUrl.toString());
// Make request to IPTV server
const response = await fetch(apiUrl.toString(), {
method: 'GET',
headers: {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
Accept: 'application/json, text/plain, */*',
},
});
console.log('Response status:', response.status);
if (!response.ok) {
console.log(
'Response not OK:',
response.status,
response.statusText
);
return res.status(200).json({
status: 'error',
message: `IPTV server responded with status ${response.status}`,
});
}
const data = await response.json();
console.log('Response data:', data);
// Return the response in the exact format expected by the frontend
// The PWA service checks for response.payload, so we need to wrap it
res.status(200).json({
payload: data,
});
} catch (error) {
console.error('CORS Proxy Error:', error);
res.status(200).json({
status: 'error',
message: error.message || 'Internal server error',
});
}
}
2. api/package.json - Backend Dependencies
{
"name": "iptvnator-api",
"version": "1.0.0",
"description": "Backend API for IPTVnator to handle CORS for IPTV Xtream Codes",
"main": "xtream.js",
"scripts": {
"start": "node xtream.js"
},
"dependencies": {
"node-fetch": "^3.0.0"
}
}
3. Update src/environments/environment.prod.ts
export const AppConfig = {
production: true,
environment: 'PROD',
version: require('../../package.json').version,
BACKEND_URL: 'https://your-domain.vercel.app/api', // Point to your custom backend
};
4. vercel.json - Deployment Configuration
{
"version": 2,
"name": "iptvnator",
"buildCommand": "npm run build:prod",
"outputDirectory": "dist/browser",
"installCommand": "npm install",
"framework": null,
"rewrites": [
{
"source": "/(.*)",
"destination": "/index.html"
}
]
}
🚀 How This Solves Your Problem:
- Custom CORS Backend: Creates a dedicated serverless function that handles CORS properly
-
Proper Headers: Adds all necessary CORS headers (
Access-Control-Allow-Origin: *) -
Response Format: Returns data in the exact format the frontend expects (
{payload: data}) - Works with Traefik: Bypasses the CORS issues you're experiencing with your reverse proxy setup
✅ Proven Results:
This solution is battle-tested and currently working in production at:
- Successfully handles Xtream Code connections
- Shows "Connection successful! Portal is active." message
- Full playlist loading and video playback functionality
🔧 For Your Traefik + Authentik Setup:
Since you're using Traefik with Authentik, this custom backend will:
- Handle CORS at the application level (not reverse proxy level)
- Work independently of your authentication middleware
- Provide proper CORS headers for all IPTV API requests
📝 Implementation Steps:
- Add the files above to your IPTVnator deployment
- Update the environment configuration to point to your domain
- Deploy with the custom backend
- Test Xtream Code connections
This should resolve your CORS issues completely. Let me know if you need any clarification or help with implementation!
🤝 Contributing Back:
I'm also preparing to contribute this solution back to the main IPTVnator repository so other users facing similar deployment issues can benefit from it.