feedBack/tests/test_audio_local_path.py
Bret Mogilefsky af2949677a
rename: slopsmith → feedBack, byron → got-feedBack (#537)
* Update GitHub repo references from feedback* to feedBack*

* rename: slopsmith -> feedBack, byron -> got-feedBack

Renames across the entire codebase:
- slopsmith/Slopsmith/SLOPSMITH/SlopSmith -> feedBack/FeedBack/FEEDBACK/FeedBack
- byron/Byron/Byrongamatos -> got-feedBack/got-feedBack/got-feedBack
- /home/byron/ -> /opt/got-feedBack/
- byron@ougsoft.com -> hi@got-feedBack.org
- github.com/byrongamatos/ -> github.com/got-feedback/
- com.byron. -> com.got-feedback.
- SLOPSMITH_ env vars -> FEEDBACK_ with backward-compat fallback
- Protocol/storage strings migrated with read-old/write-new pattern
- window.slopsmith JS API -> window.feedBack (canonical) + backward-compat alias

Refs: #rename-slopsmith

* rename: complete regen against current main + fix backward-compat alias

Regenerated the slopsmith->feedBack / byron->got-feedBack rename on top of
current main (3 commits had landed since the branch: #572/#554/#574),
resolving the four content conflicts in favour of main's newer content
(autoplay/auto-exit, accuracy-badge, Virtuoso re-home, feedpak badge).

Completion fixes on top of the mechanical rename:
- Re-apply rename to post-branch content the original rename never saw:
  window.slopsmith(.Tour) consumers in lessons.js / notifications.js /
  onboarding-tour.js, and the matching JS + python tests (autoplay_exit,
  progression_*, test_feedpak_extension FEEDBACK_* env vars). The test env
  vars now match server.py (which reads FEEDBACK_SYNC_STARTUP /
  FEEDBACK_SKIP_STARTUP_TASKS), so the sync-startup test exercises the real
  path again.
- Restore the window.slopsmith backward-compat alias dropped during conflict
  resolution, and move the bus aliases to AFTER the _feedBackExisting merge
  block so they reference the fully-assembled object (also fixes the
  loop_api.test.js API-surface regex, which the original PR latently broke).
- Drop the stray empty data/web_library.db (runtime DB lives in CONFIG_DIR)
  and gitignore it.
- Fix stale tone-source test: feed[dB]ack -> fee[dB]ack to match shipped
  source labels.

Verified locally (org CI billing-blocked): JS 819/819 pass; pytest 1669
passed / 1683 collected with 0 import errors; zero residual slopsmith/byron
except the two intentional window.slopsmith aliases.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* rename: implement advertised backward-compat + prune dead community plugins

Address gaps where PR #537's "Backward compatibility" section was advertised
but not implemented, and clean up the community plugin list.

Env vars (FEEDBACK_* canonical, legacy SLOPSMITH_* honoured):
- New lib/env_compat.py (getenv_compat / env_flag_compat) + tests. server.py
  (_env_flag + all FEEDBACK_* reads), diagnostics_hardware, gp2midi and
  tailwind_rebuild now resolve the legacy alias, so existing SLOPSMITH_UI /
  SLOPSMITH_PLUGINS_DIR / etc. deployments keep working.
- Fix the rename collapsing plugins/__init__.py and minigames/routes.py from
  `FEEDBACK_PLUGINS_DIR or SLOPSMITH_PLUGINS_DIR` into a redundant
  `FEEDBACK_ or FEEDBACK_` (the fallback was silently lost).

Storage (app.js update-channel):
- Read feedBack-update-channel, fall back to legacy slopsmith-update-channel,
  and clear the legacy key on write — so a user's update-channel preference
  survives the rename instead of resetting to "stable".

Community plugin list (README): the rename rewrote third-party repo URLs we
don't own. Probed every one; their owners never renamed, so:
- Restore the 13 live community plugins to their real slopsmith-* names.
- Prune 6 that are 404 to the public (topkoa splitscreen/stems, OmikronApex
  tuner, Jafz2001 nam-rig-builder, DeathlySin song-preview, Erikcb91 shuffle).
- Fix a pre-existing Guitar Theory clone-command typo (nam-tone -> guitar-theory).

Verified: env_compat 7/7, JS 819/819, pytest 1690 collected / 0 import errors,
rename-sensitive + startup suites green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: byrongamatos <xasiklas@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 11:03:01 +02:00

182 lines
7.4 KiB
Python

"""Tests for GET /api/audio-local-path — URL validation and path traversal safety."""
import importlib
import sys
import pytest
from fastapi.testclient import TestClient
@pytest.fixture()
def client_and_server(tmp_path, monkeypatch):
"""Loopback TestClient — simulates the Electron desktop calling from 127.0.0.1.
FEEDBACK_SYNC_STARTUP=1 prevents background scan/plugin threads from
spawning and leaking into other tests. load_plugins and startup_scan are
stubbed to no-ops so startup completes instantly.
"""
monkeypatch.setenv("CONFIG_DIR", str(tmp_path))
monkeypatch.setenv("FEEDBACK_SYNC_STARTUP", "1")
sys.modules.pop("server", None)
server = importlib.import_module("server")
monkeypatch.setattr(server, "load_plugins", lambda *a, **kw: None)
monkeypatch.setattr(server, "startup_scan", lambda: None)
# Redirect STATIC_DIR to a temp directory so no real checkout files are touched
static_tmp = tmp_path / "static"
static_tmp.mkdir()
monkeypatch.setattr(server, "STATIC_DIR", static_tmp)
# Pass client=("127.0.0.1", 50000) so request.client.host is a loopback address
test_client = TestClient(server.app, client=("127.0.0.1", 50000))
try:
yield test_client, server
finally:
test_client.close()
meta_db = getattr(server, "meta_db", None)
conn = getattr(meta_db, "conn", None)
if conn is not None:
conn.close()
@pytest.fixture()
def non_loopback_client(tmp_path, monkeypatch):
"""Non-loopback TestClient — default 'testclient' host simulates an external caller.
FEEDBACK_SYNC_STARTUP=1 prevents background scan/plugin threads from
spawning and leaking into other tests. load_plugins and startup_scan are
stubbed to no-ops so startup completes instantly.
"""
monkeypatch.setenv("CONFIG_DIR", str(tmp_path))
monkeypatch.setenv("FEEDBACK_SYNC_STARTUP", "1")
sys.modules.pop("server", None)
server = importlib.import_module("server")
monkeypatch.setattr(server, "load_plugins", lambda *a, **kw: None)
monkeypatch.setattr(server, "startup_scan", lambda: None)
# Default client=("testclient", 50000) is not a loopback address
test_client = TestClient(server.app)
try:
yield test_client
finally:
test_client.close()
meta_db = getattr(server, "meta_db", None)
conn = getattr(meta_db, "conn", None)
if conn is not None:
conn.close()
# ── happy path ────────────────────────────────────────────────────────────────
def test_resolves_file_in_audio_cache(client_and_server, tmp_path):
client, server = client_and_server
audio_file = server.AUDIO_CACHE_DIR / "audio_abc123.ogg"
audio_file.parent.mkdir(parents=True, exist_ok=True)
audio_file.touch()
r = client.get("/api/audio-local-path", params={"url": "/audio/audio_abc123.ogg"})
assert r.status_code == 200
assert r.json()["path"] == str(audio_file.resolve())
def test_resolves_file_in_static_dir(client_and_server, tmp_path):
client, server = client_and_server
# STATIC_DIR is monkeypatched to tmp_path/static — write there, not the checkout
audio_file = server.STATIC_DIR / "audio_static_test.ogg"
audio_file.touch()
r = client.get("/api/audio-local-path", params={"url": "/audio/audio_static_test.ogg"})
assert r.status_code == 200
assert r.json()["path"] == str(audio_file.resolve())
def test_returns_directory_as_404(client_and_server, tmp_path):
"""Directories must not be returned even if they exist inside the base dir."""
client, server = client_and_server
subdir = server.AUDIO_CACHE_DIR / "not_a_file"
subdir.mkdir(parents=True, exist_ok=True)
r = client.get("/api/audio-local-path", params={"url": "/audio/not_a_file"})
assert r.status_code == 404
def test_returns_404_for_nonexistent_file(client_and_server):
client, _ = client_and_server
r = client.get("/api/audio-local-path", params={"url": "/audio/does_not_exist.ogg"})
assert r.status_code == 404
assert "error" in r.json()
# ── rejected non-/audio/ inputs ───────────────────────────────────────────────
def test_rejects_sloppak_url(client_and_server):
client, _ = client_and_server
r = client.get("/api/audio-local-path", params={"url": "/api/sloppak/mysong/file/stems/full.ogg"})
assert r.status_code == 400
def test_rejects_empty_url(client_and_server):
client, _ = client_and_server
r = client.get("/api/audio-local-path", params={"url": ""})
assert r.status_code == 400
def test_rejects_url_with_scheme(client_and_server):
client, _ = client_and_server
r = client.get("/api/audio-local-path", params={"url": "http://evil.example.com/audio/x.ogg"})
assert r.status_code == 400
# ── traversal / escape attempts ───────────────────────────────────────────────
def test_rejects_dotdot_traversal(client_and_server):
client, _ = client_and_server
r = client.get("/api/audio-local-path", params={"url": "/audio/../etc/passwd"})
assert r.status_code == 400
def test_rejects_double_slash_absolute_component(client_and_server):
client, _ = client_and_server
# /audio//etc/passwd — the empty component after /audio/ becomes an absolute path join
r = client.get("/api/audio-local-path", params={"url": "/audio//etc/passwd"})
assert r.status_code == 400
def test_rejects_backslash_in_filename(client_and_server):
client, _ = client_and_server
r = client.get("/api/audio-local-path", params={"url": "/audio/foo\\bar.ogg"})
assert r.status_code == 400
def test_rejects_url_with_query(client_and_server):
client, _ = client_and_server
r = client.get("/api/audio-local-path", params={"url": "/audio/x.ogg?foo=bar"})
assert r.status_code == 400
def test_rejects_url_with_fragment(client_and_server):
client, _ = client_and_server
r = client.get("/api/audio-local-path", params={"url": "/audio/x.ogg#foo"})
assert r.status_code == 400
# ── loopback restriction ──────────────────────────────────────────────────────
def test_rejects_non_loopback_client(non_loopback_client):
"""Non-loopback clients must receive 403."""
r = non_loopback_client.get("/api/audio-local-path", params={"url": "/audio/x.ogg"})
assert r.status_code == 403
# ── serve_audio traversal protection ─────────────────────────────────────────
def test_serve_audio_dotdot_traversal_is_rejected(client_and_server):
"""GET /audio/../<path> must not escape the base directories."""
client, _ = client_and_server
r = client.get("/audio/../etc/passwd")
assert r.status_code == 404
def test_serve_audio_valid_file_is_served(client_and_server, tmp_path):
"""GET /audio/<file> serves a file that exists in AUDIO_CACHE_DIR."""
client, server = client_and_server
audio_file = server.AUDIO_CACHE_DIR / "audio_serve_test.ogg"
audio_file.parent.mkdir(parents=True, exist_ok=True)
audio_file.touch()
r = client.get("/audio/audio_serve_test.ogg")
assert r.status_code == 200