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
1432 lines
80 KiB
Clojure
1432 lines
80 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.auth-oidc-test
|
|
(:require
|
|
[app.auth.oidc :as oidc]
|
|
[app.common.data :as d]
|
|
[app.common.exceptions :as ex]
|
|
[app.common.time :as ct]
|
|
[app.common.uri :as u]
|
|
[app.common.uuid :as uuid]
|
|
[app.config :as cf]
|
|
[app.http :as-alias http]
|
|
[app.http.middleware :as mw]
|
|
[app.http.session :as session]
|
|
[app.rpc :as-alias rpc]
|
|
[app.rpc.commands.nitrate :as nitrate-rpc]
|
|
[app.setup :as-alias setup]
|
|
[app.tokens :as tokens]
|
|
[clojure.test :as t]
|
|
[cuerdas.core :as str]
|
|
[mockery.core :refer [with-mocks]]
|
|
[yetti.response :as-alias yres]))
|
|
|
|
(def ^:private oidc-provider
|
|
{:id "oidc"
|
|
:type "oidc"})
|
|
|
|
(t/deftest parse-attr-path-supports-dot-and-double-underscore
|
|
(t/is
|
|
(= [:oidc/resource-access :penpot_roles :roles]
|
|
(#'oidc/parse-attr-path oidc-provider "resource_access__penpot_roles__roles")))
|
|
(t/is
|
|
(= [:oidc/ocs :data :email]
|
|
(#'oidc/parse-attr-path oidc-provider "ocs.data.email"))))
|
|
|
|
(t/deftest process-user-info-supports-dot-notation-nested-attrs
|
|
(let [provider (assoc oidc-provider
|
|
:email-attr "ocs.data.email"
|
|
:name-attr "ocs.data.display-name")
|
|
info (#'oidc/process-user-info provider
|
|
{}
|
|
{:email_verified true
|
|
:ocs {:data {:email "nextcloud@example.com"
|
|
:display-name "Nextcloud User"}}})]
|
|
(t/is (= "nextcloud@example.com" (:email info)))
|
|
(t/is (= "Nextcloud User" (:fullname info)))
|
|
(t/is (true? (:email-verified info)))))
|
|
|
|
;; The provider's `:user-info-source` value arrives as a string (enforced by
|
|
;; the malli schema in `app.config` and used as-is by the hard-coded Google /
|
|
;; GitHub provider maps), so the dispatch must interpret strings — not
|
|
;; keywords — to actually honour `PENPOT_OIDC_USER_INFO_SOURCE=userinfo`.
|
|
(t/deftest select-user-info-source-interprets-config-strings
|
|
(t/testing "explicit string values map to keyword dispatch tokens"
|
|
(t/is (= :token (#'oidc/select-user-info-source "token")))
|
|
(t/is (= :userinfo (#'oidc/select-user-info-source "userinfo"))))
|
|
|
|
(t/testing "missing or explicit \"auto\" falls back to auto dispatch"
|
|
(t/is (= :auto (#'oidc/select-user-info-source "auto")))
|
|
(t/is (= :auto (#'oidc/select-user-info-source nil))))
|
|
|
|
(t/testing "unknown values fall back to auto dispatch safely"
|
|
(t/is (= :auto (#'oidc/select-user-info-source "unknown")))
|
|
;; Guards against the reverse regression — a stray keyword value must
|
|
;; not silently slip through as if it were the matching string.
|
|
(t/is (= :auto (#'oidc/select-user-info-source :token)))
|
|
(t/is (= :auto (#'oidc/select-user-info-source :userinfo)))))
|
|
|
|
(t/deftest token-endpoint-errors-detect-valid-client-credentials
|
|
(let [response {:status 403
|
|
:body "{\"error\":\"invalid_grant\",\"error_description\":\"Invalid authorization code\"}"}]
|
|
(t/is (#'oidc/token-endpoint-valid-client-error? response))
|
|
(t/is (not (#'oidc/token-endpoint-invalid-client-error? response)))))
|
|
|
|
(t/deftest token-endpoint-errors-detect-invalid-client-credentials
|
|
(t/is (#'oidc/token-endpoint-invalid-client-error?
|
|
{:status 401
|
|
:body "{\"error\":\"access_denied\",\"error_description\":\"Unauthorized\"}"}))
|
|
(t/is (#'oidc/token-endpoint-invalid-client-error?
|
|
{:status 400
|
|
:body "{\"error\":\"invalid_client\"}"}))
|
|
(t/is (not (#'oidc/token-endpoint-valid-client-error?
|
|
{:status 400
|
|
:body "{\"error\":\"invalid_client\"}"}))))
|
|
|
|
(t/deftest int-in-range-checks-range-correctly
|
|
(t/testing "values within range return true"
|
|
(t/is (#'oidc/int-in-range? 200 200 300))
|
|
(t/is (#'oidc/int-in-range? 250 200 300))
|
|
(t/is (#'oidc/int-in-range? 299 200 300)))
|
|
(t/testing "values outside range return false"
|
|
(t/is (not (#'oidc/int-in-range? 199 200 300)))
|
|
(t/is (not (#'oidc/int-in-range? 300 200 300)))))
|
|
|
|
(t/deftest redirect-response-builds-302-response
|
|
(let [result (#'oidc/redirect-response "https://example.com/path")]
|
|
(t/is (= 302 (::yres/status result)))
|
|
(t/is (= "https://example.com/path" (get-in result [::yres/headers "location"])))))
|
|
|
|
(t/deftest valid-info-validates-info-map
|
|
(t/testing "valid info maps pass validation"
|
|
(t/is (#'oidc/valid-info?
|
|
{:backend "oidc" :email "user@example.com" :fullname "User"
|
|
:email-verified true :props {:foo 1}})))
|
|
(t/testing "incomplete maps fail validation"
|
|
(t/is (not (#'oidc/valid-info? nil)))
|
|
(t/is (not (#'oidc/valid-info? {})))
|
|
(t/is (not (#'oidc/valid-info? {:backend "oidc"})))
|
|
(t/is (not (#'oidc/valid-info? {:backend "oidc" :email "user@example.com"})))
|
|
(t/is (not (#'oidc/valid-info? {:backend "oidc" :email "user@example.com" :fullname "User"})))
|
|
(t/is (not (#'oidc/valid-info?
|
|
{:backend "oidc" :email "user@example.com" :fullname "User" :email-verified true})))))
|
|
|
|
(t/deftest qualify-prop-key-qualifies-key
|
|
(let [provider {:type "github"}]
|
|
(t/is (= :github/email (#'oidc/qualify-prop-key provider :email)))
|
|
(t/is (= :github/full-name (#'oidc/qualify-prop-key provider :full_name)))
|
|
(t/is (= :github/my-key (#'oidc/qualify-prop-key provider :my-key)))))
|
|
|
|
(t/deftest qualify-props-qualifies-all-keys
|
|
(let [provider {:type "github"}
|
|
result (#'oidc/qualify-props provider {:email "u@e.com" :name "Test"})]
|
|
(t/is (= "u@e.com" (:github/email result)))
|
|
(t/is (= "Test" (:github/name result)))))
|
|
|
|
(t/deftest provider-has-email-verified-checks-email-verified
|
|
(let [provider {:type "github"}]
|
|
(t/testing "returns true when email_verified in props is true"
|
|
(t/is (#'oidc/provider-has-email-verified? provider {:props {:github/email-verified true}})))
|
|
(t/testing "returns false when email_verified is false or missing"
|
|
(t/is (not (#'oidc/provider-has-email-verified? provider {:props {}})))
|
|
(t/is (not (#'oidc/provider-has-email-verified? provider {:props {:github/email-verified false}}))))))
|
|
|
|
(t/deftest profile-has-provider-props-matches-provider
|
|
(t/testing "non-OIDC provider with string id checks for qualified email key"
|
|
(let [provider {:type "github" :id "github"}]
|
|
(t/is (#'oidc/profile-has-provider-props? provider {:props {:github/email "u@e.com"}}))
|
|
(t/is (not (#'oidc/profile-has-provider-props? provider {:props {}})))
|
|
(t/is (not (#'oidc/profile-has-provider-props? provider {:props nil})))))
|
|
(t/testing "OIDC provider with UUID id checks oidc/provider-id"
|
|
(let [provider {:type "oidc" :id #uuid "00000000-0000-0000-0000-000000000001"}]
|
|
(t/is (#'oidc/profile-has-provider-props?
|
|
provider {:props {:oidc/provider-id "00000000-0000-0000-0000-000000000001"}}))
|
|
(t/is (not (#'oidc/profile-has-provider-props? provider {:props {:oidc/provider-id "other"}}))))))
|
|
|
|
(t/deftest redirect-with-error-builds-error-url
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(t/testing "with error and hint"
|
|
(let [result (#'oidc/redirect-with-error "auth-error" "hint message")
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (= 302 (::yres/status result)))
|
|
(t/is (.contains loc "http://localhost:3449?screen=auth-login&"))
|
|
(t/is (.contains loc "error=auth-error"))
|
|
(t/is (.contains loc "hint=hint"))))
|
|
(t/testing "without hint omits hint param"
|
|
(let [result (#'oidc/redirect-with-error "auth-error")
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (.contains loc "error=auth-error"))
|
|
(t/is (not (.contains loc "hint=")))))))
|
|
|
|
(t/deftest redirect-with-error-preserves-public-uri-subpath
|
|
(binding [cf/config {:public-uri "http://localhost:3449/penpot"}]
|
|
(let [result (#'oidc/redirect-with-error "auth-error")
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (.contains loc "http://localhost:3449/penpot?screen=auth-login&"))
|
|
(t/is (.contains loc "error=auth-error")))))
|
|
|
|
(t/deftest redirect-to-verify-token-builds-verify-url
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(let [result (#'oidc/redirect-to-verify-token "test-token-value")
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (= 302 (::yres/status result)))
|
|
(t/is (.contains loc "http://localhost:3449?screen=auth-verify-token&"))
|
|
(t/is (.contains loc "token=test-token-value")))))
|
|
|
|
(t/deftest redirect-to-verify-token-preserves-public-uri-subpath
|
|
(binding [cf/config {:public-uri "http://localhost:3449/penpot"}]
|
|
(let [result (#'oidc/redirect-to-verify-token "test-token-value")
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (.contains loc "http://localhost:3449/penpot?screen=auth-verify-token&")))))
|
|
|
|
(t/deftest build-redirect-uri-constructs-redirect
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(t/is (= "http://localhost:3449/api/auth/oidc/callback"
|
|
(#'oidc/build-redirect-uri)))))
|
|
|
|
(t/deftest build-redirect-uri-preserves-public-uri-subpath
|
|
(binding [cf/config {:public-uri "http://localhost:3449/penpot"}]
|
|
(t/is (= "http://localhost:3449/penpot/api/auth/oidc/callback"
|
|
(#'oidc/build-redirect-uri)))))
|
|
|
|
(t/deftest build-redirect-uri-with-subpath-and-trailing-slash
|
|
(binding [cf/config {:public-uri "http://localhost:3449/penpot/"}]
|
|
(t/is (= "http://localhost:3449/penpot/api/auth/oidc/callback"
|
|
(#'oidc/build-redirect-uri)))))
|
|
|
|
(t/deftest redirect-with-error-subpath-screen-query
|
|
(binding [cf/config {:public-uri "http://localhost:3449/penpot"}]
|
|
(let [result (#'oidc/redirect-with-error "auth-error" "hint message")
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (.contains loc "http://localhost:3449/penpot?screen=auth-login&"))
|
|
(t/is (.contains loc "error=auth-error"))
|
|
(t/is (.contains loc "hint=hint"))
|
|
;; Screen routing lives in the query string, not the fragment
|
|
(t/is (not (.contains loc "#"))))))
|
|
|
|
(t/deftest redirect-to-register-subpath
|
|
(let [test-key (byte-array (map byte (range 32)))
|
|
cfg {::setup/props {:tokens-key test-key}}]
|
|
(binding [cf/config {:public-uri "http://localhost:3449/penpot"}]
|
|
(let [info {:email "u@e.com" :fullname "U" :backend "oidc"
|
|
:email-verified false :props {}}
|
|
provider {:type "oidc" :id "oidc"}
|
|
result (#'oidc/redirect-to-register cfg info provider)
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (.contains loc "http://localhost:3449/penpot?screen=auth-register-validate&"))
|
|
(t/is (.contains loc "token="))))))
|
|
|
|
(t/deftest redirect-to-register-no-subpath
|
|
(let [test-key (byte-array (map byte (range 32)))
|
|
cfg {::setup/props {:tokens-key test-key}}]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(let [info {:email "u@e.com" :fullname "U" :backend "oidc"
|
|
:email-verified false :props {}}
|
|
provider {:type "oidc" :id "oidc"}
|
|
result (#'oidc/redirect-to-register cfg info provider)
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (.contains loc "http://localhost:3449?screen=auth-register-validate&"))
|
|
(t/is (.contains loc "token="))))))
|
|
|
|
(t/deftest redirect-to-verify-token-subpath-no-trailing-slash
|
|
(binding [cf/config {:public-uri "http://localhost:3449/my-app"}]
|
|
(let [result (#'oidc/redirect-to-verify-token "test-token-value")
|
|
loc (get-in result [::yres/headers "location"])]
|
|
(t/is (.contains loc "http://localhost:3449/my-app?screen=auth-verify-token&")))))
|
|
|
|
(t/deftest fetch-user-info-returns-decoded-body-on-success
|
|
(let [cfg {}
|
|
provider {:user-uri "https://provider.example.com/userinfo"}
|
|
tdata {:token/access "test-access-token" :token/type "Bearer"}]
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return {:status 200
|
|
:body "{\"email\":\"user@example.com\",\"name\":\"Test User\"}"}}]
|
|
(let [result (#'oidc/fetch-user-info cfg provider tdata)]
|
|
(t/is (:called? @http-mock))
|
|
(t/is (= 1 (:call-count @http-mock)))
|
|
(t/is (= "user@example.com" (:email result)))
|
|
(t/is (= "Test User" (:name result)))))))
|
|
|
|
(t/deftest fetch-user-info-throws-on-non-2xx
|
|
(let [cfg {}
|
|
provider {:user-uri "https://provider.example.com/userinfo"}
|
|
tdata {:token/access "test-at" :token/type "Bearer"}]
|
|
(t/testing "401 with Bad credentials"
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return {:status 401
|
|
:body "Bad credentials"}}]
|
|
(let [e (try (#'oidc/fetch-user-info cfg provider tdata) (catch Throwable t t))]
|
|
(t/is (instance? clojure.lang.ExceptionInfo e))
|
|
(t/is (= :unable-to-retrieve-user-info (:code (ex-data e))))
|
|
(t/is (= 401 (:http-status (ex-data e))))
|
|
(t/is (= "Bad credentials" (:http-body (ex-data e)))))))
|
|
(t/testing "500 server error"
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return {:status 500
|
|
:body "Internal Server Error"}}]
|
|
(let [e (try (#'oidc/fetch-user-info cfg provider tdata) (catch Throwable t t))]
|
|
(t/is (instance? clojure.lang.ExceptionInfo e))
|
|
(t/is (= :unable-to-retrieve-user-info (:code (ex-data e))))
|
|
(t/is (= 500 (:http-status (ex-data e)))))))))
|
|
|
|
(t/deftest fetch-user-info-passes-correct-request
|
|
(let [cfg {}
|
|
provider {:user-uri "https://provider.example.com/userinfo" :skip-ssrf-check? true}
|
|
tdata {:token/access "secret-token" :token/type "Bearer"}]
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return {:status 200 :body "{}"}}]
|
|
(#'oidc/fetch-user-info cfg provider tdata)
|
|
(let [[_ req-opts opts] (-> @http-mock :call-args)]
|
|
(t/is (true? (:skip-ssrf-check? opts)))
|
|
(t/is (= "https://provider.example.com/userinfo" (:uri req-opts)))
|
|
(t/is (= "Bearer secret-token" (get-in req-opts [:headers "Authorization"])))
|
|
(t/is (= :get (:method req-opts)))))))
|
|
|
|
(t/deftest fetch-access-token-returns-token-data-on-success
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(let [cfg {}
|
|
provider {:client-id "test-client"
|
|
:client-secret "test-secret"
|
|
:token-uri "https://provider.example.com/token"}
|
|
code "auth-code-123"]
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return {:status 200
|
|
:body "{\"access_token\":\"at\",\"id_token\":\"it\",\"token_type\":\"Bearer\"}"}}]
|
|
(let [result (#'oidc/fetch-access-token cfg provider code)]
|
|
(t/is (:called? @http-mock))
|
|
(t/is (= 1 (:call-count @http-mock)))
|
|
(t/is (= "at" (:token/access result)))
|
|
(t/is (= "it" (:token/id result)))
|
|
(t/is (= "Bearer" (:token/type result))))))))
|
|
|
|
(t/deftest fetch-access-token-throws-on-error
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(let [cfg {}
|
|
provider {:client-id "test-client"
|
|
:client-secret "test-secret"
|
|
:token-uri "https://provider.example.com/token"}
|
|
code "auth-code-123"]
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return {:status 400 :body "{\"error\":\"invalid_grant\"}"}}]
|
|
(let [e (try (#'oidc/fetch-access-token cfg provider code) (catch Throwable t t))]
|
|
(t/is (instance? clojure.lang.ExceptionInfo e))
|
|
(t/is (= :unable-to-fetch-access-token (:code (ex-data e)))))))))
|
|
|
|
;; Shared mock data for get-info tests
|
|
(def ^:private mock-tdata
|
|
{:token/access "mock-at" :token/id nil :token/type "Bearer"})
|
|
|
|
(def ^:private mock-claims
|
|
{:email "user@example.com" :name "User" :exp 1 :iss "test"})
|
|
|
|
(def ^:private mock-userinfo
|
|
{:email "user@example.com" :name "User"})
|
|
|
|
(t/deftest get-info-uses-token-source
|
|
(let [provider {:type "oidc" :user-info-source "token"}
|
|
state {}
|
|
code "code"]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly mock-claims)]
|
|
(let [result (#'oidc/get-info {} provider state code)]
|
|
(t/is (= "user@example.com" (:email result)))
|
|
(t/is (= "User" (:fullname result)))
|
|
(t/is (= "oidc" (:backend result)))
|
|
(t/is (= false (:email-verified result)))))))
|
|
|
|
(t/deftest get-info-uses-userinfo-source
|
|
(let [provider {:type "oidc" :user-info-source "userinfo"}
|
|
state {}
|
|
code "code"]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly nil)
|
|
app.auth.oidc/fetch-user-info (constantly mock-userinfo)]
|
|
(let [result (#'oidc/get-info {} provider state code)]
|
|
(t/is (= "user@example.com" (:email result)))
|
|
(t/is (= "User" (:fullname result)))
|
|
(t/is (= "oidc" (:backend result)))))))
|
|
|
|
(t/deftest get-info-auto-prefers-claims
|
|
(let [provider {:type "oidc" :user-info-source "auto"}
|
|
state {}
|
|
code "code"]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly mock-claims)
|
|
app.auth.oidc/fetch-user-info (fn [& _] (throw (Exception. "should not call")))]
|
|
|
|
(let [result (#'oidc/get-info {} provider state code)]
|
|
(t/is (= "user@example.com" (:email result)))
|
|
(t/is (= "User" (:fullname result)))))))
|
|
|
|
(t/deftest get-info-auto-falls-back-to-userinfo
|
|
(let [provider {:type "oidc" :user-info-source "auto"}
|
|
state {}
|
|
code "code"]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly nil)
|
|
app.auth.oidc/fetch-user-info (constantly mock-userinfo)]
|
|
(let [result (#'oidc/get-info {} provider state code)]
|
|
(t/is (= "user@example.com" (:email result)))
|
|
(t/is (= "User" (:fullname result)))))))
|
|
|
|
(t/deftest get-info-throws-on-incomplete-info
|
|
(let [provider {:type "oidc" :user-info-source "userinfo"}
|
|
state {}
|
|
code "code"]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly nil)
|
|
app.auth.oidc/fetch-user-info (constantly {:no-email nil})]
|
|
(let [e (try (#'oidc/get-info {} provider state code) (catch Throwable t t))]
|
|
(t/is (instance? clojure.lang.ExceptionInfo e))
|
|
(t/is (= :incomplete-user-info (:code (ex-data e))))))))
|
|
|
|
(t/deftest get-info-checks-roles-satisfied
|
|
(let [provider {:type "oidc" :user-info-source "token" :roles #{"member"}}
|
|
state {}
|
|
code "code"
|
|
claims (assoc mock-claims :roles ["member" "admin"])]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly claims)]
|
|
(let [result (#'oidc/get-info {} provider state code)]
|
|
(t/is (= "user@example.com" (:email result)))
|
|
(t/is (= "oidc" (:backend result)))))))
|
|
|
|
(t/deftest get-info-throws-on-insufficient-roles
|
|
(let [provider {:type "oidc" :user-info-source "token" :roles #{"admin"}}
|
|
state {}
|
|
code "code"
|
|
claims (assoc mock-claims :roles ["member"])]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly claims)]
|
|
(let [e (try (#'oidc/get-info {} provider state code) (catch Throwable t t))]
|
|
(t/is (instance? clojure.lang.ExceptionInfo e))
|
|
(t/is (= :unable-to-auth (:code (ex-data e))))))))
|
|
|
|
(t/deftest get-info-merges-state-props
|
|
(let [provider {:type "oidc" :user-info-source "token"}
|
|
state {:invitation-token "inv-123"
|
|
:external-session-id "ext-456"
|
|
:props {:utm_source "twitter"}}
|
|
code "code"]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly mock-claims)]
|
|
(let [result (#'oidc/get-info {} provider state code)]
|
|
(t/is (= "inv-123" (:invitation-token result)))
|
|
(t/is (= "ext-456" (:external-session-id result)))
|
|
(t/is (= "twitter" (get-in result [:props :utm_source])))))))
|
|
|
|
(t/deftest get-info-adds-sso-session-id-from-claims
|
|
(let [provider {:type "oidc" :user-info-source "token"}
|
|
state {}
|
|
code "code"
|
|
claims (assoc mock-claims :sid "sso-sid")]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly claims)]
|
|
(let [result (#'oidc/get-info {} provider state code)]
|
|
(t/is (= "sso-sid" (:sso-session-id result)))))))
|
|
|
|
(t/deftest get-info-adds-sso-provider-id-for-uuid-provider
|
|
(let [provider {:type "oidc" :user-info-source "token"
|
|
:id #uuid "00000000-0000-0000-0000-000000000001"}
|
|
state {}
|
|
code "code"]
|
|
(with-redefs [app.auth.oidc/fetch-access-token (constantly mock-tdata)
|
|
app.auth.oidc/get-id-token-claims (constantly mock-claims)]
|
|
(let [result (#'oidc/get-info {} provider state code)]
|
|
(t/is (= #uuid "00000000-0000-0000-0000-000000000001" (:sso-provider-id result)))))))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; callback-handler tests
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(def ^:private test-token-key
|
|
(byte-array (map byte (range 32))))
|
|
|
|
(def ^:private base-cfg
|
|
{::setup/props {:tokens-key test-token-key}
|
|
::session/manager (session/inmemory-manager)
|
|
:app.email/blacklist #{"banned.com"}
|
|
:app.email/whitelist #{"allowed.com"}})
|
|
|
|
(def ^:private test-profile-id
|
|
#uuid "11111111-1111-1111-1111-111111111111")
|
|
|
|
(def ^:private test-organization-id
|
|
#uuid "22222222-2222-2222-2222-222222222222")
|
|
|
|
(def ^:private test-profile
|
|
{:id test-profile-id
|
|
:is-active true
|
|
:is-blocked false
|
|
:auth-backend "oidc"
|
|
:email "user@example.com"
|
|
:props {}})
|
|
|
|
(defn- make-state-token
|
|
[cfg overrides]
|
|
(tokens/generate cfg (d/without-nils (merge {:iss "oidc" :provider "oidc"
|
|
:exp (ct/in-future {:hours 1})}
|
|
overrides))))
|
|
|
|
(defn- default-request
|
|
[cfg & {:keys [state] :or {state "dummy"}}]
|
|
{:params {:state state :code "test-code"}
|
|
:method :get
|
|
:path "/api/auth/oidc/callback"
|
|
:headers {"user-agent" "TestAgent"
|
|
"x-forwarded-for" "127.0.0.1"}
|
|
:remote-addr "127.0.0.1"})
|
|
|
|
(defn- redirect-location
|
|
"Extract the Location header from a handler response."
|
|
[result]
|
|
(get-in result [::yres/headers "location"]))
|
|
|
|
(defn- location-token
|
|
"Extract the `token` query param from a redirect location."
|
|
[location]
|
|
(second (re-find #"[?&]token=([^&]+)" location)))
|
|
|
|
(t/deftest callback-param-error-redirects-to-login
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
request {:params {:error "access_denied"}}]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (= 302 (::yres/status result)))
|
|
(t/is (.contains loc "error=unable-to-auth"))
|
|
(t/is (.contains loc "hint=access_denied"))))))
|
|
|
|
(t/deftest callback-no-profile-registration-disabled
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "error=registration-disabled")))))))
|
|
|
|
(t/deftest callback-with-invitation-and-no-flags-redirects-to-register
|
|
;; Review of #12974: a verifiable invitation issued for the OIDC
|
|
;; email acts as a substitute for the SSO registration flags, so the
|
|
;; user reaches the register screen instead of the
|
|
;; registration-disabled error. The JWT is verified and the email is
|
|
;; compared here; the invitation row is revalidated under a row lock
|
|
;; in `register-profile`.
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
invitation (tokens/generate cfg {:iss :team-invitation
|
|
:exp (ct/in-future {:hours 1})
|
|
:member-email "u@e.com"})
|
|
state (make-state-token cfg {:invitation-token invitation})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:invitation-token invitation
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)
|
|
claims (tokens/decode cfg (location-token loc))]
|
|
(t/is (.contains loc "screen=auth-register-validate"))
|
|
(t/is (.contains loc "token="))
|
|
(t/is (= :prepared-register (:iss claims))
|
|
"the redirect token must be a :prepared-register token")
|
|
(t/is (= invitation (:invitation-token claims))
|
|
"the invitation token must travel inside the prepared-register claims"))))))
|
|
|
|
(t/deftest callback-with-invitation-email-mismatch-redirects-to-registration-disabled
|
|
;; Review of #12974: an invitation issued for another email must not
|
|
;; unlock registration with closed registration.
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
invitation (tokens/generate cfg {:iss :team-invitation
|
|
:exp (ct/in-future {:hours 1})
|
|
:member-email "alice@example.com"})
|
|
state (make-state-token cfg {:invitation-token invitation})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "attacker@example.com" :fullname "A"
|
|
:backend "oidc" :email-verified false
|
|
:invitation-token invitation
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "error=registration-disabled")))))))
|
|
|
|
(t/deftest callback-with-garbage-invitation-redirects-to-registration-disabled
|
|
;; Review of #12974: raw presence is not enough — an unverifiable
|
|
;; invitation string must not reach the register screen.
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {:invitation-token "inv-123"})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:invitation-token "inv-123"
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "error=registration-disabled")))))))
|
|
|
|
(t/deftest callback-profile-blocked
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{:registration}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly (assoc test-profile :is-blocked true))]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "error=profile-blocked")))))))
|
|
|
|
(t/deftest callback-provider-mismatch
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{:registration}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly (assoc test-profile :auth-backend "gitlab"))]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "error=auth-provider-not-allowed")))))))
|
|
|
|
(t/deftest callback-profile-inactive-redirects-to-register
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449/penpot"}
|
|
cf/flags #{:registration}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly (assoc test-profile :is-active false))]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "http://localhost:3449/penpot?screen=auth-register-validate&"))
|
|
(t/is (.contains loc "token=")))))))
|
|
|
|
(t/deftest callback-success-flow
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{:registration}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly test-profile)
|
|
app.auth.oidc/update-profile-with-info (fn [cfg profile info] profile)
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "http://localhost:3449?screen=auth-verify-token&"))
|
|
(t/is (.contains loc "token=")))))))
|
|
|
|
(t/deftest callback-with-invitation-mints-auth-token-with-invitation-claim
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
invitation (tokens/generate cfg {:iss :team-invitation
|
|
:exp (ct/in-future {:hours 1})
|
|
:member-id test-profile-id
|
|
:member-email "u@e.com"})
|
|
state (make-state-token cfg {:invitation-token invitation})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{:registration}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:invitation-token invitation
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly test-profile)
|
|
app.auth.oidc/update-profile-with-info (fn [cfg profile info] profile)
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)
|
|
claims (tokens/decode cfg (location-token loc))]
|
|
(t/is (= :auth (:iss claims))
|
|
"the session token must be an :auth token, not the invitation one")
|
|
(t/is (= invitation (:invitation-token claims))
|
|
"the invitation token must travel inside the :auth claims")
|
|
(t/is (= test-profile-id (:profile-id claims))))))))
|
|
|
|
(t/deftest callback-without-invitation-mints-plain-auth-token
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}
|
|
cf/flags #{:registration}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly test-profile)
|
|
app.auth.oidc/update-profile-with-info (fn [cfg profile info] profile)
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)
|
|
claims (tokens/decode cfg (location-token loc))]
|
|
(t/is (= :auth (:iss claims)))
|
|
(t/is (not (contains? claims :invitation-token))))))))
|
|
|
|
(t/deftest callback-gracefully-handles-unable-to-retrieve-user-info
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (fn [& _]
|
|
(ex/raise :type :internal
|
|
:code :unable-to-retrieve-user-info
|
|
:hint "unable to retrieve user info"
|
|
:http-status 401
|
|
:http-body "Bad credentials"))]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (= 302 (::yres/status result)))
|
|
(t/is (.contains loc "error=unable-to-auth")))))))
|
|
|
|
(t/deftest organization-sso-callback-success-emits-succeeded
|
|
;; No session on the request → auth-succeeded only (no token-issued).
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {:dest-url "https://penpot.example.com/?screen=workspace"
|
|
:organization-id test-organization-id})
|
|
request (default-request cfg :state state)
|
|
events (atom [])]
|
|
(binding [cf/config {:public-uri "https://penpot.example.com"}]
|
|
(with-redefs [app.nitrate/call (constantly {:active true})
|
|
app.auth.oidc/prepare-organization-sso-provider (constantly {:type "oidc"})
|
|
app.auth.oidc/get-info (constantly {})
|
|
app.loggers.audit/submit (fn [_cfg event] (swap! events conj event))]
|
|
(let [result (#'oidc/callback-handler cfg request)]
|
|
(t/is (= "https://penpot.example.com/?screen=workspace" (redirect-location result)))
|
|
(t/is (= ["organization-sso-auth-succeeded"] (mapv :name @events)))
|
|
(t/is (= test-organization-id (get-in (first @events) [:props :organization-id]))))))))
|
|
|
|
(t/deftest organization-sso-callback-success-with-session-emits-token-issued
|
|
(let [manager (session/inmemory-manager)
|
|
cfg (assoc (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
::session/manager manager)
|
|
state (make-state-token cfg {:dest-url "https://penpot.example.com/?screen=workspace"
|
|
:organization-id test-organization-id
|
|
:profile-id test-profile-id})
|
|
request (default-request cfg :state state)
|
|
events (atom [])
|
|
sess {:id (uuid/next)
|
|
:profile-id test-profile-id
|
|
:props {}}]
|
|
(binding [cf/config {:public-uri "https://penpot.example.com"}]
|
|
(with-redefs [app.nitrate/call (constantly {:active true})
|
|
app.auth.oidc/prepare-organization-sso-provider (constantly {:type "oidc"})
|
|
app.auth.oidc/get-info (constantly {})
|
|
app.http.session/get-session (constantly sess)
|
|
app.loggers.audit/submit (fn [_cfg event] (swap! events conj event))]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
names (mapv :name @events)
|
|
stored (session/read-session manager (:id sess))]
|
|
(t/is (= "https://penpot.example.com/?screen=workspace" (redirect-location result)))
|
|
(t/is (= ["organization-sso-token-issued"
|
|
"organization-sso-auth-succeeded"]
|
|
names))
|
|
(t/is (= test-organization-id
|
|
(get-in (first @events) [:props :organization-id])))
|
|
(t/is (= test-profile-id (:profile-id (first @events))))
|
|
(t/is (= #{:organization-id}
|
|
(set (keys (:props (first @events))))))
|
|
(t/is (contains? (get-in stored [:props :sso]) test-organization-id)))))))
|
|
|
|
(t/deftest organization-sso-callback-error-emits-failed
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {:dest-url "https://penpot.example.com/?screen=workspace"
|
|
:organization-id test-organization-id})
|
|
request (default-request cfg :state state)
|
|
events (atom [])]
|
|
(binding [cf/config {:public-uri "https://penpot.example.com"}]
|
|
(with-redefs [app.nitrate/call (fn [_cfg method _params]
|
|
(case method
|
|
:get-organization-sso {:active true}
|
|
:get-organization-summary {:name "Organization"}))
|
|
app.auth.oidc/prepare-organization-sso-provider (constantly {:type "oidc"})
|
|
app.auth.oidc/get-info (fn [& _]
|
|
(ex/raise :type :internal
|
|
:code :unable-to-retrieve-user-info))
|
|
app.loggers.audit/submit (fn [_cfg event] (swap! events conj event))]
|
|
(#'oidc/callback-handler cfg request)
|
|
(t/is (= ["organization-sso-auth-failed"] (mapv :name @events)))
|
|
(t/is (= {:organization-id test-organization-id
|
|
:failure-reason "user-info-failed"}
|
|
(:props (first @events))))))))
|
|
|
|
(t/deftest organization-sso-oauth-error-emits-failed-without-changing-redirect
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {:dest-url "https://penpot.example.com/#/workspace"
|
|
:organization-id test-organization-id})
|
|
request (assoc-in (default-request cfg :state state) [:params :error] "access_denied")
|
|
events (atom [])]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"}]
|
|
(with-redefs [app.loggers.audit/submit (fn [_cfg event] (swap! events conj event))]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "error=unable-to-auth"))
|
|
(t/is (.contains loc "hint=access_denied"))
|
|
(t/is (= ["organization-sso-auth-failed"] (mapv :name @events)))
|
|
(t/is (= {:organization-id test-organization-id
|
|
:failure-reason "access-denied"}
|
|
(:props (first @events)))))))))
|
|
|
|
(t/deftest build-dest-url-builds-the-destination-on-the-public-uri
|
|
(binding [cf/config {:public-uri "https://penpot.example.com"}]
|
|
(t/testing "the route is appended to the public uri, separator included"
|
|
(t/is (= "https://penpot.example.com/?screen=workspace&team-id=0e5f"
|
|
(oidc/build-dest-url "screen=workspace&team-id=0e5f"))))
|
|
|
|
(t/testing "the app base path of the public uri is kept"
|
|
(binding [cf/config {:public-uri "https://host/penpot"}]
|
|
(t/is (= "https://host/penpot/?screen=workspace"
|
|
(oidc/build-dest-url "screen=workspace")))))
|
|
|
|
(t/testing "a blank query string lands on the public uri"
|
|
(t/is (= "https://penpot.example.com/" (oidc/build-dest-url "")))
|
|
(t/is (= "https://penpot.example.com/" (oidc/build-dest-url nil))))
|
|
|
|
(t/testing "a value without params of its own is not a route"
|
|
(doseq [query-string ["https://evil.example/"
|
|
"//evil.example/x"]]
|
|
(t/is (= "https://penpot.example.com/"
|
|
(oidc/build-dest-url query-string)) query-string)))
|
|
|
|
(t/testing "the root of the app keeps its params (invitation, template)"
|
|
(t/is (= "https://penpot.example.com/?template=xyz"
|
|
(oidc/build-dest-url "template=xyz"))))
|
|
|
|
(t/testing "a value that still carries the separator is trimmed"
|
|
(t/is (= "https://penpot.example.com/?screen=workspace"
|
|
(oidc/build-dest-url "?screen=workspace"))))
|
|
|
|
(t/testing "control characters never reach the destination"
|
|
(let [dest (oidc/build-dest-url "screen=x\r\nSet-Cookie: a=b")]
|
|
(t/is (not (.contains dest "\r")))
|
|
(t/is (not (.contains dest "\n")))))
|
|
|
|
(t/testing "repeated params are kept"
|
|
(t/is (= "https://penpot.example.com/?a=1&a=2"
|
|
(oidc/build-dest-url "a=1&a=2"))))))
|
|
|
|
(def ^:private foreign-dest-urls
|
|
"Destinations that a naive prefix check would let through: the first one
|
|
parses the prefix as userinfo, the second one is a look-alike host."
|
|
["https://evil.example/"
|
|
"https://penpot.example.com@evil.example/"
|
|
"https://penpot.example.com.evil.example/"])
|
|
|
|
(t/deftest build-organization-sso-auth-redirect-uri-seals-a-server-built-destination
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
provider {:id "oidc"
|
|
:type "oidc"
|
|
:client-id "client"
|
|
:auth-uri "https://idp.example.com/authorize"
|
|
:scopes ["openid"]}
|
|
sso {:active true
|
|
:issuer "https://idp.example.com"
|
|
:organization-id test-organization-id}]
|
|
(binding [cf/config {:public-uri "https://penpot.example.com"}]
|
|
(doseq [[query-string expected]
|
|
[["screen=workspace" "https://penpot.example.com/?screen=workspace"]
|
|
["template=xyz" "https://penpot.example.com/?template=xyz"]
|
|
["https://evil.example/" "https://penpot.example.com/"]
|
|
["" "https://penpot.example.com/"]]]
|
|
(t/testing (str "the destination sealed for " (pr-str query-string))
|
|
(let [uri (oidc/build-organization-sso-auth-redirect-uri
|
|
cfg sso
|
|
:query-string query-string
|
|
:organization-id test-organization-id
|
|
:profile-id test-profile-id
|
|
:provider provider)
|
|
claims (tokens/decode cfg (-> (u/uri uri)
|
|
:query
|
|
(u/query-string->map)
|
|
:state))]
|
|
;; The destination is always composed by the server: the route
|
|
;; of the client, never a URL of its own.
|
|
(t/is (= expected (:dest-url claims)))
|
|
(t/is (= test-profile-id (:profile-id claims)))))))))
|
|
|
|
(t/deftest organization-sso-callback-ignores-a-destination-outside-the-public-uri
|
|
(doseq [dest-url foreign-dest-urls]
|
|
(t/testing (str "the success path ignores " dest-url)
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {:dest-url dest-url
|
|
:organization-id test-organization-id})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "https://penpot.example.com"}]
|
|
(with-redefs [app.nitrate/call (constantly {:active true})
|
|
app.auth.oidc/prepare-organization-sso-provider (constantly {:type "oidc"})
|
|
app.auth.oidc/get-info (constantly {})
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)]
|
|
(t/is (= "https://penpot.example.com/" (redirect-location result))))))))
|
|
|
|
(t/testing (str "the error path ignores " dest-url " as well")
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {:dest-url dest-url
|
|
:organization-id test-organization-id})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "https://penpot.example.com"}]
|
|
(with-redefs [app.nitrate/call (fn [_cfg method _params]
|
|
(case method
|
|
:get-organization-sso {:active true}
|
|
:get-organization-summary {:name "Organization"}))
|
|
app.auth.oidc/prepare-organization-sso-provider (constantly {:type "oidc"})
|
|
app.auth.oidc/get-info (fn [& _]
|
|
(ex/raise :type :internal
|
|
:code :unable-to-retrieve-user-info))
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (not (.contains loc "evil.example")))
|
|
(t/is (.contains loc "https://penpot.example.com/")))))))))
|
|
|
|
(t/deftest trusted-dest-url-requires-the-base-path
|
|
(binding [cf/config {:public-uri "https://host/penpot"}]
|
|
(t/testing "a destination under the app base path is trusted"
|
|
(t/is (= "https://host/penpot/?screen=workspace"
|
|
(#'oidc/trusted-dest-url "https://host/penpot/?screen=workspace"))))
|
|
|
|
(t/testing "a destination in another path of the same host is not"
|
|
(t/is (nil? (#'oidc/trusted-dest-url "https://host/otra/?screen=workspace"))))
|
|
|
|
(t/testing "a destination in another host is not"
|
|
(t/is (nil? (#'oidc/trusted-dest-url "https://otro.example/penpot/?screen=workspace"))))))
|
|
|
|
(t/deftest organization-sso-callback-does-not-grant-the-sso-entry-to-another-session
|
|
(let [manager (session/inmemory-manager)
|
|
cfg (assoc (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
::session/manager manager)
|
|
;; The state was prepared by another profile: the session that
|
|
;; completes the flow must not be granted the organization entry.
|
|
state (make-state-token cfg {:dest-url "https://penpot.example.com/?screen=workspace"
|
|
:organization-id test-organization-id
|
|
:profile-id (uuid/random)})
|
|
request (default-request cfg :state state)
|
|
events (atom [])
|
|
sess {:id (uuid/next)
|
|
:profile-id test-profile-id
|
|
:props {}}]
|
|
(binding [cf/config {:public-uri "https://penpot.example.com"}]
|
|
(with-redefs [app.nitrate/call (constantly {:active true})
|
|
app.auth.oidc/prepare-organization-sso-provider (constantly {:type "oidc"})
|
|
app.auth.oidc/get-info (constantly {})
|
|
app.http.session/get-session (constantly sess)
|
|
app.loggers.audit/submit (fn [_cfg event] (swap! events conj event))]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
stored (session/read-session manager (:id sess))]
|
|
(t/is (= "https://penpot.example.com/?screen=workspace" (redirect-location result)))
|
|
(t/is (= ["organization-sso-auth-failed"] (mapv :name @events)))
|
|
(t/is (= "session-mismatch" (get-in (first @events) [:props :failure-reason])))
|
|
(t/is (nil? (get-in stored [:props :sso]))))))))
|
|
|
|
(t/deftest prepare-organization-sso-provider-does-not-skip-ssrf-check
|
|
(t/testing "organization SSO provider must use SSRF protection"
|
|
(let [captured-params (atom nil)]
|
|
(with-redefs [oidc/prepare-oidc-provider (fn [_cfg params]
|
|
(reset! captured-params params)
|
|
{:type "oidc" :id "test"})]
|
|
(#'oidc/prepare-organization-sso-provider {}
|
|
{:client-id "test-client"
|
|
:client-secret "test-secret"
|
|
:issuer "https://idp.example.com"})
|
|
(t/is (not (true? (:skip-ssrf-check? @captured-params)))
|
|
"SSRF protection must be disabled for organization SSO")))))
|
|
|
|
(defn- ssl-handshake-failure
|
|
[]
|
|
(javax.net.ssl.SSLHandshakeException. "Remote host terminated the handshake"))
|
|
|
|
(t/deftest prepare-organization-sso-provider-raises-on-discovery-network-failure
|
|
(t/testing "SSL/network failures during OIDC discovery become controlled validation errors"
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:side-effect (fn [& _] (throw (ssl-handshake-failure)))}]
|
|
(let [e (try
|
|
(#'oidc/prepare-organization-sso-provider
|
|
{}
|
|
{:client-id "test-client"
|
|
:client-secret "test-secret"
|
|
:issuer "https://wrong-idp.example.com"})
|
|
(catch Throwable t t))]
|
|
(t/is (ex/error? e))
|
|
(t/is (= :validation (:type (ex-data e))))
|
|
(t/is (= :invalid-sso-config (:code (ex-data e))))))))
|
|
|
|
(t/deftest prepare-organization-sso-provider-raises-on-discovery-non-200
|
|
(t/testing "non-200 OIDC discovery responses become controlled validation errors"
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return {:status 404 :body "not found"}}]
|
|
(let [e (try
|
|
(#'oidc/prepare-organization-sso-provider
|
|
{}
|
|
{:client-id "test-client"
|
|
:client-secret "test-secret"
|
|
:issuer "https://idp.example.com"})
|
|
(catch Throwable t t))
|
|
data (ex-data e)]
|
|
(t/is (ex/error? e))
|
|
(t/is (= :validation (:type data)))
|
|
(t/is (= :invalid-sso-config (:code data)))
|
|
(t/is (= 404 (:response-status-code data)))
|
|
(t/is (= "unable to discover OIDC configuration" (ex-message e)))
|
|
(t/is (str/includes? (str (:discover-uri data)) "openid-configuration"))))))
|
|
|
|
(t/deftest prepare-organization-sso-provider-raises-on-ssrf-blocked-issuer
|
|
(t/testing "SSRF/DNS failures for the issuer URL become invalid-sso-config, not ssrf-blocked-target"
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:side-effect (fn [& _]
|
|
(ex/raise :type :validation
|
|
:code :ssrf-blocked-target
|
|
:hint "uri host could not be resolved"))}]
|
|
(let [e (try
|
|
(#'oidc/prepare-organization-sso-provider
|
|
{}
|
|
{:client-id "test-client"
|
|
:client-secret "test-secret"
|
|
:issuer "https://unresolvable.invalid"})
|
|
(catch Throwable t t))]
|
|
(t/is (ex/error? e))
|
|
(t/is (= :validation (:type (ex-data e))))
|
|
(t/is (= :invalid-sso-config (:code (ex-data e))))
|
|
(t/is (= :ssrf-blocked-target (:code (ex-data (ex-cause e)))))))))
|
|
|
|
(t/deftest prepare-organization-sso-provider-raises-on-jwks-network-failure
|
|
(t/testing "SSL/network failures while fetching JWKs become controlled validation errors"
|
|
(let [discovery-body (str "{\"authorization_endpoint\":\"https://idp.example.com/auth\","
|
|
"\"token_endpoint\":\"https://idp.example.com/token\","
|
|
"\"userinfo_endpoint\":\"https://idp.example.com/userinfo\","
|
|
"\"jwks_uri\":\"https://idp.example.com/jwks\"}")]
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:side-effect (fn [_cfg request & _]
|
|
(if (str/includes? (str (:uri request)) "openid-configuration")
|
|
{:status 200 :body discovery-body}
|
|
(throw (ssl-handshake-failure))))}]
|
|
(let [e (try
|
|
(#'oidc/prepare-organization-sso-provider
|
|
{}
|
|
{:client-id "test-client"
|
|
:client-secret "test-secret"
|
|
:issuer "https://idp.example.com"})
|
|
(catch Throwable t t))]
|
|
(t/is (ex/error? e))
|
|
(t/is (= :validation (:type (ex-data e))))
|
|
(t/is (= :invalid-sso-config (:code (ex-data e)))))))))
|
|
|
|
(t/deftest populate-jwks-strict-wraps-non-invalid-sso-config-errors
|
|
(t/testing "strict JWKS path wraps unrelated structured errors instead of rethrowing them"
|
|
(with-mocks [fetch-mock {:target 'app.auth.oidc/fetch-oidc-jwks
|
|
:side-effect (fn [& _]
|
|
(ex/raise :type :validation
|
|
:code :ssrf-blocked-target
|
|
:hint "uri host could not be resolved"))}]
|
|
(let [e (try
|
|
(#'oidc/populate-jwks
|
|
{}
|
|
{:id "oidc"
|
|
:jwks-uri "https://idp.example.com/jwks"
|
|
:strict-jwks? true})
|
|
(catch Throwable t t))]
|
|
(t/is (ex/error? e))
|
|
(t/is (= :validation (:type (ex-data e))))
|
|
(t/is (= :invalid-sso-config (:code (ex-data e))))
|
|
(t/is (= :ssrf-blocked-target (:code (ex-data (ex-cause e)))))))))
|
|
|
|
(t/deftest populate-jwks-strict-rethrows-invalid-sso-config
|
|
(t/testing "strict JWKS path rethrows an already-controlled invalid-sso-config"
|
|
(with-mocks [fetch-mock {:target 'app.auth.oidc/fetch-oidc-jwks
|
|
:side-effect (fn [& _]
|
|
(ex/raise :type :validation
|
|
:code :invalid-sso-config
|
|
:hint "unable to retrieve JWKs"
|
|
:jwks-uri "https://idp.example.com/jwks"))}]
|
|
(let [e (try
|
|
(#'oidc/populate-jwks
|
|
{}
|
|
{:id "oidc"
|
|
:jwks-uri "https://idp.example.com/jwks"
|
|
:strict-jwks? true})
|
|
(catch Throwable t t))]
|
|
(t/is (ex/error? e))
|
|
(t/is (= :invalid-sso-config (:code (ex-data e))))
|
|
(t/is (= "unable to retrieve JWKs" (ex-message e)))
|
|
(t/is (= "https://idp.example.com/jwks" (:jwks-uri (ex-data e))))))))
|
|
|
|
(t/deftest build-organization-sso-auth-redirect-uri-raises-on-unreachable-provider
|
|
(t/testing "check-nitrate-sso path surfaces a controlled error when the issuer is unreachable"
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:side-effect (fn [& _] (throw (ssl-handshake-failure)))}]
|
|
(let [e (try
|
|
(oidc/build-organization-sso-auth-redirect-uri
|
|
{}
|
|
{:client-id "test-client"
|
|
:client-secret "test-secret"
|
|
:issuer "https://wrong-idp.example.com"}
|
|
:query-string "screen=dashboard"
|
|
:organization-id #uuid "00000000-0000-0000-0000-000000000001")
|
|
(catch Throwable t t))]
|
|
(t/is (ex/error? e))
|
|
(t/is (= :validation (:type (ex-data e))))
|
|
(t/is (= :invalid-sso-config (:code (ex-data e))))))))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; trusted alternate origins
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(def ^:private trusted-origin-provider
|
|
{:id "oidc"
|
|
:type "oidc"
|
|
:auth-uri "https://idp.example.com/auth"
|
|
:client-id "test-client"
|
|
:scopes ["openid"]})
|
|
|
|
(t/deftest auth-handler-carries-trusted-origin-in-state
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly trusted-origin-provider)
|
|
app.loggers.audit/extract-utm-params (constantly {})
|
|
app.loggers.audit/get-external-session-id (constantly nil)]
|
|
(t/testing "trusted origin is stored in the state and used for redirect_uri"
|
|
(let [request (assoc {:params {}} :app.http/trusted-origin "https://alt.example.com")
|
|
result (binding [cf/config {:public-uri "https://alt.example.com"
|
|
:trusted-origins #{"https://alt.example.com"}}]
|
|
(#'oidc/auth-handler base-cfg request))
|
|
uri (get-in result [::yres/body :redirect-uri])
|
|
query (u/query-string->map (:query (u/uri uri)))
|
|
claims (tokens/verify base-cfg {:token (:state query) :iss "oidc"})]
|
|
(t/is (= "https://alt.example.com" (:origin claims)))
|
|
(t/is (= "https://alt.example.com/api/auth/oidc/callback" (:redirect_uri query)))))
|
|
|
|
(t/testing "without a trusted origin no :origin claim is stored"
|
|
(let [result (binding [cf/config {:public-uri "http://localhost:3449"
|
|
:trusted-origins #{"https://alt.example.com"}}]
|
|
(#'oidc/auth-handler base-cfg {:params {}}))
|
|
uri (get-in result [::yres/body :redirect-uri])
|
|
query (u/query-string->map (:query (u/uri uri)))
|
|
claims (tokens/verify base-cfg {:token (:state query) :iss "oidc"})]
|
|
(t/is (not (contains? claims :origin)))
|
|
(t/is (= "http://localhost:3449/api/auth/oidc/callback" (:redirect_uri query)))))))
|
|
|
|
(t/deftest build-organization-sso-auth-redirect-uri-stores-trusted-origin
|
|
(let [sso {:organization-id test-organization-id
|
|
:issuer "https://idp.example.com"
|
|
:client-id "test-client"
|
|
:client-secret "test-secret"}
|
|
provider trusted-origin-provider
|
|
uri (binding [cf/config {:public-uri "https://alt.example.com"
|
|
:trusted-origins #{"https://alt.example.com"}}]
|
|
(oidc/build-organization-sso-auth-redirect-uri
|
|
base-cfg sso
|
|
:query-string "screen=dashboard"
|
|
:organization-id test-organization-id
|
|
:provider provider
|
|
:origin "https://alt.example.com"))
|
|
query (u/query-string->map (:query (u/uri uri)))
|
|
claims (tokens/verify base-cfg {:token (:state query) :iss "oidc"})]
|
|
(t/is (= "https://alt.example.com" (:origin claims)))
|
|
(t/is (= "https://alt.example.com/?screen=dashboard" (:dest-url claims)))
|
|
(t/is (= "https://alt.example.com/api/auth/oidc/callback" (:redirect_uri query)))))
|
|
|
|
(t/deftest callback-applies-stored-trusted-origin
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
seen (atom nil)
|
|
state (make-state-token cfg {:origin "https://alt.example.com"})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"
|
|
:trusted-origins #{"https://alt.example.com"}}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (fn [_ _ _ _]
|
|
(reset! seen (cf/get :public-uri))
|
|
{:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly test-profile)
|
|
app.auth.oidc/update-profile-with-info (fn [_ profile _] profile)
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (= "https://alt.example.com" (str @seen)))
|
|
(t/is (.contains loc "https://alt.example.com?screen=auth-verify-token&")))))))
|
|
|
|
(t/deftest callback-token-exchange-uses-stored-trusted-origin
|
|
;; Unlike callback-applies-stored-trusted-origin, this drives the real
|
|
;; token exchange (mocking app.http.client/req instead of get-info) so
|
|
;; the redirect_uri sent to the provider is asserted, not only the
|
|
;; auth-request one and the post-exchange redirect.
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
bodies (atom [])
|
|
provider {:type "oidc" :id "oidc"
|
|
:user-info-source "userinfo"
|
|
:token-uri "https://idp.example.com/token"
|
|
:user-uri "https://idp.example.com/userinfo"}
|
|
state (make-state-token cfg {:origin "https://alt.example.com"})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"
|
|
:trusted-origins #{"https://alt.example.com"}}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly provider)
|
|
app.auth.oidc/get-profile (constantly test-profile)
|
|
app.auth.oidc/update-profile-with-info (fn [_ profile _] profile)
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return (fn [_ request & _]
|
|
(swap! bodies conj request)
|
|
(if (= (:uri request) (:token-uri provider))
|
|
{:status 200
|
|
:body "{\"access_token\":\"at\",\"token_type\":\"Bearer\"}"}
|
|
{:status 200
|
|
:body "{\"email\":\"u@e.com\",\"name\":\"U\"}"}))}]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
token-body (->> @bodies
|
|
(filter #(= (:uri %) (:token-uri provider)))
|
|
(first)
|
|
(:body)
|
|
(u/query-string->map))]
|
|
(t/is (= "https://alt.example.com/api/auth/oidc/callback"
|
|
(:redirect_uri token-body)))
|
|
(t/is (.contains (redirect-location result)
|
|
"https://alt.example.com?screen=auth-verify-token&"))))))))
|
|
|
|
(t/deftest callback-token-exchange-keeps-stored-origin-after-removal
|
|
;; Mirror of callback-token-exchange-uses-stored-trusted-origin with the
|
|
;; origin removed from :trusted-origins: the signed state was trusted when
|
|
;; the flow started, so the token exchange still uses it (up to its
|
|
;; expiry), matching the documented operator contract.
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
bodies (atom [])
|
|
provider {:type "oidc" :id "oidc"
|
|
:user-info-source "userinfo"
|
|
:token-uri "https://idp.example.com/token"
|
|
:user-uri "https://idp.example.com/userinfo"}
|
|
state (make-state-token cfg {:origin "https://alt.example.com"})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"
|
|
:trusted-origins #{}}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly provider)
|
|
app.auth.oidc/get-profile (constantly test-profile)
|
|
app.auth.oidc/update-profile-with-info (fn [_ profile _] profile)
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return (fn [_ request & _]
|
|
(swap! bodies conj request)
|
|
(if (= (:uri request) (:token-uri provider))
|
|
{:status 200
|
|
:body "{\"access_token\":\"at\",\"token_type\":\"Bearer\"}"}
|
|
{:status 200
|
|
:body "{\"email\":\"u@e.com\",\"name\":\"U\"}"}))}]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
token-body (->> @bodies
|
|
(filter #(= (:uri %) (:token-uri provider)))
|
|
(first)
|
|
(:body)
|
|
(u/query-string->map))]
|
|
(t/is (= "https://alt.example.com/api/auth/oidc/callback"
|
|
(:redirect_uri token-body)))
|
|
(t/is (.contains (redirect-location result)
|
|
"https://alt.example.com?screen=auth-verify-token&"))))))))
|
|
|
|
(t/deftest callback-error-uses-stored-trusted-origin
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {:origin "https://alt.example.com"})
|
|
request (assoc (default-request cfg :state state)
|
|
:params {:state state :error "access_denied"})]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"
|
|
:trusted-origins #{"https://alt.example.com"}}]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "https://alt.example.com?screen=auth-login&"))
|
|
(t/is (.contains loc "error=unable-to-auth"))))))
|
|
|
|
(t/deftest callback-honors-state-origin-even-when-not-in-config
|
|
;; The signed state origin was trusted when the flow started; it is used
|
|
;; as-is for the lifetime of the token, even if the operator removed it.
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
state (make-state-token cfg {:origin "https://alt.example.com"})
|
|
request (default-request cfg :state state)]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"
|
|
:trusted-origins #{}}]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly {:type "oidc" :id "oidc"})
|
|
app.auth.oidc/get-info (constantly {:email "u@e.com" :fullname "U"
|
|
:backend "oidc" :email-verified false
|
|
:props {}})
|
|
app.auth.oidc/get-profile (constantly test-profile)
|
|
app.auth.oidc/update-profile-with-info (fn [_ profile _] profile)
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (.contains loc "https://alt.example.com?screen=auth-verify-token&")))))))
|
|
|
|
(t/deftest callback-without-state-redirects-to-canonical-error
|
|
(let [cfg (dissoc base-cfg :app.email/blacklist :app.email/whitelist)
|
|
request {:params {}}]
|
|
(binding [cf/config {:public-uri "http://localhost:3449"
|
|
:trusted-origins #{"https://alt.example.com"}}]
|
|
(let [result (#'oidc/callback-handler cfg request)
|
|
loc (redirect-location result)]
|
|
(t/is (= 302 (::yres/status result)))
|
|
(t/is (.contains loc "http://localhost:3449?screen=auth-login&"))
|
|
(t/is (.contains loc "error=unable-to-auth"))))))
|
|
|
|
(defn- get-uri-query
|
|
[uri]
|
|
(u/query-string->map (:query (u/uri uri))))
|
|
|
|
(t/deftest oidc-round-trip-preserves-the-alternate-public-subpath
|
|
(let [cfg (-> base-cfg
|
|
(dissoc :app.email/blacklist :app.email/whitelist)
|
|
(assoc ::session/manager (session/inmemory-manager)))
|
|
provider (assoc trusted-origin-provider
|
|
:user-info-source "userinfo"
|
|
:token-uri "https://idp.example.com/token"
|
|
:user-uri "https://idp.example.com/userinfo")
|
|
public-config {:public-uri "http://localhost:3449/penpot"
|
|
:trusted-origins #{"https://alt.example.com"}}
|
|
authorize (mw/wrap-trusted-origin (partial #'oidc/auth-handler cfg))
|
|
callback (mw/wrap-trusted-origin (partial #'oidc/callback-handler cfg))
|
|
token-request (atom nil)]
|
|
(with-redefs [app.auth.oidc/resolve-provider (constantly provider)
|
|
app.auth.oidc/get-profile (constantly test-profile)
|
|
app.auth.oidc/update-profile-with-info (fn [_ profile _] profile)
|
|
app.loggers.audit/submit (constantly nil)]
|
|
(with-mocks [http-mock {:target 'app.http.client/req
|
|
:return (fn [_ request & _]
|
|
(case (:uri request)
|
|
"https://idp.example.com/token"
|
|
(do (reset! token-request request)
|
|
{:status 200
|
|
:body "{\"access_token\":\"at\",\"token_type\":\"Bearer\"}"})
|
|
|
|
"https://idp.example.com/userinfo"
|
|
{:status 200
|
|
:body "{\"email\":\"u@e.com\",\"name\":\"U\"}"}))}]
|
|
(let [auth-response (binding [cf/config public-config]
|
|
(authorize {:params {}
|
|
:headers {"x-penpot-origin" "https://alt.example.com"}}))
|
|
auth-query (get-uri-query (get-in auth-response [::yres/body :redirect-uri]))
|
|
state (:state auth-query)
|
|
claims (tokens/verify cfg {:token state :iss "oidc"})
|
|
request (default-request cfg :state state)]
|
|
(t/is (= "https://alt.example.com" (:origin claims)))
|
|
(t/is (= "https://alt.example.com/penpot/api/auth/oidc/callback"
|
|
(:redirect_uri auth-query)))
|
|
|
|
(t/testing "the headerless callback uses the same redirect_uri even after alias removal"
|
|
(binding [cf/config (assoc public-config :trusted-origins #{})]
|
|
(let [response (callback request)
|
|
location (redirect-location response)
|
|
token-form (u/query-string->map (:body @token-request))]
|
|
(t/is (= :post (:method @token-request)))
|
|
(t/is (= (:redirect_uri auth-query) (:redirect_uri token-form)))
|
|
(t/is (= 302 (::yres/status response)))
|
|
(t/is (= "https://alt.example.com/penpot"
|
|
(str (assoc (u/uri location) :query nil))))
|
|
(t/is (= "auth-verify-token" (:screen (get-uri-query location)))))))
|
|
|
|
(t/testing "a provider error still redirects under the alias subpath"
|
|
(binding [cf/config public-config]
|
|
(let [response (callback (assoc-in request [:params :error] "access_denied"))
|
|
location (redirect-location response)]
|
|
(t/is (= 302 (::yres/status response)))
|
|
(t/is (= "https://alt.example.com/penpot"
|
|
(str (assoc (u/uri location) :query nil))))
|
|
(t/is (= {:screen "auth-login" :error "unable-to-auth" :hint "access_denied"}
|
|
(get-uri-query location))))))
|
|
|
|
(t/testing "without state or origin headers the error keeps the canonical subpath"
|
|
(binding [cf/config public-config]
|
|
(let [response (callback (update request :params dissoc :state))
|
|
location (redirect-location response)]
|
|
(t/is (= 302 (::yres/status response)))
|
|
(t/is (= "http://localhost:3449/penpot"
|
|
(str (assoc (u/uri location) :query nil))))
|
|
(t/is (= "auth-login" (:screen (get-uri-query location))))
|
|
(t/is (= "unable-to-auth" (:error (get-uri-query location))))))))))))
|
|
|
|
(t/deftest organization-sso-round-trip-preserves-the-alternate-public-subpath
|
|
(let [manager (session/inmemory-manager)
|
|
cfg (assoc base-cfg ::session/manager manager)
|
|
sso {:active true
|
|
:organization-id test-organization-id
|
|
:issuer "https://idp.example.com"
|
|
:client-id "test-client"
|
|
:client-secret "test-secret"}
|
|
params {::rpc/profile-id test-profile-id
|
|
::rpc/request-at (ct/now)
|
|
:organization-id test-organization-id
|
|
:query-string "screen=workspace&team-id=0e5f"}
|
|
start-sso (mw/wrap-trusted-origin
|
|
(fn [request]
|
|
(nitrate-rpc/sm$check-nitrate-sso
|
|
cfg (with-meta params {::http/request request}))))
|
|
callback (mw/wrap-trusted-origin (partial #'oidc/callback-handler cfg))
|
|
events (atom [])]
|
|
(binding [cf/config {:public-uri "http://localhost:3449/penpot/"
|
|
:trusted-origins #{"https://alt.example.com"}}
|
|
cf/flags (conj cf/flags :admin-console)]
|
|
(with-redefs [app.nitrate/organization-access? (constantly true)
|
|
app.nitrate/sso-session-authorized? (constantly {:authorized false :sso sso})
|
|
app.nitrate/call (constantly sso)
|
|
app.auth.oidc/prepare-organization-sso-provider (constantly trusted-origin-provider)
|
|
app.auth.oidc/get-info (constantly {})
|
|
app.loggers.audit/submit (fn [_cfg event] (swap! events conj event))]
|
|
(let [result (start-sso {:headers {"origin" "https://alt.example.com"}})
|
|
auth-query (get-uri-query (:redirect-uri result))
|
|
state (:state auth-query)
|
|
claims (tokens/verify cfg {:token state :iss "oidc"})
|
|
destination (:dest-url claims)
|
|
sess {:id (uuid/next) :profile-id test-profile-id :props {}}
|
|
request (assoc (default-request cfg :state state) ::session/session sess)]
|
|
(t/is (false? (:authorized result)))
|
|
(t/is (= "https://alt.example.com" (:origin claims)))
|
|
(t/is (= "https://alt.example.com/penpot/api/auth/oidc/callback" (:redirect_uri auth-query)))
|
|
(t/is (= "https://alt.example.com/penpot/" (str (assoc (u/uri destination) :query nil))))
|
|
(t/is (= {:screen "workspace" :team-id "0e5f"} (get-uri-query destination)))
|
|
|
|
(t/testing "the headerless callback accepts the server-built destination and grants the session entry"
|
|
(let [response (callback request)
|
|
stored (session/read-session manager (:id sess))]
|
|
(t/is (= 302 (::yres/status response)))
|
|
(t/is (= destination (redirect-location response)))
|
|
(t/is (contains? (get-in stored [:props :sso]) test-organization-id))))
|
|
|
|
(t/testing "a different profile returns to the same destination without an SSO entry"
|
|
(let [other (assoc sess :id (uuid/next) :profile-id (uuid/next))
|
|
response (callback (assoc request ::session/session other))
|
|
stored (session/read-session manager (:id other))]
|
|
(t/is (= destination (redirect-location response)))
|
|
(t/is (nil? (get-in stored [:props :sso])))
|
|
(t/is (= "session-mismatch" (get-in (last @events) [:props :failure-reason])))))
|
|
|
|
(t/testing "a legacy destination outside the public subpath falls back to the alias base"
|
|
(let [legacy (tokens/generate cfg (assoc claims :dest-url "https://alt.example.com/other/?screen=workspace"))
|
|
response (callback (default-request cfg :state legacy))]
|
|
(t/is (= "https://alt.example.com/penpot/" (redirect-location response))))))))))
|