penpot/backend/test/backend_tests/http_sse_test.clj
Andrey Antukh a7f66b18f9
✨ Honor trusted alternate origins for backend links and OIDC (#11892)
* ✨ Honor trusted alternate origins for backend links and OIDC

Add PENPOT_TRUSTED_ORIGINS, an optional list of bare scheme://host[:port]
alternate origins. A request whose Origin matches (exact, trimmed and
lowercased) uses that origin as the base for links generated while serving
it: emails, asset URLs, audit and webhook link fields, and redirects.
Unlisted origins, requests without Origin, and background or server-to-server
work keep the canonical public-uri (fail-closed). Malformed entries warn at
startup and never match.

Stamp the trusted origin on the request with a global http middleware that
binds config public-uri for the request extent; expose the chain as
root-middleware so tests assert it runs before auth and errors. Build every
backend link through the canonical cf/get-public-uri.

Carry the origin in the signed OIDC state (auth and organization SSO) so the
callback, a navigation without Origin, re-applies it with with-public-uri for
the token-exchange redirect_uri and every redirect, including logout. Verify
the state token once per callback and reuse the claims; build redirect URIs
with ensure-path-slash plus join and append-query-param for hash routes. A
removed origin does not invalidate states signed before it (up to four hours).

Bring MCP OAuth into scope: wrap its routes, stage the origin in the Redis
request record at authorize time, and re-apply it in decision so issuer,
resource, the approval redirect and the code-exchange iss agree on one base.

Keep the log-capture watch installed while waiting for the async rejection
record in the save tests; a watch removed first lost late records and read
nil. Add a delayed-executor regression test that makes the race deterministic
and document the rule in the backend testing memory.

Closes #11625

AI-assisted-by: deepseek-v4.1-flash
AI-assisted-by: qwen3.7-plus
AI-assisted-by: gpt-5.6-luna
AI-assisted-by: muse-spark-1.3-contributor

* ✨ Serve trusted alternate origins across frontend and backend

Let one installation serve a canonical domain plus trusted alternate
origins, without letting request headers pick arbitrary link bases.

The backend normalizes `PENPOT_TRUSTED_ORIGINS` on load: it drops malformed
entries with a warning and warns about HTTP origins outside localhost. It
selects the first trusted origin from `X-Penpot-Origin`, then `Origin`, and
applies only scheme, host and port to the public URI, keeping the configured
path. MCP OAuth keeps the canonical base because its authorize navigation
has no usable origin.

The frontend reads the same list from `config.js`, filled by the container
entrypoint, and selects the browser origin only on an exact match. It sends
`X-Penpot-Origin` on RPC calls (including GETs and multipart) and OIDC
initiation, not on asset, font or external requests. SSE captures the request
bindings, and export jobs store the effective base so the download link uses
it after the request ends. A closed export creation schema excludes
`:public-uri`, which the backend adds only after validation.

Register the OIDC callback for each alias. Removing an alias does not stop
already signed OIDC state, queued mail or export jobs from using it.

Add regression tests across backend, frontend and the nginx entrypoint, and
document the proxy setup, worker upgrade and rollback, and alias retirement.

Refs #11625

AI-assisted-by: hy4-flash-free
AI-assisted-by: gpt-6-luna
AI-assisted-by: gpt-6.2-sol
2026-11-24 20:02:29 +01:00

56 lines
2.2 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns backend-tests.http-sse-test
(:require
[app.config :as cf]
[app.http.middleware :as mw]
[app.http.sse :as sse]
[clojure.test :as t]
[cuerdas.core :as str]
[yetti.response :as yres])
(:import
java.io.ByteArrayOutputStream))
(defn- run-stream
"Build an SSE response under `base`, then run its body after the
request scope has ended."
[base]
(let [seen (atom nil)
handler (fn []
(reset! seen (str (cf/get :public-uri)))
{::result true})
endpoint (sse/response handler)
response (if base
(binding [cf/config (cf/with-public-uri base cf/config)]
(endpoint {}))
(endpoint {}))
body (::yres/body response)]
(with-redefs [cf/config (assoc cf/config :public-uri "http://localhost:3449")]
(yres/write-body-to-stream body response (ByteArrayOutputStream.)))
@seen))
(t/deftest stream-uses-the-origin-of-the-request
(t/is (= "https://alt.example.com"
(run-stream "https://alt.example.com"))))
(t/deftest stream-keeps-the-canonical-origin-without-a-trusted-base
(t/is (= "http://localhost:3449" (run-stream nil))))
(t/deftest stream-writes-the-alternate-public-subpath-after-the-request
(let [public-config {:public-uri "http://localhost:3449/penpot/"
:trusted-origins #{"https://alt.example.com"}}
endpoint (mw/wrap-trusted-origin
(sse/response #(str (cf/get-public-uri "download"))))
response (binding [cf/config public-config]
(endpoint {:headers {"x-penpot-origin" "https://alt.example.com"}}))
output (ByteArrayOutputStream.)]
(binding [cf/config public-config]
(yres/write-body-to-stream (::yres/body response) response output))
(let [body (.toString output "UTF-8")]
(t/is (str/includes? body "event: end\n"))
(t/is (str/includes? body "https://alt.example.com/penpot/download")))))