mirror of
https://github.com/penpot/penpot.git
synced 2026-11-27 13:32:28 +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
98 lines
3.9 KiB
Clojure
98 lines
3.9 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 frontend-tests.util-http-test
|
|
(:require
|
|
[app.util.http :as http]
|
|
[cljs.test :as t :include-macros true]
|
|
[frontend-tests.helpers.async :as async]
|
|
[frontend-tests.helpers.http :as http-mock]))
|
|
|
|
(def ^:private uri "https://example.com/assets/file.json")
|
|
|
|
(defn- failing-response
|
|
"A response map whose native body read rejects with `cause`."
|
|
[cause]
|
|
{:status 200
|
|
:uri uri
|
|
:headers {}
|
|
::http/response #js {:text (fn [] (js/Promise.reject cause))}})
|
|
|
|
(defn- ^:async read-error
|
|
"Read the body of `response` as text and resolve with the error raised."
|
|
[response]
|
|
(try
|
|
(await (async/->promise (http/process-response-type :text response)))
|
|
nil
|
|
(catch :default cause
|
|
cause)))
|
|
|
|
(t/deftest ^:async body-read-network-failure-is-a-network-error
|
|
(let [cause (js/TypeError. "network error")
|
|
error (await (read-error (failing-response cause)))
|
|
data (ex-data error)]
|
|
(t/is (= :network (:type data)))
|
|
(t/is (= uri (:uri data)))
|
|
(t/is (identical? cause (ex-cause error)))))
|
|
|
|
(t/deftest ^:async body-parse-failure-passes-unchanged
|
|
(let [cause (js/SyntaxError. "Unexpected token < in JSON")
|
|
error (await (read-error (failing-response cause)))]
|
|
(t/is (identical? cause error))))
|
|
|
|
(t/deftest ^:async body-read-programming-error-passes-unchanged
|
|
(let [cause (js/TypeError. "body stream already read")
|
|
error (await (read-error (failing-response cause)))]
|
|
(t/is (identical? cause error))))
|
|
|
|
;; ---------------------------------------------------------------------------
|
|
;; network-failure? tests
|
|
;; ---------------------------------------------------------------------------
|
|
|
|
(defn- make-error
|
|
[error-name message]
|
|
(let [err (js/Error. message)]
|
|
(set! (.-name err) error-name)
|
|
err))
|
|
|
|
(t/deftest test-network-failure-browser-messages
|
|
(t/testing "The TypeError each browser gives a failed fetch is a network failure"
|
|
(doseq [message ["Failed to fetch"
|
|
"Failed to fetch (design.penpot.app)"
|
|
"NetworkError when attempting to fetch resource."
|
|
"Load failed"
|
|
"network error"
|
|
"Error in body stream"]]
|
|
(t/is (true? (http/network-failure? (make-error "TypeError" message)))
|
|
message))))
|
|
|
|
(t/deftest test-network-failure-needs-type-error
|
|
(t/testing "Other errors with the same message are not network failures"
|
|
(t/is (false? (http/network-failure? (make-error "Error" "Failed to fetch")))))
|
|
(t/testing "Other TypeErrors are not network failures"
|
|
(t/is (false? (http/network-failure? (make-error "TypeError" "x is not a function"))))))
|
|
|
|
(t/deftest ^:async fetch-respects-omit-default-headers
|
|
(let [requests (atom [])
|
|
original (http-mock/install-fetch-mock!
|
|
(fn [_url options]
|
|
(swap! requests conj options)
|
|
(js/Promise.resolve (js/Response. "" #js {:status 200}))))]
|
|
(try
|
|
(await (async/->promise (http/fetch {:method :get :uri uri})))
|
|
(await (async/->promise (http/fetch {:method :get
|
|
:uri uri
|
|
:omit-default-headers true})))
|
|
(finally
|
|
(http-mock/restore-fetch! original)))
|
|
(let [default-headers (aget (first @requests) "headers")
|
|
omitted-headers (aget (second @requests) "headers")]
|
|
(t/is (not (js/Object.hasOwn default-headers "x-penpot-origin")))
|
|
(t/is (not (js/Object.hasOwn omitted-headers "x-penpot-origin")))
|
|
(doseq [header ["x-frontend-version" "x-client"]]
|
|
(t/is (js/Object.hasOwn default-headers header) header)
|
|
(t/is (not (js/Object.hasOwn omitted-headers header)) header)))))
|