diff --git a/.serena/memories/backend/auth-permissions-product-domains.md b/.serena/memories/backend/auth-permissions-product-domains.md index 34db6d3183..284f21cde6 100644 --- a/.serena/memories/backend/auth-permissions-product-domains.md +++ b/.serena/memories/backend/auth-permissions-product-domains.md @@ -11,6 +11,8 @@ - Account lockout (flag `:account-lockout`, `app.auth.login-lockout`) is Redis-backed and keyed per profile id. Password and LDAP flows check/increment on the profile derived from the typed email and clear on the profile that actually logs in. - 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. +- Organization SSO (Nitrate): the RPC `check-nitrate-sso` takes the route's **query string proper** (`:query-string`, e.g. `screen=workspace&team-id=…`), never a URL and never with the `?` separator: the client strips it (`rt/token->query-string`) and the server puts it back. The server builds the destination with `app.auth.oidc/build-dest-url` (`public-uri` + `?` + the params, parsed and rendered back), so the redirect can never leave the public URI; a value whose parsed keys are not param names is not a route: a URL parses into a single key holding the whole URL, and a key carrying `:`, `/`, `?` or `#` is rejected with a `:warn` and the user lands on the public URI (the RPC never fails because of it). Do not reason from `u/map->query-string`: it keeps a valueless key as `key=`, so rendering is not what filters a URL out — the key shape is. A leading `?` is still trimmed server-side, so a client bundled before the trim keeps its destination. Never require `screen`: the root of the app carries its params instead of a screen (`?template=…`), and `check-nitrate-sso` is called from that branch too (`dtm/resolve-login-team-id`, which today reads only `:authorized` and never follows the redirect, so a wrong destination there is a silent `:warn` and not a visible break); the callback re-checks the destination before redirecting, which covers state tokens minted before that. The state token also carries `:profile-id`: only that session is granted the `props :sso` entry (`session-mismatch` otherwise, and the redirect still happens). +- Organization SSO needs `app.nitrate/organization-access?` whenever the RPC receives an `:organization-id` (that id always comes from the client): the profile must be a member of the organization or its owner; otherwise the RPC answers `{:authorized true :reason :no-organization-access}`, the same short-circuit shape as `:no-team-access`. - 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. 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. @@ -33,6 +35,7 @@ - Team/project commands mix DB changes, email, message bus notifications, media/storage cleanup, feature flags, quotas, and audit metadata. Keep mutations transactional when the existing command does so. - Invitation flows validate muted/bounced emails before sending and use tokenized invitation state. Accepting an invitation is tied to the invited member identity, not just possession of a token. - The whole invitation family is team-admin only: creating, updating and deleting invitations, minting the invitation token (`get-team-invitation-token`) and listing the pending ones (`get-team-invitations`, which carries the invited email and the offered role). The token of an invitation that grants the `owner` role is restricted to the team owner, because an admin cannot create nor promote an owner invitation (`cant-promote-to-owner`). This family is the exception to the rule above: it answers `:validation` / `:insufficient-permissions` instead of `:not-found`, so the five commands speak one language. +- With the `:admin-console` flag, the organization `send-invitations` policy (role value plus Nitrate `restrictions-met`, through `cto/allowed?`) gates every invitation command that hands out or manages access: create (and resend, which reuses it), `update-team-invitation-role`, `delete-team-invitation` and `get-team-invitation-token`. Only `get-team-invitations` stays on team admin alone, so a blocked admin still sees the list. The shared helper `check-send-invitations-policy` in `teams_invitations.clj` does a strict Nitrate lookup (fails closed; 404 means no organization) and runs outside a DB transaction, which is why the role and delete commands carry no `::db/transaction`. - Logical deletion is used for many product objects; prefer existing logical-deletion helpers over hard deletes unless the command already performs permanent cleanup. - Bounced/spam-complaint emails can mute/block a profile for login/registration and email sending. Devenv Mailpit is the normal local path for registration/email-flow testing. diff --git a/.serena/memories/backend/session-expiration.md b/.serena/memories/backend/session-expiration.md index 99c0d6acac..7efbf49089 100644 --- a/.serena/memories/backend/session-expiration.md +++ b/.serena/memories/backend/session-expiration.md @@ -19,6 +19,12 @@ - Renewal fires when `modified-at` is older than 6h (`default-renewal-max-age`, not configurable). It `UPDATE`s the same row (`modified-at` only) and issues a new token that keeps the original `:exp`. - `read-session` does not check age. Idle expiration is enforced on every request by `wrap-authz` (sessions past the idle window authenticate like missing ones and are never renewed); the GC only deletes the rows afterwards. +## Revocation paths + +- Profile deletion → `session/invalidate-all` (all sessions of the profile die). +- Authenticated password change → `session/invalidate-others` (every session except the caller's dies). +- Password recovery (`recover-profile`, GHSA-3j2p-qr45-q365) → `session/invalidate-all` inside the same transaction as the password `UPDATE`; there is no current session to preserve, and a captured session must not survive recovery. Sibling of GHSA-mj9f-5cwq-7p3q (logout deletes the current session row). + ## GC (`::tasks/gc`, cron `session-gc`, daily) - Deletes from `http_session_v2` where `modified_at < now - :auth-token-cookie-max-age` **or** `created_at < now - :auth-token-cookie-max-age-absolute`. diff --git a/.serena/memories/backend/storage.md b/.serena/memories/backend/storage.md index 678fa539d0..4a9b98ea31 100644 --- a/.serena/memories/backend/storage.md +++ b/.serena/memories/backend/storage.md @@ -118,8 +118,8 @@ Since `put-object!` uses backend-specific operations (`impl/resolve-backend` + ` ## File Data -- `file-data-backend` accepts `legacy-db`, `db`, or `storage`. -- `legacy-db` stores main data in `file.data` and snapshots in `file_change.data`. +- `file-data-backend` accepts `legacy-db`, `db`, or `storage`; `legacy-db` is read-only and the default is `db`. The configuration reads `legacy-db` as `db` at startup, with a warning (`normalize-file-data-backend`), so a deployment that kept the variable saves instead of failing on every write. +- `legacy-db` reads main data from `file.data` and snapshots from `file_change.data`; it no longer writes them (`fdata/upsert!` rejects it). - `db` stores encoded data in `file_data.data`. - `storage` stores encoded data in storage subsystem with `file-data` bucket, keeps `data` nil in `file_data` table, and points at the object with `file_data.storage_object_id`. - `file_data.storage_object_id` is a `NO ACTION DEFERRABLE INITIALLY IMMEDIATE` foreign key to `storage_object(id)`: the database refuses to physically delete a referenced object. The `IMMEDIATE` is load-bearing: the RI violation fires inside the `storage-gc-deleted` chunk transaction, so the chunk rolls back, is logged at error level with a generic hint plus the `constraint` and `table` the database reports (any `NO ACTION` foreign key on `storage_object` can block the delete, not only `file_data`'s) and is retried on the next run; with `INITIALLY DEFERRED` the check would fire at commit, outside the catch, and the sweep would never move past the blocked chunk. @@ -143,13 +143,13 @@ Since `put-object!` uses backend-specific operations (`impl/resolve-backend` + ` - `file_change` is partitioned by `HASH (file_id)` in 16 partitions, with the same bounds as `file_data`, so a join between them is resolved partition by partition (`enable_partitionwise_join` is set on every pooled connection). - The automatic migration (`0158-partition-file-change`) refuses to run when `file_change` is above 10 GiB total size or 2M rows (`pg_total_relation_size` + `pg_class.reltuples`, catalog reads). It analyzes the table first, because `reltuples` is -1 without statistics. It raises with the path of `operations/2.20.0-file-change-partition.md`, because the copy happens inside the migration transaction and a big instance would restart in a loop. Below the guard it raises `statement_timeout` to 30 minutes for the copy and the index builds, and analyzes the new partitions at the end, after the copy and the indexes, so the planner does not start with empty statistics. - Its primary key is `(file_id, id)`: a partitioned table needs the partition key in the primary key, so every access filters by `file_id`. From 2.20 the four snapshot commands (`update-file-snapshot`, `delete-file-snapshot`, `lock-file-snapshot`, `unlock-file-snapshot`) require the `file-id` param, because they use it to prune the partition; without it they answer `:params-validation`. -- It has a `kind` column: `1` is a snapshot (it has a label and its data lives in `file_data`) and `2` is a revision (it has no label and it holds the changes). The values are in `app.features.file-changes`. +- It has a `kind` column: `1` is a snapshot (it has a label and its data lives in `file_data`; the read-only `legacy-db` backend keeps old payloads in `data`) and `2` is a revision (it has no label and it holds the changes). The values are in `app.features.file-changes`. - `kind` is a deliberate reserve, not a leftover: today no query filters by it, the queries still tell a snapshot from a revision by its label (`label IS NOT NULL`), and `kind` takes over when they are rewritten. No constraint ties `kind` and `label` together on purpose: the day `kind` is the source of truth, that constraint would have to go. It stays nullable on purpose: the previous version inserts without it, and a rollback to it has to keep writing; the `NOT NULL` comes in a later migration. -- Every write of the application to a row of `file_change` moves `updated_at` to the moment of the write (`lock-by!`, `unlock!`, the snapshot rename and delete, `delete-object`, the restore of a file, and the `legacy-db` snapshot write of `fdata`). The actions of the foreign keys (`ON DELETE SET NULL` on `locked_by` and `profile_id`) change the row without touching it; during the partition both tables carry the same keys, so both receive them. The runbook of the partition uses it as the watermark of its delta passes: a write of the application that leaves it behind is lost at the cutover. +- Every write of the application to a row of `file_change` moves `updated_at` to the moment of the write (`lock-by!`, `unlock!`, the snapshot rename and delete, `delete-object`, the restore of a file). The actions of the foreign keys (`ON DELETE SET NULL` on `locked_by` and `profile_id`) change the row without touching it; during the partition both tables carry the same keys, so both receive them. The runbook of the partition uses it as the watermark of its delta passes: a write of the application that leaves it behind is lost at the cutover. - `file_change_legacy` is a frozen copy of the previous layout of the partition: it keeps the rows and the indexes of the old table. A new instance (empty table) does not keep it. On an upgraded instance the table stays until the operator drops it by hand days later (`DROP TABLE file_change_legacy;`, see `operations/2.20.0-file-change-partition.md`); nothing drops it automatically. - A revision carries only the delta of a save (the changes). The features of the file are not repeated on it: nobody read them from there. - A snapshot carries the features and the migrations of the file. -- It keeps the `data` column: the `legacy-db` backend is still supported (migration `0157-migrate-legacy-file-data` moved the existing blobs, but the backend keeps reading and writing that column). It has no `data_ref_id` or `data_backend` columns: nothing read them since the file storage refactor. +- It keeps the `data` column for reading only: migration `0157-migrate-legacy-file-data` moved the existing blobs to `file_data`, and `legacy-db` remains as a read-only backend for the payloads still in `data`. `fdata/upsert!` rejects `legacy-db` and the `file-data-backend` default is `db`. It has no `data_ref_id` or `data_backend` columns: nothing read them since the file storage refactor. - It has no index for the system snapshots. The old one filtered them by `data IS NOT NULL`, and with the payload living in `file_data` the column no longer tells snapshots apart. The new one could only filter them by `deleted_at IS NULL`, but the system snapshots always carry a deletion date in the future, so it would never match a row. The query that would use it filters that date with an "or", which no partial index can serve. - The `objects-gc` purge groups each chunk by `file_id` and locks it with `file_id = ?` and `id = ANY(?::uuid[])`: a tuple `IN ((file_id, id), ...)` gives the planner no constant partition key, so it scans all 16 partitions in every chunk. Each group is one `FOR UPDATE SKIP LOCKED` query inside the chunk transaction. diff --git a/.serena/memories/backend/subtleties.md b/.serena/memories/backend/subtleties.md index 436297b2fd..d387fb3b34 100644 --- a/.serena/memories/backend/subtleties.md +++ b/.serena/memories/backend/subtleties.md @@ -94,7 +94,7 @@ ## File data persistence -- File data backends are `legacy-db`, `db`, and `storage`. The storage backend keeps encoded file data in storage bucket `file-data`; the DB row stores nil data and points at the object with the `storage_object_id` column (FK `NO ACTION DEFERRABLE`). +- File data backends for writing are `db` and `storage`; `legacy-db` is a read-only alias for old payloads. The storage backend keeps encoded file data in storage bucket `file-data`; the DB row stores nil data and points at the object with the `storage_object_id` column (FK `NO ACTION DEFERRABLE`). - `fdata/upsert!` touches the storage object the persisted row pointed at before replacing it; the pointer travels in the params (`file->file-data-params` from the read file map, `nil` for a new row, the row for the offload) and the params of `update-file!` cannot override it. `binfile.common/update-file!` merges the read file map with the fields to change, so the pointer survives; `insert-file!` drops it because an INSERT never replaces a persisted object. The column is the only source of truth and the metadata no longer carries `storage-ref-id` (the migration removes it from the backfilled rows and the write path never stores it); the release is a coordinated restart when the storage backend is in use (the previous code cannot read the new rows), while the default `db` backend reads the same data column and tolerates a rolling deploy. Responses built from a file map go through `bfc/file->response`, which strips the pointer and a residual `storage-ref-id` from the metadata. - Pointer-map fragments are persisted separately as type `fragment`, and only modified pointer maps are written. A pointer keeps its id while nothing changes; content (`assoc`/`without`) and metadata (`with-meta`) changes produce a fresh id, so a fragment row is never rewritten after it is written. Re-assoc with an identical value returns the same map in array and hash maps alike, so a save keeps the fragment id of every page it does not change. - `enable-objects-map` skips unloaded pointer pages: their fragment was already wrapped when it was written, and loading it only to wrap it again costs one page read per untouched page on every save. A page with a plain fragment is wrapped when it is first loaded or edited; a data migration that enables `fdata/objects-map` must rewrite the data itself. @@ -108,7 +108,7 @@ - `fdata/realize` combines pointer realization and object-map realization. Use it before operations that need complete in-memory file data instead of pointer placeholders. - A `file_data` row is one of three classes, told apart by `type`: `main` is the file's current state and carries `id = file_id`; `snapshot` is a point-in-time copy whose `id` is the one its `file_change` row shares; `fragment` is a piece of the pointer-map, referenced from the file's data. - Their lifecycles differ: `main` and `snapshot` live with the file (dated when it is deleted, undated when it is restored), while a `fragment` carries no date and is collected by `file_gc` once the file stops referencing it. Neither is a session buffer, so do not read a `fragment` as an ephemeral revision. -- File snapshots (versions) keep their metadata row in `file_change` and their payload in `file_data` (`type = 'snapshot'`). With the `db` backend `file_change.data` stays NULL: that column is only written by the `legacy-db` backend, plus the rows written before the 2025-07 file storage refactor. +- File snapshots (versions) keep their metadata row in `file_change` and their payload in `file_data` (`type = 'snapshot'`). With the `db` backend `file_change.data` stays NULL: that column is only read by the `legacy-db` backend (it is no longer written), plus the rows written before the 2025-07 file storage refactor. - Tell a snapshot apart by `label IS NOT NULL` (with `created_by` / `deleted_at` as needed), never by `file_change.data IS NOT NULL`: the label holds with every file data backend, and the payload column stopped meaning "this is a snapshot" when the payload moved out. Queries that identify versions must use the label. `0157-migrate-legacy-file-data` moved the labelled leftovers to `file_data` and deleted the unlabelled payload rows, so from 2.19 on the label is the only marker there is. - The version quota counts of `app.rpc.quotes` (`sql:get-snapshots-per-file`, `sql:get-snapshots-per-team`) are served by the partial index `file_change__user_snapshots__idx`, whose predicate mirrors their where clause; changing one without the other drops the index only scan and the count walks the whole table again. - Revisions live only in `file_change` and have no `file_data` counterpart. They are born already scheduled for deletion one hour later (`app.rpc.commands.files-update`) and the snapshot paths cannot reach one, because they resolve their row through a `label IS NOT NULL` filter. diff --git a/.serena/memories/common/file-change-validation-migration-subtleties.md b/.serena/memories/common/file-change-validation-migration-subtleties.md index 75d9d80b4c..fab22a45fd 100644 --- a/.serena/memories/common/file-change-validation-migration-subtleties.md +++ b/.serena/memories/common/file-change-validation-migration-subtleties.md @@ -33,4 +33,5 @@ - `migrate-file` synthesizes legacy migration ids from old numeric versions when `:migrations` is absent, migrates legacy features, and records feature flags created through `cfeat/*new*`. - When a file had no previous `:migrations`, `migrate-file` marks all migrations as migrated in metadata so callers persist the complete migration set, not only transformations that changed data. - Closed schemas (`schema:stroke-attrs` and others) reject a key that was dropped from the declaration, so removing a declared attr needs a migration in the same change, for data already written with it. -- Rebase collision on a migration number: upstream keeps the id. Renumber the branch migration to the next free one, keep it last in `available-migrations`, and rename everything that spells the number — its test file and namespace (`files_migrations_00NN_test.cljc`), the test names, and comments in production code or memories that name it. \ No newline at end of file +- Rebase collision on a migration number: upstream keeps the id. Renumber the branch migration to the next free one, keep it last in `available-migrations`, and rename everything that spells the number — its test file and namespace (`files_migrations_00NN_test.cljc`), the test names, and comments in production code or memories that name it. +- Withdrawing a migration id: remove both the `defmethod` and the id from `available-migrations`; the replacement migration subsumes it and files that recorded the old id re-migrate through the replacement (persisted `:migrations` keep the old id via union — do not rewrite them, including playwright fixture JSONs). Idempotent/pure-cleanup migrations only; never withdraw one whose transform must not run again unless the replacement is safe for those files. Precedents: "0021-fix-shape-svg-attrs" -> "0035-fix-shape-svg-attrs-and-defs"; "legacy-36" -> "0036-clear-nil-keys-in-objects". diff --git a/.serena/memories/frontend/core.md b/.serena/memories/frontend/core.md index 139859bc98..e0e84e4d9f 100644 --- a/.serena/memories/frontend/core.md +++ b/.serena/memories/frontend/core.md @@ -12,7 +12,7 @@ Frontend: CLJS SPA; React/Rumext; Potok; RxJS; okulary refs; SCSS modules; share - `app.render_wasm.*`: frontend bridge to Rust/WASM renderer. - `app.util.*`: DOM, HTTP, i18n, keyboard, codegen, and general frontend utilities. - `frontend/packages/*` and `frontend/text-editor`: JS/TS workspace packages consumed by the app. -- Nitrate subscription/organization UI and flows live under `app.main.data.nitrate` and `app.main.ui.nitrate*`; backend/API behavior is covered by backend memories, and shared permission rules are in `common/src/app/common/types/nitrate_permissions.cljc`. +- Nitrate subscription/organization UI and flows live under `app.main.data.nitrate` and `app.main.ui.nitrate*`; backend/API behavior is covered by backend memories, and shared organization permission rules (`allowed?`, `can-send-invitations?`) are in `common/src/app/common/types/organization.cljc`. - `app.config` external bridges (`external-feature-flag`, `external-session-id`, `external-context-info`, `external-notify-register-success`, `initialize-external-context-info`) delegate to `globalThis` hooks injected by the SaaS host. Never delete `external-feature-flag`, even with zero call sites: it is the seam for future A/B tests. diff --git a/.serena/memories/frontend/dashboard-viewer-subtleties.md b/.serena/memories/frontend/dashboard-viewer-subtleties.md index 4e88a98a5d..148f5307e8 100644 --- a/.serena/memories/frontend/dashboard-viewer-subtleties.md +++ b/.serena/memories/frontend/dashboard-viewer-subtleties.md @@ -9,7 +9,7 @@ - Those metadata entries are read one at a time on purpose: a container holds thousands and every read opens its own decompression stream, which makes Chromium fail the analysis — zip.js then masks the cause behind a `Cannot close a ERRORED writable stream` — while Firefox tolerates it. Do not turn this back into a parallel read. The read is best-effort: a reference that cannot be read leaves that file without size, never the whole package. - The `:error` of an import entry is user-facing text, never an i18n key. The legacy SSE import stays in the backend for other clients. - File creation/duplication strips file `:data` before putting file summaries into dashboard state. -- The pending invitations are a team-admin only list (backend `:is-admin` already includes the owner). The dashboard hides the invitations entry from the header tabs and the team-options dropdown for non-admins, `invitations-section-content` keys the section on `:is-admin`, and `fetch-invitations` answers `insufficient-permissions` by emitting `invitations-fetched` with `[]`, so a demoted admin does not keep the cached emails and the header does not paint the invite button from a stale list. The organization `send-invitations` policy only gates inviting and resending, not the list. +- The pending invitations are a team-admin only list (backend `:is-admin` already includes the owner). The dashboard hides the invitations entry from the header tabs and the team-options dropdown for non-admins, `invitations-section-content` keys the section on `:is-admin`, and `fetch-invitations` answers `insufficient-permissions` by emitting `invitations-fetched` with `[]`, so a demoted admin does not keep the cached emails and the header does not paint the invite button from a stale list. The organization `send-invitations` policy (`can-invite?`) does not gate the list, but it gates every action on it: inviting (header and empty-state buttons render disabled with the `dashboard.invite-profile-disabled` tooltip), the role selector (read-only label), row selection (so resend and delete never show) and copy link (`invitation-link-copyable?`). The backend enforces the same rule. ## Viewer diff --git a/.serena/memories/frontend/plugin-api-to-cljs-binding.md b/.serena/memories/frontend/plugin-api-to-cljs-binding.md index 080c4b009c..31ceb4b10c 100644 --- a/.serena/memories/frontend/plugin-api-to-cljs-binding.md +++ b/.serena/memories/frontend/plugin-api-to-cljs-binding.md @@ -15,6 +15,9 @@ ## Runtime initialization and permissions - The frontend initializes `@penpot/plugins-runtime` only after `features/initialize` and only when feature `plugins/runtime` is active. It also installs the runtime `isPluginError` predicate into frontend error handling. +- `app.plugins/init-plugins-runtime` returns whether `ɵloadPlugin` was installed. It calls `preg/signal-runtime-ready` even on failure: `workspace.cljs` waits on that promise before `mcp/init`, so skipping it hangs MCP initialization forever. +- When SES bootstrap fails, `app.plugins` emits one `plugins-runtime-unavailable` telemetry event (with the SES error message), and `app.main.data.plugins/load-plugin!` shows the lazy `plugins.runtime-unavailable` toast when the user opens a plugin. `start-plugin!` (MCP path) stays silent by design. +- Runtime availability is detected by the existing `(fn? (unchecked-get global "ɵloadPlugin"))` check; do not add a second mechanism. - Manifest parsing expands write permissions to read permissions (`content:write` => `content:read`, etc.). Permission checks also allow the all-zero plugin id and the hard-coded MCP plugin id. - Manifest URL origin differs by manifest version: v1 clears the path; v2 joins `.` to the plugin URL. Existing plugin ids are reused by matching manifest name and host. - The MCP plugin id is defined in `app.plugins.register` to avoid a circular dependency with workspace MCP code. diff --git a/.serena/memories/frontend/routing-app-shell-subtleties.md b/.serena/memories/frontend/routing-app-shell-subtleties.md index 2735fa292e..ff7f334484 100644 --- a/.serena/memories/frontend/routing-app-shell-subtleties.md +++ b/.serena/memories/frontend/routing-app-shell-subtleties.md @@ -7,6 +7,8 @@ - `set-token!` and `replace-token!` are deliberately asymmetric. `set-token!` is a total no-op when the token already is the current one (no URL write, no report), so navigating to the screen you are on cannot grow the history stack. `replace-token!` always writes and always reports, because it is how the app corrects the URL in place (legacy `#/…` translation, cleaned share links) and that correction must reach the router even when the token did not change. This matches `Html5History`, which did the same; do not "fix" the asymmetry without deciding what navigates to the current screen. - `app.util.dom/replace-history-state!` is the deliberate bypass used by `app.main.ui.dashboard`/`app.main.ui.static` to rewrite the URL **without** routing. Keep using it there instead of `replace-token!`, which would re-enter the router. - Route params live entirely in the query map under the reserved `screen` key; duplicate query params can become vectors, so use `rt/get-query-param` when a scalar is required. +- The organization SSO flow sends the **query string** (`rt/get-current-query-string`), not the URL and not the token: the backend builds the destination URL from `public-uri` + `?` + those params, so the client never chooses the origin of the redirect. The history token (`rt/get-current-token`, `location.search`) keeps its leading `?` because every other consumer concatenates it over the base path (`str cf/public-uri token`, `dom/append-query-param`, `dom/remove-query-param`); `rt/token->query-string` is the only bridge between the two shapes, so do not trim at a call site by hand; `rt/token->url` is the single client builder of an absolute destination from either shape (used by the SSO retry and error flow), so do not assemble `public-uri + "?" + params` at call sites. The gate is not the only caller: `dtm/resolve-login-team-id` runs from the root branch of `on-navigate` too, and there the URL carries its params instead of a screen (`?template=…`) — that call only reads `:authorized`, so it never follows the destination, which is exactly why a wrong destination there goes unnoticed. Do not add a rule that requires `screen`. See `mem:backend/auth-permissions-product-domains`. +- The router events expose their payload through `@event` (`IDeref`): `rt/navigate` returns `{:id :params :options}` and `rt/nav-raw` returns its `:href`/`:uri`. `rt/nav-raw` is the way out of the router (raw URL, outside the token scheme) and nothing in the app listens to `::rt/nav-raw`; the destination travels in the effect, so `@event` is the only way to tell where the user is being sent (tests included). - Legacy `#/…` hash URLs translate client-side to the query format (one-version compat; see `legacy-routes` in `app.main.ui.routes`, TODO(next-version) to delete). - Unknown/empty routes trigger an extra `get-profile`/`get-teams` check before redirecting. This avoids invitation and root-route race conditions. - The root app has two error layers, and they reset differently. `:exception` in the store renders `static/exception-page*` directly and is cleared by `rt/navigated`; a React render error is caught by `app.main.ui.error-boundary` (`react-error-boundary`), whose error lives in internal React state that only `reset-error-boundary` clears. Because every exit from the error page (the logo, the no-permission dialogs, the SSO dialog, the feedback link) only *navigates*, the boundary is given `:resetKeys [(eb/reset-key route)]` — the screen name — so a navigation counts as a fresh attempt at rendering. Three traps in that `resetKeys` value, all learned the hard way: it must be a real JS array (`#js [...]`), never a ClojureScript vector, because `react-error-boundary` reads `.length` and `.some` and a `PersistentVector` has neither, so `componentDidUpdate` throws `some is not a function`; it must hold the screen name as a *string*, because the library compares entries with `Object.is` and ClojureScript does not intern keywords, so two equal keywords are two objects and a keyword key would read as new on every route rebuilt, resetting the boundary on a param change inside the same screen; and it must be the screen rather than the params, so a param change does not replay the screen that just crashed. Navigating to the *same* screen therefore does not reset, and a reload is the only way out of that case. diff --git a/.serena/memories/plugins/core.md b/.serena/memories/plugins/core.md index 64f4643860..16ec5165bf 100644 --- a/.serena/memories/plugins/core.md +++ b/.serena/memories/plugins/core.md @@ -18,6 +18,7 @@ ## Sandbox and global cleanup +- `src/index.ts` calls `bootstrapSES()` (in `src/lib/bootstrap.ts`) at module load. If `repairIntrinsics` throws (e.g. SES 2.2+ rejects a `Temporal` wrapped by a browser extension), the error is caught, `getPluginsRuntimeError()` exposes it, and `initPluginsRuntime` installs no `ɵ*` globals: the frontend app still boots, plugins and MCP are unavailable in that environment. On failure, `Function.prototype.toString` has already been replaced but no intrinsics are hardened and no compartment/sandbox is created — never run plugins on that path. - The runtime uses SES compartments. Public API return values are passed through `ses.safeReturn` before crossing back to plugin code. - Plugin `fetch` is sanitized: credentials are omitted and Authorization is blanked. The exposed response only includes ok/status/statusText/url/text/json. - Timer callbacks are wrapped to mark plugin-originated errors, and timeout/interval IDs are tracked so plugin close can clear them. diff --git a/backend/src/app/auth/oidc.clj b/backend/src/app/auth/oidc.clj index 477236c291..0de9163f84 100644 --- a/backend/src/app/auth/oidc.clj +++ b/backend/src/app/auth/oidc.clj @@ -690,15 +690,113 @@ (assoc :query (u/map->query-string params)))] (redirect-response uri)))) +(defn- public-uri + "The configured public URI, parsed, with the app base path normalized with + a trailing slash." + [] + (u/ensure-path-slash (u/uri (cf/get :public-uri)))) + +(defn- public-uri-base + "The public URI as a string, with the app base path normalized with a + trailing slash. Every URL the server builds starts here, so the app base + path is never lost." + [] + (str (public-uri))) + (defn- redirect-with-organization-sso-error [{:keys [dest-url organization-id organization-name]}] - (-> (str (or dest-url (cf/get :public-uri))) + (-> (str (or dest-url (public-uri-base))) (u/append-query-param :sso-error true) (u/append-query-param :organization-id organization-id) (cond-> organization-name (u/append-query-param :organization-name organization-name)) (redirect-response))) +(defn- routable-params? + "True when `params` are params of a route: at least one, and no key carries + a scheme, a path or a fragment. `screen=workspace&team-id=…` and the root + params of the application (`template=…`) qualify; `https://evil.example/` + does not." + [params] + (and (seq params) + (every? #(nil? (re-find #"[/:?#]" (name %))) (keys params)))) + +(defn build-dest-url + "Return the absolute URL for `query-string` under the current public URI. + + The query string is the route of the client (`screen=…¶ms`, and the root + of the application may carry only its params, like `template=…`): it does not + carry the `?` that separates it from the path, which is put back here, so the + destination is always built by the server and the client only picks the route. + A value that still comes with the separator is trimmed, so a client bundled + before that change keeps its destination. + + The params are parsed and rendered back, so a value that is not a route never + becomes a destination: it is ignored with a warning and the user lands on the + public URI, which is also where a blank one lands." + [query-string] + (try + (let [base (public-uri-base) + query (str query-string) + route (if (str/starts-with? query "?") + (subs query 1) + query) + params (u/query-string->map route)] + (cond + (str/blank? query) + base + + (not (routable-params? params)) + (do (l/warn :hint "ignoring a redirect target that is not a route" + :query-string (subs route 0 (min 200 (count route)))) + base) + + :else + (str base "?" (u/map->query-string params)))) + (catch Exception e + ;; The destination must never break the SSO flow: a query string the + ;; server cannot parse lands on the public URI instead. + (l/warn :hint "could not build the redirect target" :cause e) + (public-uri-base)))) + +(defn- same-origin? + "True when `uri` has the same origin as `base`: scheme, host (case + insensitive) and port, with the default port made explicit." + [uri base] + (let [default-port (fn [scheme] (if (= "https" (str/lower (str scheme))) 443 80)) + port (fn [u] (or (:port u) (default-port (:scheme u))))] + (and (= (str/lower (str (:scheme uri))) + (str/lower (str (:scheme base)))) + (= (str/lower (str (:host uri))) + (str/lower (str (:host base)))) + (= (port uri) (port base))))) + +(defn- under-base-path? + "True when the path of `uri` is under the base path of `base`." + [uri base] + (str/starts-with? (str (:path uri)) (:path base))) + +(defn- trusted-dest-url + "Return `dest-url` when it points inside the current public URI, nil + otherwise. + + The destination is built by the server (see `build-dest-url`), but state + tokens minted before that was the case may carry any URL, so the callback + checks the destination before redirecting to it. The check compares the + parsed origin, so neither a userinfo prefix + (`https://penpot.example.com@evil.example/`) nor a look-alike host + (`https://penpot.example.com.evil.example/`) can pass, and it requires + the path to be under the base path of the public URI, so the destination + stays inside the application." + [dest-url] + (when (some? dest-url) + (let [dest (try (u/uri (str dest-url)) (catch Exception _ nil)) + base (public-uri)] + (when (and (some? dest) + (same-origin? dest base) + (under-base-path? dest base)) + (str dest-url))))) + (defn- redirect-to-register [cfg info provider] (let [info (assoc info @@ -915,11 +1013,18 @@ (defn build-organization-sso-auth-redirect-uri "Build the OIDC authorization redirect URI for an organization SSO config. - Raises if the config is incomplete or OIDC discovery fails." - [cfg sso & {:keys [dest-url organization-id provider]}] + Raises if the config is incomplete or OIDC discovery fails. + + `query-string` is the route of the client (`screen=…¶ms`, the query + string proper, without its separator): the destination is built here, so it + can never point outside the public URI. + `profile-id` is the profile that started the flow; it travels in the + state token so the callback only grants the organization SSO entry to that + very session." + [cfg sso & {:keys [query-string organization-id provider profile-id]}] (let [organization-id (or organization-id (:organization-id sso)) issuer (organization-sso-discovery-uri sso) - dest-url (or dest-url (str (cf/get :public-uri)))] + dest-url (build-dest-url query-string)] (when-not issuer (raise-invalid-sso-config :hint "missing issuer" @@ -929,6 +1034,7 @@ state-token (tokens/generate cfg {:iss "oidc" :dest-url dest-url :organization-id organization-id + :profile-id profile-id :issuer issuer :exp (ct/in-future "4h")})] (build-auth-redirect-uri oidc-provider state-token)) @@ -1024,25 +1130,39 @@ (defn- organization-sso-callback-handler "Handle the organization-SSO branch of the OIDC callback: state carries :dest-url — exchange the authorization code with the OIDC provider to - verify authentication actually occurred, then redirect back to dest-url." + verify authentication actually occurred, then redirect back to dest-url. + + The state also carries the :profile-id that started the flow: only that + session is granted the organization SSO entry, so a state prepared by + someone else cannot mark the session of the one who completes it." [cfg request state code] - (let [dest-url (:dest-url state)] + (let [dest-url (trusted-dest-url (:dest-url state))] (try (let [organization-id (:organization-id state) sso (nitrate/call cfg :get-organization-sso {:organization-id organization-id}) provider (prepare-organization-sso-provider cfg sso) _info (get-info cfg provider state code) session (session/get-session request) - exp (ct/in-future {:hours 4})] - (when (and session organization-id) - (let [props (-> (or (:props session) {}) - (update :sso assoc organization-id exp))] - (session/update-session (::session/manager cfg) (assoc session :props props)) + exp (ct/in-future {:hours 4}) + ;; A state minted before the profile claim existed never + ;; matches, and neither does the session of another profile. + same-profile? (and (some? session) + (some? (:profile-id state)) + (= (:profile-id state) (:profile-id session)))] + (if (and (some? session) (not same-profile?)) + (submit-organization-sso-auth-event + cfg request (:profile-id session) organization-id "organization-sso-auth-failed" + :failure-reason "session-mismatch") + (do + (when (and session organization-id) + (let [props (-> (or (:props session) {}) + (update :sso assoc organization-id exp))] + (session/update-session (::session/manager cfg) (assoc session :props props)) + (submit-organization-sso-auth-event + cfg request (:profile-id session) organization-id "organization-sso-token-issued"))) (submit-organization-sso-auth-event - cfg request (:profile-id session) organization-id "organization-sso-token-issued"))) - (submit-organization-sso-auth-event - cfg request (:profile-id session) organization-id "organization-sso-auth-succeeded") - (redirect-response dest-url)) + cfg request (:profile-id session) organization-id "organization-sso-auth-succeeded"))) + (redirect-response (or dest-url (public-uri-base)))) (catch Throwable cause (let [{:keys [code]} (ex-data cause)] (binding [l/*context* (errors/request->context request)] @@ -1060,7 +1180,7 @@ (let [organization-id (:organization-id state) organization-name (:name (nitrate/call cfg :get-organization-summary {:organization-id organization-id}))] (redirect-with-organization-sso-error - {:dest-url dest-url + {:dest-url (or dest-url (public-uri-base)) :organization-id organization-id :organization-name organization-name})))))) diff --git a/backend/src/app/config.clj b/backend/src/app/config.clj index 615a009a71..16f77db14b 100644 --- a/backend/src/app/config.clj +++ b/backend/src/app/config.clj @@ -10,6 +10,7 @@ [app.common.data :as d] [app.common.exceptions :as ex] [app.common.flags :as flags] + [app.common.logging :as l] [app.common.schema :as sm] [app.common.time :as ct] [app.common.uri :as u] @@ -412,6 +413,22 @@ (def explain-config (sm/explainer schema:config)) +(defn- normalize-file-data-backend + "The file data moved to `file_data` (migration `0157-migrate-legacy-file-data`), so + `file.data` and `file_change.data` are read-only leftovers and the `legacy-db` + back end is read-only: a write against it fails on every save. Accepting it would + start a healthy instance that cannot save a single file, so it reads as `db`, + which serves the legacy rows too. Warn instead of aborting: the operator may + still be halfway through the migration." + [config] + (if (= "legacy-db" (c/get config :file-data-backend)) + (do + (l/warn :hint "file-data-backend=legacy-db is read-only, using db" + :file-data-backend "legacy-db" + :replacement "db") + (assoc config :file-data-backend "db")) + config)) + (defn read-config "Reads the configuration from environment variables and decodes all known values." @@ -419,7 +436,8 @@ (->> (read-env prefix) (merge default) (normalize-tenant) - (decode-config))) + (decode-config) + (normalize-file-data-backend))) (def version (v/parse (or (some-> (io/resource "version.txt") diff --git a/backend/src/app/features/fdata.clj b/backend/src/app/features/fdata.clj index a7af13c9f7..231c879bb3 100644 --- a/backend/src/app/features/fdata.clj +++ b/backend/src/app/features/fdata.clj @@ -182,7 +182,7 @@ result)) (defn- handle-persistence - [cfg {:keys [type backend id file-id data] :as params}] + [cfg {:keys [backend id file-id data] :as params}] (let [expected (:storage-object-id params)] (cond (= backend "storage") @@ -212,43 +212,6 @@ {:result (upsert-in-database cfg (assoc params :storage-object-id nil) expected) :storage-object-id nil} - (= backend "legacy-db") - {:result - (cond - (= type "main") - (do - (db/delete! cfg :file-data - {:id id :file-id file-id :type "main"} - {::db/return-keys false}) - (db/update! cfg :file - {:data data} - {:id file-id} - {::db/return-keys false})) - - (= type "snapshot") - (do - (db/delete! cfg :file-data - {:id id :file-id file-id :type "snapshot"} - {::db/return-keys false}) - ;; every mutation of `file_change` moves `updated_at`: the partition - ;; runbook uses it as the watermark of the delta passes - (db/update! cfg :file-change - {:data data - :updated-at (ct/now)} - {:file-id file-id :id id} - {::db/return-keys false})) - - (= type "fragment") - (upsert-in-database cfg - (-> (dissoc params :metadata) - (assoc :backend "db") - (assoc :storage-object-id nil)) - expected) - - :else - (throw (RuntimeException. "not implemented"))) - :storage-object-id nil} - :else (throw (IllegalArgumentException. (str "backend '" backend "' not supported")))))) @@ -264,7 +227,13 @@ (defn- default-backend [backend] - (or backend (cf/get :file-data-backend))) + (let [backend (or backend (cf/get :file-data-backend))] + (when (= "legacy-db" backend) + (ex/raise :type :validation + :code :legacy-backend-read-only + :hint "the legacy-db file data backend is read-only; set the file data backend to db or storage" + :backend backend)) + backend)) (defn decode-metadata [metadata] @@ -277,7 +246,7 @@ [:id ::sm/uuid] [:type [:enum "main" "snapshot" "fragment"]] [:file-id ::sm/uuid] - [:backend {:optional true} [:enum "db" "legacy-db" "storage"]] + [:backend {:optional true} [:enum "db" "storage"]] [:metadata {:optional true} ctf/schema:file-metadata] ;; The storage object the persisted row pointed at before this write; it is ;; the object to touch when the row replaces it. Required so a new caller diff --git a/backend/src/app/http/session.clj b/backend/src/app/http/session.clj index 4e9ce3bd77..12fd7353b1 100644 --- a/backend/src/app/http/session.clj +++ b/backend/src/app/http/session.clj @@ -222,7 +222,8 @@ (defn invalidate-all "Delete all sessions for a given profile. Used when a profile is deleted - to ensure immediate access revocation across all devices." + or a password is recovered, to ensure immediate access revocation across + all devices." [cfg profile-id] (let [sql "delete from http_session_v2 where profile_id = ?"] (-> (db/exec-one! cfg [sql profile-id]) diff --git a/backend/src/app/media/local.clj b/backend/src/app/media/local.clj index f3811e83aa..6cd9b0c866 100644 --- a/backend/src/app/media/local.clj +++ b/backend/src/app/media/local.clj @@ -241,8 +241,12 @@ foutput (fs/path (str finput ".ttf"))] (try (io/write* finput data) + ;; CID-keyed fonts (common in CJK) load as several sub-fonts and + ;; Generate only writes the active one unless they are merged. (let [res (exec-font! system ["fontforge" "-lang=ff" "-c" - (str/fmt "Open('%s'); Generate('%s')" + (str/fmt (str "Open('%s'); " + "if ($cidfontname != \"\"); CIDFlatten(); endif; " + "Generate('%s')") (str finput) (str foutput))])] (when (zero? (:exit res)) diff --git a/backend/src/app/nitrate.clj b/backend/src/app/nitrate.clj index d0d8c3e219..6fb33b6ebf 100644 --- a/backend/src/app/nitrate.clj +++ b/backend/src/app/nitrate.clj @@ -398,6 +398,20 @@ (generate-nitrate-uri "api/subscriptions/" profile-id) schema:subscription params)) +(def ^:private schema:profile-subscription + [:map {:title "ProfileSubscription"} + [:profile-id ::sm/uuid] + [:type ::sm/text] + [:status ::sm/text]]) + +(defn- get-subscriptions-api + [cfg {:keys [profile-ids] :as params}] + (let [params (assoc params :request-params {:profile-ids profile-ids})] + (request-to-nitrate cfg :post + (generate-nitrate-uri "api/subscriptions/bulk") + [:vector schema:profile-subscription] + params))) + (def ^:private schema:subscription-warning [:maybe [:map {:title "SubscriptionWarning"} @@ -533,6 +547,7 @@ :delete-team (partial delete-team-api cfg) :remove-team-from-organization (partial remove-team-from-organization-api cfg) :get-subscription (partial get-subscription-api cfg) + :get-subscriptions (partial get-subscriptions-api cfg) :get-subscription-warning (partial get-subscription-warning-api cfg) :connectivity (partial get-connectivity-api cfg) :get-identity (partial get-identity-api cfg) @@ -699,6 +714,47 @@ team)) teams)))) +(def ^:private valid-licence-statuses + "The statuses `app.main.data.nitrate/is-valid-license?` accepts on the + frontend." + #{"active" "past_due" "trialing"}) + +(defn- get-valid-licence-statuses + "Returns a map of profile id to the status of its valid Nitrate licence. + Returns an empty map when Nitrate does not answer: the licences of team + owners only decide which plan a team shows." + [cfg profile-ids params] + (try + (let [params (assoc (or params {}) :profile-ids (vec profile-ids)) + subscriptions (call cfg :get-subscriptions params)] + (into {} + (comp (filter #(contains? valid-licence-statuses (:status %))) + (map (juxt :profile-id :status))) + subscriptions)) + (catch Throwable cause + (l/wrn :hint "failed to get team owner licences" + :cause cause) + {}))) + +(defn add-owner-licence-to-teams + "Sets type \"nitrate\" in the `:subscription` of each team outside an + organization whose owner has a valid Nitrate licence, so every member + sees the plan of the owner, not only the owner. The database only knows + the Stripe subscription of the owner, which teams keep when Nitrate does + not answer. Teams in an organization take their plan from it." + [cfg teams params] + (let [owner-id #(when (nil? (:organization %)) + (get-in % [:subscription :owner-id])) + owner-ids (into #{} (keep owner-id) teams)] + (if (empty? owner-ids) + teams + (let [statuses (get-valid-licence-statuses cfg owner-ids params)] + (mapv (fn [team] + (if-let [status (get statuses (owner-id team))] + (update team :subscription assoc :type "nitrate" :status status) + team)) + teams))))) + (defn set-team-organization "Associates a team with an organization in Nitrate. Requires organization-id and is-default in params. @@ -716,12 +772,19 @@ :organization-id (:organization-id params)})) team)) +(defn- organization-membership + "The membership of `profile-id` in `organization-id`, as Nitrate reports it. + The profile travels in the path of the Nitrate endpoint, so no acting + profile is sent." + [cfg profile-id organization-id] + (call cfg :get-organization-membership {:profile-id profile-id + :organization-id organization-id})) + (defn assert-membership "Verifies that the user is a member of the organization. Raises an exception if the organization doesn't exist or the user is not a member." [cfg profile-id organization-id] - (let [membership (call cfg :get-organization-membership {:profile-id profile-id - :organization-id organization-id})] + (let [membership (organization-membership cfg profile-id organization-id)] (when-not (:organization-id membership) (ex/raise :type :validation :code :organization-does-not-exist)) @@ -729,3 +792,18 @@ (when-not (:is-member membership) (ex/raise :type :validation :code :user-doesnt-belong-organization)))) + +(defn organization-access? + "True when `profile-id` may act for `organization-id`: it is a member of + the organization or its owner. + + The organization id reaches the backend from the client, so the flows that + take one have to check it: without this check any authenticated profile + could start a flow for an organization it does not belong to." + [cfg profile-id organization-id] + (let [membership (organization-membership cfg profile-id organization-id)] + (if (:is-member membership) + true + (= (:owner-id (call cfg :get-organization-summary {:organization-id organization-id + ::rpc/profile-id profile-id})) + profile-id)))) diff --git a/backend/src/app/rpc/commands/auth.clj b/backend/src/app/rpc/commands/auth.clj index 50361fd9dc..11d32a636f 100644 --- a/backend/src/app/rpc/commands/auth.clj +++ b/backend/src/app/rpc/commands/auth.clj @@ -205,6 +205,11 @@ (let [profile (some->> (validate-token token) (update-password conn))] (when profile + ;; Recovery must revoke every existing session: a previously + ;; captured cookie otherwise keeps working after the password + ;; change (GHSA-3j2p-qr45-q365). Runs inside the same transaction + ;; as the password update (::db/transaction true). + (session/invalidate-all cfg (:id profile)) (login-lockout/clear-attempts! cfg (:id profile)) (eml/send-template cfg {::eml/reuse-conn true ::eml/template "password-changed" diff --git a/backend/src/app/rpc/commands/nitrate.clj b/backend/src/app/rpc/commands/nitrate.clj index af52bede6f..61243719c8 100644 --- a/backend/src/app/rpc/commands/nitrate.clj +++ b/backend/src/app/rpc/commands/nitrate.clj @@ -704,7 +704,7 @@ [:map {:title "CheckNitrateSsoParams"} [:team-id {:optional true} ::sm/uuid] [:organization-id {:optional true} ::sm/uuid] - [:url ::sm/uri]] + [:query-string [:string {:max 2048}]]] [::sm/contains-any #{:team-id :organization-id}]]) (sv/defmethod ::check-nitrate-sso @@ -714,40 +714,57 @@ session already holds a valid entry for the organization, and {:authorized true :reason :no-team-access} when the gate was skipped because the user cannot access the team; the reason lets the client tell a usable session - apart from a plain permission failure. + apart from a plain permission failure. When an organization id comes with the + request, the gate is also skipped with + {:authorized true :reason :no-organization-access} when the profile neither + belongs to that organization nor owns it. Returns {:authorized false :redirect-uri } when SSO is active; the client must redirect there. The OIDC provider itself handles re-authentication transparently if the user already has an active SSO session. - A nil :redirect-uri means SSO is required but the provider is not usable." + A nil :redirect-uri means SSO is required but the provider is not usable. + + The :query-string is the route of the client as a query string + (`screen=…¶ms`, without the `?` that separates it from the path; the root + of the application may carry only its params, like `template=…`): the server + builds the destination URL from it, so it can never point outside the public + URI. A value without params of its own is not a route, is ignored with a + warning, and the user lands on the home page." {::rpc/auth true ::doc/added "2.18" ::sm/params schema:check-nitrate-sso ::nitrate/sso false} - [cfg {:keys [::rpc/profile-id team-id organization-id url] :as params}] + [cfg {:keys [::rpc/profile-id team-id organization-id query-string] :as params}] (if (contains? cf/flags :admin-console) (if (and team-id (not (teams/has-read-permissions? cfg profile-id team-id))) ;; Let the destination RPC enforce its own permissions. Starting SSO before ;; access is established sends unrelated users through the organization's IdP. {:authorized true :reason :no-team-access} - (let [request (rph/get-request params) - {:keys [authorized sso]} (nitrate/sso-session-authorized? cfg organization-id team-id request)] - (if authorized - {:authorized true :reason :sso-satisfied} - (if (oidc/organization-sso-discovery-uri sso) - (try - (let [redirect-uri (oidc/build-organization-sso-auth-redirect-uri - cfg sso - :dest-url url - :organization-id organization-id) - organization-id (or organization-id (:organization-id sso))] - (oidc/submit-organization-sso-auth-started-event - cfg request profile-id organization-id) - {:authorized false :redirect-uri redirect-uri}) - (catch Throwable cause - (oidc/submit-organization-sso-auth-failed-event - cfg request profile-id (or organization-id (:organization-id sso)) cause) - (throw cause))) - {:authorized false - :redirect-uri nil})))) + (if (and organization-id + (not (nitrate/organization-access? cfg profile-id organization-id))) + ;; The organization id comes from the client: without this check any + ;; authenticated profile could start a flow for an organization it does + ;; not belong to. + {:authorized true :reason :no-organization-access} + (let [request (rph/get-request params) + {:keys [authorized sso]} (nitrate/sso-session-authorized? cfg organization-id team-id request)] + (if authorized + {:authorized true :reason :sso-satisfied} + (if (oidc/organization-sso-discovery-uri sso) + (try + (let [redirect-uri (oidc/build-organization-sso-auth-redirect-uri + cfg sso + :query-string query-string + :organization-id organization-id + :profile-id profile-id) + organization-id (or organization-id (:organization-id sso))] + (oidc/submit-organization-sso-auth-started-event + cfg request profile-id organization-id) + {:authorized false :redirect-uri redirect-uri}) + (catch Throwable cause + (oidc/submit-organization-sso-auth-failed-event + cfg request profile-id (or organization-id (:organization-id sso)) cause) + (throw cause))) + {:authorized false + :redirect-uri nil}))))) {:authorized true :reason :sso-satisfied})) diff --git a/backend/src/app/rpc/commands/teams.clj b/backend/src/app/rpc/commands/teams.clj index 14a4e6786e..4c75aa9689 100644 --- a/backend/src/app/rpc/commands/teams.clj +++ b/backend/src/app/rpc/commands/teams.clj @@ -162,7 +162,8 @@ WHEN 'professional' THEN 'active' ELSE COALESCE(p.props->'~:subscription'->>'~:status', 'incomplete') END, - '~:seats', p.props->'~:subscription'->'~:quantity' + '~:seats', p.props->'~:subscription'->'~:quantity', + '~:owner-id', '~u' || p.id::text ) AS subscription FROM team_profile_rel AS tp JOIN team AS t ON (t.id = tp.team_id) @@ -212,8 +213,9 @@ (dm/with-open [conn (db/open pool)] (let [teams (get-teams conn profile-id)] (if (contains? cf/flags :admin-console) - (->> (nitrate/add-organization-info-to-teams cfg teams params) - (remove #(get-in % [:organization :expired-license]))) + (as-> (nitrate/add-organization-info-to-teams cfg teams params) teams + (remove #(get-in % [:organization :expired-license]) teams) + (nitrate/add-owner-licence-to-teams cfg teams params)) teams)))) (def ^:private sql:get-owned-teams @@ -259,7 +261,8 @@ [cfg {:keys [::rpc/profile-id id file-id] :as params}] (let [team (get-team cfg :profile-id profile-id :team-id id :file-id file-id)] (if (contains? cf/flags :admin-console) - (nitrate/add-organization-info-to-team cfg team params) + (let [team (nitrate/add-organization-info-to-team cfg team params)] + (first (nitrate/add-owner-licence-to-teams cfg [team] params))) team))) (defn- get-organization-owner-viewer-team diff --git a/backend/src/app/rpc/commands/teams_invitations.clj b/backend/src/app/rpc/commands/teams_invitations.clj index 83d09d3219..e60f8f26c8 100644 --- a/backend/src/app/rpc/commands/teams_invitations.clj +++ b/backend/src/app/rpc/commands/teams_invitations.clj @@ -474,6 +474,31 @@ "The number of invitations can be sent in a single rpc request" 25) +(defn- check-send-invitations-policy + "Raises `:insufficient-permissions` when the organization of the team does + not let the profile send invitations. The policy covers the pending + invitations too: changing their role, deleting them and minting their token + manage the same access that sending one grants. + + Only applies with the `:admin-console` flag. Then it returns the team with + its organization info, so the caller reuses the same Nitrate answer." + [cfg profile-id team-id perms] + (when (contains? cf/flags :admin-console) + (let [team (db/get-by-id cfg :team team-id) + ;; Strict lookup: a failed Nitrate call must not skip the + ;; organization policy by looking like a team without one. + team (nitrate/add-organization-info-to-team-strict cfg team {::rpc/profile-id profile-id}) + organization (:organization team)] + (when (and organization + (not (cto/allowed? :send-invitations + {:organization-perms organization + :profile-id profile-id + :team-perms perms}))) + (ex/raise :type :validation + :code :insufficient-permissions + :hint "Organization policy does not allow you to send invitations")) + team))) + (sv/defmethod ::create-team-invitations "A rpc call that allows to send single or multiple invitations to join the team. @@ -486,12 +511,10 @@ [cfg {:keys [::rpc/profile-id team-id role emails] :as params}] (let [perms (teams/check-invitation-permissions cfg profile-id team-id) profile (db/get-by-id cfg :profile profile-id) - team (db/get-by-id cfg :team team-id) - ;; Strict lookup: a failed Nitrate call must not skip the - ;; organization policy below by looking like a team without one. - team-with-org (when (contains? cf/flags :admin-console) - (nitrate/add-organization-info-to-team-strict cfg team {::rpc/profile-id profile-id})) - organization (:organization team-with-org) + ;; Reuse the organization lookup of the policy check, so every + ;; organization rule below sees the same Nitrate answer. + team (or (check-send-invitations-policy cfg profile-id team-id perms) + (db/get-by-id cfg :team team-id)) ;; Determine which format is being used using-emails-format? (and emails role) ;; Handle both parameter formats @@ -503,16 +526,6 @@ (count emails) (count (:invitations params)))] - (when (and (contains? cf/flags :admin-console) - organization - (not (cto/allowed? :send-invitations - {:organization-perms organization - :profile-id profile-id - :team-perms perms}))) - (ex/raise :type :validation - :code :insufficient-permissions - :hint "Organization policy does not allow you to send invitations")) - ;; Don't allow promote to owner to admin users. (when (and (not (:is-owner perms)) (or (= role :owner) @@ -536,10 +549,7 @@ ;; Check if the current profile is allowed to send emails (teams/check-profile-muted cfg profile) - (let [;; Reuse the strict organization lookup made for the policy - ;; check above, so every rule sees the same Nitrate answer. - team (or team-with-org team) - ;; NOTE: Is important pass RPC method params down to the + (let [;; NOTE: Is important pass RPC method params down to the ;; `create-team-invitations` because it uses the implicit ;; RPC properties from params for fill necessary data on ;; emiting an entry to the audit-log @@ -647,6 +657,9 @@ :code :cant-promote-to-owner :hint "only the team owner can get an owner invitation token")) + ;; Handing out the link invites as much as sending the email does. + (check-send-invitations-policy cfg profile-id team-id perms) + (let [member (profile/get-profile-by-email pool (:email-to invit)) token (create-invitation-token cfg {:team-id (:team-id invit) ;; The inviter is the creator of @@ -678,17 +691,20 @@ (sv/defmethod ::update-team-invitation-role {::doc/added "1.17" ::doc/module :teams - ::sm/params schema:update-team-invitation-role - ::db/transaction true} - [{:keys [::db/conn]} {:keys [::rpc/profile-id team-id email role] :as params}] - (let [perms (teams/check-invitation-permissions conn profile-id team-id)] + ::sm/params schema:update-team-invitation-role} + [cfg {:keys [::rpc/profile-id team-id email role] :as params}] + (let [perms (teams/check-invitation-permissions cfg profile-id team-id)] ;; Don't allow promote to owner to admin users. (when (and (not (:is-owner perms)) (= role :owner)) (ex/raise :type :validation :code :cant-promote-to-owner)) - (db/update! conn :team-invitation + ;; Checked outside a transaction: it may call Nitrate over HTTP, and the + ;; update below is a single statement. + (check-send-invitations-policy cfg profile-id team-id perms) + + (db/update! cfg :team-invitation {:role (name role) :updated-at (ct/now)} {:team-id team-id :email-to (profile/clean-email email)}) @@ -703,16 +719,19 @@ (sv/defmethod ::delete-team-invitation {::doc/added "1.17" - ::sm/params schema:delete-team-invition - ::db/transaction true} - [{:keys [::db/conn]} {:keys [::rpc/profile-id team-id email] :as params}] - (teams/check-invitation-permissions conn profile-id team-id) + ::sm/params schema:delete-team-invition} + [cfg {:keys [::rpc/profile-id team-id email] :as params}] + (let [perms (teams/check-invitation-permissions cfg profile-id team-id)] - (let [invitation (db/delete! conn :team-invitation - {:team-id team-id - :email-to (profile/clean-email email)} - {::db/return-keys true})] - (rph/wrap nil {::audit/props {:invitation-id (:id invitation)}}))) + ;; Checked outside a transaction: it may call Nitrate over HTTP, and the + ;; delete below is a single statement. + (check-send-invitations-policy cfg profile-id team-id perms) + + (let [invitation (db/delete! cfg :team-invitation + {:team-id team-id + :email-to (profile/clean-email email)} + {::db/return-keys true})] + (rph/wrap nil {::audit/props {:invitation-id (:id invitation)}})))) ;; --- Mutation: Request Team Invitation diff --git a/backend/test/backend_tests/auth_oidc_test.clj b/backend/test/backend_tests/auth_oidc_test.clj index 42a7e4f9d4..8b624a6fbb 100644 --- a/backend/test/backend_tests/auth_oidc_test.clj +++ b/backend/test/backend_tests/auth_oidc_test.clj @@ -10,6 +10,7 @@ [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.session :as session] @@ -648,69 +649,73 @@ (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/#/workspace" + 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 [])] - (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/#/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]))))))) + (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/#/workspace" - :organization-id test-organization-id}) + 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 {}}] - (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/#/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)))))) + (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/#/workspace" + 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 [])] - (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))))))) + (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) @@ -729,6 +734,155 @@ :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 {})] + (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))] + (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)] @@ -870,7 +1024,7 @@ {:client-id "test-client" :client-secret "test-secret" :issuer "https://wrong-idp.example.com"} - :dest-url "https://localhost:3449/#/dashboard" + :query-string "screen=dashboard" :organization-id #uuid "00000000-0000-0000-0000-000000000001") (catch Throwable t t))] (t/is (ex/error? e)) diff --git a/backend/test/backend_tests/config_test.clj b/backend/test/backend_tests/config_test.clj index 74a159a703..fa168708ef 100644 --- a/backend/test/backend_tests/config_test.clj +++ b/backend/test/backend_tests/config_test.clj @@ -6,9 +6,11 @@ (ns backend-tests.config-test (:require + [app.common.logging :as l] [app.common.schema :as sm] [app.common.time :as ct] [app.config :as cf] + [clojure.core :as c] [clojure.test :as t])) (t/deftest tenant-is-normalized-to-lowercase @@ -86,3 +88,51 @@ (t/testing "and falls back to a week" (with-redefs [cf/config (dissoc cf/config :jobs-user-ttl)] (t/is (= (ct/duration {:days 7}) (cf/get-jobs-user-ttl)))))) + +;; `legacy-db` is read-only: the file data moved to `file_data`, and +;; `file.data` / `file_change.data` are read-only leftovers. An instance +;; that keeps it starts healthy and then fails on every save. It is read +;; as `db` instead, which also serves the legacy rows. + +(defn- read-config + "`read-config` with the environment replaced by `env`." + [env & {:keys [default] :or {default {}}}] + (with-redefs [cf/read-env (constantly env)] + (cf/read-config :default default))) + +(defn- read-config-with-warning + "Reads the configuration and collects the warnings it emits. Logging is + emitted asynchronously, so the record is awaited (bounded)." + [env] + (let [records (atom [])] + (add-watch l/log-record ::capture + (fn [_ _ _ record] (swap! records conj record))) + (try + (let [config (read-config env)] + [(loop [remaining 100] + (let [warns (filter #(= :warn (::l/level %)) @records)] + (cond + (seq warns) warns + (zero? remaining) warns + :else (do (Thread/sleep 10) (recur (dec remaining)))))) + config]) + (finally + (remove-watch l/log-record ::capture))))) + +(t/deftest read-config-replaces-the-read-only-legacy-db + (let [[warns config] (read-config-with-warning {:file-data-backend "legacy-db"})] + (t/testing "the configuration writes through db" + (t/is (= "db" (c/get config :file-data-backend)))) + (t/testing "the operator is warned" + (t/is (contains? (into #{} (map #(c/get (::l/props %) :hint)) warns) + "file-data-backend=legacy-db is read-only, using db"))))) + +(t/deftest read-config-keeps-the-writable-back-ends + (doseq [backend ["db" "storage"]] + (t/testing backend + (t/is (= backend (c/get (read-config {:file-data-backend backend}) + :file-data-backend)))))) + +(t/deftest read-config-takes-the-default-back-end + (t/is (= "db" (c/get (read-config {} :default cf/default) + :file-data-backend)))) diff --git a/backend/test/backend_tests/fdata_upsert_test.clj b/backend/test/backend_tests/fdata_upsert_test.clj index 0bfa89b6f1..c2fa034da3 100644 --- a/backend/test/backend_tests/fdata_upsert_test.clj +++ b/backend/test/backend_tests/fdata_upsert_test.clj @@ -5,55 +5,40 @@ ;; Copyright (c) KALEIDOS SUBSIDIARY SL (ns backend-tests.fdata-upsert-test - "Covers the invariants of `fdata/upsert!` that the partition runbook relies - on." + "Covers the write contract of `fdata/upsert!` after the `legacy-db` backend + became read-only." (:require - [app.common.time :as ct] [app.common.uuid :as uuid] + [app.config :as cf] [app.db :as db] [app.features.fdata :as fdata] - [app.features.file-changes :as fchanges] [backend-tests.helpers :as th] [clojure.test :as t])) (t/use-fixtures :once th/state-init) (t/use-fixtures :each th/database-reset) -(t/deftest legacy-db-snapshot-moves-the-updated-at-watermark - ;; The runbook of the partition picks the rows changed since the bulk copy - ;; with `updated_at`, so every mutation of `file_change` has to move it. The - ;; `legacy-db` snapshot path writes the payload into the row, and it used to - ;; leave the watermark behind. - (let [profile (th/create-profile* 1) - file (th/create-file* 1 {:profile-id (:id profile) - :project-id (:default-project-id profile)}) - id (uuid/next) - past (ct/in-past "2h") - payload (byte-array [1 2 3])] +(defn- upsert-cause + [params] + (try + (fdata/upsert! th/*system* params) + nil + (catch Exception cause + cause))) - (th/db-insert! :file-change - {:id id - :file-id (:id file) - :revn 1 - :label "v1" - :kind fchanges/snapshot-kind - :updated-at past}) - - (t/is (true? (:written (fdata/upsert! th/*system* - {:id id - :file-id (:id file) - :type "snapshot" - :backend "legacy-db" - :storage-object-id nil - :data payload})))) - - (let [row (th/db-get :file-change {:id id} {::db/remove-deleted false})] - (t/testing "the payload lands in the legacy column" - (t/is (= (seq payload) (seq ^bytes (:data row))))) - - (t/testing "the watermark moves to the moment of the mutation" - (t/is (pos? (compare (inst-ms (:updated-at row)) (inst-ms past)))))))) +(t/deftest legacy-db-param-is-rejected + ;; `legacy-db` is not a write backend anymore: the schema refuses it before + ;; touching the database. + (let [cause (upsert-cause {:id (uuid/next) + :file-id (uuid/next) + :type "snapshot" + :backend "legacy-db" + :storage-object-id nil + :data (byte-array [1 2 3])})] + (t/is (some? cause)) + (t/is (= :assertion (:type (ex-data cause)))) + (t/is (= :data-validation (:code (ex-data cause)))))) (t/deftest upsert-reports-the-pointer-it-created ;; The write reports the object it created, so a caller that persists again @@ -124,3 +109,19 @@ (t/is (= (:backend before) (:backend after))) (t/is (= (:storage-object-id before) (:storage-object-id after))) (t/is (= (vec (:data before)) (vec (:data after)))))))) + +(t/deftest legacy-db-config-is-rejected-as-read-only + ;; The configuration value is kept for reading old data, but a write with + ;; it as the default backend fails with a clear message. + (with-redefs [cf/config (assoc cf/config :file-data-backend "legacy-db")] + (let [cause (upsert-cause {:id (uuid/next) + :file-id (uuid/next) + :type "snapshot" + :storage-object-id nil + :data (byte-array [1 2 3])})] + (t/is (some? cause)) + (t/is (= :validation (:type (ex-data cause)))) + (t/is (= :legacy-backend-read-only (:code (ex-data cause))))))) + +(t/deftest file-data-backend-defaults-to-db + (t/is (= "db" (cf/get :file-data-backend)))) diff --git a/backend/test/backend_tests/media_test.clj b/backend/test/backend_tests/media_test.clj index ddc8c6ca3d..aac2cd6c98 100644 --- a/backend/test/backend_tests/media_test.clj +++ b/backend/test/backend_tests/media_test.clj @@ -12,7 +12,10 @@ [backend-tests.helpers :as th] [clojure.test :as t] [cuerdas.core :as str] - [datoteka.fs :as fs])) + [datoteka.fs :as fs] + [datoteka.io :as io]) + (:import + java.nio.ByteBuffer)) (t/use-fixtures :once th/state-init) @@ -554,3 +557,23 @@ :height 200})] (t/is (= :webp (:format thumb))) (t/is (= "image/webp" (:mtype thumb)))))) + +(defn- sfnt-num-glyphs + "Returns numGlyphs from the maxp table of the sfnt font in `data`." + [^bytes data] + (let [buf (ByteBuffer/wrap data) + num-tables (.getShort buf 4)] + (some (fn [i] + (let [rec (+ 12 (* i 16))] + (when (= "maxp" (String. data rec 4 "ISO-8859-1")) + (.getShort buf (+ (.getInt buf (+ rec 8)) 4))))) + (range num-tables)))) + +(t/deftest generate-fonts-cid-keyed-otf + (t/testing "otf->ttf keeps the glyphs of every CID sub-font" + ;; font-cid.otf has 5 glyphs split across two CID sub-fonts + (let [data (io/read* (io/resource "backend_tests/test_files/font-cid.otf")) + result (media/run th/*system* {:cmd :generate-fonts + :input {"font/otf" data}}) + ttf (io/read* (get result "font/ttf"))] + (t/is (<= 5 (sfnt-num-glyphs ttf)))))) diff --git a/backend/test/backend_tests/rpc_auth_test.clj b/backend/test/backend_tests/rpc_auth_test.clj index e2a4a54f8c..32eda259d2 100644 --- a/backend/test/backend_tests/rpc_auth_test.clj +++ b/backend/test/backend_tests/rpc_auth_test.clj @@ -10,6 +10,7 @@ [app.auth.login-lockout :as lol] [app.common.flags :as flags] [app.common.generic-pool :as gpool] + [app.common.time :as ct] [app.common.uuid :as uuid] [app.http.session :as session] [app.loggers.audit :as-alias audit] @@ -17,6 +18,7 @@ [app.rpc.commands.auth :as auth] [app.rpc.commands.ldap :as ldap-cmd] [app.storage :as sto] + [app.tokens :as tokens] [backend-tests.helpers :as th] [clojure.test :as t] [datoteka.io :as io] @@ -28,6 +30,80 @@ (t/use-fixtures :once th/state-init) (t/use-fixtures :each th/database-reset) +(t/deftest recover-profile-invalidates-existing-sessions + "Regression: password recovery must delete the victim's server-side + sessions, otherwise a previously captured cookie keeps working + (GHSA-3j2p-qr45-q365)." + (let [prof (th/create-profile* 1) + manager (::session/manager th/*system*) + sid1 (uuid/random) + sid2 (uuid/random) + _ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)" + sid1 (:id prof) "agent-1"]) + _ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)" + sid2 (:id prof) "agent-2"]) + token (tokens/generate th/*system* + {:iss :password-recovery + :exp (ct/in-future "15m") + :profile-id (:id prof)})] + (with-mocks [_ {:target 'app.email/send-template :return nil}] + (let [out (th/command! {::th/type :recover-profile + :token token + :password "Foobar12!"})] + (t/is (nil? (:error out))))) + (t/is (nil? (session/read-session manager sid1)) + "recover-profile must delete the victim's sessions (GHSA-3j2p-qr45-q365)") + (t/is (nil? (session/read-session manager sid2)) + "recover-profile must delete the victim's sessions (GHSA-3j2p-qr45-q365)"))) + +(t/deftest recover-profile-keeps-other-profiles-sessions + (let [prof-a (th/create-profile* 1) + prof-b (th/create-profile* 2) + manager (::session/manager th/*system*) + sid-a (uuid/random) + sid-b (uuid/random) + _ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)" + sid-a (:id prof-a) "agent-a"]) + _ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)" + sid-b (:id prof-b) "agent-b"]) + token (tokens/generate th/*system* + {:iss :password-recovery + :exp (ct/in-future "15m") + :profile-id (:id prof-a)})] + (with-mocks [_ {:target 'app.email/send-template :return nil}] + (let [out (th/command! {::th/type :recover-profile + :token token + :password "Foobar12!"})] + (t/is (nil? (:error out))))) + (t/is (nil? (session/read-session manager sid-a))) + (t/is (some? (session/read-session manager sid-b)) + "sessions of other profiles must survive the recovery"))) + +(t/deftest recover-profile-rollback-keeps-password-and-sessions + "A failure after the password UPDATE rolls the transaction back, so the + captured session and the old password both survive + (GHSA-3j2p-qr45-q365)." + (let [prof (th/create-profile* 1) + manager (::session/manager th/*system*) + sid (uuid/random) + _ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)" + sid (:id prof) "agent-1"]) + before (:password (th/db-exec-one! ["SELECT password FROM profile WHERE id = ?" (:id prof)])) + token (tokens/generate th/*system* + {:iss :password-recovery + :exp (ct/in-future "15m") + :profile-id (:id prof)})] + (with-mocks [_ {:target 'app.email/send-template + :throw (ex-info "smtp down" {})}] + (let [out (th/command! {::th/type :recover-profile + :token token + :password "Foobar12!"})] + (t/is (some? (:error out))))) + (t/is (some? (session/read-session manager sid)) + "a rolled-back recovery must not delete sessions") + (t/is (= before (:password (th/db-exec-one! ["SELECT password FROM profile WHERE id = ?" (:id prof)]))) + "a rolled-back recovery must not change the password"))) + (t/deftest logout-invalidates-current-session (let [prof (th/create-profile* 1) manager (::session/manager th/*system*) diff --git a/backend/test/backend_tests/rpc_nitrate_test.clj b/backend/test/backend_tests/rpc_nitrate_test.clj index abbfc679fc..26f0de65ec 100644 --- a/backend/test/backend_tests/rpc_nitrate_test.clj +++ b/backend/test/backend_tests/rpc_nitrate_test.clj @@ -87,6 +87,15 @@ :issuer "https://idp.example.com" :organization-id organization-id} + :get-organization-membership + {:is-member true + :organization-id organization-id} + + :get-organization-summary + {:id organization-id + :owner-id organization-owner-id + :teams []} + nil))) (defn- unauthorized-sso-mock @@ -127,7 +136,7 @@ {::th/type :check-nitrate-sso ::rpc/profile-id (:id external-profile) :team-id (:id team) - :url "https://penpot.example.com/#/workspace"} + :query-string "screen=workspace&team-id=0e5f-1a2b"} {::http/request {}})] (binding [cf/flags (conj cf/flags :admin-console)] (with-redefs [nitrate/call @@ -154,7 +163,7 @@ {::th/type :check-nitrate-sso ::rpc/profile-id (:id team-owner) :team-id (:id team) - :url "https://penpot.example.com/#/workspace"} + :query-string "screen=workspace&team-id=0e5f-1a2b"} {::http/request {}})] (binding [cf/flags (conj cf/flags :admin-console)] (with-redefs [nitrate/call @@ -175,8 +184,9 @@ (t/is (= {:authorized false :redirect-uri redirect-uri} (:result out))) - (t/is (= #{:dest-url :organization-id} (set (keys @redirect-options)))) - (t/is (= "https://penpot.example.com/#/workspace" (str (:dest-url @redirect-options)))) + (t/is (= #{:query-string :organization-id :profile-id} (set (keys @redirect-options)))) + (t/is (= "screen=workspace&team-id=0e5f-1a2b" (:query-string @redirect-options))) + (t/is (= (:id team-owner) (:profile-id @redirect-options))) (t/is (nil? (:organization-id @redirect-options))) (t/is (= {:profile-id (:id team-owner) :organization-id organization-id} @@ -191,10 +201,16 @@ {::th/type :check-nitrate-sso ::rpc/profile-id (:id profile) :organization-id organization-id - :url "https://penpot.example.com/#/workspace"} + :query-string "screen=workspace&team-id=0e5f-1a2b"} {::http/request {}})] (binding [cf/flags (conj cf/flags :admin-console)] (with-redefs [nitrate/sso-session-authorized? (unauthorized-sso-mock organization-id) + nitrate/call + (fn [_cfg method _params] + (case method + :get-organization-membership {:is-member true + :organization-id organization-id} + nil)) oidc/build-organization-sso-auth-redirect-uri (fn [& _] (throw cause)) oidc/submit-organization-sso-auth-failed-event (fn [_cfg _request profile-id received-organization-id received-cause] @@ -218,7 +234,7 @@ {::th/type :check-nitrate-sso ::rpc/profile-id (:id organization-owner) :team-id (:id team) - :url "https://penpot.example.com/#/workspace"} + :query-string "screen=workspace&team-id=0e5f-1a2b"} {::http/request {}})] (binding [cf/flags (conj cf/flags :admin-console)] (with-redefs [nitrate/organization-owner-of-team? @@ -246,7 +262,7 @@ {::th/type :check-nitrate-sso ::rpc/profile-id (:id team-owner) :team-id (:id team) - :url "https://penpot.example.com/#/workspace"} + :query-string "screen=workspace&team-id=0e5f-1a2b"} {::http/request {}})] (binding [cf/flags (conj cf/flags :admin-console)] (with-redefs [nitrate/sso-session-authorized? @@ -259,6 +275,153 @@ (t/is (th/success? out)) (t/is (= {:authorized true :reason :sso-satisfied} (:result out)))))))) +(t/deftest check-nitrate-sso-denies-an-organization-the-profile-does-not-belong-to + (let [profile (th/create-profile* 1 {:is-active true}) + organization-id (uuid/random) + params (with-meta + {::th/type :check-nitrate-sso + ::rpc/profile-id (:id profile) + :organization-id organization-id + :query-string "screen=workspace"} + {::http/request {}})] + (binding [cf/flags (conj cf/flags :admin-console)] + (with-redefs [nitrate/call + (fn [_cfg method _params] + (case method + :get-organization-membership {:is-member false + :organization-id organization-id} + :get-organization-summary {:id organization-id + :owner-id (uuid/random) + :teams []} + :get-organization-sso {:active true + :issuer "https://idp.example.com" + :organization-id organization-id} + nil)) + oidc/build-organization-sso-auth-redirect-uri + (constantly "https://idp.example.com/authorize")] + (let [out (th/command! params)] + (t/is (th/success? out)) + ;; The organization id comes from the client: a profile that does + ;; not belong to the organization must not start a flow for it. + (t/is (= {:authorized true :reason :no-organization-access} (:result out)))))))) + +(t/deftest check-nitrate-sso-denies-an-organization-the-profile-does-not-belong-to-even-with-a-team + (let [profile (th/create-profile* 1 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id profile)}) + organization-id (uuid/random) + params (with-meta + {::th/type :check-nitrate-sso + ::rpc/profile-id (:id profile) + :team-id (:id team) + :organization-id organization-id + :query-string "screen=workspace"} + {::http/request {}})] + (binding [cf/flags (conj cf/flags :admin-console)] + (with-redefs [nitrate/call + (fn [_cfg method _params] + (case method + :get-organization-membership {:is-member false + :organization-id organization-id} + :get-organization-summary {:id organization-id + :owner-id (uuid/random) + :teams []} + :get-organization-sso {:active true + :issuer "https://idp.example.com" + :organization-id organization-id} + nil)) + oidc/build-organization-sso-auth-redirect-uri + (constantly "https://idp.example.com/authorize")] + (let [out (th/command! params)] + (t/is (th/success? out)) + ;; The organization id comes from the client too when the team is + ;; also sent, so the check applies to both shapes. + (t/is (= {:authorized true :reason :no-organization-access} (:result out)))))))) + +(t/deftest check-nitrate-sso-survives-an-organization-that-does-not-exist + (let [profile (th/create-profile* 1 {:is-active true}) + organization-id (uuid/random) + params (with-meta + {::th/type :check-nitrate-sso + ::rpc/profile-id (:id profile) + :organization-id organization-id + :query-string "screen=workspace"} + {::http/request {}})] + (binding [cf/flags (conj cf/flags :admin-console)] + (with-redefs [nitrate/call + (fn [_cfg _method _params] nil) + oidc/build-organization-sso-auth-redirect-uri + (constantly "https://idp.example.com/authorize")] + (let [out (th/command! params)] + (t/is (th/success? out)) + ;; Nitrate knows nothing about the organization: that is a denial, + ;; not a crash. + (t/is (= {:authorized true :reason :no-organization-access} (:result out)))))))) + +(t/deftest check-nitrate-sso-ignores-a-value-that-is-not-a-route + (let [profile (th/create-profile* 1 {:is-active true}) + organization-id (uuid/random) + redirect-uri "https://idp.example.com/authorize"] + (doseq [query-string ["https://evil.example/" ""]] + (let [params (with-meta + {::th/type :check-nitrate-sso + ::rpc/profile-id (:id profile) + :organization-id organization-id + :query-string query-string} + {::http/request {}})] + (binding [cf/flags (conj cf/flags :admin-console)] + (with-redefs [nitrate/call + (fn [_cfg method _params] + (case method + :get-organization-membership {:is-member true + :organization-id organization-id} + :get-organization-summary {:id organization-id + :owner-id (:id profile) + :teams []} + :get-organization-sso {:active true + :issuer "https://idp.example.com" + :organization-id organization-id} + nil)) + oidc/prepare-organization-sso-provider (constantly {:type "oidc"}) + oidc/build-auth-redirect-uri (constantly redirect-uri)] + (let [out (th/command! params)] + (t/is (th/success? out)) + ;; A destination the server cannot render must not break the + ;; flow: the user lands on the public uri instead. + (t/is (= {:authorized false + :redirect-uri redirect-uri} + (:result out)))))))))) + +(t/deftest check-nitrate-sso-allows-the-owner-of-the-organization + (let [profile (th/create-profile* 1 {:is-active true}) + organization-id (uuid/random) + redirect-uri "https://idp.example.com/authorize" + params (with-meta + {::th/type :check-nitrate-sso + ::rpc/profile-id (:id profile) + :organization-id organization-id + :query-string "screen=workspace"} + {::http/request {}})] + (binding [cf/flags (conj cf/flags :admin-console)] + (with-redefs [nitrate/call + (fn [_cfg method _params] + (case method + :get-organization-membership {:is-member false + :organization-id organization-id} + :get-organization-summary {:id organization-id + :owner-id (:id profile) + :teams []} + :get-organization-sso {:active true + :issuer "https://idp.example.com" + :organization-id organization-id} + nil)) + oidc/build-organization-sso-auth-redirect-uri + (constantly redirect-uri)] + (let [out (th/command! params)] + (t/is (th/success? out)) + (t/is (= {:authorized false + :redirect-uri redirect-uri} + (:result out)))))))) + (t/deftest nitrate-sso-required-error-resolves-the-team-from-the-file (t/testing "the workspace path, where the file id arrives as :id, still reports the team" (let [profile (th/create-profile* 1 {:is-active true}) diff --git a/backend/test/backend_tests/rpc_team_test.clj b/backend/test/backend_tests/rpc_team_test.clj index f562fc5980..a59d2759da 100644 --- a/backend/test/backend_tests/rpc_team_test.clj +++ b/backend/test/backend_tests/rpc_team_test.clj @@ -1093,6 +1093,112 @@ (t/is (nil? (:result out))) (t/is (nil? res))))) +;; The organization `send-invitations` policy covers the pending invitations +;; too: changing their role, deleting them and getting their link manage the +;; same access that sending one grants. + +(def ^:private pending-invitation-email "pending@example.com") + +(defn- reset-pending-invitation + "Leaves one pending `editor` invitation in the team, whatever the previous + scenario did to it." + [team-id] + (db/delete! th/*pool* :team-invitation + {:team-id team-id :email-to pending-invitation-email}) + (db/insert! th/*pool* :team-invitation + {:team-id team-id + :email-to pending-invitation-email + :role "editor" + :valid-until (ct/in-future "48h")})) + +(defn- get-pending-invitation-role + [team-id] + (:role (db/get* th/*pool* :team-invitation + {:team-id team-id :email-to pending-invitation-email}))) + +(defn- organization-with-policy + "A team organization lookup that answers with an organization whose + `send-invitations` value and attribute restriction answer are the given + ones." + [owner-id send-invitations restrictions-met] + (fn [_params] + {:organization {:id (uuid/random) + :name "Acme" + :slug "acme" + :owner-id owner-id + :avatar-bg-url "https://example.com/avatar.svg" + :permissions {:send-invitations send-invitations} + :restrictions-met {:send-invitations restrictions-met}} + :is-your-penpot false})) + +(defn- organization-lookup-failure + [status] + (fn [_params] + (throw (ex-info "nitrate error" {:type :nitrate-http-error + :status status})))) + +(defn- check-follows-send-invitations-policy + "Runs the invitation `command` (a params map without profile, team and + email) under each organization policy and checks who may go through." + [command] + (let [owner (th/create-profile* 440 {:is-active true}) + admin (th/create-profile* 441 {:is-active true}) + team (th/create-team* 440 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id admin) + :role :admin}) + team-id (:id team) + + execute + (fn [profile get-team-organization] + (reset-pending-invitation team-id) + (with-redefs [cf/flags (conj cf/flags :admin-console) + nitrate/call (fn [_cfg method params] + (case method + :get-team-organization (get-team-organization params) + nil))] + (th/command! (assoc command + ::rpc/profile-id (:id profile) + :team-id team-id + :email pending-invitation-email))))] + + (t/testing "an admin is denied when only team owners may invite" + (let [out (execute admin (organization-with-policy (:id owner) "owners" true))] + (t/is (= :insufficient-permissions (th/ex-code (:error out)))) + (t/is (= "editor" (get-pending-invitation-role team-id))))) + + (t/testing "an admin is denied when an attribute restriction leaves them out" + (let [out (execute admin (organization-with-policy (:id owner) "ownersAndAdmins" false))] + (t/is (= :insufficient-permissions (th/ex-code (:error out)))) + (t/is (= "editor" (get-pending-invitation-role team-id))))) + + (t/testing "a failed organization lookup denies the admin" + (let [out (execute admin (organization-lookup-failure 403))] + (t/is (= :nitrate-http-error (th/ex-type (:error out)))) + (t/is (= "editor" (get-pending-invitation-role team-id))))) + + (t/testing "a team owner is allowed when only team owners may invite" + (t/is (th/success? (execute owner (organization-with-policy (:id owner) "owners" true))))) + + (t/testing "an admin is allowed when the policy and the restriction include them" + (t/is (th/success? (execute admin (organization-with-policy (:id owner) "ownersAndAdmins" true))))) + + (t/testing "an admin is allowed in a team without organization" + (t/is (th/success? (execute admin (organization-lookup-failure 404))))))) + +(t/deftest update-team-invitation-role-follows-send-invitations-policy + (check-follows-send-invitations-policy + {::th/type :update-team-invitation-role + :role :admin})) + +(t/deftest delete-team-invitation-follows-send-invitations-policy + (check-follows-send-invitations-policy + {::th/type :delete-team-invitation})) + +(t/deftest get-team-invitation-token-follows-send-invitations-policy + (check-follows-send-invitations-policy + {::th/type :get-team-invitation-token})) + (t/deftest get-owned-teams (let [profile1 (th/create-profile* 1 {:is-active true}) @@ -1210,6 +1316,127 @@ (t/is (= :get-teams-organizations (ffirst @calls))))))) +(defn- find-team + [teams team-id] + (d/seek #(= team-id (:id %)) teams)) + +(t/deftest get-teams-shows-owner-nitrate-licence-to-every-member + (let [member (th/create-profile* 1 {:is-active true}) + owner (th/create-profile* 2 {:is-active true}) + org-owner (th/create-profile* 3 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + organization-team (th/create-team* 2 {:profile-id (:id org-owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + _ (th/create-team-role* {:team-id (:id organization-team) + :profile-id (:id member) + :role :editor}) + calls (atom []) + nitrate-call (fn [_cfg method params] + (swap! calls conj [method params]) + (case method + :get-teams-organizations + [{:id (:id organization-team) + :is-your-penpot false + :organization {:id (uuid/random) + :name "Acme" + :slug "acme" + :owner-id (:id org-owner)}}] + :get-subscriptions + [{:profile-id (:id owner) :type "nitrate" :status "trialing"} + {:profile-id (:id org-owner) :type "nitrate" :status "active"}])) + params {::th/type :get-teams + ::rpc/profile-id (:id member)}] + (with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions) + nitrate/call nitrate-call] + (let [out (th/command! params) + teams (:result out)] + (t/is (th/success? out)) + (t/is (= [:get-teams-organizations :get-subscriptions] (mapv first @calls))) + ;; One request with the owners of the teams outside an organization, + ;; the member included as owner of its default team. + (t/is (= #{(:id owner) (:id member)} + (-> @calls second second :profile-ids set))) + (t/is (= {:type "nitrate" :status "trialing"} + (-> (find-team teams (:id team)) + :subscription + (select-keys [:type :status])))) + ;; A team in an organization keeps the Stripe plan of its owner. + (t/is (= "professional" + (-> (find-team teams (:id organization-team)) :subscription :type))) + (t/is (= "professional" + (-> (find-team teams (:default-team-id member)) :subscription :type))))))) + +(t/deftest get-teams-ignores-owner-nitrate-licence-that-is-not-valid + (let [member (th/create-profile* 1 {:is-active true}) + owner (th/create-profile* 2 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + nitrate-call (fn [_cfg method _params] + (case method + :get-teams-organizations [] + :get-subscriptions [{:profile-id (:id owner) + :type "nitrate" + :status "canceled"}])) + params {::th/type :get-teams + ::rpc/profile-id (:id member)}] + (with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions) + nitrate/call nitrate-call] + (let [out (th/command! params)] + (t/is (th/success? out)) + (t/is (= "professional" + (-> (find-team (:result out) (:id team)) :subscription :type))))))) + +(t/deftest get-teams-keeps-stripe-plan-when-owner-licences-fail + (let [member (th/create-profile* 1 {:is-active true}) + owner (th/create-profile* 2 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + nitrate-call (fn [_cfg method _params] + (case method + :get-teams-organizations [] + :get-subscriptions (throw (ex-info "nitrate is down" + {:type :nitrate-unavailable})))) + params {::th/type :get-teams + ::rpc/profile-id (:id member)}] + (with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions) + nitrate/call nitrate-call] + (let [out (th/command! params)] + (t/is (th/success? out)) + (t/is (= "professional" + (-> (find-team (:result out) (:id team)) :subscription :type))))))) + +(t/deftest get-team-shows-owner-nitrate-licence-to-a-member + (let [member (th/create-profile* 1 {:is-active true}) + owner (th/create-profile* 2 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + calls (atom []) + nitrate-call (fn [_cfg method params] + (swap! calls conj [method params]) + (case method + :get-team-organization nil + :get-subscriptions [{:profile-id (:id owner) + :type "nitrate" + :status "active"}])) + params {::th/type :get-team + ::rpc/profile-id (:id member) + :id (:id team)}] + (with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions) + nitrate/call nitrate-call] + (let [out (th/command! params)] + (t/is (th/success? out)) + (t/is (= [(:id owner)] (-> (last @calls) second :profile-ids))) + (t/is (= "nitrate" (-> out :result :subscription :type))))))) + + (t/deftest team-deletion-1 (let [profile1 (th/create-profile* 1 {:is-active true}) team (th/create-team* 1 {:profile-id (:id profile1)}) diff --git a/backend/test/backend_tests/test_files/font-cid.otf b/backend/test/backend_tests/test_files/font-cid.otf new file mode 100644 index 0000000000..d9fc9d18ca Binary files /dev/null and b/backend/test/backend_tests/test_files/font-cid.otf differ diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index 1d935272d8..70e78627af 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -716,17 +716,6 @@ (update :pages-index d/update-vals update-container) (d/update-when :components d/update-vals update-container)))) -(defmethod migrate-data "legacy-36" - [data _] - (letfn [(update-container [container] - (d/update-when container :objects (fn [objects] - (if (contains? objects nil) - (dissoc objects nil) - objects))))] - (-> data - (update :pages-index d/update-vals update-container) - (d/update-when :components d/update-vals update-container)))) - (defmethod migrate-data "legacy-37" [data _] (d/without-nils data)) @@ -2341,13 +2330,13 @@ (let [shapes' (filterv #(contains? objects %) shapes)] (when (not= shapes shapes') (l/trc :hint "fix dangling children" - :container container-id - :shape-id (:id shape) + :container (str container-id) + :shape-id (str (:id shape)) :removed (remove #(contains? objects %) shapes))) shapes')))) (fix-objects [container-id objects] - (update-vals objects (partial fix-shape container-id objects))) + (d/update-vals objects (partial fix-shape container-id objects))) (update-container [container] (d/update-when container :objects #(fix-objects (:id container) %)))] @@ -2402,6 +2391,26 @@ (update :pages-index d/update-vals migrate-container) (d/update-when :components d/update-vals migrate-container)))) +(defmethod migrate-data "0036-clear-nil-keys-in-objects" + [data _] + ;; Soft-delete can leave a nil key in `:objects` (gsh/move on a missing + ;; main, then index-by :id). That fails the file schema on duplicate-file + ;; (#12336). "legacy-36" did the same cleanup but its id was withdrawn + ;; from `available-migrations` below: files that recorded it migrate + ;; again through this one. + (letfn [(update-container [container] + (d/update-when container :objects + (fn [objects] + (if (contains? objects nil) + (do + (l/trc :hint "clear nil key in objects" + :container-id (:id container)) + (dissoc objects nil)) + objects))))] + (-> data + (update :pages-index d/update-vals update-container) + (d/update-when :components d/update-vals update-container)))) + (def available-migrations (into (d/ordered-set) ["legacy-2" @@ -2429,7 +2438,6 @@ "legacy-32" "legacy-33" "legacy-34" - "legacy-36" "legacy-37" "legacy-38" "legacy-39" @@ -2494,4 +2502,5 @@ "0033-normalize-main-instance-names" "0034-fix-path-content-offset" "0035-fix-dangling-children" - "0035-fix-shape-svg-attrs-and-defs"])) + "0035-fix-shape-svg-attrs-and-defs" + "0036-clear-nil-keys-in-objects"])) diff --git a/common/src/app/common/render_wasm/api/upload.cljs b/common/src/app/common/render_wasm/api/upload.cljs index 8f53145b05..146de90edf 100644 --- a/common/src/app/common/render_wasm/api/upload.cljs +++ b/common/src/app/common/render_wasm/api/upload.cljs @@ -27,6 +27,7 @@ (def ^:const BASE-PROPS-SIZE 104) (def ^:const FLAG-CLIP-CONTENT 0x01) (def ^:const FLAG-HIDDEN 0x02) +(def ^:const FLAG-HIDE-FILL-ON-EXPORT 0x04) (def ^:const SECTION-CHILDREN 0x01) (def ^:const SECTION-BLUR-LAYER 0x02) @@ -61,9 +62,11 @@ (not (get shape :show-content)) false) hidden (get shape :hidden false) + hide-fill (get shape :hide-fill-on-export false) flags (cond-> 0 clip-content (bit-or FLAG-CLIP-CONTENT) - hidden (bit-or FLAG-HIDDEN)) + hidden (bit-or FLAG-HIDDEN) + hide-fill (bit-or FLAG-HIDE-FILL-ON-EXPORT)) blend-mode (sr/translate-blend-mode (get shape :blend-mode)) constraint-h (sr/translate-constraint-h (or (get shape :constraints-h) :none)) constraint-v (sr/translate-constraint-v (or (get shape :constraints-v) :none)) diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 78fa9cf3b4..359cbd3d6d 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -615,16 +615,28 @@ ([file-data component] (load-component-objects file-data component (gpt/point 0 0))) ([file-data component delta] - (if (and component (empty? (:objects component))) ;; This operation may be called twice, e.g. in an idempotent change + ;; nil? not empty?: a missing-main soft-delete stores {}, which must + ;; still count as already loaded so render/library callers do not re-enter. + (if (and component (nil? (:objects component))) ;; This operation may be called twice, e.g. in an idempotent change (let [component-page (get-component-page file-data component) page-objects (:objects component-page) - objects (->> (cons (:main-instance-id component) - (cfh/get-children-ids page-objects (:main-instance-id component))) - (map #(get page-objects %)) + ids (into [] + (cons (:main-instance-id component) + (cfh/get-children-ids page-objects (:main-instance-id component)))) + ;; Skip ids already gone from the page. Calling gsh/move on nil + ;; builds {:selrect nil :points []} and index-by :id stores it + ;; under nil, which later fails duplicate-file schema checks. + shapes (into [] (keep #(get page-objects %)) ids) + objects (->> shapes ;; when it is an undo of a cut-paste, we need to undo the movement ;; of the shapes so we need to move them delta (map #(gsh/move % delta)) (d/index-by :id))] + (when (not= (count shapes) (count ids)) + (l/wrn :hint "skipping missing shapes when loading component objects" + :component-id (str (:id component)) + :main-instance-id (str (:main-instance-id component)) + :file-id (str (:id file-data)))) (assoc component :objects objects)) component))) diff --git a/common/test/common_tests/files_migrations_0036_test.cljc b/common/test/common_tests/files_migrations_0036_test.cljc new file mode 100644 index 0000000000..d9789ea072 --- /dev/null +++ b/common/test/common_tests/files_migrations_0036_test.cljc @@ -0,0 +1,130 @@ +;; 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 common-tests.files-migrations-0036-test + (:require + [app.common.files.migrations :as cfm] + [app.common.types.file :as ctf] + [app.common.types.shape :as cts] + [app.common.uuid :as uuid] + [clojure.test :as t])) + +;; ====================================================================== +;; 0036-clear-nil-keys-in-objects +;; Soft-delete can leave a nil key in a deleted component's `:objects` +;; (`{nil {:selrect nil :points []}}`). That fails the file schema on +;; duplicate-file (#12336). It replaces `legacy-36` (id withdrawn from +;; `available-migrations`): files that recorded it migrate again +;; through this one. +;; ====================================================================== + +(def ^:private migration "0036-clear-nil-keys-in-objects") + +(def ^:private nil-placeholder + {:selrect nil :points []}) + +(defn- root + [children] + (cts/setup-shape {:id uuid/zero + :type :frame + :name "Root" + :shapes children})) + +(t/deftest removes-nil-key-from-component-objects + (let [component-id (uuid/next) + data {:components + {component-id + {:id component-id + :name "Trashed" + :path "" + :deleted true + :main-instance-id (uuid/next) + :main-instance-page (uuid/next) + :objects {nil nil-placeholder}}}} + migrated (cfm/migrate-data data migration) + objects' (get-in migrated [:components component-id :objects])] + + (t/is (not (contains? objects' nil)) + "nil key is removed from the deleted component :objects") + (t/is (= {} objects') + "only the nil placeholder was present, so objects is empty"))) + +(t/deftest removes-nil-key-from-page-objects + (let [page-id (uuid/next) + frame-id (uuid/next) + frame (cts/setup-shape {:id frame-id :type :frame :name "Frame" :shapes []}) + data {:pages-index + {page-id + {:id page-id + :name "Page" + :objects {uuid/zero (root [frame-id]) + frame-id frame + nil nil-placeholder}}}} + migrated (cfm/migrate-data data migration) + objects' (get-in migrated [:pages-index page-id :objects])] + + (t/is (not (contains? objects' nil)) + "nil key is removed from the page :objects") + (t/is (some? (get objects' frame-id)) + "real shapes are kept"))) + +(t/deftest migrated-file-data-passes-schema + ;; Same failure mode duplicate-file hits: full schema check after migrate. + (let [file-id (uuid/next) + page-id (uuid/next) + component-id (uuid/next) + data (-> (ctf/make-file-data file-id page-id) + (assoc-in [:components component-id] + {:id component-id + :name "Trashed" + :path "" + :deleted true + :main-instance-id (uuid/next) + :main-instance-page page-id + :objects {nil nil-placeholder}}))] + + (t/is (thrown? #?(:clj Exception :cljs js/Error) + (ctf/check-file-data data)) + "corrupt trash objects fail the file schema") + + (let [data' (cfm/migrate-data data migration)] + (t/is (not (contains? (get-in data' [:components component-id :objects]) nil))) + (t/is (= data' (ctf/check-file-data data')) + "after clearing nil keys the file data passes the schema")))) + +(t/deftest migration-0036-runs-through-file-migration + ;; A file that recorded the withdrawn "legacy-36" (and everything else + ;; current) still needs migration: legacy-36 left + ;; `available-migrations`, which re-arms it for 0036. + (let [component-id (uuid/next) + file (ctf/make-file {:name "Legacy nil objects"}) + page-id (first (get-in file [:data :pages])) + file (-> file + (assoc :migrations (-> cfm/available-migrations + (disj migration) + (conj "legacy-36"))) + (assoc-in [:data :components component-id] + {:id component-id + :name "Trashed" + :path "" + :deleted true + :main-instance-id (uuid/next) + :main-instance-page page-id + :objects {nil nil-placeholder}})) + file' (cfm/migrate-file file {})] + + (t/is (cfm/need-migration? file) + "the withdrawn legacy-36 re-arms the cleanup for this file") + (t/is (not (cfm/need-migration? file')) + "new migration recorded") + (t/is (contains? (:migrations file') migration) + "0036 migration id persisted") + (t/is (contains? (:migrations file') "legacy-36") + "the old id stays in the recorded list") + (t/is (not (contains? cfm/available-migrations "legacy-36")) + "the withdrawn legacy-36 id stays out of available-migrations") + (t/is (not (contains? (get-in file' [:data :components component-id :objects]) nil)) + "nil key is cleared through the full migrate-file path"))) diff --git a/common/test/common_tests/runner.cljc b/common/test/common_tests/runner.cljc index b07cbe29ee..03f398b1b9 100644 --- a/common/test/common_tests/runner.cljc +++ b/common/test/common_tests/runner.cljc @@ -25,6 +25,7 @@ [common-tests.files-migrations-0033-test] [common-tests.files-migrations-0034-test] [common-tests.files-migrations-0035-test] + [common-tests.files-migrations-0036-test] [common-tests.files-migrations-test] [common-tests.files.comp-processors-test] [common-tests.files.helpers-test] @@ -105,6 +106,7 @@ [common-tests.types.color-test] [common-tests.types.components-test] [common-tests.types.container-test] + [common-tests.types.file-load-component-objects-test] [common-tests.types.fill-test] [common-tests.types.font-test] [common-tests.types.modifiers-test] @@ -142,6 +144,7 @@ 'common-tests.files-migrations-0033-test 'common-tests.files-migrations-0034-test 'common-tests.files-migrations-0035-test + 'common-tests.files-migrations-0036-test 'common-tests.files-migrations-test 'common-tests.files.comp-processors-test 'common-tests.files.helpers-test @@ -222,6 +225,7 @@ 'common-tests.types.color-test 'common-tests.types.components-test 'common-tests.types.container-test + 'common-tests.types.file-load-component-objects-test 'common-tests.types.fill-test 'common-tests.types.font-test 'common-tests.types.modifiers-test diff --git a/common/test/common_tests/types/file_load_component_objects_test.cljc b/common/test/common_tests/types/file_load_component_objects_test.cljc new file mode 100644 index 0000000000..a1c8bfb0d7 --- /dev/null +++ b/common/test/common_tests/types/file_load_component_objects_test.cljc @@ -0,0 +1,107 @@ +;; 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 common-tests.types.file-load-component-objects-test + (:require + [app.common.types.file :as ctf] + [app.common.types.shape :as cts] + [app.common.uuid :as uuid] + [clojure.test :as t])) + +;; Soft-delete snapshots the main instance into the component via +;; `load-component-objects`. When the main id is already missing from +;; the page, the old path called `gsh/move` on nil and indexed by `:id`, +;; storing `{nil {:selrect nil :points []}}` — which later breaks +;; duplicate-file schema validation (#12336). + +(defn- root + [children] + (cts/setup-shape {:id uuid/zero + :type :frame + :name "Root" + :shapes children})) + +(defn- file-data + [page-id component] + {:pages-index {page-id {:id page-id + :name "Page" + :objects {uuid/zero (root [])}}} + :components {(:id component) component}}) + +(t/deftest load-component-objects-skips-missing-main + (let [page-id (uuid/next) + component-id (uuid/next) + main-id (uuid/next) + component {:id component-id + :name "Orphan" + :path "" + :main-instance-id main-id + :main-instance-page page-id} + result (ctf/load-component-objects + (file-data page-id component) + component) + objects (:objects result)] + + (t/is (not (contains? objects nil)) + "missing main must not create a nil key in :objects") + (t/is (= {} objects) + "missing main yields an empty snapshot, not a placeholder shape"))) + +(t/deftest load-component-objects-keeps-present-main + (let [page-id (uuid/next) + component-id (uuid/next) + main-id (uuid/next) + main (cts/setup-shape {:id main-id + :type :frame + :name "Main" + :shapes []}) + component {:id component-id + :name "Present" + :path "" + :main-instance-id main-id + :main-instance-page page-id} + file-data {:pages-index {page-id {:id page-id + :name "Page" + :objects {uuid/zero (root [main-id]) + main-id main}}} + :components {component-id component}} + result (ctf/load-component-objects file-data component) + objects (:objects result)] + + (t/is (contains? objects main-id) + "present main is stored under its uuid") + (t/is (not (contains? objects nil)) + "present main never introduces a nil key") + (t/is (= main-id (get-in objects [main-id :id]))))) + +(t/deftest load-component-objects-treats-empty-objects-as-already-loaded + ;; A missing-main soft-delete stores `:objects {}`. Render/library paths + ;; call load-component-objects again on that component. The guard must + ;; treat `{}` as already loaded — otherwise it reloads from the page + ;; (and re-warns). Put the main on the page now so a mistaken re-entry + ;; would populate objects and fail this assertion. + (let [page-id (uuid/next) + component-id (uuid/next) + main-id (uuid/next) + main (cts/setup-shape {:id main-id + :type :frame + :name "Main" + :shapes []}) + component {:id component-id + :name "AlreadySnapshotted" + :path "" + :main-instance-id main-id + :main-instance-page page-id + :objects {}} + file-data {:pages-index {page-id {:id page-id + :name "Page" + :objects {uuid/zero (root [main-id]) + main-id main}}} + :components {component-id component}} + result (ctf/load-component-objects file-data component)] + + (t/is (= {} (:objects result)) + "empty :objects means already loaded; do not reload from the page"))) diff --git a/docs/technical-guide/configuration.md b/docs/technical-guide/configuration.md index 88c196f938..4cda6efef4 100644 --- a/docs/technical-guide/configuration.md +++ b/docs/technical-guide/configuration.md @@ -837,12 +837,13 @@ These settings are equally useful if you have a Minio storage system. __Since version 2.11.0__ -You can change the default file data storage backend with `PENPOT_FILE_DATA_BACKEND` environment variable. Possible values are: +You can change the file data storage backend with `PENPOT_FILE_DATA_BACKEND` environment variable. Possible values are: -- `legacy-db`: the current default backend, continues storing the file data of files and snapshots in the same location as previous versions of Penpot (< 2.11.0), this is a conservative default behaviour and will be changed to `db` in next versions. -- `db`: stores the file data on an specific table (the future default backend). +- `db`: the default. Stores the file data on an specific table. - `storage`: stores the file data using the objects storage system (S3 or FS, depending on which one is configured) +The `legacy-db` value is still accepted as a **read-only** alias for the payloads written by versions before 2.11.0: the backend reads them, but it no longer writes file data there. **If you upgrade a deployment with `PENPOT_FILE_DATA_BACKEND=legacy-db`, remove the variable (or set it to `db`) before the upgrade**: the backend reads `legacy-db` as `db` and logs a warning, so saves keep working, but remove the variable anyway so the deployment does not claim a read-only backend. + This also comes with an additional feature that allows offload the "inactive" files on file storage backend and leaves the database only for the active files. To enable it, you should use the `enable-tiered-file-data-storage` flag and `db` as file data storage backend. ```bash diff --git a/frontend/playwright/data/workspace/get-file-tokens-alias.json b/frontend/playwright/data/workspace/get-file-tokens-alias.json new file mode 100644 index 0000000000..3a0609a3d0 --- /dev/null +++ b/frontend/playwright/data/workspace/get-file-tokens-alias.json @@ -0,0 +1,293 @@ +{ + "~:features": { + "~#set": [ + "fdata/path-data", + "plugins/runtime", + "design-tokens/v1", + "variants/v1", + "layout/grid", + "styles/v2", + "fdata/objects-map", + "tokens/numeric-input", + "render-wasm/v1", + "components/v2", + "fdata/shape-data-type" + ] + }, + "~:team-id": "~uc6b102e2-5aaa-809c-8007-dcd1eab2135d", + "~:permissions": { + "~:type": "~:membership", + "~:is-owner": true, + "~:is-admin": true, + "~:can-edit": true, + "~:can-read": true, + "~:is-logged": true + }, + "~:has-media-trimmed": false, + "~:comment-thread-seqn": 0, + "~:name": "New File 11", + "~:revn": 3, + "~:modified-at": "~m1779204621124", + "~:vern": 0, + "~:id": "~uc7ce0794-0992-8105-8004-38f280443849", + "~:is-shared": false, + "~:migrations": { + "~#ordered-set": [ + "legacy-2", + "legacy-3", + "legacy-5", + "legacy-6", + "legacy-7", + "legacy-8", + "legacy-9", + "legacy-10", + "legacy-11", + "legacy-12", + "legacy-13", + "legacy-14", + "legacy-16", + "legacy-17", + "legacy-18", + "legacy-19", + "legacy-25", + "legacy-26", + "legacy-27", + "legacy-28", + "legacy-29", + "legacy-31", + "legacy-32", + "legacy-33", + "legacy-34", + "legacy-36", + "legacy-37", + "legacy-38", + "legacy-39", + "legacy-40", + "legacy-41", + "legacy-42", + "legacy-43", + "legacy-44", + "legacy-45", + "legacy-46", + "legacy-47", + "legacy-48", + "legacy-49", + "legacy-50", + "legacy-51", + "legacy-52", + "legacy-53", + "legacy-54", + "legacy-55", + "legacy-56", + "legacy-57", + "legacy-59", + "legacy-62", + "legacy-65", + "legacy-66", + "legacy-67", + "0001-remove-tokens-from-groups", + "0002-normalize-bool-content-v2", + "0002-clean-shape-interactions", + "0003-fix-root-shape", + "0003-convert-path-content-v2", + "0005-deprecate-image-type", + "0006-fix-old-texts-fills", + "0008-fix-library-colors-v4", + "0009-clean-library-colors", + "0009-add-partial-text-touched-flags", + "0010-fix-swap-slots-pointing-non-existent-shapes", + "0011-fix-invalid-text-touched-flags", + "0012-fix-position-data", + "0013-fix-component-path", + "0013-clear-invalid-strokes-and-fills", + "0014-fix-tokens-lib-duplicate-ids", + "0014-clear-components-nil-objects", + "0015-fix-text-attrs-blank-strings", + "0015-clean-shadow-color", + "0016-copy-fills-from-position-data-to-text-node", + "0017-fix-layout-flex-dir", + "0018-remove-unneeded-objects-from-components", + "0019-fix-missing-swap-slots", + "0020-sync-component-id-with-near-main" + ] + }, + "~:version": 67, + "~:project-id": "~u4cdd76d8-0e6d-8168-8008-0118118e1a1a", + "~:created-at": "~m1779204571619", + "~:backend": "legacy-db", + "~:data": { + "~:pages": [ + "~u9fb430ed-e1e9-81bc-8008-0b7ae978d9c5" + ], + "~:pages-index": { + "~u9fb430ed-e1e9-81bc-8008-0b7ae978d9c5": { + "~:objects": { + "~#penpot/objects-map/v2": { + "~u00000000-0000-0000-0000-000000000000": "[\"~#shape\",[\"^ \",\"~:y\",0,\"~:hide-fill-on-export\",false,\"~:transform\",[\"~#matrix\",[\"^ \",\"~:a\",1.0,\"~:b\",0.0,\"~:c\",0.0,\"~:d\",1.0,\"~:e\",0.0,\"~:f\",0.0]],\"~:rotation\",0,\"~:name\",\"Root Frame\",\"~:width\",0.01,\"~:type\",\"~:frame\",\"~:points\",[[\"~#point\",[\"^ \",\"~:x\",0.0,\"~:y\",0.0]],[\"^:\",[\"^ \",\"~:x\",0.01,\"~:y\",0.0]],[\"^:\",[\"^ \",\"~:x\",0.01,\"~:y\",0.01]],[\"^:\",[\"^ \",\"~:x\",0.0,\"~:y\",0.01]]],\"~:r2\",0,\"~:proportion-lock\",false,\"~:transform-inverse\",[\"^3\",[\"^ \",\"~:a\",1.0,\"~:b\",0.0,\"~:c\",0.0,\"~:d\",1.0,\"~:e\",0.0,\"~:f\",0.0]],\"~:r3\",0,\"~:r1\",0,\"~:id\",\"~u00000000-0000-0000-0000-000000000000\",\"~:parent-id\",\"~u00000000-0000-0000-0000-000000000000\",\"~:frame-id\",\"~u00000000-0000-0000-0000-000000000000\",\"~:strokes\",[],\"~:x\",0,\"~:proportion\",1.0,\"~:r4\",0,\"~:selrect\",[\"~#rect\",[\"^ \",\"~:x\",0,\"~:y\",0,\"^6\",0.01,\"~:height\",0.01,\"~:x1\",0,\"~:y1\",0,\"~:x2\",0.01,\"~:y2\",0.01]],\"~:fills\",[[\"^ \",\"~:fill-color\",\"#FFFFFF\",\"~:fill-opacity\",1]],\"~:flip-x\",null,\"^H\",0.01,\"~:flip-y\",null,\"~:shapes\",[]]]" + } + }, + "~:id": "~u9fb430ed-e1e9-81bc-8008-0b7ae978d9c5", + "~:name": "Page 1" + } + }, + "~:id": "~uc7ce0794-0992-8105-8004-38f280443849", + "~:options": { + "~:components-v2": true, + "~:base-font-size": "16px" + }, + "~:tokens-status": { + "~#penpot/tokens-status": { + "~:active-theme-ids": { + "~#set": [] + }, + "~:active-set-ids": { + "~#set": [ + "~u1dc5cafc-6d57-808d-8008-0b7af8180cbd" + ] + } + } + }, + "~:tokens-lib": { + "~#penpot/tokens-lib": { + "~:sets": { + "~#ordered-map": [ + [ + "S-Global", + { + "~#penpot/token-set": { + "~:id": "~u1dc5cafc-6d57-808d-8008-0b7af8180cbd", + "~:name": "Global", + "~:description": "", + "~:modified-at": "~m1779204621126", + "~:tokens": { + "~#ordered-map": [ + [ + "color.primary", + { + "~#penpot/token": { + "~:id": "~u1dc5cafc-6d57-808d-8008-0b7af8181201", + "~:name": "color.primary", + "~:type": "~:color", + "~:value": "#ff0000", + "~:description": "", + "~:modified-at": "~m1779204586592" + } + } + ], + [ + "color.alias", + { + "~#penpot/token": { + "~:id": "~u1dc5cafc-6d57-808d-8008-0b7af8181202", + "~:name": "color.alias", + "~:type": "~:color", + "~:value": "{color.primary}", + "~:description": "", + "~:modified-at": "~m1779204586592" + } + } + ], + [ + "font-size.base", + { + "~#penpot/token": { + "~:id": "~u1dc5cafc-6d57-808d-8008-0b7af8181203", + "~:name": "font-size.base", + "~:type": "~:font-size", + "~:value": "16", + "~:description": "", + "~:modified-at": "~m1779204586592" + } + } + ], + [ + "font-size.alias", + { + "~#penpot/token": { + "~:id": "~u1dc5cafc-6d57-808d-8008-0b7af8181204", + "~:name": "font-size.alias", + "~:type": "~:font-size", + "~:value": "{font-size.base}", + "~:description": "", + "~:modified-at": "~m1779204586592" + } + } + ], + [ + "typography.base", + { + "~#penpot/token": { + "~:id": "~u1dc5cafc-6d57-808d-8008-0b7af8181205", + "~:name": "typography.base", + "~:type": "~:typography", + "~:value": { + "~:font-family": [ + "Roboto" + ], + "~:font-size": "18", + "~:font-weight": "bold" + }, + "~:description": "", + "~:modified-at": "~m1779204586592" + } + } + ], + [ + "typography.alias", + { + "~#penpot/token": { + "~:id": "~u1dc5cafc-6d57-808d-8008-0b7af8181206", + "~:name": "typography.alias", + "~:type": "~:typography", + "~:value": "{typography.base}", + "~:description": "", + "~:modified-at": "~m1779204586592" + } + } + ] + ] + } + } + } + ] + ] + }, + "~:themes": { + "~#ordered-map": [ + [ + "", + { + "~#ordered-map": [ + [ + "__PENPOT__HIDDEN__TOKEN__THEME__", + { + "~#penpot/token-theme": { + "~:id": "~u00000000-0000-0000-0000-000000000000", + "~:name": "__PENPOT__HIDDEN__TOKEN__THEME__", + "~:group": "", + "~:description": "", + "~:is-source": false, + "~:external-id": "", + "~:modified-at": "~m1779204586593", + "~:sets": { + "~#set": [ + "Global" + ] + } + } + } + ] + ] + } + ] + ] + }, + "~:active-themes": { + "~#set": [ + "/__PENPOT__HIDDEN__TOKEN__THEME__" + ] + } + } + } + } +} \ No newline at end of file diff --git a/frontend/playwright/ui/specs/tokens/crud.spec.js b/frontend/playwright/ui/specs/tokens/crud.spec.js index 6c8d3c0d35..f9d4fe3284 100644 --- a/frontend/playwright/ui/specs/tokens/crud.spec.js +++ b/frontend/playwright/ui/specs/tokens/crud.spec.js @@ -2129,6 +2129,76 @@ test.describe("Tokens tab - edition", () => { await expect(valueField).toHaveValue(/^rgba(.*)$/); }); + const openEditTokenForm = async (setup, type, name) => { + const { tokensSidebar, tokenContextMenuForToken, tokensUpdateCreateModal } = + setup; + const kebabType = type.replace(/\s/g, "-"); + + await unfoldTokenType(tokensSidebar, type); + await tokensSidebar + .getByTestId(`section-${kebabType}`) + .getByRole("button", { name }) + .first() + .click({ button: "right" }); + await expect(tokenContextMenuForToken).toBeVisible(); + await tokenContextMenuForToken.getByText("Edit token").click(); + await expect(tokensUpdateCreateModal).toBeVisible(); + }; + + test("BUG: 12672 Edit form shows the resolved color of an alias token", async ({ + page, + }) => { + const setup = await setupTokensFileRender(page, { + file: "workspace/get-file-tokens-alias.json", + }); + const { tokensUpdateCreateModal } = setup; + + await openEditTokenForm(setup, "color", "alias"); + + await expect(tokensUpdateCreateModal.getByLabel("Value")).toHaveValue( + "{color.primary}", + ); + await expect( + tokensUpdateCreateModal.getByText("Resolved value: #ff0000"), + ).toBeVisible(); + + const swatchColor = tokensUpdateCreateModal + .getByTestId("token-form-color-bullet") + .locator("div[style*='background']") + .first(); + await expect(swatchColor).toHaveCSS("background-color", "rgb(255, 0, 0)"); + }); + + test("BUG: 12788 Edit form shows the resolved value of an alias token on open", async ({ + page, + }) => { + const setup = await setupTokensFileRender(page, { + file: "workspace/get-file-tokens-alias.json", + }); + const { tokensUpdateCreateModal } = setup; + const resolvedValue = tokensUpdateCreateModal.getByText("Resolved value:"); + + // Font size alias + await openEditTokenForm(setup, "font size", "alias"); + await expect( + tokensUpdateCreateModal.getByText("Resolved value: 16"), + ).toBeVisible(); + await page.keyboard.press("Escape"); + + // Font size with a plain value: no hint repeating the value + await openEditTokenForm(setup, "font size", "base"); + await expect(tokensUpdateCreateModal.getByLabel("Value")).toHaveValue("16"); + await expect(resolvedValue).not.toBeVisible(); + await page.keyboard.press("Escape"); + + // Typography alias + await openEditTokenForm(setup, "typography", "alias"); + await expect(resolvedValue).toBeVisible(); + await expect( + tokensUpdateCreateModal.getByText("Font Size: 18"), + ).toBeVisible(); + }); + test("User sees self-reference error when editing a token to create a circular reference", async ({ page, }) => { diff --git a/frontend/src/app/main/data/common.cljs b/frontend/src/app/main/data/common.cljs index f6653e7695..229f5852cc 100644 --- a/frontend/src/app/main/data/common.cljs +++ b/frontend/src/app/main/data/common.cljs @@ -238,8 +238,8 @@ (defn- check-team-sso [team-id] - (let [url (rt/get-current-href)] - (->> (rp/cmd! :check-nitrate-sso {:team-id team-id :url url}) + (let [query-string (rt/get-current-query-string)] + (->> (rp/cmd! :check-nitrate-sso {:team-id team-id :query-string query-string}) (rx/mapcat (fn [{:keys [authorized redirect-uri]}] (if authorized (rx/empty) diff --git a/frontend/src/app/main/data/exports/wasm.cljs b/frontend/src/app/main/data/exports/wasm.cljs index b23556474a..f6e6b4aab8 100644 --- a/frontend/src/app/main/data/exports/wasm.cljs +++ b/frontend/src/app/main/data/exports/wasm.cljs @@ -55,7 +55,7 @@ :pdf (wasm.api/render-shape-pdf object-id) :svg (wasm.api/render-shape-svg object-id scale) (if (= :client (wasm.api/export-target object-id scale type)) - (wasm.api/render-shape-pixels object-id scale type) + (wasm.api/render-shape-pixels object-id scale type true) (wasm.api/render-shape-raster object-id scale type))))) (defn export-image-uri diff --git a/frontend/src/app/main/data/nitrate.cljs b/frontend/src/app/main/data/nitrate.cljs index 1fbbce503c..7d1889e779 100644 --- a/frontend/src/app/main/data/nitrate.cljs +++ b/frontend/src/app/main/data/nitrate.cljs @@ -412,8 +412,11 @@ :add-method "move-existing-team-to-organization" :subscription-status subscription-status}) pending-id (str (uuid/next)) - callback-url (dom/append-query-param (rt/get-current-href) - :pending-action-id pending-id) + ;; The callback travels as a query string; the token is kept in + ;; its own shape because `append-query-param` works on URLs. + callback-query-string (-> (rt/get-current-token) + (dom/append-query-param :pending-action-id pending-id) + rt/token->query-string) ;; The permissions may have changed in Nitrate since the modal ;; was opened: refresh them and explain why the action failed. on-error @@ -428,7 +431,8 @@ (rx/concat (when-not skip-audit? (rx/of audit-event)) - (->> (rp/cmd! :check-nitrate-sso {:organization-id organization-id :url callback-url}) + (->> (rp/cmd! :check-nitrate-sso {:organization-id organization-id + :query-string callback-query-string}) (rx/mapcat (fn [{:keys [authorized redirect-uri]}] (if authorized @@ -446,32 +450,35 @@ (defn check-organization-sso "Asks the backend whether the organization SSO gate can be satisfied for - `dest-url`, returning an observable of the raw `:check-nitrate-sso` - result: `:authorized` with a `:reason` of `:sso-satisfied` or - `:no-team-access`, or `:authorized false` with a `:redirect-uri` (nil - when SSO is required but the provider is unusable). Failures are not - caught, so a network blip stays a network error for the caller to - handle instead of masquerading as an answer." - [{:keys [team-id organization-id dest-url]}] + `query-string` (the route params of the client, `screen=…¶ms`, without + the separator), returning an observable of the raw `:check-nitrate-sso` + result: `:authorized` with a `:reason` of `:sso-satisfied`, + `:no-team-access` or `:no-organization-access`, or `:authorized false` with + a `:redirect-uri` (nil when SSO is required but the provider is unusable). + Failures are not caught, so a network blip stays a network error for the + caller to handle instead of masquerading as an answer." + [{:keys [team-id organization-id query-string]}] (rp/cmd! :check-nitrate-sso (d/without-nils {:team-id team-id :organization-id organization-id - :url dest-url}))) + :query-string query-string}))) (defn retry-organization-sso "Retries the organization SSO login flow after a failed attempt, reusing the same check-nitrate-sso RPC used elsewhere to move the user through the organization's identity provider. Passing `team-id` enables the backend's non-member short-circuit. Falls back to navigating straight - to `dest-url` when no fresh SSO redirect is needed or available." - [{:keys [dest-url] :as params}] - (ptk/reify ::retry-organization-sso - ptk/WatchEvent - (watch [_ _ _] - (->> (check-organization-sso params) - (rx/map (fn [{:keys [redirect-uri]}] - (rt/nav-raw :uri (or redirect-uri dest-url)))) - (rx/catch (fn [_] - (rx/of (rt/nav-raw :uri dest-url)))))))) + to the route of `query-string` when no fresh SSO redirect is needed or + available." + [{:keys [query-string] :as params}] + (let [dest-url (rt/token->url query-string)] + (ptk/reify ::retry-organization-sso + ptk/WatchEvent + (watch [_ _ _] + (->> (check-organization-sso params) + (rx/map (fn [{:keys [redirect-uri]}] + (rt/nav-raw :uri (or redirect-uri dest-url)))) + (rx/catch (fn [_] + (rx/of (rt/nav-raw :uri dest-url))))))))) (defn- fetch-organizations-allowed "Returns an rx observable of an `organizations-allowed` map (organization-id -> boolean). diff --git a/frontend/src/app/main/data/plugins.cljs b/frontend/src/app/main/data/plugins.cljs index f68806fc4a..4d3f8b5ef5 100644 --- a/frontend/src/app/main/data/plugins.cljs +++ b/frontend/src/app/main/data/plugins.cljs @@ -125,7 +125,8 @@ (log/warn :hint "Plugin runtime not initialized yet" :plugin-id plugin-id :action "load-plugin!") - (st/emit! (remove-current-plugin plugin-id))))))) + (st/emit! (remove-current-plugin plugin-id) + (ntf/warn (tr "plugins.runtime-unavailable")))))))) (defn open-plugin! [{:keys [url] :as manifest} user-can-edit?] diff --git a/frontend/src/app/main/data/team.cljs b/frontend/src/app/main/data/team.cljs index 80ab5e0fad..6659fa8aae 100644 --- a/frontend/src/app/main/data/team.cljs +++ b/frontend/src/app/main/data/team.cljs @@ -48,7 +48,7 @@ (if (or (not (contains? cf/flags :admin-console)) (= team-id default-team-id)) (rx/of team-id) - (->> (rp/cmd! :check-nitrate-sso {:team-id team-id :url (rt/get-current-href)}) + (->> (rp/cmd! :check-nitrate-sso {:team-id team-id :query-string (rt/get-current-query-string)}) (rx/map (fn [{:keys [authorized]}] (if authorized team-id default-team-id)))))) diff --git a/frontend/src/app/main/errors.cljs b/frontend/src/app/main/errors.cljs index fceac0d344..b4c248823c 100644 --- a/frontend/src/app/main/errors.cljs +++ b/frontend/src/app/main/errors.cljs @@ -806,11 +806,11 @@ [{:keys [organization-id team-id] :as error}] (when-not @sso-renewal-pending? (vreset! sso-renewal-pending? true) - (let [dest-url (rt/get-current-href)] + (let [query-string (rt/get-current-query-string)] (->> (dnt/check-organization-sso {:organization-id organization-id :team-id team-id - :dest-url dest-url}) + :query-string query-string}) ;; Release the guard however the check ends, including an ;; unsubscription or a completion without a result: a stuck guard ;; would silently drop every later rejection. diff --git a/frontend/src/app/main/router.cljs b/frontend/src/app/main/router.cljs index a466f9908b..070c2a764c 100644 --- a/frontend/src/app/main/router.cljs +++ b/frontend/src/app/main/router.cljs @@ -192,8 +192,15 @@ (ts/asap (partial dom/reload-current-window force?))))) (defn nav-raw - [& {:keys [href uri]}] + "Navigate to a raw URL, out of the router. + + The destination is exposed through `@event`, like `navigate` does, so + listeners and tests can tell where the user is being sent." + [& {:keys [href uri] :as props}] (ptk/reify ::nav-raw + IDeref + (-deref [_] props) + ptk/EffectEvent (effect [_ _ _] (cond @@ -207,6 +214,41 @@ [] (.-href globals/location)) +(defn token->query-string + "The query string of `token`, without the `?` that separates it from the + path. The history token is `location.search`, so it carries that delimiter; + a query string does not, and this is what the organization SSO RPC takes: the + server joins it under its own public URI and puts the separator back." + [token] + (let [token (or token "")] + (if (str/starts-with? token "?") + (subs token 1) + token))) + +(defn token->url + "Build the absolute URL for a route token under `cf/public-uri`. Accepts + the token in either shape: with its leading `?` or as a bare query string. + A blank token lands on the public URI, without a trailing separator." + [token] + (let [query-string (token->query-string token)] + (if (str/blank? query-string) + (str cf/public-uri) + (str cf/public-uri "?" query-string)))) + +(defn get-current-token + "Return the current history token: the query string that carries the route, + with the leading `?` that separates it from the path. The application + concatenates it over the base path, so it keeps the delimiter; use + `get-current-query-string` for the value the SSO flow sends to the backend. + The server builds the destination URL out of that one, so the client never + chooses the origin of it." + [] + (bhistory/get-token)) + +(defn get-current-query-string + "The query string of the current route (see `token->query-string`)." + [] + (token->query-string (get-current-token))) ;; --- History API diff --git a/frontend/src/app/main/ui/dashboard/subscription.cljs b/frontend/src/app/main/ui/dashboard/subscription.cljs index 61c96506bc..d983f789ac 100644 --- a/frontend/src/app/main/ui/dashboard/subscription.cljs +++ b/frontend/src/app/main/ui/dashboard/subscription.cljs @@ -42,11 +42,13 @@ (get-subscription-type (dm/get-in profile [:props :subscription])))) (defn show-team-plan? - [is-owner team profile] - (not (and is-owner - (nil? (dm/get-in team [:organization :id])) - (contains? #{"enterprise" "nitrate"} - (get-profile-subscription-type profile))))) + "The plan of a team outside an organization is the plan of its owner, + which the backend puts in the team `:subscription`. It is hidden from + every member when the owner has an Enterprise or Nitrate plan." + [team] + (or (some? (dm/get-in team [:organization :id])) + (not (contains? #{"enterprise" "nitrate"} + (get-subscription-type (:subscription team)))))) (mf/defc cta-power-up* [{:keys [top-title top-description bottom-description bottom-button bottom-button-href has-dropdown is-highlighted]}] @@ -330,7 +332,7 @@ "enterprise" (tr "subscription.current-plan.enterprise"))]])) (mf/defc team* - [{:keys [is-owner team profile]}] + [{:keys [is-owner team]}] (let [subscription (:subscription team) subscription-type (get-subscription-type subscription) team-plan-type (get-team-plan-type team) @@ -348,7 +350,7 @@ href (str "payments/subscriptions/show?returnUrl=" href)] (st/emit! (rt/nav-raw :href href)))))] - (when (show-team-plan? is-owner team profile) + (when (show-team-plan? team) [:div {:class (stl/css :team)} [:div {:class (stl/css :team-label)} (tr "subscription.dashboard.team-plan")] diff --git a/frontend/src/app/main/ui/dashboard/team.cljs b/frontend/src/app/main/ui/dashboard/team.cljs index 1004a97231..b126559cac 100644 --- a/frontend/src/app/main/ui/dashboard/team.cljs +++ b/frontend/src/app/main/ui/dashboard/team.cljs @@ -665,7 +665,7 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (mf/defc invitation-role-selector* - [{:keys [role status on-change]}] + [{:keys [role status on-change can-invite]}] (let [show? (mf/use-state false) label (cond (= role :owner) (tr "labels.owner") @@ -686,7 +686,7 @@ (on-change role event))))] [:* - (if (= status :pending) + (if (and ^boolean can-invite (= status :pending)) [:button {:class (stl/css :rol-selector :has-priv) :type "button" :aria-haspopup "listbox" @@ -714,15 +714,17 @@ (tr "labels.viewer")]]]])) (defn invitation-link-copyable? - "The link of an owner invitation can only be copied by the team owner: an - admin cannot create nor promote an owner invitation, so the backend does - not mint its token for them." - [is-owner? role] - (or (not= :owner role) is-owner?)) + "The link of an invitation invites as much as sending it, so it needs the + organization invite policy (`can-invite?`). The link of an owner invitation + can only be copied by the team owner: an admin cannot create nor promote an + owner invitation, so the backend does not mint its token for them." + [can-invite? is-owner? role] + (and can-invite? + (or (not= :owner role) is-owner?))) (mf/defc invitation-actions* {::mf/private true} - [{:keys [invitation team-id is-owner?]}] + [{:keys [invitation team-id is-owner? can-invite]}] (let [email (:email invitation) copied* (mf/use-state false) copied? (deref copied*) @@ -768,7 +770,7 @@ (-> (dtm/copy-invitation-link params) (with-meta {::ev/origin :team}))))))] - (when (invitation-link-copyable? is-owner? (:role invitation)) + (when (invitation-link-copyable? can-invite is-owner? (:role invitation)) [:> icon-button* {:variant "ghost" :aria-label (tr "labels.copy-invitation-link") :on-click on-copy @@ -777,7 +779,7 @@ (mf/defc invitation-row* {::mf/wrap [mf/memo] ::mf/private true} - [{:keys [invitation team-id selected on-select-change is-owner?]}] + [{:keys [invitation team-id selected on-select-change is-owner? can-invite]}] (let [expired? (:expired invitation) email (:email invitation) @@ -811,24 +813,29 @@ [:div {:class (stl/css :table-row :table-row-invitations)} [:div {:class (stl/css :table-field :field-email)} - [:div {:class (stl/css :input-wrapper)} - [:label - [:span {:class (stl/css-case :input-checkbox true - :global/checked (is-selected? email))} - deprecated-icon/status-tick] + ;; Selecting an invitation only leads to resending or deleting it, and + ;; both need the organization invite policy. + (if ^boolean can-invite + [:div {:class (stl/css :input-wrapper)} + [:label + [:span {:class (stl/css-case :input-checkbox true + :global/checked (is-selected? email))} + deprecated-icon/status-tick] - [:input {:type "checkbox" - :id (dm/str "email-" email) - :data-attr email - :value email - :checked (is-selected? email) - :on-change on-change}] - email]]] + [:input {:type "checkbox" + :id (dm/str "email-" email) + :data-attr email + :value email + :checked (is-selected? email) + :on-change on-change}] + email]] + [:div email])] [:div {:class (stl/css :table-field :field-roles)} [:> invitation-role-selector* {:role role :status status + :can-invite can-invite :on-change on-change-role}]] [:div {:class (stl/css :table-field :field-status)} @@ -838,11 +845,12 @@ [:> invitation-actions* {:invitation invitation :team-id team-id - :is-owner? is-owner?}]]])) + :is-owner? is-owner? + :can-invite can-invite}]]])) (mf/defc empty-invitation-table* {::mf/private true} - [{:keys [team]}] + [{:keys [team can-invite]}] (let [route (mf/deref refs/route) invite-email (-> route :query-params :invite-email) @@ -855,11 +863,21 @@ [:div {:class (stl/css :empty-invitations)} [:div (tr "labels.no-invitations")] [:div (tr "labels.no-invitations-gather-people")] - [:> button* {:variant "primary" - :class (stl/css :btn-empty-invitations) - :on-click on-invite-member - :data-testid "invite-member"} - (tr "dashboard.invite-profile")]])) + ;; Same as the header button: disabled with its reason when the + ;; organization invite policy leaves the profile out. + (let [invite-button (mf/html + [:> button* {:variant "primary" + :class (stl/css :btn-empty-invitations) + :on-click on-invite-member + :disabled (not can-invite) + :data-testid "invite-member"} + (tr "dashboard.invite-profile")])] + (if ^boolean can-invite + invite-button + [:> tooltip* {:content (tr "dashboard.invite-profile-disabled") + :id "empty-invite-member-disabled-tooltip" + :tab-index 0} + invite-button]))])) (mf/defc invitation-modal {::mf/register modal/components @@ -1224,18 +1242,17 @@ (tr "dashboard.invitations.no-permission")]] [:* - (when (> (count @selected) 0) + ;; Managing the pending invitations (role, resend, delete, link) + ;; needs the organization invite policy; the list only needs admin. + (when (and can-invite? (> (count @selected) 0)) [:div {:class (stl/css :invitations-actions)} [:div (tr "team.invitations-selected" (i18n/c (count @selected)))] - ;; Resending goes through the invite command, so it needs the - ;; organization invite policy; deleting only needs team admin. - (when can-invite? - [:div - [:> button* {:variant "secondary" - :type "button" - :on-click on-resend} - (tr "labels.resend-invitation")]]) + [:div + [:> button* {:variant "secondary" + :type "button" + :on-click on-resend} + (tr "labels.resend-invitation")]] [:> icon-button* {:on-click on-delete :variant "destructive" :aria-label (tr "labels.delete-invitation") @@ -1261,13 +1278,14 @@ "arrow-down") :on-click on-order-by-status}]]] (if (empty? @invitations) - [:> empty-invitation-table* {:team team}] + [:> empty-invitation-table* {:team team :can-invite can-invite?}] [:div {:class (stl/css :table-rows)} (for [invitation @invitations] [:> invitation-row* {:key (:email invitation) :invitation invitation :is-owner? is-owner? + :can-invite can-invite? :team-id team-id :selected selected :on-select-change on-select-change}])])])])) @@ -1783,5 +1801,4 @@ (when (contains? cfg/flags :subscriptions) [:> team* {:is-owner (:is-owner permissions) - :team team - :profile profile}])]]])) + :team team}])]]])) diff --git a/frontend/src/app/main/ui/routes.cljs b/frontend/src/app/main/ui/routes.cljs index fa9926911b..618e4ed99f 100644 --- a/frontend/src/app/main/ui/routes.cljs +++ b/frontend/src/app/main/ui/routes.cljs @@ -164,7 +164,7 @@ active. If so, calls :check-nitrate-sso and either proceeds with navigation or redirects to the SSO provider URL. Successful checks are cached for five minutes per profile and team; redirect results are never cached." - [match send-event-info? url] + [match send-event-info? query-string] (let [route-name (name (get-in match [:data :name])) relevant? (and (contains? cf/flags :admin-console) (or (str/starts-with? route-name "dashboard") @@ -188,7 +188,7 @@ (navigate) :else - (->> (rp/cmd! :check-nitrate-sso {:team-id team-id :url url}) + (->> (rp/cmd! :check-nitrate-sso {:team-id team-id :query-string query-string}) (rx/subs! (fn [{:keys [authorized redirect-uri]}] (if authorized @@ -206,7 +206,7 @@ and deliberately do NOT proceed with normal navigation: emitting `rt/navigated` would clear the exception that was just assigned. Otherwise, delegate to `check-sso-and-navigate`." - [match send-event-info? url] + [match send-event-info? query-string] (let [route-name (name (get-in match [:data :name])) sso-error? (some? (get-in match [:query-params :sso-error])) organization-id (some-> (get-in match [:query-params :organization-id]) uuid/parse*) @@ -223,7 +223,7 @@ :team-id team-id :is-workspace is-workspace? :is-dashboard is-dashboard?})) - (check-sso-and-navigate match send-event-info? url)))) + (check-sso-and-navigate match send-event-info? query-string)))) (declare on-query-navigate) @@ -260,9 +260,7 @@ (defn- on-query-navigate [router token send-event-info?] - (let [token-query (if (str/starts-with? (or token "") "?") - (subs token 1) - (or token "")) + (let [token-query (rt/token->query-string token) query-params (u/query-string->map token-query) ;; Without a `screen` the token targets the app root; its params ;; (e.g. `template`) are forwarded to the dashboard or kept for @@ -270,7 +268,7 @@ root? (nil? (rt/get-query-param query-params :screen)) match (rt/match router token)] (if (some? match) - (handle-sso-error-and-navigate match send-event-info? (rt/get-current-href)) + (handle-sso-error-and-navigate match send-event-info? (rt/get-current-query-string)) ;; We just recheck with an additional profile request; this ;; avoids some race conditions that causes unexpected redirects diff --git a/frontend/src/app/main/ui/static.cljs b/frontend/src/app/main/ui/static.cljs index 588dd0f970..2b58bb8db2 100644 --- a/frontend/src/app/main/ui/static.cljs +++ b/frontend/src/app/main/ui/static.cljs @@ -524,12 +524,14 @@ SSO exchange with the identity provider fails." {::mf/private true} [{:keys [organization-id team-id profile is-workspace is-dashboard organization-name]}] - (let [clean-url + (let [clean-token (mf/with-memo [] - (-> (rt/get-current-href) + (-> (rt/get-current-token) (dom/remove-query-param :sso-error) (dom/remove-query-param :organization-id))) + clean-url (rt/token->url clean-token) + _ (mf/with-effect [] ;; Consume the marker once: scrub it from the URL bar so a ;; browser refresh doesn't keep re-showing this dialog. @@ -545,14 +547,14 @@ on-retry (mf/use-fn - (mf/deps organization-id team-id clean-url) + (mf/deps organization-id team-id clean-token) (fn [] (st/emit! (rt/assign-exception nil)) (if (or team-id organization-id) ;; Retry with team-id and/or organization-id to trigger SSO check (st/emit! (dnt/retry-organization-sso {:team-id team-id :organization-id organization-id - :dest-url clean-url})) + :query-string (rt/token->query-string clean-token)})) ;; Fallback: just navigate to clean URL (st/emit! (rt/nav-raw :uri clean-url)))))] diff --git a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/color_input.cljs b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/color_input.cljs index 477d7327ea..115fbddd45 100644 --- a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/color_input.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/color_input.cljs @@ -281,19 +281,28 @@ (d/update-when result :error wte/resolve-error-message))) (rx/subs! (fn [{:keys [error value]}] (let [touched? (get-in @form [:touched input-name])] - (when touched? - (if error - (if (csu/group-name-conflict-error? error token-name) - (swap! form assoc-in [:extra-errors ""] {:message error}) - (do - (swap! form assoc-in [:extra-errors input-name] {:message error}) - (swap! form assoc-in [:data :color-result] "") - (reset! hint* {:message error :type "error"}))) + (cond + ;; Untouched field (form just opened) + (not touched?) + (when-not error + (swap! form assoc-in [:data :color-result] value) + (when (csu/show-hint-on-open? (get-in @form [:data input-name]) value) + (reset! hint* {:message (tr "workspace.tokens.resolved-value" (dwtf/format-token-value value)) + :type "hint"}))) - (let [message (tr "workspace.tokens.resolved-value" (dwtf/format-token-value value))] - (swap! form update :extra-errors dissoc input-name) - (swap! form assoc-in [:data :color-result] value) - (reset! hint* {:message message :type "hint"}))))))))] + error + (if (csu/group-name-conflict-error? error token-name) + (swap! form assoc-in [:extra-errors ""] {:message error}) + (do + (swap! form assoc-in [:extra-errors input-name] {:message error}) + (swap! form assoc-in [:data :color-result] "") + (reset! hint* {:message error :type "error"}))) + + :else + (let [message (tr "workspace.tokens.resolved-value" (dwtf/format-token-value value))] + (swap! form update :extra-errors dissoc input-name) + (swap! form assoc-in [:data :color-result] value) + (reset! hint* {:message message :type "hint"})))))))] (fn [] (rx/dispose! subs)))) diff --git a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/combobox.cljs b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/combobox.cljs index a98e51ce74..730ced7b99 100644 --- a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/combobox.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/combobox.cljs @@ -285,16 +285,25 @@ ((:error/fn error) (:error/value error)))))) (rx/subs! (fn [{:keys [error value]}] (let [touched? (get-in @form [:touched name])] - (when touched? - (if error - (if (csu/group-name-conflict-error? error token-name) - (swap! form assoc-in [:extra-errors ""] {:message error}) - (do - (swap! form assoc-in [:extra-errors name] {:message error}) - (reset! hint* {:message error :type "error"}))) - (let [message (tr "workspace.tokens.resolved-value" value)] - (swap! form update :extra-errors dissoc name) - (reset! hint* {:message message :type "hint"}))))))))] + (cond + ;; Untouched field (form just opened) + (not touched?) + (when (and (not error) + (csu/show-hint-on-open? (get-in @form [:data name]) value)) + (reset! hint* {:message (tr "workspace.tokens.resolved-value" value) + :type "hint"})) + + error + (if (csu/group-name-conflict-error? error token-name) + (swap! form assoc-in [:extra-errors ""] {:message error}) + (do + (swap! form assoc-in [:extra-errors name] {:message error}) + (reset! hint* {:message error :type "error"}))) + + :else + (let [message (tr "workspace.tokens.resolved-value" value)] + (swap! form update :extra-errors dissoc name) + (reset! hint* {:message message :type "hint"})))))))] (fn [] (rx/dispose! subs)))) diff --git a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/fonts_combobox.cljs b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/fonts_combobox.cljs index 7833bd95ea..8838adc844 100644 --- a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/fonts_combobox.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/fonts_combobox.cljs @@ -175,17 +175,26 @@ (d/update-when result :error wte/resolve-error-assoc-message))) (rx/subs! (fn [{:keys [error value]}] (let [touched? (get-in @form [:touched input-name])] - (when touched? - (if error - (let [error' (:message error)] - (if (csu/group-name-conflict-error? error' token-name) - (swap! form assoc-in [:extra-errors ""] error') - (do - (swap! form assoc-in [:extra-errors input-name] error') - (reset! hint* {:message error' :type "error"})))) - (let [message (tr "workspace.tokens.resolved-value" value)] - (swap! form update :extra-errors dissoc input-name) - (reset! hint* {:message message :type "hint"}))))))))] + (cond + ;; Untouched field (form just opened) + (not touched?) + (when (and (not error) + (csu/show-hint-on-open? (get-in @form [:data input-name]) value)) + (reset! hint* {:message (tr "workspace.tokens.resolved-value" value) + :type "hint"})) + + error + (let [error' (:message error)] + (if (csu/group-name-conflict-error? error' token-name) + (swap! form assoc-in [:extra-errors ""] error') + (do + (swap! form assoc-in [:extra-errors input-name] error') + (reset! hint* {:message error' :type "error"})))) + + :else + (let [message (tr "workspace.tokens.resolved-value" value)] + (swap! form update :extra-errors dissoc input-name) + (reset! hint* {:message message :type "hint"})))))))] (fn [] (rx/dispose! subs)))) diff --git a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/input.cljs b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/input.cljs index b7dd512b7f..4a057d72bd 100644 --- a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/input.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/input.cljs @@ -255,16 +255,25 @@ (d/update-when result :error wte/resolve-error-message))) (rx/subs! (fn [{:keys [error value]}] (let [touched? (get-in @form [:touched input-name])] - (when touched? - (if error - (if (csu/group-name-conflict-error? error token-name) - (swap! form assoc-in [:extra-errors ""] {:message error}) - (do - (swap! form assoc-in [:extra-errors input-name] {:message error}) - (reset! hint* {:message error :type "error"}))) - (let [message (tr "workspace.tokens.resolved-value" value)] - (swap! form update :extra-errors dissoc input-name) - (reset! hint* {:message message :type "hint"}))))))))] + (cond + ;; Untouched field (form just opened) + (not touched?) + (when (and (not error) + (csu/show-hint-on-open? (get-in @form [:data input-name]) value)) + (reset! hint* {:message (tr "workspace.tokens.resolved-value" value) + :type "hint"})) + + error + (if (csu/group-name-conflict-error? error token-name) + (swap! form assoc-in [:extra-errors ""] {:message error}) + (do + (swap! form assoc-in [:extra-errors input-name] {:message error}) + (reset! hint* {:message error :type "error"}))) + + :else + (let [message (tr "workspace.tokens.resolved-value" value)] + (swap! form update :extra-errors dissoc input-name) + (reset! hint* {:message message :type "hint"})))))))] (fn [] (rx/dispose! subs)))) @@ -309,7 +318,11 @@ resolve-stream (mf/with-memo [token] - (if-let [value (get-in token [:value input-name])] + ;; An alias token keeps its reference as a plain string value + (if-let [value (if (= input-name :reference) + (let [value (:value token)] + (when (string? value) value)) + (get-in token [:value input-name]))] (rx/behavior-subject value) (rx/subject))) diff --git a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/utils.cljs b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/utils.cljs index 75e2317814..4213b5d5d2 100644 --- a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/utils.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/utils.cljs @@ -2,6 +2,7 @@ (:require [app.common.data.macros :as dm] [app.common.types.token :as cto] + [app.main.data.workspace.tokens.format :as dwtf] [app.util.i18n :refer [tr]] [cuerdas.core :as str])) @@ -126,6 +127,14 @@ (let [translated-string (tr "errors.tokens.name-collision" token-name)] (= error translated-string))) +(defn show-hint-on-open? + "True when an untouched field (form just opened) shows the resolved + value: only when it differs from the written one, so a plain `16` + gets no `Resolved value: 16` hint." + [input-value resolved-value] + (not= (str/lower (str/trim (dwtf/format-token-value input-value))) + (str/lower (str/trim (dwtf/format-token-value resolved-value))))) + (defn- referenced-token-name "Name of the token `reference` points to, when `reference` is exactly one `{name}` reference." diff --git a/frontend/src/app/plugins.cljs b/frontend/src/app/plugins.cljs index 921cf740b9..f10e4213d4 100644 --- a/frontend/src/app/plugins.cljs +++ b/frontend/src/app/plugins.cljs @@ -8,6 +8,7 @@ "RPC for plugins runtime." (:require ["@penpot/plugins-runtime" :as runtime] + [app.main.data.event :as ev] [app.main.errors :as errors] [app.main.features :as features] [app.main.store :as st] @@ -20,14 +21,26 @@ [app.plugins.register :as preg] [app.plugins.ruler-guides :as rg] [app.plugins.shape :as shape] + [app.util.globals :as ug] [beicon.v2.core :as rx] [potok.v2.core :as ptk])) +(defn- runtime-available? + [] + (fn? (unchecked-get ug/global "ɵloadPlugin"))) + (defn init-plugins-runtime [] (runtime/initPluginsRuntime (fn [plugin-id] (api/create-context plugin-id))) - ;; Signal that runtime is ready - (preg/signal-runtime-ready)) + ;; Signal even when the runtime failed to start: MCP initialization waits + ;; for this promise and would otherwise hang forever. + (preg/signal-runtime-ready) + (runtime-available?)) + +(defn- runtime-unavailable-event + [] + (ev/event {::ev/name "plugins-runtime-unavailable" + :message (some-> (runtime/getPluginsRuntimeError) .-message)})) (defn initialize [] @@ -41,8 +54,10 @@ (rx/observe-on :async) (rx/filter #(features/active-feature? @st/state "plugins/runtime")) (rx/take 1) - (rx/tap init-plugins-runtime) - (rx/ignore))))) + (rx/map (fn [_] + (when-not (init-plugins-runtime) + (runtime-unavailable-event)))) + (rx/filter some?))))) ;; Prevent circular dependency (set! flex/shape-proxy? shape/shape-proxy?) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index d9ad59efc5..612f9756fc 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -1130,6 +1130,10 @@ [hidden] (h/call wasm/internal-module "_set_shape_hidden" hidden)) +(defn set-shape-hide-fill-on-export + [hide] + (h/call wasm/internal-module "_set_shape_hide_fill_on_export" hide)) + (defn clear-shape-fills! "Clear the fills of the currently-selected shape (call `use-shape` first). Equivalent to `set-shape-fills` with an empty collection." @@ -2934,8 +2938,9 @@ (defn render-shape-pixels "Renders a shape subtree to encoded image bytes on the GPU export surface. `format` is :png, :jpeg or :webp; jpeg is flattened onto white on the Rust - side, since it has no alpha channel." - [shape-id scale format] + side, since it has no alpha channel. With `export?` the root drops its + fills when it hides them on export." + [shape-id scale format export?] (when (initialized?) (let [buffer (uuid/get-u32 shape-id) offset (h/call wasm/internal-module "_render_shape_pixels" @@ -2944,7 +2949,8 @@ (aget buffer 2) (aget buffer 3) scale - (sr/translate-raster-format format))] + (sr/translate-raster-format format) + export?)] (read-raster-result offset)))) (defn render-shape-raster diff --git a/frontend/src/app/render_wasm/shape.cljs b/frontend/src/app/render_wasm/shape.cljs index dfbcf37bb4..7190062f9f 100644 --- a/frontend/src/app/render_wasm/shape.cljs +++ b/frontend/src/app/render_wasm/shape.cljs @@ -191,6 +191,9 @@ :hidden (api/set-shape-hidden v) + :hide-fill-on-export + (api/set-shape-hide-fill-on-export (boolean v)) + :shapes (api/set-shape-children v) diff --git a/frontend/src/app/worker/component_thumbnails.cljs b/frontend/src/app/worker/component_thumbnails.cljs index 70c918a6b5..94dd83120c 100644 --- a/frontend/src/app/worker/component_thumbnails.cljs +++ b/frontend/src/app/worker/component_thumbnails.cljs @@ -44,7 +44,9 @@ {sel-w :width sel-h :height} (dm/get-in objects [root-id :selrect]) longest-side (mth/max (or ext-w sel-w) (or ext-h sel-h)) scale (thumbnail-scale longest-side) - bytes (wasm.api/render-shape-pixels root-id scale :png)] + ;; A thumbnail is a preview, not an export: never drop the root's + ;; fills, even when it hides them on export. + bytes (wasm.api/render-shape-pixels root-id scale :png false)] (when (and (some? bytes) (pos? (.-length ^js bytes))) (wapi/create-blob bytes "image/png")))) diff --git a/frontend/test/frontend_tests/data/dashboard_test.cljs b/frontend/test/frontend_tests/data/dashboard_test.cljs index a6328acfdd..1b513c4455 100644 --- a/frontend/test/frontend_tests/data/dashboard_test.cljs +++ b/frontend/test/frontend_tests/data/dashboard_test.cljs @@ -26,7 +26,7 @@ "Runs handle-change-team-organization and returns recorded i18n/tr calls." [notification organization] (let [team-id (uuid/next) - current-url (str "https://penpot.example.com/#/dashboard/recent?team-id=" team-id) + current-query-string (str "screen=dashboard-recent&team-id=" team-id) state {:current-team-id team-id :teams {team-id {:id team-id :name "Team-Apple"}}} event (dcm/handle-change-team-organization @@ -42,11 +42,11 @@ rp/cmd! (mock/stub (fn [cmd params] (if (= [:check-nitrate-sso - {:team-id team-id :url current-url}] + {:team-id team-id :query-string current-query-string}] [cmd params]) (rx/of {:authorized true}) (rx/throw (ex-info "unexpected RPC" {:cmd cmd :params params}))))) - rt/get-current-href (mock/stub (constantly current-url))} + rt/get-current-query-string (mock/stub (constantly current-query-string))} (await (async/observe (ptk/watch event state nil) :on-next (fn [ev] (t/is (contains? #{::ntf/show ::dtm/fetch-teams} @@ -119,7 +119,7 @@ (t/async done (let [team-id (uuid/next) organization {:id (uuid/next) :name "OrgA"} - current-url (str "https://penpot.example.com/#/dashboard/recent?team-id=" team-id) + current-query-string (str "screen=dashboard-recent&team-id=" team-id) redirect-url "https://idp.example.com/authorize" state {:current-team-id team-id} event (dcm/handle-change-team-organization @@ -130,11 +130,11 @@ rp/cmd! (mock/stub (fn [cmd params] (if (= [:check-nitrate-sso - {:team-id team-id :url current-url}] + {:team-id team-id :query-string current-query-string}] [cmd params]) (rx/of {:authorized false :redirect-uri redirect-url}) (rx/throw (ex-info "unexpected RPC" {:cmd cmd :params params}))))) - rt/get-current-href (mock/stub (constantly current-url))} + rt/get-current-query-string (mock/stub (constantly current-query-string))} (fn [done'] (->> (ptk/watch event state nil) (rx/reduce conj []) @@ -151,7 +151,7 @@ (t/async done (let [team-id (uuid/next) organization-id (uuid/next) - current-url (str "https://penpot.example.com/#/workspace?team-id=" team-id) + current-query-string (str "screen=workspace&team-id=" team-id) redirect-url "https://idp.example.com/authorize" state {:current-team-id team-id :teams {team-id {:id team-id @@ -163,11 +163,11 @@ rp/cmd! (mock/stub (fn [cmd params] (if (= [:check-nitrate-sso - {:team-id team-id :url current-url}] + {:team-id team-id :query-string current-query-string}] [cmd params]) (rx/of {:authorized false :redirect-uri redirect-url}) (rx/throw (ex-info "unexpected RPC" {:cmd cmd :params params}))))) - rt/get-current-href (mock/stub (constantly current-url))} + rt/get-current-query-string (mock/stub (constantly current-query-string))} (fn [done'] (->> (ptk/watch event state nil) (rx/reduce conj []) diff --git a/frontend/test/frontend_tests/data/nitrate_test.cljs b/frontend/test/frontend_tests/data/nitrate_test.cljs index 2ea4442157..bb539cccfb 100644 --- a/frontend/test/frontend_tests/data/nitrate_test.cljs +++ b/frontend/test/frontend_tests/data/nitrate_test.cljs @@ -8,6 +8,7 @@ (:require [app.common.time :as ct] [app.common.uri :as u] + [app.config :as cf] [app.main.data.event :as ev] [app.main.data.modal :as modal] [app.main.data.nitrate :as dnt] @@ -367,7 +368,7 @@ state {:teams {(:id team) team}}] (await (mock/with-mocks* - {rt/get-current-href (mock/stub (constantly "http://localhost/#/dashboard")) + {rt/get-current-token (mock/stub (constantly "?screen=dashboard&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d")) rp/cmd! (mock/stub (fn [cmd _params] (case cmd @@ -394,3 +395,32 @@ (t/is (= ::dt/fetch-teams (ptk/type (first emitted)))) (t/is (= {:modal-show [:no-permission-modal {:type :no-organizations-create}]} (second emitted))))) + +(def ^:private sso-query-string + "screen=workspace&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d") + +(defn- ^:async observe-retry-organization-sso + "Runs `retry-organization-sso` with the given answer from the backend and + resolves to the uri the user is sent to." + [rpc-answer] + (let [emitted (atom []) + event (dnt/retry-organization-sso {:team-id "team-1" + :organization-id "org-1" + :query-string sso-query-string})] + (await + (mock/with-mocks* + {rp/cmd! (mock/stub (fn [_cmd _params] (rx/of rpc-answer)))} + (await (async/observe (ptk/watch event {} nil) + :on-next #(swap! emitted conj %))))) + ;; `nav-raw` exposes its destination through `@event`. + (-> @emitted first deref :uri))) + +(t/deftest ^:async retry-organization-sso-lands-on-the-route-of-the-token + (t/testing "without a redirect uri the user goes back to the route of the token" + (let [target (await (observe-retry-organization-sso {:authorized true}))] + (t/is (= (str cf/public-uri "?" sso-query-string) target)))) + + (t/testing "the redirect uri of the backend wins over the route of the token" + (let [target (await (observe-retry-organization-sso {:authorized false + :redirect-uri "https://idp.example.com/authorize"}))] + (t/is (= "https://idp.example.com/authorize" target))))) diff --git a/frontend/test/frontend_tests/data/team_test.cljs b/frontend/test/frontend_tests/data/team_test.cljs index 87ef76be68..d05041c0db 100644 --- a/frontend/test/frontend_tests/data/team_test.cljs +++ b/frontend/test/frontend_tests/data/team_test.cljs @@ -162,7 +162,16 @@ (t/deftest owner-invitation-link-is-copyable-only-by-the-team-owner ;; An admin cannot create nor promote an owner invitation, so the backend ;; does not mint its token for them: the action is not offered either. - (t/is (true? (dtui/invitation-link-copyable? true :owner))) - (t/is (false? (dtui/invitation-link-copyable? false :owner))) - (t/is (true? (dtui/invitation-link-copyable? false :editor))) - (t/is (true? (dtui/invitation-link-copyable? false :viewer)))) + (t/is (true? (dtui/invitation-link-copyable? true true :owner))) + (t/is (false? (dtui/invitation-link-copyable? true false :owner))) + (t/is (true? (dtui/invitation-link-copyable? true false :editor))) + (t/is (true? (dtui/invitation-link-copyable? true false :viewer)))) + +(t/deftest invitation-link-is-not-copyable-without-the-invite-policy + ;; The link invites as much as sending the invitation does, so a profile + ;; that the organization invite policy leaves out cannot copy it, not even + ;; the team owner. + (t/is (false? (dtui/invitation-link-copyable? false true :owner))) + (t/is (false? (dtui/invitation-link-copyable? false true :editor))) + (t/is (false? (dtui/invitation-link-copyable? false false :editor))) + (t/is (false? (dtui/invitation-link-copyable? false false :viewer)))) diff --git a/frontend/test/frontend_tests/main_errors_test.cljs b/frontend/test/frontend_tests/main_errors_test.cljs index e58317b322..1d0f5f0a51 100644 --- a/frontend/test/frontend_tests/main_errors_test.cljs +++ b/frontend/test/frontend_tests/main_errors_test.cljs @@ -600,6 +600,9 @@ ;; being told they have no access to the file. ;; --------------------------------------------------------------------------- +(def ^:private workspace-query-string + "screen=workspace&team-id=b8f8bb52-8b70-8144-8004-4a5085f0bdc9") + (def ^:private workspace-href "https://penpot.example.com/#/workspace?team-id=b8f8bb52-8b70-8144-8004-4a5085f0bdc9") @@ -621,7 +624,8 @@ (fn [_command _params] (rx/of {:authorized false :redirect-uri "https://idp.example.com/authorize"}))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) st/emit! (mock/stub (fn [& emitted] (swap! events into emitted)))} (errors/on-error (sso-required-error)) (await (async/settle)) @@ -637,14 +641,15 @@ (swap! rpc-calls conj {:command command :params params}) (rx/of {:authorized false :redirect-uri "https://idp.example.com/authorize"}))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) st/emit! mock/noop} (errors/on-error (sso-required-error)) (await (async/settle)) (t/is (= [{:command :check-nitrate-sso :params {:team-id "b8f8bb52-8b70-8144-8004-4a5085f0bdc9" :organization-id organization-id - :url workspace-href}}] + :query-string workspace-query-string}}] @rpc-calls))))))) (t/deftest ^:async already-satisfied-organization-sso-retries-the-location @@ -655,7 +660,8 @@ {rp/cmd! (mock/stub (fn [_command _params] (rx/of {:authorized true :reason :sso-satisfied}))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) st/emit! (mock/stub (fn [& emitted] (swap! events into emitted)))} (errors/on-error (sso-required-error)) (await (async/settle)) @@ -669,7 +675,8 @@ {rp/cmd! (mock/stub (fn [_command _params] (rx/of {:authorized false :redirect-uri nil}))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) rt/assign-exception (fn [error] (reset! assigned* error) (ptk/data-event ::assigned error))} @@ -687,7 +694,8 @@ {rp/cmd! (mock/stub (fn [_command _params] (rx/of {:authorized true :reason :no-team-access}))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) rt/assign-exception (fn [error] (reset! assigned* error) (ptk/data-event ::assigned error))} @@ -702,7 +710,8 @@ (await (mock/with-mocks* {rp/cmd! (mock/stub (fn [_command _params] (rx/of {:authorized true}))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) rt/assign-exception (fn [error] (ptk/data-event ::assigned error)) st/async-emit! (fn [& emitted] (swap! events into emitted))} (errors/on-error (sso-required-error)) @@ -718,7 +727,8 @@ {rp/cmd! (mock/stub (fn [_command _params] (swap! rpc-calls inc) (rx/empty))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) rt/assign-exception (fn [error] (reset! assigned* error) (ptk/data-event ::assigned error))} @@ -736,7 +746,8 @@ {rp/cmd! (mock/stub (fn [_command _params] (swap! rpc-calls inc) (rx/empty))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) st/emit! mock/noop} (errors/on-error (sso-required-error)) (errors/on-error (sso-required-error)) @@ -770,6 +781,8 @@ rt/get-current-href (constantly workspace-href) + rt/get-current-query-string + (constantly workspace-query-string) rt/assign-exception (fn [error] @@ -1027,7 +1040,8 @@ (fn [_command _params] (rx/of {:authorized false :redirect-uri "https://idp.example.com/authorize"}))) - rt/get-current-href (constantly workspace-href) + rt/get-current-href (constantly workspace-href) + rt/get-current-query-string (constantly workspace-query-string) st/emit! (mock/stub (fn [& emitted] (swap! events into emitted)))} (errors/flash-persistence (ex-info "SSO required" (sso-required-error))) (await (async/settle)) diff --git a/frontend/test/frontend_tests/plugins/runtime_unavailable_test.cljs b/frontend/test/frontend_tests/plugins/runtime_unavailable_test.cljs new file mode 100644 index 0000000000..1c2d16bc56 --- /dev/null +++ b/frontend/test/frontend_tests/plugins/runtime_unavailable_test.cljs @@ -0,0 +1,57 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.plugins.runtime-unavailable-test + (:require + [app.main.data.plugins :as dp] + [app.main.store :as st] + [app.util.globals :as ug] + [app.util.object :as obj] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.mock :as mock] + [potok.v2.core :as ptk] + [promesa.core :as p])) + +(def ^:private test-manifest + {:plugin-id "test-plugin" + :name "Test plugin" + :description "A plugin used by the runtime availability tests" + :host "https://example.com" + :code ""}) + +(defn- with-load-plugin + "Calls dp/open-plugin! with a mocked st/emit! and the given ɵloadPlugin + value on the global object, returning the emitted events." + [load-plugin-fn] + (let [global ug/global + had-plugin? (obj/contains? global "ɵloadPlugin") + previous (obj/get global "ɵloadPlugin") + emitted (atom [])] + (if (fn? load-plugin-fn) + (obj/set! global "ɵloadPlugin" load-plugin-fn) + (obj/unset! global "ɵloadPlugin")) + (-> (mock/with-mocks* + {st/emit! (fn [event & events] (swap! emitted into (cons event events)))} + (dp/open-plugin! test-manifest nil)) + (p/then (fn [_] @emitted)) + (p/finally (fn [] + (if had-plugin? + (obj/set! global "ɵloadPlugin" previous) + (obj/unset! global "ɵloadPlugin"))))))) + +(t/deftest ^:async load-plugin-notifies-when-runtime-is-unavailable + (let [emitted (await (with-load-plugin nil)) + types (mapv ptk/type emitted)] + (t/is (some #{:app.main.data.notifications/show} types) + "emits a notification when the plugin runtime is unavailable") + (t/is (some #{::dp/remove-current-plugin} types) + "still removes the plugin from the open list"))) + +(t/deftest ^:async load-plugin-does-not-notify-when-runtime-is-available + (let [emitted (await (with-load-plugin (fn [_ _] (p/resolved nil)))) + types (mapv ptk/type emitted)] + (t/is (not-any? #{:app.main.data.notifications/show} types) + "does not emit a notification when the plugin runtime is available"))) diff --git a/frontend/test/frontend_tests/router_test.cljs b/frontend/test/frontend_tests/router_test.cljs index c6f39433dd..10d5713541 100644 --- a/frontend/test/frontend_tests/router_test.cljs +++ b/frontend/test/frontend_tests/router_test.cljs @@ -6,6 +6,7 @@ (ns frontend-tests.router-test (:require + [app.config :as cf] [app.main.router :as rt] [cljs.test :as t :include-macros true])) @@ -43,3 +44,28 @@ (t/is (nil? (rt/match test-routes ""))) (t/is (nil? (rt/match test-routes "?team-id=team-1"))) (t/is (nil? (rt/match test-routes "?screen=nope&team-id=team-1")))) + +(t/deftest token->query-string-drops-the-separator + ;; The history token is `location.search`, so it carries the `?` that + ;; separates it from the path; a query string does not carry it. + (t/is (= "screen=workspace&team-id=t1" + (rt/token->query-string "?screen=workspace&team-id=t1"))) + (t/is (= "" (rt/token->query-string "?"))) + ;; A value already without separator is not touched: no double strip. + (t/is (= "screen=workspace" (rt/token->query-string "screen=workspace"))) + (t/is (= "" (rt/token->query-string ""))) + (t/is (= "" (rt/token->query-string nil)))) + +(t/deftest token->url-joins-the-token-to-the-public-uri + (let [base (str cf/public-uri)] + (t/is (= (str base "?screen=workspace") + (rt/token->url "?screen=workspace"))) + (t/is (= (str base "?screen=workspace") + (rt/token->url "screen=workspace"))) + (t/is (= base (rt/token->url ""))) + (t/is (= base (rt/token->url nil))))) + +(t/deftest get-current-query-string-comes-from-the-current-token + (with-redefs [rt/get-current-token (constantly "?screen=workspace&team-id=t1")] + (t/is (= "screen=workspace&team-id=t1" + (rt/get-current-query-string))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index ef4e8475e4..aa4989db98 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -107,6 +107,7 @@ [frontend-tests.plugins.parser-test] [frontend-tests.plugins.read-only-test] [frontend-tests.plugins.register-test] + [frontend-tests.plugins.runtime-unavailable-test] [frontend-tests.plugins.shape-bugfixes-test] [frontend-tests.plugins.system-events-test] [frontend-tests.plugins.text-test] @@ -328,6 +329,7 @@ 'frontend-tests.plugins.parser-test 'frontend-tests.plugins.read-only-test 'frontend-tests.plugins.register-test + 'frontend-tests.plugins.runtime-unavailable-test 'frontend-tests.plugins.shape-bugfixes-test 'frontend-tests.plugins.system-events-test 'frontend-tests.plugins.text-test diff --git a/frontend/test/frontend_tests/ui/routes_test.cljs b/frontend/test/frontend_tests/ui/routes_test.cljs index 68ef15c5d1..08c2203187 100644 --- a/frontend/test/frontend_tests/ui/routes_test.cljs +++ b/frontend/test/frontend_tests/ui/routes_test.cljs @@ -38,15 +38,17 @@ (fn [command params] (t/is (= :check-nitrate-sso command)) (t/is (= team-id (:team-id params))) + (t/is (= "screen=workspace&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d" + (:query-string params))) (swap! rpc-calls inc) (rx/of {:authorized true}))) st/emit! (mock/stub (fn [& emitted] (swap! events into emitted)))} (fn [done'] - (#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace") + (#'routes/check-sso-and-navigate match true "screen=workspace&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d") (reset! now (ct/plus @now #js {:minutes 4 :seconds 59})) - (#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace") + (#'routes/check-sso-and-navigate match true "screen=workspace&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d") (t/is (= 1 @rpc-calls)) (t/is (= 2 (count @events))) (done')) @@ -67,9 +69,9 @@ (rx/of {:authorized true}))) st/emit! mock/noop} (fn [done'] - (#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace") + (#'routes/check-sso-and-navigate match true "screen=workspace&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d") (reset! now (ct/plus @now #js {:minutes 5})) - (#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace") + (#'routes/check-sso-and-navigate match true "screen=workspace&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d") (t/is (= 2 @rpc-calls)) (done')) done)))) @@ -91,8 +93,8 @@ (fn [& emitted] (swap! events into emitted)))} (fn [done'] - (#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace") - (#'routes/check-sso-and-navigate match true "https://penpot.example.com/#/workspace") + (#'routes/check-sso-and-navigate match true "screen=workspace&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d") + (#'routes/check-sso-and-navigate match true "screen=workspace&team-id=1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d") (t/is (= 2 @rpc-calls)) (t/is (= 2 (count @events))) (done')) diff --git a/frontend/test/frontend_tests/ui/subscription_test.cljs b/frontend/test/frontend_tests/ui/subscription_test.cljs index f85628dd15..8724d47c31 100644 --- a/frontend/test/frontend_tests/ui/subscription_test.cljs +++ b/frontend/test/frontend_tests/ui/subscription_test.cljs @@ -6,7 +6,6 @@ (ns frontend-tests.ui.subscription-test (:require - [app.main.data.nitrate :as dnt] [app.main.ui.dashboard.subscription :as ds] [cljs.test :as t :include-macros true])) @@ -23,45 +22,26 @@ (ds/get-team-plan-type {:subscription {:type owner-plan :status "active"}}))))) -(t/deftest owner-enterprise-plan-is-hidden-for-team-outside-organization - (t/is (false? (ds/show-team-plan? true - {:subscription {:type "enterprise" - :status "active"}} - {:props {:subscription {:type "enterprise" - :status "active"}}})))) - -(t/deftest team-plan-is-visible-for-other-owner-and-organization-cases +(t/deftest team-plan-is-visible-when-owner-has-professional-or-unlimited-plan (doseq [owner-plan ["professional" "unlimited"]] - (t/is (true? (ds/show-team-plan? true - {:subscription {:type owner-plan - :status "active"}} - {:props {:subscription {:type owner-plan - :status "active"}}})))) - (t/is (true? (ds/show-team-plan? true - {:subscription {:type "enterprise" - :status "active"} - :organization {:id "org-1"}} - {:props {:subscription {:type "enterprise" - :status "active"}}}))) - (t/is (true? (ds/show-team-plan? false - {:subscription {:type "enterprise" - :status "active"}} - {:props {:subscription {:type "enterprise" - :status "active"}}})))) + (t/is (true? (ds/show-team-plan? {:subscription {:type owner-plan + :status "active"}}))))) -(t/deftest owner-enterprise-plan-is-detected-even-when-team-subscription-falls-back - (t/is (false? (ds/show-team-plan? true - {:subscription {:type "professional" - :status "active"}} - {:props {:subscription {:type "enterprise" - :status "trialing"}}})))) +(t/deftest team-plan-is-hidden-when-owner-of-team-outside-organization-has-enterprise-plan + ;; The team `:subscription` carries the plan of the owner, so the result + ;; is the same for the owner and for any other member. + (doseq [owner-plan ["enterprise" "nitrate"] + status ["active" "trialing" "past_due"]] + (t/is (false? (ds/show-team-plan? {:subscription {:type owner-plan + :status status}}))))) -(t/deftest nitrate-owner-plan-hides-team-plan-when-team-is-outside-organization - (with-redefs [dnt/is-valid-license? (constantly true)] - (t/is (false? (ds/show-team-plan? true - {:subscription {:type "professional" - :status "active"}} - {:subscription {:type "nitrate" - :status "active"} - :props {:subscription {:type "professional" - :status "active"}}}))))) +(t/deftest team-plan-is-visible-for-team-in-organization-whatever-the-owner-plan + (doseq [owner-plan ["professional" "unlimited" "enterprise" "nitrate"]] + (t/is (true? (ds/show-team-plan? {:subscription {:type owner-plan + :status "active"} + :organization {:id "org-1"}}))))) + +(t/deftest team-plan-is-visible-when-owner-enterprise-plan-is-no-longer-paid + (doseq [status ["unpaid" "canceled"]] + (t/is (true? (ds/show-team-plan? {:subscription {:type "enterprise" + :status status}}))))) diff --git a/frontend/translations/en.po b/frontend/translations/en.po index 2f31fdcd88..97b210489e 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -4966,6 +4966,13 @@ msgstr "Start designing" msgid "options.deleted-token-with-name" msgstr "{%s} token does not exist or has been deleted." +#: src/app/main/data/plugins.cljs:129 +msgid "plugins.runtime-unavailable" +msgstr "" +"Plugins are unavailable in this browser. Something in your environment (for " +"example, an extension such as Tampermonkey) is preventing the plugin " +"runtime from starting. Disable extensions or reload the page to try again." + #: src/app/plugins/utils.cljs:579 msgid "plugins.validation.invalid-value" msgstr "expected %s, got %s (%s)" diff --git a/frontend/translations/es.po b/frontend/translations/es.po index 322a471f76..08a7b56a21 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -5016,6 +5016,13 @@ msgstr "Empezar a diseñar" msgid "options.deleted-token-with-name" msgstr "El token {%s} no existe o ha sido borrado." +#: src/app/main/data/plugins.cljs:129 +msgid "plugins.runtime-unavailable" +msgstr "" +"Los plugins no están disponibles en este navegador. Algo en tu entorno (por " +"ejemplo, una extensión como Tampermonkey) impide que se inicie el entorno de " +"plugins. Desactiva las extensiones o vuelve a cargar la página." + #: src/app/plugins/utils.cljs:579 msgid "plugins.validation.invalid-value" msgstr "esperado %s, obtenido %s (%s)" diff --git a/media-processor/src/services/font.ts b/media-processor/src/services/font.ts index 16e4a0a091..11965ba177 100644 --- a/media-processor/src/services/font.ts +++ b/media-processor/src/services/font.ts @@ -122,9 +122,10 @@ async function fontConvert( // FontForge's own lexer uses doubled single quotes for escaping. const escInput = inputPath.replace(/'/g, "''"); const escOutput = outputPath.replace(/'/g, "''"); - await execCommand("fontforge", ["-lang=ff", "-c", `Open('${escInput}'); Generate('${escOutput}')`], undefined, { - signal, - }); + // CID-keyed fonts (common in CJK) load as several sub-fonts and Generate + // only writes the active one unless they are merged first. + const script = `Open('${escInput}'); if ($cidfontname != ""); CIDFlatten(); endif; Generate('${escOutput}')`; + await execCommand("fontforge", ["-lang=ff", "-c", script], undefined, { signal }); return await readFile(outputPath); } catch (err: unknown) { const error = err as NodeJS.ErrnoException & { killed?: boolean; signal?: string }; diff --git a/media-processor/test/fixtures/font-cid.otf b/media-processor/test/fixtures/font-cid.otf new file mode 100644 index 0000000000..d9fc9d18ca Binary files /dev/null and b/media-processor/test/fixtures/font-cid.otf differ diff --git a/media-processor/test/font.test.ts b/media-processor/test/font.test.ts index cb0a462801..1ab1706c59 100644 --- a/media-processor/test/font.test.ts +++ b/media-processor/test/font.test.ts @@ -11,13 +11,27 @@ let ttfData: Buffer; let otfData: Buffer; let woffData: Buffer; let woff2Data: Buffer; +let cidOtfData: Buffer; + +// Returns numGlyphs from the maxp table of an sfnt font. +function sfntNumGlyphs(data: Buffer): number { + const numTables = data.readUInt16BE(4); + for (let i = 0; i < numTables; i++) { + const rec = 12 + i * 16; + if (data.toString("latin1", rec, rec + 4) === "maxp") { + return data.readUInt16BE(data.readUInt32BE(rec + 8) + 4); + } + } + throw new Error("maxp table not found"); +} beforeAll(async () => { - [ttfData, otfData, woffData, woff2Data] = await Promise.all([ + [ttfData, otfData, woffData, woff2Data, cidOtfData] = await Promise.all([ readFile(join(FIXTURES, "font-1.ttf")), readFile(join(FIXTURES, "font-1.otf")), readFile(join(FIXTURES, "font-1.woff")), readFile(join(FIXTURES, "font-1.woff2")), + readFile(join(FIXTURES, "font-cid.otf")), ]); }); @@ -58,6 +72,13 @@ describe("convertFont", () => { expect(result).toBeInstanceOf(Buffer); expect(result!.length).toBeGreaterThan(0); }); + + it("otf→ttf keeps the glyphs of every CID sub-font", async () => { + // font-cid.otf has 5 glyphs split across two CID sub-fonts + const result = await convertFont(cidOtfData, "font/otf", "font/ttf"); + expect(result!.subarray(0, 4).toString("hex")).toBe("00010000"); + expect(sfntNumGlyphs(result!)).toBeGreaterThanOrEqual(5); + }); }); describe("sourceType=woff", () => { diff --git a/plugins/CHANGELOG.md b/plugins/CHANGELOG.md index 3ee7edcfc4..b86307e031 100644 --- a/plugins/CHANGELOG.md +++ b/plugins/CHANGELOG.md @@ -18,6 +18,7 @@ - **plugins-runtime:** `Shape.componentRoot()` returns the root of the shape's own component tree. It used to return a shape that pointed at nothing when the copy was on another page or file than its main, and threw when the copy's library was not available. - **plugins-runtime:** `LibraryComponent.mainInstance()` returns `null` for a deleted component, instead of a shape with an empty `id` whose `parent` throws. - **plugins-runtime**: `TokenFontFamilies.resolvedValue` and the `fontFamilies` member of `TokenTypographyValue` now keep a family whose name has several words (`Hanken Grotesk`) as one entry, instead of splitting it into an array of its words, and keep names with words the resolver would otherwise read as colors or numbers (`Red Hat Display`, `Press Start 2P`) unchanged. +- **plugins-runtime:** A SES `repairIntrinsics` failure at startup (for example, when a browser extension wraps the native `Temporal` global) no longer leaves the whole app blank. Plugins are skipped for that session, the error is logged, and a telemetry event reports the unavailable runtime; the editor, login and sign-up pages load as usual. Plugins work as before in every other environment. When startup fails, SES has already replaced `Function.prototype.toString` in the host realm before throwing, but no intrinsics are hardened and no sandbox is created. Closes #12924. ## 1.6.0 (2026-11-10) diff --git a/plugins/libs/plugins-runtime/src/index.ts b/plugins/libs/plugins-runtime/src/index.ts index a38203471d..ecb69143b9 100644 --- a/plugins/libs/plugins-runtime/src/index.ts +++ b/plugins/libs/plugins-runtime/src/index.ts @@ -1,6 +1,7 @@ import 'ses'; import './lib/modal/plugin-modal'; +import { bootstrapSES } from './lib/bootstrap.js'; import { ɵloadPlugin, setContextBuilder, @@ -15,19 +16,27 @@ import type { Context } from '@penpot/plugin-types'; console.log('%c[PLUGINS] Loading plugin system', 'color: #008d7c'); -repairIntrinsics({ - evalTaming: 'unsafeEval', - stackFiltering: 'verbose', - errorTaming: 'unsafe', - consoleTaming: 'unsafe', - errorTrapping: 'none', - unhandledRejectionTrapping: 'none', -}); +const sesError = bootstrapSES(); + +if (sesError) { + console.error( + '[PLUGINS] SES bootstrap failed; plugins disabled in this environment.', + sesError, + ); +} + +/** Returns the SES bootstrap error, or null when the runtime can be used. */ +export const getPluginsRuntimeError = (): Error | null => sesError; // eslint-disable-next-line @typescript-eslint/no-explicit-any const globalThisAny$ = globalThis as any; export const initPluginsRuntime = (contextBuilder: (id: string) => Context) => { + if (sesError) { + console.error('[PLUGINS] Skipping plugin runtime initialization.'); + return; + } + try { console.log('%c[PLUGINS] Initialize runtime', 'color: #008d7c'); setContextBuilder(contextBuilder); diff --git a/plugins/libs/plugins-runtime/src/lib/bootstrap.spec.ts b/plugins/libs/plugins-runtime/src/lib/bootstrap.spec.ts new file mode 100644 index 0000000000..d77b3e0803 --- /dev/null +++ b/plugins/libs/plugins-runtime/src/lib/bootstrap.spec.ts @@ -0,0 +1,131 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { bootstrapSES } from './bootstrap.js'; + +// Intentional snapshot of the production options in bootstrap.ts: if they +// change, this test must fail. Do not replace this literal with an import. +const REPAIR_OPTIONS = { + evalTaming: 'unsafeEval', + stackFiltering: 'verbose', + errorTaming: 'unsafe', + consoleTaming: 'unsafe', + errorTrapping: 'none', + unhandledRejectionTrapping: 'none', +}; + +const temporalError = () => + new TypeError('unexpected Temporal __proto__: [object Object]'); + +afterEach(() => { + vi.unstubAllGlobals(); + vi.restoreAllMocks(); + vi.doUnmock('ses'); + vi.doUnmock('./modal/plugin-modal'); + vi.resetModules(); +}); + +describe('bootstrapSES', () => { + it('repairs intrinsics with the production options and reports success', () => { + const repair = vi.fn(); + vi.stubGlobal('repairIntrinsics', repair); + + expect(bootstrapSES()).toBeNull(); + expect(repair).toHaveBeenCalledWith(REPAIR_OPTIONS); + }); + + it('returns the SES error instead of throwing it', () => { + const error = temporalError(); + vi.stubGlobal('repairIntrinsics', () => { + throw error; + }); + + expect(bootstrapSES()).toBe(error); + }); +}); + +describe('plugins runtime entry point', () => { + it('installs the runtime globals when SES bootstrap succeeds', async () => { + const globalAny = globalThis as unknown as Record; + const context = { theme: 'dark' }; + const runtimeKeys = [ + 'ɵcontext', + 'ɵloadPlugin', + 'ɵloadPluginByUrl', + 'ɵunloadPlugin', + ]; + const previous = runtimeKeys.map((key) => globalAny[key]); + + vi.stubGlobal('repairIntrinsics', vi.fn()); + vi.doMock('ses', () => ({})); + // The custom element is registered at module load; mock it so repeated + // entry-point imports do not redefine it in the same jsdom registry. + vi.doMock('./modal/plugin-modal', () => ({})); + vi.resetModules(); + + try { + const runtime = await import('../index.js'); + runtime.initPluginsRuntime(() => context as never); + + expect(runtime.getPluginsRuntimeError()).toBeNull(); + expect(globalAny['ɵcontext']).toBe(context); + expect(typeof globalAny['ɵloadPlugin']).toBe('function'); + expect(typeof globalAny['ɵloadPluginByUrl']).toBe('function'); + expect(typeof globalAny['ɵunloadPlugin']).toBe('function'); + } finally { + for (const [index, key] of runtimeKeys.entries()) { + if (previous[index] === undefined) { + delete globalAny[key]; + } else { + globalAny[key] = previous[index]; + } + } + } + }); + + it('loads with a broken SES bootstrap without installing runtime globals', async () => { + const error = temporalError(); + const globalAny = globalThis as unknown as Record; + const runtimeKeys = [ + 'ɵcontext', + 'ɵloadPlugin', + 'ɵloadPluginByUrl', + 'ɵunloadPlugin', + ]; + const previous = runtimeKeys.map((key) => globalAny[key]); + + vi.spyOn(console, 'error').mockImplementation(() => {}); + vi.stubGlobal('repairIntrinsics', () => { + throw error; + }); + // Keep the stub in place: `import 'ses'` in index.ts would otherwise + // install a new repairIntrinsics global before bootstrap runs. + vi.doMock('ses', () => ({})); + // The custom element is registered at module load; mock it so repeated + // entry-point imports do not redefine it in the same jsdom registry. + vi.doMock('./modal/plugin-modal', () => ({})); + vi.resetModules(); + + try { + for (const key of runtimeKeys) { + delete globalAny[key]; + } + + const runtime = await import('../index.js'); + + expect(runtime.getPluginsRuntimeError()).toBe(error); + expect(() => + runtime.initPluginsRuntime(() => ({}) as never), + ).not.toThrow(); + for (const key of runtimeKeys) { + expect(globalAny[key]).toBeUndefined(); + } + } finally { + for (const [index, key] of runtimeKeys.entries()) { + if (previous[index] === undefined) { + delete globalAny[key]; + } else { + globalAny[key] = previous[index]; + } + } + } + }); +}); diff --git a/plugins/libs/plugins-runtime/src/lib/bootstrap.ts b/plugins/libs/plugins-runtime/src/lib/bootstrap.ts new file mode 100644 index 0000000000..0057f0f413 --- /dev/null +++ b/plugins/libs/plugins-runtime/src/lib/bootstrap.ts @@ -0,0 +1,27 @@ +/** + * Repairs the JavaScript intrinsics needed by the SES sandbox. + * + * SES can throw during this step when the host page already contains a + * `Temporal` global it does not recognize (for example, one wrapped by a + * browser extension). The exception happens at module load time in + * `index.ts`, so an uncaught failure here takes down the whole app. + * + * Returns the caught error so callers can report it and disable plugins, + * instead of propagating it to the bundle loader. + */ +export function bootstrapSES(): Error | null { + try { + repairIntrinsics({ + evalTaming: 'unsafeEval', + stackFiltering: 'verbose', + errorTaming: 'unsafe', + consoleTaming: 'unsafe', + errorTrapping: 'none', + unhandledRejectionTrapping: 'none', + }); + + return null; + } catch (err) { + return err instanceof Error ? err : new Error(String(err)); + } +} diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index fe38624844..239594020e 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -588,6 +588,15 @@ pub extern "C" fn set_shape_hidden(hidden: bool) -> Result<()> { Ok(()) } +#[no_mangle] +#[wasm_error] +pub extern "C" fn set_shape_hide_fill_on_export(hide: bool) -> Result<()> { + with_current_shape_mut!(state, |shape: &mut Shape| { + shape.set_hide_fill_on_export(hide); + }); + Ok(()) +} + #[no_mangle] #[wasm_error] pub extern "C" fn set_shape_corners(r1: f32, r2: f32, r3: f32, r4: f32) -> Result<()> { @@ -775,7 +784,8 @@ pub extern "C" fn get_shape_extrect(a: u32, b: u32, c: u32, d: u32) -> Result<*m /// Raster image via the GPU surface. Returns `[len][width][height][bytes]` /// (LE). `format` selects the encoder: 0 = PNG, 1 = JPEG, 2 = WEBP (see -/// `RasterFormat`). +/// `RasterFormat`). `export` drops the root's fills when it hides them on +/// export; thumbnails keep them. #[no_mangle] #[wasm_error] pub extern "C" fn render_shape_pixels( @@ -785,6 +795,7 @@ pub extern "C" fn render_shape_pixels( d: u32, scale: f32, format: u32, + export: bool, ) -> Result<*mut u8> { let id = uuid_from_u32_quartet(a, b, c, d); @@ -796,7 +807,7 @@ pub extern "C" fn render_shape_pixels( with_state!(state, { let (data, width, height) = - state.render_shape_pixels(&id, scale, performance::get_time(), format)?; + state.render_shape_pixels(&id, scale, performance::get_time(), format, export)?; let len = data.len() as u32; let mut buf = Vec::with_capacity(4 + data.len()); diff --git a/render-wasm/src/render/pdf.rs b/render-wasm/src/render/pdf.rs index 746b26242d..29ddd4f863 100644 --- a/render-wasm/src/render/pdf.rs +++ b/render-wasm/src/render/pdf.rs @@ -3,7 +3,7 @@ use std::mem::ManuallyDrop; use skia_safe as skia; use crate::error::{Error, Result}; -use crate::state::ShapesPoolRef; +use crate::state::{ShapesPoolMutRef, ShapesPoolRef}; use crate::uuid::Uuid; use super::vector; @@ -143,7 +143,7 @@ pub fn export_abort(slot: &mut Option) { pub fn render_to_pdf( shared: &mut RenderResources, id: &Uuid, - tree: ShapesPoolRef, + tree: ShapesPoolMutRef, ) -> Result> { render_to_pdf_multi(shared, &[*id], tree) } @@ -154,7 +154,7 @@ pub fn render_to_pdf( pub fn render_to_pdf_multi( shared: &mut RenderResources, ids: &[Uuid], - tree: ShapesPoolRef, + tree: ShapesPoolMutRef, ) -> Result> { if ids.is_empty() { return Err(Error::CriticalError( @@ -164,7 +164,9 @@ pub fn render_to_pdf_multi( let mut session = None; export_begin(&mut session)?; for id in ids { - if let Err(err) = export_add_page(&mut session, shared, id, tree) { + let page = + tree.with_export_root(id, |tree| export_add_page(&mut session, shared, id, tree)); + if let Err(err) = page { export_abort(&mut session); return Err(err); } @@ -234,7 +236,7 @@ mod tests { let mut resources = RenderResources::try_new_headless().expect("headless resources"); let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); - let pdf = render_to_pdf_multi(&mut resources, &[a, b], &pool).expect("pdf"); + let pdf = render_to_pdf_multi(&mut resources, &[a, b], &mut pool).expect("pdf"); assert!(pdf.starts_with(b"%PDF"), "missing PDF header"); assert_eq!(page_count(&pdf), 2, "expected two pages"); } @@ -270,11 +272,11 @@ mod tests { #[test] fn empty_id_list_is_an_error() { - let pool = ShapesPool::new(); + let mut pool = ShapesPool::new(); let mut resources = RenderResources::try_new_headless().expect("headless resources"); let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); - let err = render_to_pdf_multi(&mut resources, &[], &pool).expect_err("empty"); + let err = render_to_pdf_multi(&mut resources, &[], &mut pool).expect_err("empty"); assert!( matches!(err, Error::CriticalError(_)), "empty ids must fail critically: {err:?}" @@ -295,7 +297,7 @@ mod tests { let mut resources = RenderResources::try_new_headless().expect("headless resources"); let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); - let pdf = render_to_pdf(&mut resources, &id, &pool).expect("pdf"); + let pdf = render_to_pdf(&mut resources, &id, &mut pool).expect("pdf"); assert!(pdf.starts_with(b"%PDF")); assert_eq!(page_count(&pdf), 1); } diff --git a/render-wasm/src/render/pdf/to_unicode.rs b/render-wasm/src/render/pdf/to_unicode.rs index ffb36f233e..412e6aea85 100644 --- a/render-wasm/src/render/pdf/to_unicode.rs +++ b/render-wasm/src/render/pdf/to_unicode.rs @@ -781,7 +781,7 @@ mod tests { .add(family, LIGATURE_FONT, false, false) .expect("ligature font"); let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); - let pdf = super::super::render_to_pdf(&mut resources, &id, &pool).expect("pdf"); + let pdf = super::super::render_to_pdf(&mut resources, &id, &mut pool).expect("pdf"); let trailer = parse_xref(&pdf).expect("xref").trailer; assert!( diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index e119d133c7..111745cbae 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -191,6 +191,7 @@ pub struct Shape { pub background_blur: Option, pub opacity: f32, pub hidden: bool, + pub hide_fill_on_export: bool, pub svg: Option, pub svg_attrs: Option, pub shadows: Vec, @@ -295,6 +296,7 @@ impl Shape { vertical_align: VerticalAlign::Top, opacity: 1., hidden: false, + hide_fill_on_export: false, blur: None, background_blur: None, svg: None, @@ -466,6 +468,10 @@ impl Shape { self.hidden = value; } + pub fn set_hide_fill_on_export(&mut self, value: bool) { + self.hide_fill_on_export = value; + } + pub fn svg_transform(&self) -> Option { self.svg_transform } diff --git a/render-wasm/src/state.rs b/render-wasm/src/state.rs index 97296058b0..eadfd7616f 100644 --- a/render-wasm/src/state.rs +++ b/render-wasm/src/state.rs @@ -98,16 +98,24 @@ impl State { scale: f32, timestamp: i32, format: RasterFormat, + export: bool, ) -> Result<(Vec, i32, i32)> { - get_render_state().render_shape_pixels(id, &self.shapes, scale, timestamp, format) + let render = |tree: &ShapesPool| { + get_render_state().render_shape_pixels(id, tree, scale, timestamp, format) + }; + if export { + self.shapes.with_export_root(id, render) + } else { + render(&self.shapes) + } } pub fn render_shape_pdf(&mut self, id: &Uuid) -> Result> { - crate::render::pdf::render_to_pdf(get_resources(), id, &self.shapes) + crate::render::pdf::render_to_pdf(get_resources(), id, &mut self.shapes) } pub fn render_shapes_pdf(&mut self, ids: &[Uuid]) -> Result> { - crate::render::pdf::render_to_pdf_multi(get_resources(), ids, &self.shapes) + crate::render::pdf::render_to_pdf_multi(get_resources(), ids, &mut self.shapes) } pub fn pdf_export_begin(&mut self) -> Result<()> { @@ -115,7 +123,10 @@ impl State { } pub fn pdf_export_add_page(&mut self, id: &Uuid) -> Result<()> { - crate::render::pdf::export_add_page(&mut self.pdf_export, get_resources(), id, &self.shapes) + let session = &mut self.pdf_export; + self.shapes.with_export_root(id, |tree| { + crate::render::pdf::export_add_page(session, get_resources(), id, tree) + }) } pub fn pdf_export_finish(&mut self) -> Result> { @@ -127,7 +138,9 @@ impl State { } pub fn render_shape_svg(&mut self, id: &Uuid, scale: f32) -> Result> { - crate::render::svg::render_to_svg(get_resources(), id, &self.shapes, scale) + self.shapes.with_export_root(id, |tree| { + crate::render::svg::render_to_svg(get_resources(), id, tree, scale) + }) } pub fn render_shapes_svg(&mut self, ids: &[Uuid], scale: f32) -> Result> { @@ -142,7 +155,9 @@ impl State { scale: f32, format: RasterFormat, ) -> Result<(Vec, i32, i32)> { - crate::render::raster::render_to_raster(get_resources(), id, &self.shapes, scale, format) + self.shapes.with_export_root(id, |tree| { + crate::render::raster::render_to_raster(get_resources(), id, tree, scale, format) + }) } /// Distinct font families used by the (visible) subtree rooted at `id`, in diff --git a/render-wasm/src/state/shapes_pool.rs b/render-wasm/src/state/shapes_pool.rs index cd9542068a..e4ee3d22c3 100644 --- a/render-wasm/src/state/shapes_pool.rs +++ b/render-wasm/src/state/shapes_pool.rs @@ -142,6 +142,24 @@ impl ShapesPoolImpl { self.uuid_to_idx.contains_key(id) } + /// Runs `render` with the fills of `id` removed when it hides them on + /// export. Only the exported board applies the flag; nested boards keep + /// their fills. + pub fn with_export_root(&mut self, id: &Uuid, render: impl FnOnce(&Self) -> T) -> T { + let hide = self.get(id).is_some_and(|shape| shape.hide_fill_on_export); + let fills = if hide { + self.get_mut(id) + .map(|shape| std::mem::take(&mut shape.fills)) + } else { + None + }; + let result = render(self); + if let (Some(fills), Some(shape)) = (fills, self.get_mut(id)) { + shape.fills = fills; + } + result + } + pub fn get_mut(&mut self, id: &Uuid) -> Option<&mut Shape> { let idx = *self.uuid_to_idx.get(id)?; // The caller may change the shape, so its modified copy goes stale. @@ -611,6 +629,24 @@ mod tests { (pool, parent, child) } + #[test] + fn with_export_root_hides_only_the_root_fills() { + let (mut pool, parent, child) = pool_with_parent_and_child(); + let fills = vec![shapes::Fill::Solid(shapes::SolidColor(skia::Color::RED))]; + for id in [parent, child] { + let shape = pool.get_mut(&id).expect("shape"); + shape.set_fills(fills.clone()); + shape.set_hide_fill_on_export(true); + } + + pool.with_export_root(&parent, |tree| { + assert!(tree.get(&parent).expect("parent").fills.is_empty()); + assert_eq!(tree.get(&child).expect("child").fills, fills); + }); + + assert_eq!(pool.get(&parent).expect("parent").fills, fills); + } + #[test] fn set_modifiers_passes_a_move_down_to_children() { let (mut pool, parent, child) = pool_with_parent_and_child(); diff --git a/render-wasm/src/wasm/shapes/base_props.rs b/render-wasm/src/wasm/shapes/base_props.rs index 9ab65f78fe..826876ec75 100644 --- a/render-wasm/src/wasm/shapes/base_props.rs +++ b/render-wasm/src/wasm/shapes/base_props.rs @@ -11,6 +11,7 @@ use super::RawShapeType; const FLAG_CLIP_CONTENT: u8 = 0b0000_0001; const FLAG_HIDDEN: u8 = 0b0000_0010; +const FLAG_HIDE_FILL_ON_EXPORT: u8 = 0b0000_0100; pub(crate) const RAW_BASE_PROPS_SIZE: usize = std::mem::size_of::(); @@ -78,6 +79,10 @@ impl RawBasePropsData { (self.flags & FLAG_HIDDEN) != 0 } + fn hide_fill_on_export(&self) -> bool { + (self.flags & FLAG_HIDE_FILL_ON_EXPORT) != 0 + } + fn blend_mode(&self) -> BlendMode { RawBlendMode::from(self.blend_mode).into() } @@ -112,6 +117,7 @@ pub(crate) fn apply_base_props(raw: &RawBasePropsData) -> Result<()> { shape.set_shape_type(shape_type.into()); shape.set_clip(raw.clip_content()); shape.set_hidden(raw.hidden()); + shape.set_hide_fill_on_export(raw.hide_fill_on_export()); shape.set_blend_mode(raw.blend_mode()); shape.set_opacity(raw.opacity); shape.set_constraint_h(raw.constraint_h()); diff --git a/tracebitmap.txt b/tracebitmap.txt deleted file mode 100644 index a14ad92425..0000000000 --- a/tracebitmap.txt +++ /dev/null @@ -1,28 +0,0 @@ -How to trace a bitmap to svg in multiple colors: - - -Dependencies: imagemagick, netpbm, potrace - -Generate the PPM format from PNG: - - convert download.png download.ppm - -Generate a mask file for each color of the file: - - ppmcolormask "#184d47" download.ppm > download-mask1.pbm - ppmcolormask "#66462C" download.ppm > download-mask2.pbm - -Then trace the bitmap (bitmap -> svg): - - potrace --flat -b svg download-mask1.pbm -o download1.svg - potrace --flat -b svg download-mask2.pbm -o download2.svg - -Extract a list of colors from an image: - - convert download.png +dither -colors 10 -unique-colors txt: - - -Articles: - -- http://confluence.concord.org/display/CCTR/Removing%20Antialiasing.html -- http://confluence.concord.org/display/CCTR/Tracing%20Color%20Raster%20Images.html