* Set lock_timeout on request transactions
Lock waits in API transactions now fail after 5s with a 503 instead of
holding a pool connection until the statement timeout.
* Map query timeouts to request_timeout in batch sub-requests
* fix: Fail fast on Redis for cache and rate limits, lock migrations in Postgres
* fix: Use the fail-fast Redis client for locks
* fix: Require a database pool of at least two connections
* chore: Rename cache Redis client to ephemeralClient
* chore: Prune redundant comments and normalize lint directives
Removes comments that restate the following line and stale TODOs, drops
the passive event listener feature check now that all supported browsers
support it, and renames eslint directives to oxlint.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH
* fix: Evaluate restricted team domains at validation time
The restricted email provider list was captured when the model loaded, so
it could not follow the hosting environment and the tests that covered it
never reached the model.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH
* chore: Replace type suppressions with accurate types
Adds a Concrete helper type for registries of abstract classes, declares
missing Sequelize and refractor typings, uses public ProseMirror APIs, a
typed mock context helper in middleware tests, and a StaticLink component
for sidebar rows being edited.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH
---------
Co-authored-by: Claude <noreply@anthropic.com>
* feat: Support OAuth Client ID Metadata Documents for MCP clients
MCP clients can send an https URL as their client_id. The server fetches
the metadata document at that URL and caches one team-less client row per
URL, so MCP works on the root domain without dynamic registration.
Closes#14010
* fix: Address review findings for client ID metadata documents
- Only count unexpired tokens when cleaning up metadata document clients
- Reject backslashes, whitespace and trailing host dots in client_id URLs
- Strip trailing dots before installation host comparisons
- Type clientSecret as nullable
* fix: Keep OAuth clients with pending authorization codes during cleanup
* refactor: Simplify client ID metadata document handling
- Gate metadata document fetches on the MCP preference inside the helper
- Add isMCPClient getter and a shared client ID prefix constant
- Enforce the team/metadata document CHECK in both directions
- Index oauthClientId on OAuth token and code tables
- Reuse parseDomain, clamp and time helpers
* revert: Remove pending authorization code check from OAuth client cleanup
* fix: Block all special-use addresses when fetching client metadata documents
ALLOWED_PRIVATE_IP_ADDRESSES no longer applies to client ID metadata
document requests (draft -02 section 8.6). The fetch wrapper takes an
allowIPAddressList option to override the environment list.
* fix: Reject empty userinfo in client metadata document URLs
https://@host and https://:@host have a userinfo component that the URL
parser drops, so check the raw authority (draft -02 section 3).
* fix: Accept a root path in client metadata document URLs
Draft -02 section 3 marks a root path as NOT RECOMMENDED, not invalid, so
https://example.com/ is a valid client_id. A URL with no path is still
rejected.
* fix: Reject client metadata documents with private key material
A jwks key with private or symmetric members (d, p, q, dp, dq, qi, oth,
k) makes the document invalid, so it is not cached (draft -02 section 4.1).
Public keys are still ignored.
* fix: Respect no-cache, no-store and Age for client metadata documents
no-store and no-cache now expire the cached document at once, so it is
fetched again before the next authorization. Freshness from max-age or
Expires (measured from Date) is reduced by Age before clamping (draft -02
section 5.2).
* feat: Copy DCR and CIMD client logos to file storage in the background
logo_uri was rendered directly on the consent screen, which lets the logo
host track users and change the image at any time (draft -02 sections 8.8
and 9.2). A background task now fetches the logo with the strict fetch
rules, accepts only PNG, JPEG, GIF and WebP by their leading bytes, and
stores it content addressed in the public bucket. Requests never wait for
the upload.
* fix: Match loopback redirect URIs on any port
Native apps such as Claude Code register http://localhost/callback and
then listen on a free port, so an exact match rejected them. Loopback
redirect URIs now match on any port (RFC 8252 section 7.3); the scheme,
host and path must still match, and other URIs still match exactly.
* fix: Only break the redirect URL mid-word on the consent screen
break-all on the whole notice split words such as "continue" in the
loopback warning. It now applies only to the redirect URL.
* chore: Shorten the loopback warning on the consent screen
* fix: Show the punycode host of client metadata documents on the consent screen
A lookalike Unicode host was shown as written. Hostnames now come from a
shared getHostname helper that returns the punycode form.
* refactor: Move image type detection to ImageHelper and use it for avatars
Avatars and integration logos copied from a URL are now stored only if
they are PNG, JPEG, GIF or WebP, with the content type detected from the
file data rather than the remote host.
* fix: Address review findings for OAuth client logos and metadata caching
- Count the time since the Date header as age when caching metadata
documents, so a document is not cached past its Expires time
- Log logo scheduling failures instead of failing registration requests
- Run dynamic client registration in a transaction
- Delete copied logos from storage when no client uses them
* fix: Store one logo per OAuth client and return a distinct metadata document error
- Logos are stored per client, so a file is only deleted when its own
client stops using it. This removes the reference count and its race.
- A metadata document that cannot be fetched or is invalid now returns
invalid_client_metadata with the reason, not a 404. Network errors
are only logged.
- Move scheduleOptional to BaseTask so that any task can use it.
* fix: Revision and document attribution to the wrong user in collaborative edits
Capture the editing user synchronously with each Yjs update and persist an
ordered, sequenced collaborator set in Redis so that snapshots and revisions
are attributed to the user who actually made the change.
* fix: Do not fail a committed save when the update event cannot be scheduled
* fix: Consume collaborator attribution when a revision is skipped as identical
* refactor: Add zlatestWithSequence to the Redis adapter
Adds a server-shared Vitest project with isolate disabled and moves every
server test into it by default. Files that mock or reset modules opt out
with a header marker and stay in the isolated project.
Restores env values that leaked between files: the test URL is reset after
each file, and tests that mutate env restore the previous value. The test
server no longer closes the shared sequelize pool.
* fix: Make count-based limit checks reliable under concurrency
Limits enforced by counting rows before an insert could be exceeded by
requests that ran at the same time, as each one read the same count
before any of them had written. Add a LockHelper that takes a Postgres
advisory lock for the remainder of the transaction, and use it to
serialize the check for webhook subscriptions, team domains, share
subscriptions and pins. The counts now also run on the transaction so
they include rows written earlier in the same request.
* Remove excessive tests
* Also guard simultaneous row removal
The file name added to Content-Disposition was written into the header
unencoded whenever it fell inside Latin-1, so é, ü and ñ survived as raw
high bytes. Request signing hashes header values as UTF-8 while Node
writes them as Latin-1, giving the storage provider different bytes to
those that were signed, and the upload was rejected.
Pass an ASCII-only fallback so any such name is carried in the RFC 5987
filename* parameter instead. Browsers still read the original name from
there, so the extension is preserved.
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix: Stabilize signed attachment urls so caches can be reused
Every call to getSignedUrl produced a unique signature, so the same
attachment was downloaded again for each URL. On a public share the
first image was fetched twice at full size: once by the preload scanner
from the server-rendered HTML, and again when the client rendered the
document from the API response.
- Round the signing timestamp down to a window so repeated signatures of
the same key are identical. The window is capped at half the requested
lifetime, so a URL is always valid for at least half of expiresIn and
never outlives the lifetime it had before.
- Apply to all three signing paths: the S3 presigner, CloudFront, and
the JWT used by local storage.
- Sign the share page HTML with the same lifetime the API presenter
uses, so the two agree.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* test: Freeze time in signed url tests
The suites read the wall clock, so two calls could straddle a signing
window boundary and fail intermittently. Freeze the clock at a time
offset from a boundary, then advance it explicitly to cover both cases:
within a window the signature holds, across a boundary it rotates.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
* feat: Re-authorize collaboration connections when access changes
Collaboration connections are authorized once, when the websocket is first
established, so a connection outlives changes to the access it was granted.
Add a processor that publishes access changes to the collaboration servers
over Redis. Each server closes the connections it holds within the scope of
the change and clients reconnect, at which point they are authorized as
normal. Authorization stays in one place, the published scope only has to
cover the connections that may be affected.
The API update subscriber now shares a single Redis connection rather than
opening its own.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* refactor: Log an access change once rather than per connection
Every closed connection is already logged by LoggerExtension, so record the
reason and the number of connections affected once instead. The count also
makes a scope that matches nothing visible.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Include the file name in the Content-Disposition header set on S3 objects
at upload time, so browsers keep the uploaded extension instead of
deriving one from the content type.
* fix: Sanitize user-provided name when building export storage keys
Export keys interpolated a document title or collection name straight
into the storage path, so a name containing path separators could move
the archive out of the uploads bucket and into public, where files.get
serves content without authorization.
Sanitize the name down to a single path segment, dedupe the duplicated
key builder in collectionExporter, and reject traversal in LocalStorage
where resolve-path only guards the root, not movement between buckets.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* chore: Complete JSDoc on export key helpers
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix: Friendly error and retry for duplicate team subdomains
Picking a subdomain already in use in settings surfaced the raw unique
constraint violation as a 500, and concurrent signups could race between
the availability check and the insert.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* test: Use setCloudHosted helper rather than spying on env
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* refactor: Move retryOnUniqueConstraintError to storage/database
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix: Statement timeouts reported as 404
A Postgres statement_timeout was set to exactly REQUEST_TIMEOUT, so the
socket was destroyed by the server timeout at the same moment the query
was canceled. The error handler could no longer write a response, leaving
Koa's placeholder 404 status for logging and tracing to report, while the
client saw a reset connection.
The statement timeout is now set below the request timeout so the response
can be delivered, canceled queries return a 503, and the status code is
corrected even when the response can no longer be written.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix: Statement timeout floor could exceed the request timeout
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
* perf: Add team-scoped composite index for document search
The global tsvector GIN index scanned matches across all teams before
filtering by teamId. A composite (teamId, searchVector) index via
btree_gin confines the scan to a single team's documents.
* fix: Bound search queries with the request statement timeout
Search queries ran outside a transaction and so never received the
SET LOCAL statement_timeout applied to request-handling processes,
leaving cancelled requests running unbounded on the database.
* Bind queries to read-only replica where available
* Remove readOnly option for now
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
* fix: Temporary directory left behind after S3 storage file handle
* Remove unused ZipHelper.extract
Use pipeline everywhere instead of manually wiring streams
* fix: Cleanup failure should not mask original error
Removing the temporary directory is best-effort, log the failure and
propagate the underlying error instead.
---------
Co-authored-by: Claude <noreply@anthropic.com>
* Reduce server memory usage and add container memory best practices
Profiling a full-service boot showed ~300MB RSS per service process, much
of it client-only dependencies pulled into the server module graph, and no
heap limits applied in containers:
- Import date-fns locales individually rather than via the locale index,
which loaded all ~90 locales into every process
- Load @sentry/react dynamically in insertFiles so the browser Sentry SDK
stays out of the server-side editor graph
- Replace class-validator isHexColor with the existing validateColorHex
in the Highlight mark
- Extract icon names into a standalone IconNames module so server-side
schema validation no longer imports Font Awesome icon packs, with a
test to keep it in sync with IconLibrary
- Require S3Storage lazily so the AWS SDK and native CRT binding are only
loaded when S3 file storage is configured
- Default each forked service process to a V8 heap limit derived from the
cgroup memory constraint, preventing several processes from together
committing more memory than the container allows
- Cap glibc malloc arenas in the Docker image to reduce resident memory
fragmentation in multi-threaded processes
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016YMX5ReGHyxrR4ZE5ZbaXa
* Require both file storage backends lazily for consistency
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016YMX5ReGHyxrR4ZE5ZbaXa
* Load AWS SDK via dynamic imports compatible with test environment
Requiring the storage TypeScript modules lazily broke under Vitest, which
cannot resolve require() of source files and does not apply vi.mock to
bare requires. Move the laziness into S3Storage instead: the module is
imported statically, and the AWS SDK packages are loaded with dynamic
imports on first use, including deferred creation of the S3 client.
Also address review feedback: handle rejection of the dynamic Sentry
import, and warn when the minimum per-process heap limit exceeds the
memory budget of the container.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016YMX5ReGHyxrR4ZE5ZbaXa
* Consolidate S3 SDK loading into a single lazy accessor
The commands are loaded together with the client in getS3() rather than
as separate dynamic imports per method. The global S3Client test mock is
now a class, as a mock function is not constructable when reached through
a dynamic import.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016YMX5ReGHyxrR4ZE5ZbaXa
---------
Co-authored-by: Claude <noreply@anthropic.com>
* feat: support presigned PUT uploads for S3-compatible storage
Add `AWS_S3_UPLOAD_METHOD` config ("post" default / "put") so providers
that do not implement presigned POST (e.g. Cloudflare R2) can use
presigned PUT instead.
- BaseStorage.getPresignedPut() returns undefined by default; S3Storage
overrides with PutObjectCommand + getSignedUrl.
- Content-Length is signed into the presigned PUT URL so S3 rejects
uploads that do not match the declared size.
- API route and MCP tool branch on the config: only one upload method
is returned per request.
- Frontend uses existing presignedPutUrl presence check — unchanged.
- When put is configured but unsupported (e.g. LocalStorage), a clear
InvalidRequestError is returned.
Defaults to "post" — zero behavior change for existing deployments.
* refactor: return explicit upload mode instead of inferring from fields
Address review feedback from @tommoor: the client should not need to
infer the upload method from the presence of presigned-PUT-specific
fields. The API now returns an explicit `mode` ("put" | "post"), and the
PUT branch uses generic `url` / `headers` fields so the client has no
special knowledge of "presigned puts".
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* refactor: use this.getBucket() in presigned post/put methods
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
* feat(s3): add optional CloudFront support for signed attachment URLs
Serve attachment downloads through CloudFront when configured, while keeping
uploads on the direct S3 endpoint. Signed URLs require a key pair; missing or
invalid signing config falls back to S3 presigned URLs instead of unsigned CDN
links. CloudFront secrets are kept server-side only.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Update .env.sample
Co-authored-by: Tom Moor <tom.moor@gmail.com>
* Update server/env.ts
Co-authored-by: Tom Moor <tom.moor@gmail.com>
* fix(s3): address CloudFront PR review feedback"
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Tom Moor <tom.moor@gmail.com>
* perf: lazy-load jsdom in DocumentHelper to keep it off startup path
DocumentHelper is imported by the Document and Collection models, so its
eager `import { JSDOM } from "jsdom"` pulled jsdom (~140ms) into every
server process at boot. jsdom is only needed by toEmailDiff for HTML
export, so load it lazily there — matching the existing pattern in the
sibling ProsemirrorHelper.toHTML.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* perf: defer AWS CRT signer load out of the startup path
S3Storage had a top-level `import "@aws-sdk/signature-v4-crt"` side effect
that loaded the native CRT binding (~140ms) whenever the module was
imported — which happens at boot via the Attachment model in every
process, even for local-storage deployments that never construct S3Storage.
Move the import into the S3Storage constructor so the native binding only
loads when S3 storage is actually instantiated, keeping it off the startup
path for local-storage deployments.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* perf: load dd-trace only when APM is enabled
tracer.ts imported dd-trace unconditionally, so the library (which patches
Node internals on require, ~35-80ms) loaded at startup in every process —
including the common self-hosted case where DD_API_KEY is unset and APM
does nothing. tracer.ts is the first import in server/index.ts, so this
cost is fully serial on the boot path.
Require dd-trace only when DD_API_KEY is set; otherwise fall back to a
no-op tracer covering the surface used by callers (scope/startSpan and a
no-op span), so tracing.ts and the named helpers work unchanged. The
dd-trace/ext/tags import used by tracing.ts is a standalone constants
module and does not pull in the full tracer.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
* fix: Apply Postgres statement_timeout on request-handling processes
Sets `statement_timeout` to REQUEST_TIMEOUT on the Sequelize connection
pool when the process handles HTTP requests (web/api/collaboration/
websockets/admin) and does not also run worker/cron. Prevents a single
runaway query from saturating the shared Postgres instance and cascading
into timeouts across all endpoints.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* Drop dead `api` service check
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* Only apply statement_timeout in forked cluster workers
Skips the timeout in the master process so startup migrations driven
from `checkPendingMigrations` are not cancelled mid-execution.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
* wip
* Remove obsolete snapshots
* simplify
* chore(test): Convert mocks to TypeScript and tighten fetch mock types
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* Remove unneccessary patches
* Migrate to msw instead of custom fetch mock
* Address PR review comments
- Split chained vi.useFakeTimers().setSystemTime() into separate calls.
- Switch test setup to dynamic imports so EventEmitter.defaultMaxListeners
assignment runs before module init (static imports were hoisted above it).
- Drop redundant NODE_ENV guard in monkeyPatchSequelizeErrorsForJest; its
sole caller already gates on env.isTest.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
* chore: Replace lodash with es-toolkit
Migrate all direct lodash imports to es-toolkit/compat for a smaller,
faster, lodash-compatible utility library. Transitive lodash usage from
other packages remains unchanged.
* fix: Restore isPlainObject semantics in CanCan policy
The lodash migration aliased `isObject` to `lodash/isPlainObject` and
the codemod incorrectly mapped the local name to es-toolkit's `isObject`,
which also returns true for arrays and functions. This caused condition
objects in policy definitions to be skipped, breaking authorization
checks across the codebase.
* fix: Restore unicode-aware length counting in validators
es-toolkit/compat's size() returns string.length, while lodash's _.size()
counts unicode code points. Switch to [...value].length to preserve the
previous behavior so multi-byte characters like emoji count as one.
* chore: promote no-explicit-any from warn to error and resolve violations
Upgrades the oxlint rule severity and removes all 40 existing
`no-explicit-any` warnings across the codebase. Most call sites gained
proper types (SharedEditor refs, JSONNode/JSONMark for ProseMirror JSON
walking, DocumentsStore, dd-trace `Span` parameter inference, prosemirror
Fragment public API in place of internal `(fragment as any).content`).
A few load-bearing `any` uses were preserved with scoped disable
comments where changing the type would cascade widely (Sequelize JSONB
columns on `Event`, the `withTracing` higher-order function generic,
`Extension.options` consumed by many subclasses, dd-trace's `req`
patching).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Replaces `as any` casts when constructing OAuth2Server Request/Response
with explicit objects containing the fields the library actually
consumes, and switches BaseStorage's manual header spread to a
node-fetch `Headers` instance to avoid the no-misused-spread warning.
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
* fix: Suspended users should not be included in cached member count for groups
* fix: Defer CounterCache hook registration until model is initialized
The previous test-only no-op hid a timing bug where setImmediate could
fire before the Sequelize instance had registered the related model,
causing "Model not initialized" failures. Poll until the model is
ready, and unref the pending immediate so it does not keep the event
loop alive in environments where the database is never initialized.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* perf: Reduce overhead of group member count invalidation
Select only the groupId column with raw queries and de-duplicate before
issuing Redis deletes, avoiding loading full GroupUser rows into memory
when a user belongs to many groups.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* chore: unref Redis healthcheck interval
Don't keep the Node event loop alive solely for the periodic ping; the
event loop should drain on its own when the application is shutting
down or a Jest worker is finishing.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: Centralize counter cache key in RedisPrefixHelper
Avoid duplicating the "count:<Model>:<relation>:<id>" string between
the CounterCache decorator and the User suspension hook by routing
both through a single getCounterCacheKey helper.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix: Walk to parent transaction when scheduling cache invalidation
Nested savepoints commit independently of their outer transaction, so
afterCommit callbacks attached to the inner transaction may run after
the outer rolls back, or never run at all. Match the pattern used in
Collection, Event, and base/Model and walk to the parent transaction
so the cache invalidation fires after the real outer commit.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
* chore: resolve remaining unbound-method lint warnings
Apply targeted fixes per call pattern: arrow wrappers when passing a
method as a callback, arrow-function class fields when the method
doesn't depend on `this`, and `.bind()` when capturing for later
invocation.
Also replaces the rfc6902 hasOwnProperty re-export with a small wrapper
function so callers don't reference an unbound prototype method.
* chore: memoize history.goBack callbacks
Stable identity prevents Button re-renders and avoids re-subscribing
the global keydown handler in RegisterKeyDown when the parent renders.