From d5ac22777fc4698a523e7cecdd8c4f3e59c8ac8f Mon Sep 17 00:00:00 2001 From: Dymas Date: Sat, 24 Oct 2026 11:11:42 +0200 Subject: [PATCH] Harden public proxy access, filesystem boundaries and content policy --- CHANGELOG.md | 7 +++ README.md | 11 +++- SECURITY_DEPLOYMENT.md | 85 ++++++++++++++++++++++++++++++ VERSION | 2 +- deploy/compose.proxy.yaml | 12 +++++ deploy/coreplay-security-http.conf | 12 +++++ deploy/npm-login-rate-limit.conf | 4 ++ deploy/npm-tools-root.conf | 5 ++ docker-compose.yaml | 2 +- http_handler.py | 46 ++++++++++++---- test_app.py | 4 +- test_proxy_auth.py | 37 +++++++++++++ 12 files changed, 213 insertions(+), 14 deletions(-) create mode 100644 SECURITY_DEPLOYMENT.md create mode 100644 deploy/compose.proxy.yaml create mode 100644 deploy/coreplay-security-http.conf create mode 100644 deploy/npm-login-rate-limit.conf create mode 100644 deploy/npm-tools-root.conf diff --git a/CHANGELOG.md b/CHANGELOG.md index 04c0775..29354c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.55.2 — 2026-10-24 + +- Enforce filesystem roots for authenticated/public installations even when the proxy connects over loopback without forwarded client headers; deny browsing without roots. +- Restrict executable scripts to hashes of trusted page templates, including rewritten mount paths; add HSTS, frame and browser capability headers. Reject cross-site request metadata and untrusted forwarded host/protocol overrides. +- Publish the Docker backend on loopback by default; document private proxy networking, separate subdomains and proxy rate limiting. Preserve state, downloads, queues and credentials. +- Checked all three providers against upstream revision 04d3645b926197d0fc3cc51a6413627f6cbb0ee0; no updates were required and local patches were retained. + ## 0.55.1 - 2026-10-22 - Moved Sign out to the bottom center of the left sidebar on every page, below navigation and page-specific controls. Desktop sidebars stay within the viewport and scroll when needed; mobile layouts keep the button below the sidebar content. diff --git a/README.md b/README.md index 069cd2e..de384ee 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ docker compose up -d --build Prebuilt image: `gitea.coreplay.eu/dymas/kaizoku:latest` (also tagged by release version). The Docker image uses a separate build stage for npm dependencies to keep the runtime image smaller. -Open **http://127.0.0.1:8421/**. For access from another device, set `KAIZOKU_ALLOW_REMOTE=1`, `KAIZOKU_AUTH_USERNAME`, and `KAIZOKU_AUTH_PASSWORD` before starting Compose. +Open **http://127.0.0.1:8421/**. The backend binds to host loopback by default. For access through NPM, use the private-network overlay in [SECURITY_DEPLOYMENT.md](SECURITY_DEPLOYMENT.md). For direct access from another device, explicitly set `KAIZOKU_BIND_ADDRESS` to a private interface address, restrict it with the host firewall, and set `KAIZOKU_ALLOW_REMOTE=1`, `KAIZOKU_AUTH_USERNAME`, and `KAIZOKU_AUTH_PASSWORD` before starting Compose. For a local installation, install Python 3.12+, Node.js/npm, `ffmpeg`, and `openssl`, then run: @@ -62,7 +62,7 @@ Open **https://tools.coreplay.eu/kaizoku/** and sign in. `/kaizoku` redirects to ### Troubleshooting local login and Nginx Proxy Manager -For a container published as `8231:8421`, use `http://SERVER_LAN_IP:8231/kaizoku/` directly and `https://tools.coreplay.eu/kaizoku/` through the proxy. Keep `KAIZOKU_BASE_URL=/kaizoku` if both addresses must work. A full HTTPS base URL fixes the expected login origin and enables Secure cookies, so direct HTTP login at a different host will not work with that setting. Credentials are case-sensitive; environment credentials override saved settings. +For an explicitly private-interface container binding such as `192.168.1.10:8231:8421`, use `http://SERVER_LAN_IP:8231/kaizoku/` directly and `https://tools.coreplay.eu/kaizoku/` through the proxy. Keep `KAIZOKU_BASE_URL=/kaizoku` if both addresses must work. A full HTTPS base URL fixes the expected login origin and enables Secure cookies, so direct HTTP login at a different host will not work with that setting. Credentials are case-sensitive; environment credentials override saved settings. When Nginx Proxy Manager connects through the Docker host, its upstream must be the host's reachable LAN address on port **8231**. When it shares a Docker network with Kaizoku, use `kaizoku` on port **8421**. Verify the hostname carefully (for example, `hade` and `hades` are different hosts). For the published host port, the location should contain one copy of each header: @@ -100,3 +100,10 @@ See the **[Kaizoku wiki](https://gitea.coreplay.eu/Dymas/kaizoku/wiki)** for det Provider parsers are adapted from [TheYogMehta/extensions](https://github.com/TheYogMehta/extensions). Bundled versions are Anikoto **6.0.0**, AniNeko **4.0.0**, and AnimePahe **6.0.1**, checked on 2026-10-22. Anikoto retains Kaizoku search, dub detection, and stream patches; AnimePahe uses only the official `.pw`, `.com`, and `.org` mirrors with bounded search retries and bridge-compatible logging. Upstream hooks that require StrawVerse's browser or mapping database are inactive in Kaizoku. Live source checks on 2026-10-17 returned Anikoto search results; AniNeko returned no matches and all three AnimePahe mirrors returned HTTP 403 from the test host. Current provider code does not guarantee source availability. Kaizoku does not host media. + +## Security deployment + +See [SECURITY_DEPLOYMENT.md](SECURITY_DEPLOYMENT.md) for private proxy networking, +separate application subdomains, NPM login throttling, and the domain-root 404. + +Public or authenticated installations enforce configured filesystem roots even for loopback proxy peers. Configure `KAIZOKU_REMOTE_PATH_ROOTS` server-side before selecting new download/library directories outside the current roots. A canonical HTTPS `KAIZOKU_BASE_URL` is recommended; direct remote clients cannot override host/protocol checks with forwarded headers. Pages authorize their bundled scripts by CSP hash; inline event-handler injection is blocked. diff --git a/SECURITY_DEPLOYMENT.md b/SECURITY_DEPLOYMENT.md new file mode 100644 index 0000000..45a1ce8 --- /dev/null +++ b/SECURITY_DEPLOYMENT.md @@ -0,0 +1,85 @@ +# Security deployment — 2026-10-24 + +The code hardening does not change the live VPS, DNS, certificates, Nginx Proxy +Manager (NPM), firewall, or existing containers. These steps complete the +infrastructure findings from the tools.coreplay.eu assessment. Existing URLs +continue to work until you explicitly migrate them. + +## Keep backends private + +The default Compose host binding is loopback. A containerized NPM cannot reach +another container through its own 127.0.0.1. On the same Docker host, create a +private proxy network once (`docker network create coreplay-proxy`), attach NPM +to it in its own Compose configuration, then use the supplied overlay: + +```sh +docker compose -f docker-compose.yaml -f deploy/compose.proxy.yaml config +docker compose -f docker-compose.yaml -f deploy/compose.proxy.yaml up -d +``` + +The overlay removes host port publishing and adds the proxy network while +preserving the default network and existing volumes. It requires a current +Docker Compose with `!reset` support. Set the NPM upstream to `kaizoku:8421` +using HTTP and preserve the public Host header. Check the rendered Compose +configuration before recreating containers. Never use `down -v` during upgrades. +If NPM runs on another machine, use a private VPN/interface and a firewall rule +allowing only that proxy, rather than publishing the port to the Internet. + +## Separate browser origins + +Path-scoped cookies, CSRF tokens and content policies do not isolate applications +sharing https://tools.coreplay.eu. Create separate DNS records and NPM Proxy Hosts +for pwatcher.coreplay.eu, cbzcleaner.coreplay.eu and kaizoku.coreplay.eu (and +workspace.coreplay.eu when used), with valid certificates and Force SSL. For this +application configure: + +```dotenv +KAIZOKU_BASE_URL=https://kaizoku.coreplay.eu +KAIZOKU_ALLOW_REMOTE=1 +``` + +Keep the existing credentials and all persistent mounts. Remove the old mount +path from the public URL only when the dedicated hostname is ready. Forward the +URI without prefix stripping. Recreate the app container to apply environment +changes, sign in again on the new origin, and remove the old proxy location after +verifying pages, APIs, images and logout. Avoid redirects that rehost one app on +another app's origin. Keep cookies host-only; do not set Domain=.coreplay.eu. +For trusted HTTP development, local settings remain supported; public production +URLs should explicitly use HTTPS. + +## Limit authentication at NPM + +Merge `deploy/coreplay-security-http.conf` ONCE into NPM's persistent +`/data/nginx/custom/http_top.conf` (an http-context include). It defines a shared +per-client login budget of five requests/minute with a burst of five for POSTs to +the known login endpoints, including both existing and dedicated-host URLs. +Append `deploy/npm-login-rate-limit.conf` in each affected Proxy Host's Advanced +configuration. Ordinary polling uses an empty limiter key and is not counted. +Application-level authentication and throttling remain enabled, including for +Basic Auth API requests. Keep any existing NPM access lists in place. + +Nginx uses the connecting address. If another proxy/CDN precedes NPM, configure +its real-IP trust only for that proxy's actual source networks; never trust all +forwarded addresses. The application limits may be stricter, especially when +several users share one connecting proxy IP. + +Run `nginx -t` inside the NPM container before reloading. Preserve existing custom +configuration rather than replacing it. NPM's documented include points and +Docker networking pattern are described in its [advanced configuration guide](https://nginxproxymanager.com/advanced-config/). + +## Intentional domain root + +Append `deploy/npm-tools-root.conf` only to the shared tools.coreplay.eu Proxy +Host. Its exact `/` location returns 404 without changing the existing app path +locations. Do not apply this root snippet to dedicated app hosts, where `/` is +an application page. Once every app has migrated, remove the old locations and +use NPM's 404 host for the retired tools hostname. + +## Verify after rollout + +From outside the VPS, inspect each login page's HTTPS headers and cookie flags; +confirm unauthenticated sensitive APIs are denied and cross-origin mutations are +rejected. Check `docker ps --format 'table {{.Names}}\t{{.Ports}}'` and the host +firewall: no app backend should be reachable through the public IP. Confirm the +root returns 404 and app login/navigation still work. Review NPM/app logs for +429s and CSP violations after rollout. Existing data must remain available. diff --git a/VERSION b/VERSION index b0b5281..df174c9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.55.1 +0.55.2 diff --git a/deploy/compose.proxy.yaml b/deploy/compose.proxy.yaml new file mode 100644 index 0000000..751c7a4 --- /dev/null +++ b/deploy/compose.proxy.yaml @@ -0,0 +1,12 @@ +# Merge with ../docker-compose.yaml; requires Docker Compose support for !reset. +# Create coreplay-proxy first and attach NPM to it as well. +services: + kaizoku: + ports: !reset [] + networks: + - default + - coreplay_proxy +networks: + coreplay_proxy: + external: true + name: coreplay-proxy diff --git a/deploy/coreplay-security-http.conf b/deploy/coreplay-security-http.conf new file mode 100644 index 0000000..75185ba --- /dev/null +++ b/deploy/coreplay-security-http.conf @@ -0,0 +1,12 @@ +# Include ONCE in nginx's http context (NPM: /data/nginx/custom/http_top.conf). +# Empty keys bypass throttling; ordinary page/API polling is unaffected. +map "$request_method:$uri" $coreplay_login_client { + default ""; + "POST:/pwatcher/api/auth/login" $binary_remote_addr; + "POST:/api/auth/login" $binary_remote_addr; + "POST:/cbzcleaner/login" $binary_remote_addr; + "POST:/login" $binary_remote_addr; + "POST:/kaizoku/auth/login" $binary_remote_addr; + "POST:/auth/login" $binary_remote_addr; +} +limit_req_zone $coreplay_login_client zone=coreplay_login:10m rate=5r/m; diff --git a/deploy/npm-login-rate-limit.conf b/deploy/npm-login-rate-limit.conf new file mode 100644 index 0000000..b07740c --- /dev/null +++ b/deploy/npm-login-rate-limit.conf @@ -0,0 +1,4 @@ +# Add to each affected NPM Proxy Host's Advanced configuration. +# Requires coreplay-security-http.conf to be loaded in the http context first. +limit_req zone=coreplay_login burst=5 nodelay; +limit_req_status 429; diff --git a/deploy/npm-tools-root.conf b/deploy/npm-tools-root.conf new file mode 100644 index 0000000..cf85df5 --- /dev/null +++ b/deploy/npm-tools-root.conf @@ -0,0 +1,5 @@ +# Add ONLY to the shared tools.coreplay.eu Proxy Host's Advanced configuration. +# Keep existing /pwatcher, /cbzcleaner and /kaizoku custom locations. +location = / { + return 404; +} diff --git a/docker-compose.yaml b/docker-compose.yaml index 6a18e17..ddd6ff1 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,7 +7,7 @@ services: container_name: kaizoku restart: unless-stopped ports: - - "${KAIZOKU_PORT:-8421}:8421" + - "${KAIZOKU_BIND_ADDRESS:-127.0.0.1}:${KAIZOKU_PORT:-8421}:8421" environment: USER_UID: ${USER_UID:-} USER_GID: ${USER_GID:-} diff --git a/http_handler.py b/http_handler.py index ce1e1f9..b4651c8 100644 --- a/http_handler.py +++ b/http_handler.py @@ -12,7 +12,7 @@ import json import mimetypes import shutil import traceback -from base64 import b64decode +from base64 import b64decode, b64encode from binascii import Error as BinasciiError from dataclasses import dataclass from http import HTTPStatus @@ -390,9 +390,6 @@ class Handler(BaseHTTPRequestHandler): canonical = urlparse(Handler._base_url(self)) if canonical.netloc: return canonical.netloc - host = str(self.headers.get("X-Forwarded-Host") or self.headers.get("x-forwarded-host") or "").strip() - if host: - return host.split(",", 1)[0].strip() return str(self.headers.get("Host") or self.headers.get("host") or "").strip() def _origin(self): @@ -405,6 +402,9 @@ class Handler(BaseHTTPRequestHandler): canonical = urlparse(Handler._base_url(self)) if canonical.scheme: return canonical.scheme == "https" + # Forwarded headers have authority only when the socket peer is local. + if not self.client_address or not client_address_is_local(self.client_address[0]): + return False forwarded_proto = str(self.headers.get("X-Forwarded-Proto") or self.headers.get("x-forwarded-proto") or "").strip() if forwarded_proto: return forwarded_proto.split(",", 1)[0].strip().lower() == "https" @@ -712,6 +712,8 @@ class Handler(BaseHTTPRequestHandler): ) def enforce_same_origin(self): + if self.headers.get("Sec-Fetch-Site") == "cross-site": + raise PermissionError("Cross-site browser requests are not allowed.") origin = Handler._origin(self) if not origin: if Handler._session_cookie(self): @@ -723,9 +725,15 @@ class Handler(BaseHTTPRequestHandler): def _remote_path_roots(self): return configured_remote_path_roots(Handler._context(self).get_config_snapshot()) + def _unrestricted_local_paths(self): + # A public proxy may use a loopback socket and omit forwarded headers. + # Public installations must still enforce the server-side filesystem roots. + return (client_address_is_local(Handler._effective_client_host(self)) + and not Handler._base_url(self) + and not remote_access_credentials_configured(Handler._config(self))) + def validate_remote_path(self, path_value, label): - client_host = Handler._effective_client_host(self) - if client_address_is_local(client_host): + if Handler._unrestricted_local_paths(self): return roots = Handler._remote_path_roots(self) if not roots: @@ -793,8 +801,9 @@ class Handler(BaseHTTPRequestHandler): self.json(Handler._context(self).get_provider_update_status()) elif parsed.path == "/api/fs/browse": params = parse_qs(parsed.query) - client_host = Handler._effective_client_host(self) - allowed_roots = None if client_address_is_local(client_host) else Handler._remote_path_roots(self) + allowed_roots = None if Handler._unrestricted_local_paths(self) else Handler._remote_path_roots(self) + if allowed_roots == []: + raise PermissionError("Remote filesystem browsing requires configured path roots.") self.json( browse_filesystem( (params.get("path") or [""])[0], @@ -934,6 +943,8 @@ class Handler(BaseHTTPRequestHandler): payload = Handler.body_form(self) if not Handler._check_login_token(self, payload.get("csrf")): raise PermissionError("Sign-in form expired. Reload the page and try again.") + if self.headers.get("Sec-Fetch-Site") == "cross-site": + raise PermissionError("Cross-site sign-in is not allowed.") if Handler._origin(self) and not Handler._origin_matches_host(self): raise PermissionError("Cross-origin sign-in is not allowed.") username = str(payload.get("username") or "").strip() @@ -1279,15 +1290,32 @@ class Handler(BaseHTTPRequestHandler): headers = dict(headers) content_type = headers.get("Content-Type", "") base = Handler._base_path(self) + # Only scripts from the application's trusted page templates may run. + # Do not authorize arbitrary script tags found in a response body. + scripts = [] + if "text/html" in content_type: + context = Handler._context(self) + templates = [getattr(context, name, None) for name in + ("index_html", "queue_html", "config_html", "watchlist_html")] + if payload.decode("utf-8") in [*templates, AI_SUBTITLES_HTML]: + scripts = re.findall(r"]*>(.*?)", payload.decode("utf-8"), re.DOTALL | re.IGNORECASE) if base and "text/html" in content_type: content = payload.decode("utf-8") content = re.sub(r"([\"'`])/(?=api/|auth/|favicon\.|queue[\"'`?#]|config[\"'`?#]|watchlist[\"'`?#]|ai-subtitles[\"'`?#]|[\"'`])", lambda match: match[1] + base + "/", content) payload = content.encode("utf-8") + # Mount rewriting also changes trusted script text. + if scripts: + scripts = re.findall(r"]*>(.*?)", content, re.DOTALL | re.IGNORECASE) + hashes = " ".join("'sha256-" + b64encode(hashlib.sha256(script.encode()).digest()).decode() + "'" for script in scripts) headers.setdefault("Cache-Control", "no-store") headers.setdefault("X-Content-Type-Options", "nosniff") headers.setdefault("Referrer-Policy", "same-origin") - headers.setdefault("Content-Security-Policy", "frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'") + headers.setdefault("Content-Security-Policy", f"default-src 'self'; script-src 'self' {hashes}; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; object-src 'none'") + headers.setdefault("X-Frame-Options", "DENY") + headers.setdefault("Permissions-Policy", "camera=(), microphone=(), geolocation=()") + if Handler._request_is_secure(self): + headers.setdefault("Strict-Transport-Security", "max-age=31536000") self.send_response(status) for key, value in headers.items(): self.send_header(key, value) diff --git a/test_app.py b/test_app.py index 87631bf..06317ba 100644 --- a/test_app.py +++ b/test_app.py @@ -430,6 +430,7 @@ class NetworkGuardTests(unittest.TestCase): "KAIZOKU_ALLOW_REMOTE": "1", "KAIZOKU_AUTH_USERNAME": "demo", "KAIZOKU_AUTH_PASSWORD": "secret-pass", + "KAIZOKU_BASE_URL": "https://anime.example:8421", }, clear=False, ): @@ -5002,7 +5003,8 @@ class HandlerRouteTests(unittest.TestCase): post_body = b'{"download_dir":"/tmp/updated","mode":"dub","quality":"720"}' post_handler = DummyHandler("/api/config", body=post_body, content_length=len(post_body)) post_handler.headers["Authorization"] = "Basic " + base64.b64encode(b"demo:secret-pass").decode() - APP.Handler.do_POST(post_handler) + with mock.patch.dict(os.environ, {"KAIZOKU_REMOTE_PATH_ROOTS": "/tmp/updated"}): + APP.Handler.do_POST(post_handler) self.assertEqual(post_handler.json_status, HTTPStatus.OK) self.assertNotIn("auth_username", post_handler.json_payload) diff --git a/test_proxy_auth.py b/test_proxy_auth.py index b20cab9..013f5f9 100644 --- a/test_proxy_auth.py +++ b/test_proxy_auth.py @@ -1,5 +1,7 @@ """Exercise the public proxy contract without touching an existing installation.""" import json +import hashlib +from base64 import b64encode import os import re import unittest @@ -121,6 +123,41 @@ class ProxyAuthTests(unittest.TestCase): for value in ("//evil.test", "/\\evil.test", "https://evil.test", "/\r\nLocation: bad"): self.assertEqual(APP.Handler._normalized_next_path(handler, value), "/") + def test_public_proxy_loopback_cannot_bypass_filesystem_roots(self): + handler = self.handler("/kaizoku/api/fs/browse") + with mock.patch.object(http_handler, "configured_remote_path_roots", return_value=[]): + with self.assertRaises(PermissionError): + APP.Handler.validate_remote_path(handler, "/etc", "download directory") + self.assertFalse(APP.Handler._unrestricted_local_paths(handler)) + with mock.patch.object(http_handler, "configured_remote_path_roots", return_value=["/downloads"]): + with self.assertRaises(PermissionError): + APP.Handler.validate_remote_path(handler, "/etc", "download directory") + APP.Handler.validate_remote_path(handler, "/downloads/show", "download directory") + + def test_csp_authorizes_only_trusted_scripts_after_mount_rewriting(self): + handler = self.handler("/kaizoku/") + trusted = handler.handler_context.index_html + APP.Handler.html(handler, trusted) + headers = dict(handler.response_headers) + policy = headers["Content-Security-Policy"] + script = re.search(r"", handler.wfile.getvalue().decode(), re.S)[1] + digest = b64encode(hashlib.sha256(script.encode()).digest()).decode() + self.assertIn("'sha256-" + digest + "'", policy) + self.assertNotIn("'unsafe-inline'", policy.split("style-src")[0]) + self.assertEqual(headers["Strict-Transport-Security"], "max-age=31536000") + injected = self.handler("/kaizoku/") + APP.Handler.html(injected, '') + self.assertNotIn("sha256-", dict(injected.response_headers)["Content-Security-Policy"]) + + def test_cross_site_metadata_and_untrusted_forwarded_host_are_rejected(self): + handler = self.handler("/kaizoku/") + handler.headers["Sec-Fetch-Site"] = "cross-site" + with self.assertRaises(PermissionError): + APP.Handler.enforce_same_origin(handler) + self.config["base_url"] = "" + handler.headers.update({"X-Forwarded-Host": "evil.example", "Origin": "http://evil.example"}) + self.assertFalse(APP.Handler._origin_matches_host(handler)) + if __name__ == "__main__": unittest.main()