mirror of
https://github.com/penpot/penpot.git
synced 2026-11-27 05:10:32 +00:00
* ✨ 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
180 lines
7.8 KiB
Clojure
180 lines
7.8 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.helpers-test
|
|
"Focused unit tests for the shared test request constructor.
|
|
|
|
`make-dummy-request` is called with three styles across the suite
|
|
(no arguments, a single options map, and keyword arguments). These
|
|
tests pin that contract so a future redefinition or reordering of
|
|
the helper fails loudly here instead of as a flood of unrelated
|
|
test errors."
|
|
(:require
|
|
[app.config :as cf]
|
|
[backend-tests.helpers :as th]
|
|
[clojure.test :as t]
|
|
[yetti.request :as yreq]))
|
|
|
|
(defn- utf8-bytes
|
|
^bytes
|
|
[^String s]
|
|
(.getBytes s "UTF-8"))
|
|
|
|
;; --- DEFAULTS (ZERO ARGUMENTS)
|
|
|
|
(t/deftest make-dummy-request-zero-args-uses-defaults
|
|
(let [req (th/make-dummy-request)]
|
|
(t/is (instance? backend_tests.helpers.DummyRequest req))
|
|
(t/is (= :get (yreq/method req)))
|
|
(t/is (= "/test" (yreq/path req)))
|
|
(t/is (= :http (yreq/scheme req)))
|
|
(t/is (= "HTTP/1.1" (yreq/protocol req)))
|
|
(t/is (= "127.0.0.1" (yreq/remote-addr req)))
|
|
(t/is (= "test" (yreq/server-name req)))
|
|
(t/is (= 0 (yreq/server-port req)))
|
|
(t/is (nil? (yreq/query req)))
|
|
(t/is (nil? (yreq/body req)))))
|
|
|
|
;; --- SINGLE OPTIONS MAP
|
|
|
|
(t/deftest make-dummy-request-single-map-arg-overrides-given-keys
|
|
(let [req (th/make-dummy-request {:method :post
|
|
:headers {"x-test" "yes"}
|
|
:path "/custom"
|
|
:query "a=1"})]
|
|
(t/is (= :post (yreq/method req)))
|
|
(t/is (= "yes" (yreq/get-header req "x-test")))
|
|
(t/is (= "/custom" (yreq/path req)))
|
|
(t/is (= "a=1" (yreq/query req)))))
|
|
|
|
(t/deftest make-dummy-request-single-map-arg-keeps-defaults-for-omitted-keys
|
|
(let [req (th/make-dummy-request {:method :post})]
|
|
(t/is (= "/test" (yreq/path req)))
|
|
(t/is (= :http (yreq/scheme req)))
|
|
(t/is (= "test" (yreq/server-name req)))))
|
|
|
|
;; --- KEYWORD ARGUMENTS
|
|
|
|
(t/deftest make-dummy-request-keyword-args-override-given-keys
|
|
(let [req (th/make-dummy-request :method :delete
|
|
:path "/by-id"
|
|
:server-port 8080)]
|
|
(t/is (= :delete (yreq/method req)))
|
|
(t/is (= "/by-id" (yreq/path req)))
|
|
(t/is (= 8080 (yreq/server-port req)))))
|
|
|
|
;; --- BODY
|
|
|
|
(t/deftest make-dummy-request-string-body-bytes-wrapped-in-stream
|
|
(let [req (th/make-dummy-request :method :post :body-bytes "hello")]
|
|
(t/is (instance? java.io.ByteArrayInputStream (yreq/body req)))
|
|
(t/is (= "hello" (slurp (yreq/body req))))))
|
|
|
|
(t/deftest make-dummy-request-byte-array-body-bytes-wrapped-in-stream
|
|
(let [req (th/make-dummy-request :body-bytes (utf8-bytes "raw"))]
|
|
(t/is (= "raw" (slurp (yreq/body req))))))
|
|
|
|
(t/deftest make-dummy-request-body-stream-takes-precedence-over-body-bytes
|
|
(let [stream (java.io.ByteArrayInputStream. (utf8-bytes "stream"))
|
|
req (th/make-dummy-request :body-stream stream :body-bytes "bytes")]
|
|
(t/is (identical? stream (yreq/body req)))
|
|
(t/is (= "stream" (slurp (yreq/body req))))))
|
|
|
|
;; --- COOKIES
|
|
|
|
(t/deftest make-dummy-request-cookie-readable-via-get-cookie
|
|
(let [req (th/make-dummy-request {:cookies {"auth-token" "abc"}})]
|
|
(t/is (= {:value "abc"} (yreq/get-cookie req "auth-token")))))
|
|
|
|
(t/deftest make-dummy-request-missing-cookie-returns-nil-value
|
|
(t/is (= {:value nil} (yreq/get-cookie (th/make-dummy-request) "missing"))))
|
|
|
|
;; --- RPC PARAMS: MULTIPART FLAG
|
|
|
|
(t/deftest prepare-rpc-params-marks-multipart-request
|
|
(let [params (#'th/prepare-rpc-params
|
|
(with-meta {::th/type :dummy
|
|
:events [{:name "event"}]}
|
|
{:app.http/request {:headers {"content-type" "multipart/form-data; boundary=xxx"}}}))]
|
|
(t/is (true? (:app.rpc/is-multipart params)))))
|
|
|
|
(t/deftest prepare-rpc-params-leaves-non-multipart-request-unmarked
|
|
(let [params (#'th/prepare-rpc-params
|
|
(with-meta {::th/type :dummy
|
|
:events [{:name "event"}]}
|
|
{:app.http/request {:headers {"content-type" "application/transit+json"}}}))]
|
|
(t/is (false? (:app.rpc/is-multipart params)))))
|
|
|
|
(t/deftest prepare-rpc-params-leaves-missing-content-type-unmarked
|
|
(let [params (#'th/prepare-rpc-params {::th/type :dummy
|
|
:events [{:name "event"}]})]
|
|
(t/is (false? (:app.rpc/is-multipart params)))))
|
|
|
|
(t/deftest prepare-rpc-params-body-cannot-forge-multipart-flag
|
|
(let [params (#'th/prepare-rpc-params
|
|
(with-meta {::th/type :dummy
|
|
:app.rpc/is-multipart true
|
|
:events [{:name "event"}]}
|
|
{:app.http/request {:headers {"content-type" "application/json"}}}))]
|
|
(t/is (false? (:app.rpc/is-multipart params)))))
|
|
|
|
;; --- RPC PARAMS: REQUEST METADATA
|
|
|
|
(t/deftest prepare-rpc-params-preserves-supplied-request
|
|
(let [supplied {:headers {"x-frontend-version" "1.2.3"}
|
|
:app.http/auth-key-id :admin-console}
|
|
params (#'th/prepare-rpc-params
|
|
(with-meta {::th/type :dummy
|
|
:events [{:name "event"}]}
|
|
{:app.http/request supplied}))
|
|
request (:app.http/request (meta params))]
|
|
(t/is (= "1.2.3" (yreq/get-header request "x-frontend-version")))
|
|
(t/is (= :admin-console (:app.http/auth-key-id request)))
|
|
(t/is (= {:events [{:name "event"}]} (:params request)))))
|
|
|
|
(t/deftest prepare-rpc-params-builds-dummy-request-without-metadata
|
|
(let [params (#'th/prepare-rpc-params {::th/type :dummy
|
|
:events [{:name "event"}]})
|
|
request (:app.http/request (meta params))]
|
|
(t/is (instance? backend_tests.helpers.DummyRequest request))
|
|
(t/is (= {:events [{:name "event"}]} (:params request)))))
|
|
|
|
(t/deftest prepare-rpc-params-falls-back-to-dummy-for-non-map-request
|
|
(let [supplied (reify yreq/IRequest
|
|
(get-header [_ _] nil))
|
|
params (#'th/prepare-rpc-params
|
|
(with-meta {::th/type :dummy
|
|
:events [{:name "event"}]}
|
|
{:app.http/request supplied}))
|
|
request (:app.http/request (meta params))]
|
|
(t/is (instance? backend_tests.helpers.DummyRequest request))
|
|
(t/is (= {:events [{:name "event"}]} (:params request)))))
|
|
|
|
(t/deftest command-through-middleware-preserves-processed-request
|
|
(let [origin "https://alt.example.com"
|
|
data (with-meta {::th/type :dummy :name "new"}
|
|
{:app.http/request {:headers th/multipart-headers
|
|
:params {:name "old" :description "kept"}
|
|
:remote-addr "192.0.2.1"}})
|
|
method (fn [params]
|
|
(let [request (:app.http/request (meta params))]
|
|
{:origin (:app.http/trusted-origin request)
|
|
:remote-addr (:remote-addr request)
|
|
:auth-data (:app.http/auth-data request)
|
|
:body (:params request)
|
|
:multipart (:app.rpc/is-multipart params)}))
|
|
options {:headers {"origin" origin}
|
|
:request-attrs {:app.http/auth-data {:provider "test"}}}]
|
|
(binding [cf/config (assoc cf/config :trusted-origins #{origin})
|
|
th/*system* {:app.rpc/methods {:dummy [{} method]}}]
|
|
(t/is (= {:error nil
|
|
:result {:origin origin
|
|
:remote-addr "192.0.2.1"
|
|
:auth-data {:provider "test"}
|
|
:body {:name "new" :description "kept"}
|
|
:multipart true}}
|
|
(th/command-through-middleware data options))))))
|