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
356 lines
17 KiB
Clojure
356 lines
17 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.email-sending-test
|
|
(:require
|
|
[app.config :as cf]
|
|
[app.db :as db]
|
|
[app.email :as emails]
|
|
[app.jobs :as jobs]
|
|
[app.metrics :as-alias mtx]
|
|
[backend-tests.helpers :as th]
|
|
[clojure.test :as t]
|
|
[cuerdas.core :as str]
|
|
[integrant.core :as ig]
|
|
[lambdaisland.uri :as u]
|
|
[promesa.core :as p]))
|
|
|
|
(t/use-fixtures :once th/state-init)
|
|
(t/use-fixtures :each th/database-reset)
|
|
|
|
(t/deftest register-email-rendering
|
|
(let [result (emails/render emails/register {:to "example@app.io" :name "foo"})]
|
|
(t/is (map? result))
|
|
(t/is (contains? result :subject))
|
|
(t/is (contains? result :body))
|
|
(t/is (contains? result :to))
|
|
#_(t/is (contains? result :reply-to))
|
|
(t/is (map? (:body result)))))
|
|
|
|
(def ^:private sso-notice-snippet
|
|
"has set up single sign-on (SSO) in Penpot")
|
|
|
|
(defn- email-text-body
|
|
[result]
|
|
(get-in result [:body "text/plain"]))
|
|
|
|
(t/deftest password-recovery-renders-links-under-the-public-subpath
|
|
(doseq [[public-uri expected-base]
|
|
[["https://canonical.example.com/penpot" "https://canonical.example.com/penpot"]
|
|
["https://canonical.example.com/penpot/" "https://canonical.example.com/penpot"]
|
|
["https://alternate.example.com/penpot" "https://alternate.example.com/penpot"]
|
|
["https://alternate.example.com/penpot/" "https://alternate.example.com/penpot"]]]
|
|
(t/testing public-uri
|
|
(let [result (emails/render-template "password-recovery"
|
|
{:to "member@example.com"
|
|
:name "Member"
|
|
:token "test-token"
|
|
:public-uri public-uri})
|
|
html (get-in result [:body "text/html"])
|
|
link (str expected-base "/?screen=auth-recovery&token=test-token")]
|
|
(t/is (str/includes? (email-text-body result) link))
|
|
(t/is (str/includes? html (str "href=\"" link "\"")))
|
|
(t/is (str/includes? html (str "src=\"" expected-base "/images/email/logo-penpot.svg\"")))))))
|
|
|
|
(defn- invite-email-params
|
|
[organization]
|
|
{:to "invitee@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:user-name "Invitee User"
|
|
:token "test-token"
|
|
:organization organization})
|
|
|
|
(t/deftest invite-to-organization-includes-sso-notice-when-active
|
|
(let [result (emails/render emails/invite-to-organization
|
|
(invite-email-params {:name "Acme Inc"
|
|
:sso-active true}))]
|
|
(t/is (str/includes? (email-text-body result) sso-notice-snippet))
|
|
(t/is (str/includes? (get-in result [:body "text/html"]) sso-notice-snippet))))
|
|
|
|
(t/deftest invite-to-organization-omits-sso-notice-when-inactive
|
|
(let [result (emails/render emails/invite-to-organization
|
|
(invite-email-params {:name "Acme Inc"
|
|
:sso-active false}))]
|
|
(t/is (not (str/includes? (email-text-body result) sso-notice-snippet)))
|
|
(t/is (not (str/includes? (get-in result [:body "text/html"]) sso-notice-snippet)))))
|
|
|
|
(t/deftest invite-to-team-includes-sso-notice-when-active
|
|
(let [result (emails/render emails/invite-to-team
|
|
{:to "invitee@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:team "Design Team"
|
|
:token "test-token"
|
|
:organization {:name "Acme Inc"
|
|
:sso-active true}})]
|
|
(t/is (str/includes? (email-text-body result) sso-notice-snippet))
|
|
(t/is (str/includes? (get-in result [:body "text/html"]) sso-notice-snippet))))
|
|
|
|
(t/deftest invite-to-team-omits-sso-notice-when-inactive
|
|
(let [result (emails/render emails/invite-to-team
|
|
{:to "invitee@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:team "Design Team"
|
|
:token "test-token"
|
|
:organization {:name "Acme Inc"
|
|
:sso-active false}})]
|
|
(t/is (not (str/includes? (email-text-body result) sso-notice-snippet)))
|
|
(t/is (not (str/includes? (get-in result [:body "text/html"]) sso-notice-snippet)))))
|
|
|
|
(t/deftest invite-to-team-omits-sso-notice-without-organization
|
|
(let [result (emails/render emails/invite-to-team
|
|
{:to "invitee@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:team "Design Team"
|
|
:token "test-token"})]
|
|
(t/is (not (str/includes? (email-text-body result) sso-notice-snippet)))
|
|
(t/is (not (str/includes? (get-in result [:body "text/html"]) sso-notice-snippet)))))
|
|
|
|
(defn- renewal-notice-params
|
|
[user-name]
|
|
{:to "billing@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:user-name user-name
|
|
:renewal-date "2026-01-01"
|
|
:estimated-amount "$42.00"
|
|
:organizations [{:name "Acme"
|
|
:initials "AC"}]})
|
|
|
|
(t/deftest renewal-notice-greets-by-name-when-user-name-present
|
|
(let [result (emails/render emails/renewal-notice (renewal-notice-params "Acme Org"))]
|
|
(t/is (str/includes? (email-text-body result) "Hi Acme Org,"))
|
|
(t/is (str/includes? (get-in result [:body "text/html"]) "Hi Acme Org,"))))
|
|
|
|
(t/deftest renewal-notice-omits-space-before-comma-when-user-name-is-empty
|
|
(let [result (emails/render emails/renewal-notice (renewal-notice-params ""))]
|
|
(t/is (str/includes? (email-text-body result) "Hi,"))
|
|
(t/is (not (str/includes? (email-text-body result) "Hi ,")))
|
|
(t/is (str/includes? (get-in result [:body "text/html"]) "Hi,"))
|
|
(t/is (not (str/includes? (get-in result [:body "text/html"]) "Hi ,")))))
|
|
|
|
(t/deftest renewal-notice-omits-space-before-comma-when-user-name-is-nil
|
|
(let [result (emails/render emails/renewal-notice (renewal-notice-params nil))]
|
|
(t/is (str/includes? (email-text-body result) "Hi,"))
|
|
(t/is (not (str/includes? (email-text-body result) "Hi ,")))
|
|
(t/is (str/includes? (get-in result [:body "text/html"]) "Hi,"))
|
|
(t/is (not (str/includes? (get-in result [:body "text/html"]) "Hi ,")))))
|
|
|
|
(t/deftest send-with-reuse-conn-and-no-conn-raises
|
|
(t/testing "reuse-conn without a caller connection fails fast"
|
|
(t/is (thrown-with-msg? clojure.lang.ExceptionInfo #"reuse-conn"
|
|
(emails/send {}
|
|
{::emails/reuse-conn true
|
|
::emails/factory (fn [_] {:to "a@example.com"})})))))
|
|
|
|
(t/deftest send-with-reuse-conn-joins-caller-transaction
|
|
(let [job-def (ig/init-key ::emails/job-def
|
|
{::emails/sendmail (fn [_] nil)})
|
|
defs {:sendmail job-def}
|
|
email {:to "a@example.com" :subject "hi" :body "hello"}
|
|
job-id (db/tx-run! {::db/pool th/*pool*}
|
|
(fn [cfg]
|
|
(emails/send (assoc cfg
|
|
::jobs/defs defs
|
|
::mtx/metrics (get th/*system* :app.metrics/metrics))
|
|
(assoc email ::emails/reuse-conn true))))]
|
|
(t/testing "the email job is submitted through the caller conn"
|
|
(t/is (= "sendmail" (:name (th/db-get :job {:id job-id})))))))
|
|
|
|
(defn- templated-job-def
|
|
[sendmail]
|
|
(ig/init-key ::emails/templated-job-def
|
|
{::emails/sendmail sendmail}))
|
|
|
|
(defn- submit-templated
|
|
[job-def params]
|
|
(db/tx-run! {::db/pool th/*pool*}
|
|
(fn [cfg]
|
|
(emails/send-template (assoc cfg
|
|
::jobs/defs {:send-templated-mail job-def}
|
|
::mtx/metrics (get th/*system* :app.metrics/metrics))
|
|
(assoc params ::emails/reuse-conn true)))))
|
|
|
|
(def ^:private invite-to-team-recipe
|
|
{:to "invitee@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:team "Design Team"
|
|
:token "test-token"
|
|
:organization {:name "Acme Inc"
|
|
:sso-active false}})
|
|
|
|
(t/deftest templated-registry-covers-all-factories
|
|
(t/is (= #{"feedback" "register" "password-recovery" "password-changed"
|
|
"change-email" "invite-to-team" "invite-to-organization"
|
|
"organization-setup-sso" "renewal-notice" "join-team"
|
|
"request-file-access" "request-file-access-yourpenpot"
|
|
"request-file-access-yourpenpot-view" "request-team-access"
|
|
"comment-mention" "comment-thread" "comment-notification"}
|
|
(set (keys emails/templates)))))
|
|
|
|
(t/deftest send-template-validation
|
|
(t/testing "missing template fails fast"
|
|
(t/is (thrown-with-msg? clojure.lang.ExceptionInfo #"template"
|
|
(emails/send-template {} {:to "a@example.com"}))))
|
|
(t/testing "unknown template fails fast"
|
|
(t/is (thrown-with-msg? clojure.lang.ExceptionInfo #"unknown email template"
|
|
(emails/send-template {} {:to "a@example.com"
|
|
::emails/template "nope"}))))
|
|
(t/testing "template ids are strings, never keywords"
|
|
(t/is (thrown-with-msg? clojure.lang.ExceptionInfo #"unknown email template"
|
|
(emails/send-template {} {:to "a@example.com"
|
|
::emails/template :app.email/register}))))
|
|
(t/testing "reuse-conn without a caller connection fails fast"
|
|
(t/is (thrown-with-msg? clojure.lang.ExceptionInfo #"reuse-conn"
|
|
(emails/send-template {} {:to "a@example.com"
|
|
::emails/reuse-conn true
|
|
::emails/template "register"})))))
|
|
|
|
(t/deftest send-template-stores-recipe-not-rendered-mail
|
|
(let [job-def (templated-job-def (fn [_] nil))
|
|
job-id (submit-templated job-def (assoc invite-to-team-recipe
|
|
::emails/template "invite-to-team"))
|
|
row (th/db-get :job {:id job-id})
|
|
params (jobs/decode-params job-def (:params row))
|
|
rendered (emails/render emails/invite-to-team invite-to-team-recipe)]
|
|
(t/testing "the job lands on the new queue"
|
|
(t/is (= "send-templated-mail" (:name row))))
|
|
(t/testing "stored params carry the template id and no rendered content"
|
|
(t/is (= "invite-to-team" (:template params)))
|
|
(t/is (not (contains? params :subject)))
|
|
(t/is (not (contains? params :body))))
|
|
(t/testing "stored params are smaller than the rendered html alone"
|
|
(let [stored-size (count (str (:params row)))
|
|
rendered-html (count (get-in rendered [:body "text/html"]))]
|
|
(t/is (< stored-size rendered-html))))
|
|
(t/testing "the worker renders the same mail from the stored recipe"
|
|
(t/is (= rendered (emails/render-template (:template params)
|
|
(dissoc params :template)))))))
|
|
|
|
(defn- decode-template-data
|
|
[params]
|
|
(let [decode (get-in emails/templates [(:template params) ::emails/decode])]
|
|
(decode (dissoc params :template))))
|
|
|
|
(t/deftest send-template-roundtrips-uuids-and-profile
|
|
(let [sent (atom nil)
|
|
job-def (templated-job-def (fn [_ email] (reset! sent email)))
|
|
team-id (random-uuid)
|
|
recipe {:to "member@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:team "Design Team"
|
|
:team-id team-id}
|
|
job-id (submit-templated job-def (assoc recipe
|
|
::emails/template "join-team"))
|
|
params (jobs/decode-params job-def (:params (th/db-get :job {:id job-id})))]
|
|
(t/testing "uuids survive the plain-JSON queue roundtrip typed"
|
|
(let [data (decode-template-data params)]
|
|
(t/is (uuid? (:team-id data)))
|
|
(t/is (= team-id (:team-id data)))))
|
|
(t/testing "the handler renders and delivers the mail"
|
|
((::jobs/handler job-def) nil params)
|
|
(t/is (= (:subject (emails/render emails/join-team recipe))
|
|
(:subject @sent)))
|
|
(t/is (= "member@example.com" (:to @sent)))))
|
|
(let [sent (atom nil)
|
|
job-def (templated-job-def (fn [_ email] (reset! sent email)))
|
|
recipe {:to "dest@example.com"
|
|
:reply-to "user@example.com"
|
|
:email "user@example.com"
|
|
:attachments {"error-report.txt" "trace"}
|
|
:feedback-subject "subj"
|
|
:feedback-type "bug"
|
|
:feedback-content "content"
|
|
:profile {:id (random-uuid) :fullname "User" :email "user@example.com"}}
|
|
job-id (submit-templated job-def (assoc recipe
|
|
::emails/template "feedback"))
|
|
params (jobs/decode-params job-def (:params (th/db-get :job {:id job-id})))]
|
|
(t/testing "nested profile id survives the roundtrip typed"
|
|
(t/is (uuid? (get-in (decode-template-data params) [:profile :id]))))
|
|
(t/testing "the handler renders the feedback mail"
|
|
((::jobs/handler job-def) nil params)
|
|
(t/is (= (:subject (emails/render emails/user-feedback recipe))
|
|
(:subject @sent))))))
|
|
|
|
(def ^:private organization-with-uris
|
|
{:id (random-uuid)
|
|
:name "Acme Inc"
|
|
:initials "AI"
|
|
:logo (u/uri "https://example.com/assets/by-id/logo")
|
|
:avatar-bg-url (u/uri "https://example.com/images/avatar-bg.jpg")
|
|
:sso-active false})
|
|
|
|
(t/deftest send-template-roundtrips-organization-uris
|
|
(doseq [[template factory recipe]
|
|
[["invite-to-team" emails/invite-to-team
|
|
{:to "invitee@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:team "Design Team"
|
|
:organization organization-with-uris
|
|
:token "test-token"}]
|
|
["invite-to-organization" emails/invite-to-organization
|
|
{:to "invitee@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:user-name "Invitee"
|
|
:organization organization-with-uris
|
|
:token "test-token"}]
|
|
["renewal-notice" emails/renewal-notice
|
|
{:to "billing@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:user-name "Billing"
|
|
:renewal-date "2026-12-01"
|
|
:estimated-amount "$10.00"
|
|
:organizations [organization-with-uris]}]]]
|
|
(t/testing template
|
|
(let [sent (atom nil)
|
|
job-def (templated-job-def (fn [_ email] (reset! sent email)))
|
|
job-id (submit-templated job-def (assoc recipe ::emails/template template))
|
|
params (jobs/decode-params job-def (:params (th/db-get :job {:id job-id})))]
|
|
((::jobs/handler job-def) nil params)
|
|
(t/is (= (emails/render factory recipe) @sent))))))
|
|
|
|
;; `send-template` drops nil values before enqueuing, so a nil
|
|
;; `:user-name` (e.g. inviting an unregistered email) must still render.
|
|
(t/deftest send-template-renders-with-nil-user-name
|
|
(doseq [[template factory recipe]
|
|
[["invite-to-organization" emails/invite-to-organization
|
|
{:to "invitee@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:invited-by "Owner User"
|
|
:user-name nil
|
|
:organization {:name "Acme Inc"}
|
|
:token "test-token"}]
|
|
["renewal-notice" emails/renewal-notice
|
|
{:to "billing@example.com"
|
|
:public-uri (cf/get :public-uri)
|
|
:user-name nil
|
|
:renewal-date "2026-12-01"
|
|
:estimated-amount "$10.00"
|
|
:organizations [{:name "Acme"}]}]]]
|
|
(t/testing template
|
|
(let [sent (atom nil)
|
|
job-def (templated-job-def (fn [_ email] (reset! sent email)))
|
|
job-id (submit-templated job-def (assoc recipe ::emails/template template))
|
|
params (jobs/decode-params job-def (:params (th/db-get :job {:id job-id})))]
|
|
((::jobs/handler job-def) nil params)
|
|
(t/is (= (emails/render factory recipe) @sent))))))
|
|
|
|
(t/deftest send-templated-mail-handler-rejects-unknown-template
|
|
(let [job-def (templated-job-def (fn [_] nil))
|
|
code (try
|
|
((::jobs/handler job-def) nil {:template "nope"
|
|
:to "a@example.com"})
|
|
nil
|
|
(catch clojure.lang.ExceptionInfo e
|
|
(-> e ex-data :code)))]
|
|
(t/is (= :unknown-email-template code))))
|