Merge remote-tracking branch 'origin/staging-2.19.x' into staging

This commit is contained in:
Andrey Antukh 2026-11-16 10:05:00 +01:00
commit 7ef8c1cf36
18 changed files with 760 additions and 76 deletions

View File

@ -12,7 +12,7 @@
- Lockout and RPC rate limits share the `app.http.errors/handle-error :rate-limit` HTTP path: status 429, body `{:type :rate-limit :code ... :hint ... :ttl ...}`, and any supplied `::http/headers` preserved. Account lockout raises `:code :account-locked` and, when it carries a non-nil `:ttl` (seconds), the handler adds `retry-after`. The RPC limiter (`app.rpc.rlimit`) raises `:code :request-blocked` and sets `retry-after` itself in `::http/headers` (seconds until the longest rejecting limit resets), alongside `x-rate-limit-remaining`/`x-rate-limit-reset`. CORS (`app.http.middleware/with-cors-headers`) exposes `content-type`, `retry-after`, and both `x-rate-limit-*` headers. External flags: `enable-account-lockout`, `enable-rpc-rlimit`.
- Logout may return an OIDC provider redirect URI when the session claims include provider/session data and the provider has a logout URI.
- Invitation tokens are verified through token issuers and only accepted when the token member id/email matches the authenticated profile; otherwise login proceeds without consuming the invitation.
- When public registration is disabled, invitation-based password registration also requires a matching, non-expired `team_invitation` row; registration revalidates it under a row lock before creating the profile.
- When public registration is disabled, invitation-based password registration also requires a matching, non-expired `team_invitation` row; registration revalidates it under a row lock before creating the profile. SSO registration (OIDC and compatible providers) follows the OIDC callback gate: it requires `:registration` or `:oidc-registration`, or a team invitation token issued for the registering email (an invitation substitutes the flags only when `member-email` matches the OIDC/`prepared-register` email via `clean-email`, as in the password flow, which raises `:email-does-not-match-invitation` otherwise). The OIDC callback verifies the invitation JWT and the email before redirecting to register, and `register-profile` revalidates the invitation under a row lock when no SSO flag is set; it never requires password login. Only OIDC-typed backends (`oidc-provider-types` in `app.auth.oidc`) take the SSO registration gate; any other `:backend` in a prepared-register token is rejected as `:invalid-token`.
- Access tokens (`app.http.access-token`): personal tokens have JWT issuer `access-token` and DB type other than `mcp`; MCP tokens have issuer `urn:penpot:mcp-token` (`actoken/mcp-token-issuer`) and DB type `mcp`. Authentication requires issuer and type to agree, and the claim `uid` to match the row's profile; other issuers and undecryptable tokens are treated as anonymous.
- Management RPC (`/api/management/methods/:name`) is shared-key only. The middleware authenticates `<id> <secret>`; the RPC wrapper `wrap-shared-key-authorization` authorizes the key id against the method's `::rpc/allowed-shared-keys` set. Absent metadata accepts any configured key; all current methods declare one (`exporter`/`media-processor` for `upload-tempfile`, `nexus` for file analysis, `exporter` for jobs, `admin-console` for Nitrate). A valid key outside the set answers 403 `:shared-key-not-allowed`; denied calls do not reach the audit wrapper.
- Personal tokens need flag `:access-tokens`. MCP tokens need `:mcp` or `:access-tokens`, and only on routes whose `actoken/authz` gets `::actoken/allow-mcp true` (the RPC routes); elsewhere (e.g. assets) they are anonymous.

View File

@ -1064,6 +1064,32 @@
:organization-id organization-id
:organization-name organization-name}))))))
(def oidc-provider-types
"Backends authenticating through an OIDC-compatible provider.
Only these backends take the SSO registration gate."
#{"oidc" "google" "github" "gitlab"})
(defn sso-registration-allowed?
"SSO registration is allowed when public registration or explicit
OIDC registration is enabled. Shared by the callback gate and the
`register-profile` token gate — keep one definition."
[]
(or (contains? cf/flags :registration)
(contains? cf/flags :oidc-registration)))
(defn- valid-invitation-for?
"Returns true when `invitation-token` is a verifiable
`:team-invitation` JWT issued for `email`. Never throws: a garbage
token means there is no valid invitation."
[cfg invitation-token email]
(try
(boolean
(when (and (some? invitation-token) (some? email))
(let [invitation (tokens/verify cfg {:token invitation-token :iss :team-invitation})]
(= (profile/clean-email (:member-email invitation))
(profile/clean-email email)))))
(catch Throwable _ false)))
(defn- callback-handler
[cfg {:keys [params] :as request}]
(if-let [error (get params :error)]
@ -1096,8 +1122,8 @@
(redirect-with-error "email-domain-not-allowed")
:else
(if (or (contains? cf/flags :registration)
(contains? cf/flags :oidc-registration))
(if (or (sso-registration-allowed?)
(valid-invitation-for? cfg (:invitation-token info) (:email info)))
(redirect-to-register cfg info provider)
(redirect-with-error "registration-disabled")))

View File

@ -70,6 +70,7 @@
(def defaults
{::name :main
::min-size 0
::max-size 60
::connection-timeout 30000
::validation-timeout 10000
@ -92,8 +93,7 @@
:read-only read-only
:credentials (and (contains? cfg ::username)
(contains? cfg ::password))
:min-size (or (::min-size cfg)
(::max-size cfg))
:min-size (::min-size cfg)
:max-size (::max-size cfg))
(create-pool cfg))))
@ -117,9 +117,7 @@
[{:keys [::uri] :as cfg}]
;; (app.common.pprint/pprint cfg)
(let [config (HikariConfig.)
max-size (::max-size cfg)
min-size (or (::min-size cfg) max-size)]
(let [config (HikariConfig.)]
(doto config
(.setJdbcUrl (str "jdbc:" uri))
(.setPoolName (d/name (::name cfg)))
@ -129,8 +127,8 @@
(.setValidationTimeout (::validation-timeout cfg))
(.setIdleTimeout (::idle-timeout cfg))
(.setMaxLifetime (::max-lifetime cfg))
(.setMinimumIdle min-size)
(.setMaximumPoolSize max-size)
(.setMinimumIdle (::min-size cfg))
(.setMaximumPoolSize (::max-size cfg))
(.setConnectionInitSql initsql)
(.setInitializationFailTimeout -1))

View File

@ -46,6 +46,10 @@
(def schema:token
[::sm/word-string {:max 6000}])
(def ^:private penpot-backend
"Auth backend identifier for the password flow."
"penpot")
(defn- elapsed-verify-threshold?
[profile]
(let [elapsed (ct/diff (:modified-at profile) (ct/now))
@ -226,25 +230,72 @@
;; ---- COMMAND: Prepare Register
(defn- validate-registration-flags!
([cfg invitation]
(validate-registration-flags! cfg invitation false))
([cfg invitation lock-invitation?]
(when-not (contains? cf/flags :login-with-password)
(ex/raise :type :restriction
:code :registration-disabled
:hint "registration disabled"))
(defn- check-registration-flags
"The `:backend` is the one embedded in the verified
`:prepared-register` token: the password flow backend, one of the
`oidc/oidc-provider-types` backends for SSO flows, or any other
value, which is rejected loudly."
[backend invitation]
(cond
(= backend penpot-backend)
(do
(when-not (contains? cf/flags :login-with-password)
(ex/raise :type :restriction
:code :registration-disabled
:hint "registration disabled"))
(when-not (contains? cf/flags :registration)
(if (nil? invitation)
(ex/raise :type :restriction
:code :registration-disabled
:hint "registration disabled")
(let [opts (when lock-invitation? {::db/for-update true})]
(when (nil? (teams-invitations/active-invitation cfg invitation opts))
(ex/raise :type :validation
:code :invalid-token
:hint "no active invitation associated with the token")))))))
(when-not (or (contains? cf/flags :registration)
(some? invitation))
(ex/raise :type :restriction
:code :registration-disabled
:hint "registration disabled")))
(contains? oidc/oidc-provider-types backend)
;; SSO backends share the OIDC callback gate
;; (`oidc/sso-registration-allowed?`): registration is allowed with
;; `:registration` or `:oidc-registration`, without requiring
;; password login. An invitation token is a substitute for the
;; flags, as in the password flow.
(when-not (or (oidc/sso-registration-allowed?)
(some? invitation))
(ex/raise :type :restriction
:code :registration-disabled
:hint "registration disabled"))
:else
(ex/raise :type :validation
:code :invalid-token
:hint "unsupported registration backend")))
(defn- check-registration-invitation
"Verifies that the invitation is still active. No-op unless a live
invitation is required: a backend whose registration flags do not
allow public registration, with an invitation at hand."
[cfg backend invitation lock-invitation?]
(let [flag-allows? (if (= backend penpot-backend)
(contains? cf/flags :registration)
(oidc/sso-registration-allowed?))]
(when (and (some? invitation)
(not flag-allows?))
(let [opts (when lock-invitation? {::db/for-update true})]
(when (nil? (teams-invitations/active-invitation cfg invitation opts))
(ex/raise :type :validation
:code :invalid-token
:hint "no active invitation associated with the token"))))))
(defn- check-registration
"Shared registration gate (register-profile and prepare-register):
validates the flags, the invitation email and the team invitation
row in order."
[cfg backend invitation email lock-invitation?]
(check-registration-flags backend invitation)
(when (and (some? invitation)
(not= (profile/clean-email (:member-email invitation))
(profile/clean-email email)))
(ex/raise :type :restriction
:code :email-does-not-match-invitation
:hint "email should match the invitation"))
(check-registration-invitation cfg backend invitation lock-invitation?))
(defn- validate-register-attempt!
[cfg params]
@ -254,23 +305,7 @@
{:token (:invitation-token params)
:iss :team-invitation}))]
(when (or (not (contains? cf/flags :login-with-password))
(and (not (contains? cf/flags :registration))
(not invitation?)))
(ex/raise :type :restriction
:code :registration-disabled
:hint "registration disabled"))
(when (and invitation?
(not= (profile/clean-email (:email params))
(profile/clean-email (:member-email invitation))))
(ex/raise :type :restriction
:code :email-does-not-match-invitation
:hint "email should match the invitation"))
(when (and invitation?
(not (contains? cf/flags :registration)))
(validate-registration-flags! cfg invitation)))
(check-registration cfg penpot-backend invitation (:email params) false))
(when (and (email.blacklist/enabled? cfg)
(email.blacklist/contains? cfg (:email params)))
@ -342,7 +377,7 @@
:fullname fullname
:password (:password params)
:invitation-token (:invitation-token params)
:backend "penpot"
:backend penpot-backend
:iss :prepared-register
:exp (ct/in-future {:days 7})
:props props}
@ -419,7 +454,7 @@
locale (when (and (string? locale) (not (str/blank? locale)))
locale)
backend (:backend params "penpot")
backend (:backend params penpot-backend)
is-demo (:is-demo params false)
is-muted (:is-muted params false)
is-active (:is-active params false)
@ -505,12 +540,25 @@
:token vtoken
:extra-data ptoken}))))
(def ^:private schema:prepared-register-claims
[:map {:title "PreparedRegisterClaims"}
[:backend ::sm/text]
[:email ::sm/email]])
(def ^:private valid-prepared-register-claims?
(sm/lazy-validator schema:prepared-register-claims))
(defn register-profile
[{:keys [::db/conn] :as cfg} {:keys [token] :as params}]
(let [claims (tokens/verify cfg {:token token :iss :prepared-register})
_ (when-not (valid-prepared-register-claims? claims)
(ex/raise :type :validation
:code :invalid-token
:hint "prepared-register token contains unexpected data"))
backend (:backend claims)
invitation (when-let [token (:invitation-token claims)]
(tokens/verify cfg {:token token :iss :team-invitation}))
_ (validate-registration-flags! cfg invitation true)
_ (check-registration cfg backend invitation (:email claims) true)
params (cond-> claims
(:accept-newsletter-updates params)
(update :props assoc :newsletter-updates true))

View File

@ -448,6 +448,76 @@
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 {})

View File

@ -987,6 +987,10 @@
(t/is (= :email-does-not-match-invitation (:code edata))))))
(t/deftest prepare-register-with-invitation-and-disabled-registration-and-wrong-email
;; The email check runs before the invitation row lookup, so a
;; mismatched email is reported even when the invitation row is gone.
;; Flags stay `#{:login-with-password}`: password registration is
;; allowed only via invitation.
(with-redefs [app.config/flags #{:login-with-password}]
(let [itoken (tokens/generate th/*system*
{:iss :team-invitation
@ -994,7 +998,6 @@
:role :editor
:team-id uuid/zero
:member-email "user2@example.com"})
data {::th/type :prepare-register-profile
:invitation-token itoken
:email "user@example.com"
@ -1046,6 +1049,216 @@
(t/is (= :registration-disabled (-> out :error ex-data :code)))
(t/is (nil? (th/db-get :profile {:email email})))))))))
(defn- create-oidc-register-token
"Mint a `:prepared-register` token shaped like the ones the OIDC
callback produces in `redirect-to-register` (no real provider needed)."
[email extra]
(tokens/generate th/*system*
(merge {:iss :prepared-register
:exp (ct/in-future "48h")
:backend "oidc"
:email email
:fullname "OIDC User"
:email-verified true
:props {}}
extra)))
(t/deftest register-profile-allows-oidc-registration-with-password-login-disabled
;; Issue #12964: with password login and public registration disabled,
;; a new user arriving from OIDC must still be able to register when
;; `oidc-registration` is enabled.
(with-redefs [app.config/flags #{:login-with-oidc :oidc-registration}]
(let [email "oidc-user@example.com"
token (create-oidc-register-token email nil)
out (th/command! {::th/type :register-profile
:token token})
result (:result out)
mdata (meta result)]
(t/is (th/success? out))
;; A session is minted: the user lands on the dashboard.
(t/is (seq (:app.rpc/response-transform-fns mdata)))
(let [profile (th/db-get :profile {:email email})]
(t/is (some? profile))
(t/is (= "oidc" (:auth-backend profile)))
(t/is (true? (:is-active profile))))
;; No password fallback while password login is disabled.
(let [login-out (th/command! {::th/type :login-with-password
:email email
:password "Whatever1!"})]
(t/is (not (th/success? login-out)))
(t/is (= :restriction (-> login-out :error ex-data :type)))
(t/is (= :login-disabled (-> login-out :error ex-data :code)))))))
(t/deftest register-profile-rejects-oidc-without-registration-flags
(with-redefs [app.config/flags #{:login-with-oidc}]
(let [email "oidc-rejected@example.com"
token (create-oidc-register-token email nil)
out (th/command! {::th/type :register-profile
:token token})]
(t/is (not (th/success? out)))
(t/is (= :registration-disabled (-> out :error ex-data :code)))
(t/is (nil? (th/db-get :profile {:email email}))))))
(t/deftest register-profile-allows-oidc-with-public-registration-and-password-login-disabled
(with-redefs [app.config/flags #{:login-with-oidc :registration}]
(let [email "oidc-public@example.com"
token (create-oidc-register-token email nil)
out (th/command! {::th/type :register-profile
:token token})]
(t/is (th/success? out))
(let [profile (th/db-get :profile {:email email})]
(t/is (some? profile))
(t/is (= "oidc" (:auth-backend profile)))))))
(t/deftest register-profile-allows-oidc-with-invitation-and-registration-disabled
(let [email "oidc-invited@example.com"
fixture (create-invitation-fixture email (ct/in-future "48h"))]
(with-redefs [app.config/flags #{:login-with-oidc :oidc-registration}]
(let [itoken (create-test-invitation-token fixture email)
token (create-oidc-register-token email {:invitation-token itoken})
out (th/command! {::th/type :register-profile
:token token})
result (:result out)
mdata (meta result)]
(t/is (th/success? out))
;; Invitation branch: the token is echoed back and a session is
;; minted so the frontend can complete the team-invitation flow.
(t/is (string? (:invitation-token result)))
(t/is (seq (:app.rpc/response-transform-fns mdata)))
(let [profile (th/db-get :profile {:email email})]
(t/is (some? profile))
(t/is (= "oidc" (:auth-backend profile))))))))
(t/deftest register-profile-allows-oidc-with-canceled-invitation
;; The invitation row is only consulted when no SSO registration
;; flag is set: with `:oidc-registration` (as here), registration
;; succeeds even when the invitation was canceled, and team
;; membership fails later at `verify-token`.
(let [email "oidc-canceled@example.com"
fixture (create-invitation-fixture email (ct/in-future "48h"))]
(with-redefs [app.config/flags #{:login-with-oidc :oidc-registration}]
(let [itoken (create-test-invitation-token fixture email)
token (create-oidc-register-token email {:invitation-token itoken})]
(th/db-delete! :team-invitation
{:team-id (:id (:team fixture))
:email-to email})
(let [out (th/command! {::th/type :register-profile
:token token})
result (:result out)
mdata (meta result)]
(t/is (th/success? out))
(t/is (string? (:invitation-token result)))
(t/is (seq (:app.rpc/response-transform-fns mdata)))
(let [profile (th/db-get :profile {:email email})]
(t/is (some? profile))
(t/is (= "oidc" (:auth-backend profile))))
;; The team is not joined: with the invitation row gone,
;; accepting it fails.
(let [vout (th/command! {::th/type :verify-token
:token (:invitation-token result)})]
(t/is (not (th/success? vout)))
(t/is (= :validation (-> vout :error ex-data :type)))
(t/is (= :invalid-token (-> vout :error ex-data :code)))))))))
(t/deftest register-profile-rejects-prepared-token-without-backend
;; The `:backend` claim is mandatory: a prepared-register token
;; without it is rejected loudly instead of falling into any
;; registration branch.
(with-redefs [app.config/flags #{:login-with-oidc :oidc-registration}]
(let [token (tokens/generate th/*system*
{:iss :prepared-register
:exp (ct/in-future "48h")
:email "no-backend@example.com"
:fullname "No Backend"
:email-verified true
:props {}})
out (th/command! {::th/type :register-profile
:token token})]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (= :invalid-token (-> out :error ex-data :code)))
(t/is (nil? (th/db-get :profile {:email "no-backend@example.com"}))))))
(t/deftest register-profile-rejects-non-oidc-backend-token
;; The SSO registration gate only applies to backends of OIDC type;
;; a token carrying any other backend is rejected loudly instead of
;; silently taking the SSO branch.
(with-redefs [app.config/flags #{:login-with-oidc :oidc-registration}]
(let [token (tokens/generate th/*system*
{:iss :prepared-register
:exp (ct/in-future "48h")
:backend "ldap"
:email "unknown-backend@example.com"
:fullname "Unknown Backend"
:email-verified true
:props {}})
out (th/command! {::th/type :register-profile
:token token})]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (= :invalid-token (-> out :error ex-data :code)))
(t/is (nil? (th/db-get :profile {:email "unknown-backend@example.com"}))))))
(t/deftest register-profile-allows-oidc-with-invitation-and-no-registration-flags
;; Review of #12974: an invitation token is a substitute for the
;; SSO registration flags, as it is for the password flow. With no
;; `:registration` nor `:oidc-registration` flag, a user arriving
;; through a team invitation can still register via OIDC.
(let [email "oidc-invited-noflags@example.com"
fixture (create-invitation-fixture email (ct/in-future "48h"))]
(with-redefs [app.config/flags #{:login-with-oidc}]
(let [itoken (create-test-invitation-token fixture email)
token (create-oidc-register-token email {:invitation-token itoken})
out (th/command! {::th/type :register-profile
:token token})
result (:result out)
mdata (meta result)]
(t/is (th/success? out))
;; Invitation branch: the token is echoed back and a session is
;; minted so the frontend can complete the team-invitation flow.
(t/is (string? (:invitation-token result)))
(t/is (seq (:app.rpc/response-transform-fns mdata)))
(let [profile (th/db-get :profile {:email email})]
(t/is (some? profile))
(t/is (= "oidc" (:auth-backend profile))))))))
(t/deftest register-profile-rejects-oidc-invitation-email-mismatch-with-no-registration-flags
;; Review of #12974: the invitation is a substitute for the SSO
;; registration flags only when it was issued for the registering
;; email. A forwarded invitation for another email must not unlock
;; registration with closed registration.
(let [invited-email "oidc-invited-other@example.com"
attacker-mail "oidc-attacker-noflags@example.com"
fixture (create-invitation-fixture invited-email (ct/in-future "48h"))]
(with-redefs [app.config/flags #{:login-with-oidc}]
(let [itoken (create-test-invitation-token fixture invited-email)
token (create-oidc-register-token attacker-mail {:invitation-token itoken})
out (th/command! {::th/type :register-profile
:token token})]
(t/is (not (th/success? out)))
(t/is (= :restriction (-> out :error ex-data :type)))
(t/is (= :email-does-not-match-invitation (-> out :error ex-data :code)))
(t/is (nil? (th/db-get :profile {:email attacker-mail})))))))
(t/deftest register-profile-rejects-oidc-with-canceled-invitation-and-no-registration-flags
;; Review of #12974: with no registration flag set, the invitation
;; row is revalidated under a row lock before creating the profile.
;; A canceled invitation stops signup and no account is created.
(let [email "oidc-canceled-noflags@example.com"
fixture (create-invitation-fixture email (ct/in-future "48h"))]
(with-redefs [app.config/flags #{:login-with-oidc}]
(let [itoken (create-test-invitation-token fixture email)
token (create-oidc-register-token email {:invitation-token itoken})]
(th/db-delete! :team-invitation
{:team-id (:id (:team fixture))
:email-to email})
(let [out (th/command! {::th/type :register-profile
:token token})]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (= :invalid-token (-> out :error ex-data :code)))
(t/is (nil? (th/db-get :profile {:email email}))))))))
(t/deftest verify-token-redirects-invitation-based-on-member-id
(with-redefs [app.config/flags #{:login-with-password}]
(let [owner (th/create-profile* 1 {:is-active true})

View File

@ -86,7 +86,7 @@
(defn- unique-name
[name state]
(let [container-id (::current-page-id state)
unames (dm/get-in state [:unames container-id])]
unames (dm/get-in state [::unames container-id])]
(d/unique-name name (or unames #{}))))
(defn- clear-names [file]
@ -469,12 +469,24 @@
frame-id
(or frame-id (get state ::current-frame-id))
name
(or name "anonymous")
path
(d/nilv path "")
;; The main instance root name must match the component's path+name, so
;; the frame is renamed if needed. This keeps the builder output valid
;; under the :invalid-main-instance-name integrity check.
normalized-name
(cpn/merge-path-item path name)
change1
(d/without-nils
{:type :add-component
:id component-id
:name (or name "anonmous")
:path (d/nilv path "")
:name name
:path path
:main-instance-id frame-id
:main-instance-page page-id
:variant-id variant-id
@ -488,11 +500,13 @@
[{:type :set :attr :component-root :val true}
{:type :set :attr :main-instance :val true}
{:type :set :attr :component-id :val component-id}
{:type :set :attr :component-file :val file-id}]}]
{:type :set :attr :component-file :val file-id}
{:type :set :attr :name :val normalized-name}]}]
(-> state
(commit-change change1)
(commit-change change2))))
(commit-change change2)
(track-used-name normalized-name))))
(defn add-tokens-lib
[state tokens-lib]

View File

@ -18,6 +18,7 @@
[app.common.geom.shapes.common :as gsc]
[app.common.geom.shapes.transforms :as gst]
[app.common.math :as mth]
[app.common.path-names :as cpn]
[app.common.schema :as sm :refer [max-safe-int min-safe-int]]
[app.common.svg :as csvg]
[app.common.types.color :as clr]
@ -200,7 +201,7 @@
(let [[vb-x vb-y vb-width vb-height] (svg-dimensions svg-data)
unames (cfh/get-used-names objects)
svg-name (str/replace (:name svg-data) ".svg" "")
svg-name (cpn/remove-extension (:name svg-data))
svg-data (-> svg-data
(assoc :x (mth/round

View File

@ -1184,26 +1184,69 @@
initial-root? (:component-root shape-inst)
;; The component name is normalized (path separators with one space at
;; each side), so the name synced to the main instance root must be
;; normalized too. Otherwise the root would not match the component's
;; path+name and the file would fail :invalid-main-instance-name.
rename? (and (some? component)
(contains? (:touched shape-inst) :name-group))
original-name (:name shape-inst)
clean-name (when rename?
(cpn/clean-path original-name))
shape-inst (cond-> shape-inst
(some? clean-name)
(assoc :name clean-name))
root-inst shape-inst
root-main (ctf/get-component-root library component)
changes (cond-> changes
(and component (contains? (:touched shape-inst) :name-group))
(generate-rename-component (:component-id shape-inst) (:name shape-inst) library))]
;; When the synced shape is itself the component's main instance root,
;; `find-remote-shape` returns nil and the recursive sync never reaches
;; it, so it must be renamed explicitly to keep it matching the
;; component's path+name.
main-root? (and (some? component)
(= (:id shape-inst) (:main-instance-id component)))
(if component
(generate-sync-shape-inverse-recursive changes
container
shape-inst
component
library
file
libraries
shape-main
root-inst
root-main
initial-root?)
changes)))
changes (cond-> changes
(some? clean-name)
(generate-rename-component (:component-id shape-inst) clean-name library))
changes (if component
(generate-sync-shape-inverse-recursive changes
container
shape-inst
component
library
file
libraries
shape-main
root-inst
root-main
initial-root?)
changes)
changes (cond-> changes
(and (some? clean-name)
main-root?
(not= clean-name original-name))
(-> (update :redo-changes conj
(make-change container
{:type :mod-obj
:id (:id shape-inst)
:local-change? true
:operations [{:type :set
:attr :name
:val clean-name}]}))
(update :undo-changes conj
(make-change container
{:type :mod-obj
:id (:id shape-inst)
:local-change? true
:operations [{:type :set
:attr :name
:val original-name}]}))))]
changes))
(defn- generate-sync-shape-inverse-recursive
[changes container shape-inst component library file libraries shape-main root-inst root-main initial-root?]
@ -2754,10 +2797,20 @@
[path name] (cpn/split-group-name name)
;; The name of the main instance root must match the path+name of the
;; component. The component name is normalized here (via split-group-name),
;; so the root shape name must be normalized too. Otherwise creating a
;; component from a shape with an unnormalized name (e.g. "Group/Subgroup")
;; would leave a main instance failing :invalid-main-instance-name.
normalized-name (cpn/merge-path-item path name)
[root-shape updated-shapes]
(ctn/convert-shape-in-component root objects file-id)
(ctn/convert-shape-in-component (assoc root :name normalized-name) objects file-id)
changes (-> changes
(cond-> (not= normalized-name (:name root))
(pcb/update-shapes [(:id root)] #(assoc % :name normalized-name)
{:ignore-touched true}))
(pcb/add-component (:id root-shape)
path
name

View File

@ -147,6 +147,20 @@ Some naming conventions:
[(subs s 0 (inc last-period)) (subs s (inc last-period))]
[s ""]))
(defn remove-extension
"Removes the extension (the text after the last period, including the
period itself) from a name:
'photo.svg' -> 'photo'
'icon.SVG' -> 'icon'
'my.svg.icon.svg' -> 'my.svg.icon'
'archive.tar.gz' -> 'archive.tar'
'design' -> 'design'"
[name]
(let [[base ext] (split-by-last-period name)]
(if (str/empty? ext)
name
(subs base 0 (dec (count base))))))
;; Tree building functions --------------------------------------------------
(defn- attach-remaining-path

View File

@ -324,3 +324,20 @@
(t/is (some? rect))
(t/is (= (clr/parse "#0000ff") (:stroke-color stroke)) "child style beats group attr")
(t/is (nil? (get-in rect [:svg-attrs :stroke])) "group attr is not inherited over child style")))
;; Regression for https://github.com/penpot/penpot/issues/12773: the SVG
;; root shape name must have any extension stripped (not only ".svg"),
;; so the component name and the main instance root name match.
(t/deftest create-svg-shapes-strips-name-extension
(let [svg-data {:name "my.svg.icon.SVG"
:tag :svg
:attrs {:xmlns "http://www.w3.org/2000/svg"
:width "24" :height "24"
:viewBox "0 0 24 24"}
:content [{:tag :rect
:attrs {:x "0" :y "0" :width "24" :height "24"}
:content []}]}
[root _children] (sb/create-svg-shapes svg-data {:x 0 :y 0} {} nil nil #{} false)]
(t/is (= "my.svg.icon" (:name root)))))

View File

@ -7,6 +7,8 @@
(ns common-tests.files-builder-test
(:require
[app.common.files.builder :as fb]
[app.common.files.validate :as cfv]
[app.common.path-names :as cpn]
[app.common.uuid :as uuid]
[clojure.test :as t]))
@ -120,3 +122,30 @@
:height 10}))
shape (fb/get-shape state shape-id)]
(t/is (= "Group / Subgroup / Name" (:name shape)))))
(t/deftest add-component-renames-frame-to-match-component
(t/testing "the main instance frame name is aligned with the component's path+name"
(let [file-id (uuid/next)
page-id (uuid/next)
frame-id (uuid/next)
component-id (uuid/next)
state (-> (fb/create-state)
(fb/add-file {:id file-id :name "Test file"})
(fb/add-page {:id page-id :name "Page 1"})
(fb/add-board {:id frame-id
:name "Wrong / Frame / Name"
:x 0
:y 0
:width 10
:height 10})
(fb/add-component {:component-id component-id
:name "Component"
:path "Group / Subgroup"}))
shape (fb/get-shape state frame-id)
file-data (get-in state [::fb/files file-id :data])
component (get-in file-data [:components component-id])
errors (cfv/validate-file {:id file-id :data file-data} {})]
(t/is (= "Group / Subgroup / Component" (:name shape)))
(t/is (= (:name shape) (cpn/merge-path-item (:path component) (:name component))))
(t/is (nil? errors)))))

View File

@ -9,6 +9,7 @@
[app.common.data :as d]
[app.common.files.changes-builder :as pcb]
[app.common.files.shapes-helpers :as cfsh]
[app.common.files.validate :as cfv]
[app.common.geom.point :as gpt]
[app.common.logic.libraries :as cll]
[app.common.logic.shapes :as cls]
@ -91,6 +92,37 @@
(t/is (ctk/main-instance? root))
(t/is (ctk/main-instance-of? (:id root) (:id page) component))))
(t/deftest test-add-component-normalizes-root-name
(t/testing "main instance root name matches the component even if the source name is not normalized"
(let [;; ==== Setup
file (-> (thf/sample-file :file1)
(ths/add-sample-shape :frame1 :type :frame :name "Group/Subgroup / Name"))
page (thf/current-page file)
frame1 (ths/get-shape file :frame1)
;; ==== Action
[_ component-id changes]
(cll/generate-add-component (pcb/empty-changes)
[frame1]
(:objects page)
(:id page)
(:id file)
nil)
file' (thf/apply-changes file changes)
;; ==== Get
component (thc/get-component-by-id file' component-id)
root (ths/get-shape-by-id file' (:main-instance-id component))
errors (cfv/validate-file file' {})]
;; ==== Check
(t/is (= "Group / Subgroup" (:path component)))
(t/is (= "Name" (:name component)))
(t/is (= "Group / Subgroup / Name" (:name root)))
(t/is (nil? errors)))))
(t/deftest test-add-component-from-several-shapes
(let [;; ==== Setup
file (-> (thf/sample-file :file1)

View File

@ -8,16 +8,19 @@
(:require
[app.common.data :as d]
[app.common.files.changes-builder :as pcb]
[app.common.files.validate :as cfv]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.logic.libraries :as cll]
[app.common.logic.shapes :as cls]
[app.common.path-names :as cpn]
[app.common.test-helpers.components :as thc]
[app.common.test-helpers.compositions :as tho]
[app.common.test-helpers.files :as thf]
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as ths]
[app.common.types.component :as ctk]
[app.common.types.components-list :as ctkl]
[app.common.types.container :as ctn]
[app.common.types.modifiers :as ctm]
[app.common.types.shape-tree :as ctst]
@ -1120,3 +1123,64 @@
(t/is (not= (geometry main-group) (geometry main-group')))
(t/is (= (:shapes main-group) (:shapes main-group'')))
(t/is (= (geometry main-group) (geometry main-group'')))))
(defn- run-sync-inverse
"Run the inverse sync on `shape-tag`, which has been renamed to an
unnormalized name, and return the resulting main root and component
names, and the updated file."
[shape-tag]
(let [file (-> (thf/sample-file :file1)
(tho/add-simple-component-with-copy :component1
:main-root
:main-child
:copy-root)
(tho/change-shape shape-tag #(update % :touched (fnil conj #{}) :name-group))
(ths/update-shape shape-tag :name "Group/Subgroup"))
page (thf/current-page file)
container (ctn/make-container page :page)
changes (-> (pcb/empty-changes)
(pcb/with-container container)
(cll/generate-sync-shape-inverse (:data file)
{(:id file) file}
container
(thi/id shape-tag)))
file' (thf/apply-changes file changes)
main-root' (ths/get-shape file' :main-root)
component' (ctkl/get-component (:data file') (thi/id :component1))]
[(:name main-root')
(cpn/merge-path-item (:path component') (:name component'))
file']))
(t/deftest test-sync-inverse-normalizes-copy-name
(t/testing "syncing an unnormalized copy name keeps the component and its main root matching"
(let [file (-> (thf/sample-file :file1)
(tho/add-simple-component-with-copy :component1
:main-root
:main-child
:copy-root)
(tho/change-shape :copy-root #(update % :touched (fnil conj #{}) :name-group))
(ths/update-shape :copy-root :name "Group/Subgroup"))
page (thf/current-page file)
container (ctn/make-container page :page)
changes (-> (pcb/empty-changes)
(pcb/with-container container)
(cll/generate-sync-shape-inverse (:data file)
{(:id file) file}
container
(thi/id :copy-root)))
file' (thf/apply-changes file changes)
main-root' (ths/get-shape file' :main-root)
component' (ctkl/get-component (:data file') (thi/id :component1))]
;; ==== Check
(t/is (= "Group / Subgroup" (:name main-root')))
(t/is (= "Group / Subgroup" (cpn/merge-path-item (:path component') (:name component'))))
(t/is (nil? (cfv/validate-file file' {}))))))
(t/deftest test-sync-inverse-normalizes-main-name
(t/testing "syncing an unnormalized main name keeps the component and the main root matching"
(let [[main-root-name component-name file'] (run-sync-inverse :main-root)]
;; ==== Check
(t/is (= "Group / Subgroup" main-root-name))
(t/is (= "Group / Subgroup" component-name))
(t/is (nil? (cfv/validate-file file' {}))))))

View File

@ -9,6 +9,14 @@
[app.common.path-names :as cpn]
[clojure.test :as t]))
(t/deftest remove-extension
(t/is (= "photo" (cpn/remove-extension "photo.svg")))
(t/is (= "icon" (cpn/remove-extension "icon.SVG")))
(t/is (= "my.svg.icon" (cpn/remove-extension "my.svg.icon.svg")))
(t/is (= "archive.tar" (cpn/remove-extension "archive.tar.gz")))
(t/is (= "design" (cpn/remove-extension "design")))
(t/is (= "" (cpn/remove-extension ".svg"))))
(t/deftest split-group-name
(t/is (= ["foo" "bar"] (cpn/split-group-name "foo/bar")))
(t/is (= ["" "foo"] (cpn/split-group-name "foo")))

View File

@ -270,7 +270,11 @@ PENPOT_OIDC_USER_INFO_SOURCE:
__Since version 2.1.2__
Allows users to register and login with oidc without having to previously
register with another method.
register with another method. It also works with password login disabled
(`disable-login-with-password`): OIDC registration only needs this flag,
not password login. An invitation link also works as a substitute for
this flag: an invited user can register with OIDC even with both
registration flags disabled.
```bash
PENPOT_FLAGS: [...] enable-oidc-registration
@ -757,6 +761,25 @@ POSTGRES_USER: penpot
POSTGRES_PASSWORD: penpot
```
#### Connection pool
The backend keeps a pool of database connections. Its size can be tuned with:
```bash
# Backend
PENPOT_DATABASE_MIN_POOL_SIZE: 0
PENPOT_DATABASE_MAX_POOL_SIZE: 60
```
The values above are the defaults. The pool keeps at least `MIN_POOL_SIZE` idle
connections open and never opens more than `MAX_POOL_SIZE`, growing only under load
and closing idle connections after two minutes. A minimum larger than the maximum is
capped at the maximum.
If the PostgreSQL server is shared with other applications, make sure
`PENPOT_DATABASE_MAX_POOL_SIZE` plus the connections used by everything else stays below
the server's `max_connections` (100 by default).
### Storage
Storage refers to storing the user uploaded different objects in Penpot (assets, file data,...).

View File

@ -14,6 +14,7 @@
[app.common.files.shapes-builder :as sb]
[app.common.math :as mth]
[app.common.media :as media]
[app.common.path-names :as cpn]
[app.common.schema :as sm]
[app.common.types.container :as ctn]
[app.common.types.fills :as types.fills]
@ -435,10 +436,12 @@
fdata (dsh/lookup-file-data state file-id)
page (dsh/get-page fdata page-id)
pos (dsh/get-viewport-center state)]
pos (dsh/get-viewport-center state)
name (cpn/remove-extension (:name svg-data))]
(->> (create-shapes-svg file-id (:objects page) pos svg-data)
(rx/map (partial add-shapes-and-component it fdata page (:name svg-data))))))))
(rx/map (partial add-shapes-and-component it fdata page name)))))))
(defn upload-media-components
[params]

View File

@ -8,12 +8,24 @@
"Integration tests for the chunked-upload logic in
app.main.data.workspace.media."
(:require
[app.common.path-names :as cpn]
[app.common.test-helpers.files :as cthf]
[app.common.test-helpers.ids-map :as cthi]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.main.data.workspace.media :as media]
[app.main.store :as st]
[beicon.v2.core :as rx]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.http :as http]))
[frontend-tests.helpers.async :as async]
[frontend-tests.helpers.http :as http]
[frontend-tests.helpers.mock :as mock]
[frontend-tests.helpers.state :as ths]
[potok.v2.core :as ptk]
[tubax.core :as tubax]))
(t/use-fixtures :each
{:before cthi/reset-idmap!})
;; ---------------------------------------------------------------------------
;; Local helpers
@ -187,3 +199,62 @@
(http/restore-fetch! orig)
(t/is (= 3 @chunk-calls))
(done))))))))
;; ---------------------------------------------------------------------------
;; SVG component creation: any file extension must be stripped from both
;; the component name and the main instance root shape name.
;; ---------------------------------------------------------------------------
(def ^:private svg-content
(str "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 100\">"
"<rect x=\"0\" y=\"0\" width=\"100\" height=\"100\"/>"
"</svg>"))
;; The `sax` XML parser used by `tubax/xml->clj` is not available in the
;; ESM test build, so the parse step is mocked with an equivalent tree.
;; Everything else (name handling included) runs for real.
(def ^:private parsed-svg
{:tag :svg
:attrs {:xmlns "http://www.w3.org/2000/svg"
:width "100" :height "100"
:viewBox "0 0 100 100"}
:content [{:tag :rect
:attrs {:x "0" :y "0" :width "100" :height "100"}
:content []}]})
(t/deftest ^:async upload-svg-component-strips-svg-extension
(t/testing "creating a component from an uploaded SVG removes the extension from both names"
;; Any extension must be stripped (not only ".svg"), including
;; uppercase ones, extensions in the middle of the name, and
;; filenames with no extension at all.
(doseq [[filename expected]
[["my-component.svg" "my-component"]
["icon.SVG" "icon"]
["my.svg.icon.svg" "my.svg.icon"]
["design" "design"]]]
(let [file (cthf/sample-file :file1)
store (ths/setup-store file)
blob (js/File. #js [svg-content]
filename
#js {:type "image/svg+xml"})]
(await
(mock/with-mocks*
{st/state store
st/emit! (mock/stub (fn [& events] (apply ptk/emit! store events)))
tubax/xml->clj (mock/stub (fn [_source] parsed-svg))}
;; The SVG is placed at the viewport center; without a vbox the
;; geometry setup would receive nil coordinates.
(ptk/emit! store #(assoc % :workspace-local {:vbox {:x 0 :y 0 :width 800 :height 600}}))
(ptk/emit! store (media/upload-media-components {:file-id (:id file)
:blobs [blob]}))
(await (async/wait-for #(seq (get-in @store [:files (:id file) :data :components]))
"component created"))
(let [data (get-in @store [:files (:id file) :data])
component (first (vals (:components data)))
root (get-in data [:pages-index (:main-instance-page component)
:objects (:main-instance-id component)])]
(t/is (some? component) (str "component not created for " filename))
(t/is (some? root) (str "root not created for " filename))
(t/is (= expected (:name component)))
(t/is (= expected (:name root)))
(t/is (= expected (cpn/merge-path-item (:path component) (:name component)))))))))))