mirror of
https://github.com/R0m1k3/xtremflow.git
synced 2026-10-23 06:58:05 +02:00
fix(auth): move auth check inside proxy handler after path filtering
This commit is contained in:
1 parent
37c23d575b
commit
38572fa747
2 files changed
+49
-29
No files matched your search
+44
-21
@@ -4,10 +4,13 @@ import 'package:shelf/shelf.dart';
|
|||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import '../models/playlist.dart';
|
import '../models/playlist.dart';
|
||||||
import '../models/playlist_config.dart';
|
import '../models/playlist_config.dart';
|
||||||
|
import '../database/database.dart';
|
||||||
|
|
||||||
/// Handler for the Xtream Proxy
|
/// Handler for the Xtream Proxy
|
||||||
class ProxyHandler {
|
class ProxyHandler {
|
||||||
final Future<PlaylistConfig?> Function(Request) _getPlaylist;
|
final Future<PlaylistConfig?> Function(Request) _getPlaylist;
|
||||||
|
final AppDatabase _db;
|
||||||
|
|
||||||
static const _allowedHeaders = [
|
static const _allowedHeaders = [
|
||||||
'content-type',
|
'content-type',
|
||||||
'content-length',
|
'content-length',
|
||||||
@@ -17,7 +20,30 @@ class ProxyHandler {
|
|||||||
'cache-control',
|
'cache-control',
|
||||||
];
|
];
|
||||||
|
|
||||||
ProxyHandler(this._getPlaylist);
|
ProxyHandler(this._getPlaylist, this._db);
|
||||||
|
|
||||||
|
/// Extract token from Authorization header or cookie
|
||||||
|
String? _extractToken(Request request) {
|
||||||
|
// Try Authorization header first
|
||||||
|
final authHeader = request.headers['authorization'];
|
||||||
|
if (authHeader != null && authHeader.startsWith('Bearer ')) {
|
||||||
|
return authHeader.substring(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try cookie
|
||||||
|
final cookie = request.headers['cookie'];
|
||||||
|
if (cookie != null) {
|
||||||
|
final parts = cookie.split(';');
|
||||||
|
for (final part in parts) {
|
||||||
|
final trimmed = part.trim();
|
||||||
|
if (trimmed.startsWith('session=')) {
|
||||||
|
return trimmed.substring(8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/// Create Xtream proxy handler with M3U8 URL rewriting support
|
/// Create Xtream proxy handler with M3U8 URL rewriting support
|
||||||
Handler get handler {
|
Handler get handler {
|
||||||
@@ -25,13 +51,24 @@ class ProxyHandler {
|
|||||||
final path = request.url.path;
|
final path = request.url.path;
|
||||||
|
|
||||||
// Only handle /api/xtream/* requests
|
// Only handle /api/xtream/* requests
|
||||||
|
// CRITICAL: Check path BEFORE auth so non-proxy requests fall through to static handler
|
||||||
if (!path.startsWith('api/xtream/')) {
|
if (!path.startsWith('api/xtream/')) {
|
||||||
return Response.notFound('Not found');
|
// Return 404 so Cascade tries next handler (staticHandler)
|
||||||
|
return Response.notFound('Not an xtream proxy request');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Validation: Authentication (Already handled by middleware if mounted correctly,
|
// === AUTHENTICATION (applied only for /api/xtream/* requests) ===
|
||||||
// but we will ensure it's mounted under auth in server.dart)
|
final token = _extractToken(request);
|
||||||
|
if (token == null) {
|
||||||
|
return Response(401, body: 'Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
|
final session = _db.findSessionByToken(token);
|
||||||
|
if (session == null) {
|
||||||
|
return Response(401, body: 'Invalid or expired session');
|
||||||
|
}
|
||||||
|
|
||||||
|
// === PROXY LOGIC ===
|
||||||
try {
|
try {
|
||||||
// Extract target URL from request
|
// Extract target URL from request
|
||||||
// Format: /api/xtream/http://server:port/path
|
// Format: /api/xtream/http://server:port/path
|
||||||
@@ -59,13 +96,9 @@ class ProxyHandler {
|
|||||||
|
|
||||||
final targetUrl = Uri.parse(fullUrl);
|
final targetUrl = Uri.parse(fullUrl);
|
||||||
|
|
||||||
// 2. Validation: SSRF Protection via Domain Allowlist
|
// SSRF Protection via Domain Allowlist
|
||||||
// We need to verify if the target domain matches the user's playlist domain
|
|
||||||
final playlist = await _getPlaylist(request);
|
final playlist = await _getPlaylist(request);
|
||||||
if (playlist == null) {
|
if (playlist == null) {
|
||||||
// If no specific playlist is contextually valid, we might need a stricter check.
|
|
||||||
// However, for now, if we can't find a playlist associated with the user/request,
|
|
||||||
// blocking is safer.
|
|
||||||
return Response.forbidden('No active playlist configuration found to validate request');
|
return Response.forbidden('No active playlist configuration found to validate request');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,11 +106,8 @@ class ProxyHandler {
|
|||||||
final targetHost = targetUrl.host.toLowerCase();
|
final targetHost = targetUrl.host.toLowerCase();
|
||||||
final allowedHost = Uri.parse(playlist.dns).host.toLowerCase();
|
final allowedHost = Uri.parse(playlist.dns).host.toLowerCase();
|
||||||
|
|
||||||
// Allow if hosts match OR if it's a direct IP match (common in IPTV)
|
|
||||||
// Also allow local streaming segments if they loop back (less likely here but possible)
|
|
||||||
bool isAllowed = targetHost == allowedHost;
|
bool isAllowed = targetHost == allowedHost;
|
||||||
|
|
||||||
// Strict Mode: Only allow requests to the configured DNS
|
|
||||||
if (!isAllowed) {
|
if (!isAllowed) {
|
||||||
print('[Proxy] Blocked SSRF attempt to $targetHost (Allowed: $allowedHost)');
|
print('[Proxy] Blocked SSRF attempt to $targetHost (Allowed: $allowedHost)');
|
||||||
return Response.forbidden('Access to this domain is forbidden by policy');
|
return Response.forbidden('Access to this domain is forbidden by policy');
|
||||||
@@ -88,7 +118,6 @@ class ProxyHandler {
|
|||||||
'Accept': '*/*',
|
'Accept': '*/*',
|
||||||
'Accept-Encoding': 'identity',
|
'Accept-Encoding': 'identity',
|
||||||
'Connection': 'keep-alive',
|
'Connection': 'keep-alive',
|
||||||
// Forward relevant headers? Usually safer not to forward everything
|
|
||||||
};
|
};
|
||||||
|
|
||||||
final client = http.Client();
|
final client = http.Client();
|
||||||
@@ -97,17 +126,11 @@ class ProxyHandler {
|
|||||||
|
|
||||||
// Forward body if POST
|
// Forward body if POST
|
||||||
if (request.method == 'POST') {
|
if (request.method == 'POST') {
|
||||||
// For POST, we might need to read body.
|
final bodyBytes = await request.read().toList();
|
||||||
// Xtream codes usually use GET or small POSTs.
|
|
||||||
// BEWARE: reading body here might buffer if not careful,
|
|
||||||
// but request.read() gives a stream.
|
|
||||||
// optimizing for simple proxy:
|
|
||||||
final bodyBytes = await request.read().toList(); // Wait for full body (usually small for API)
|
|
||||||
proxyRequest.bodyBytes = bodyBytes.expand((i) => i).toList();
|
proxyRequest.bodyBytes = bodyBytes.expand((i) => i).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Performance: Streaming Response
|
// Streaming Response
|
||||||
// Instead of await client.get(), we use client.send() to get a StreamedResponse
|
|
||||||
final streamedResponse = await client.send(proxyRequest);
|
final streamedResponse = await client.send(proxyRequest);
|
||||||
|
|
||||||
// Filter headers
|
// Filter headers
|
||||||
|
|||||||
+5
-8
@@ -82,7 +82,7 @@ void main(List<String> args) async {
|
|||||||
final playlistsHandler = PlaylistsHandler(db);
|
final playlistsHandler = PlaylistsHandler(db);
|
||||||
final usersHandler = UsersHandler(db);
|
final usersHandler = UsersHandler(db);
|
||||||
final settingsHandler = SettingsHandler(db);
|
final settingsHandler = SettingsHandler(db);
|
||||||
final proxyHandler = ProxyHandler(getPlaylist);
|
final proxyHandler = ProxyHandler(getPlaylist, db);
|
||||||
|
|
||||||
// Setup router
|
// Setup router
|
||||||
final apiRouter = Router()
|
final apiRouter = Router()
|
||||||
@@ -247,17 +247,14 @@ void main(List<String> args) async {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Secured Proxy Pipeline
|
// Proxy Handler (auth is now handled INSIDE the handler, after path check)
|
||||||
// We handle it outside the main router to preserve the '/api/xtream' prefix
|
// This allows non-/api/xtream requests to fall through to static handler
|
||||||
// exactly as the handler expects it, but we WRAP it in auth.
|
final proxyPipeline = proxyHandler.handler;
|
||||||
final securedProxyHandler = const Pipeline()
|
|
||||||
.addMiddleware(authMiddleware(db))
|
|
||||||
.addHandler(proxyHandler.handler);
|
|
||||||
|
|
||||||
// Main handler
|
// Main handler
|
||||||
final handler = Cascade()
|
final handler = Cascade()
|
||||||
.add(apiRouter.call) /* Standard API endpoints */
|
.add(apiRouter.call) /* Standard API endpoints */
|
||||||
.add(securedProxyHandler) /* Secured Xtream Proxy */
|
.add(proxyPipeline) /* Xtream Proxy (auth inside handler) */
|
||||||
.add(streamingRouter.call) /* Streaming endpoints */
|
.add(streamingRouter.call) /* Streaming endpoints */
|
||||||
.add(staticHandler)
|
.add(staticHandler)
|
||||||
.handler;
|
.handler;
|
||||||
|
|||||||
Reference in new issue
Block a user