From 2785aeff0f5bebcd78d86be55488396674e0609d Mon Sep 17 00:00:00 2001 From: deltaaa00 Date: Tue, 17 Nov 2026 22:50:34 +0200 Subject: [PATCH] Default to local mode and keep hosted OAuth redirects server-only --- example.env | 3 ++- main.py | 2 +- readme.md | 10 ++++++++-- tests/test_youtube_upload.py | 19 +++++++++++++++++++ youtube_upload.py | 4 ++++ 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/example.env b/example.env index 6da399c..43e4e92 100644 --- a/example.env +++ b/example.env @@ -14,7 +14,8 @@ GEMINI_API_KEY=your_google_ai_studio_key DEEPSEEK_API_KEY=your_deepseek_api_key -SERVER=TRUE +# Local mode is the default. Set TRUE only in the server deployment .env. +SERVER=FALSE WEB_LOGIN=deltaaa WEB_PASSWORD=deltaaa00! # Optional stable session signing secret; otherwise generated in instance/. diff --git a/main.py b/main.py index 41afead..58e253a 100644 --- a/main.py +++ b/main.py @@ -104,7 +104,7 @@ def __getattr__(self, name): requests = _PooledRequests() -WORKER_URL = (os.environ.get("YTDLP_URL") or "http://yt-dlp-worker:8080").rstrip('/') +WORKER_URL = (os.environ.get("YTDLP_URL") or "http://127.0.0.1:8080").rstrip('/') def _available_fonts(): diff --git a/readme.md b/readme.md index 4236b1a..de1d610 100644 --- a/readme.md +++ b/readme.md @@ -333,13 +333,19 @@ if png is not None: | --- | --- | | `GEMINI_API_KEY` | Server key for Gemini/Gemma models | | `DEEPSEEK_API_KEY` | Server key for DeepSeek models | -| `SERVER` | Exact `TRUE` enables Waitress and web authentication | +| `SERVER` | Defaults to local mode (`FALSE`). Exact `TRUE` enables Waitress and web authentication | | `WEB_LOGIN` | Login name; default `deltaaa` | | `WEB_PASSWORD` | Login password; default `deltaaa00!` | | `WEB_SECRET_KEY` | Optional stable session-signing secret | | `WEB_AUTH_SECRET_FILE` | Native deployment override for the generated secret file | | `PORT` | Native service port; web default `5000`, worker default `8080` | -| `YTDLP_URL` | Web-to-worker URL; default `http://yt-dlp-worker:8080` | +| `YTDLP_URL` | Native default `http://127.0.0.1:8080`; Docker Compose sets `http://yt-dlp-worker:8080` | + +Local mode is enabled by default, including in `example.env`. Its YouTube callback +uses the current local host and port (`localhost:3000` with Compose, or +`localhost:5000` for a native web launch), ignoring a leftover hosted callback. +The server keeps `SERVER=TRUE` and its public `YOUTUBE_REDIRECT_URI` in its own +`.env`; these deployment values survive GitHub Actions updates. Login sessions are valid for up to a year. Changing the credentials or signing secret invalidates existing sessions. Without `WEB_SECRET_KEY`, a secret is generated in `instance/web-session-secret`; keep that file across native restarts or retain the Compose session volume. For Compose, add an explicit environment entry to use `WEB_AUTH_SECRET_FILE`. Compose already fixes `YTDLP_URL` to the private worker address; edit that entry only if you change the service layout. diff --git a/tests/test_youtube_upload.py b/tests/test_youtube_upload.py index cbcba59..6c455d1 100644 --- a/tests/test_youtube_upload.py +++ b/tests/test_youtube_upload.py @@ -255,3 +255,22 @@ def test_csrf_rejection_identifies_safe_to_retry_requests(web, tmp_path): response = client.post('/api/youtube/disconnect', headers={'X-YouTube-CSRF': 'stale'}) assert response.status_code == 403 and response.json['code'] == 'youtube_csrf_invalid' assert client.post('/api/youtube/disconnect', headers=headers).status_code == 200 + + +@pytest.mark.parametrize('origin', ['http://localhost:3000', 'http://127.0.0.1:5000']) +def test_local_mode_uses_local_callback_even_with_hosted_override(web, monkeypatch, origin): + web.app.config['WEB_AUTH_ENABLED'] = False + web.app.config['YOUTUBE_REDIRECT_URI'] = 'https://hosted.example/api/youtube/callback' + monkeypatch.setenv('YOUTUBE_REDIRECT_URI', 'https://old-host.example/api/youtube/callback') + with web.app.test_request_context(base_url=origin): + assert web.youtube_uploads.callback_uri() == origin + '/api/youtube/callback' + + +def test_server_mode_keeps_external_callback_behind_reverse_proxy(web, monkeypatch): + web.app.config['WEB_AUTH_ENABLED'] = True + monkeypatch.setenv('YOUTUBE_REDIRECT_URI', 'https://auto-map.duckdns.org/api/youtube/callback') + with web.app.test_request_context(base_url='http://localhost:5000'): + assert web.youtube_uploads.callback_uri() == 'https://auto-map.duckdns.org/api/youtube/callback' + web.app.config['YOUTUBE_REDIRECT_URI'] = 'https://configured.example/api/youtube/callback' + with web.app.test_request_context(base_url='http://localhost:5000'): + assert web.youtube_uploads.callback_uri() == 'https://configured.example/api/youtube/callback' diff --git a/youtube_upload.py b/youtube_upload.py index e1d9ec2..bfed143 100644 --- a/youtube_upload.py +++ b/youtube_upload.py @@ -104,6 +104,10 @@ def load_secret(self, path): raise ValueError('Saved YouTube credentials cannot be opened. Connect your account again.') from None def callback_uri(self): + # Local launches must return to their own host/port, even if a hosted + # callback was left in the environment from another deployment. + if not self.app.config.get('WEB_AUTH_ENABLED', False): + return url_for('youtube_callback', _external=True) return (self.app.config.get('YOUTUBE_REDIRECT_URI') or os.environ.get('YOUTUBE_REDIRECT_URI') or url_for('youtube_callback', _external=True))