Tenants
Every row that holds data of a customer belongs to one tenant, and a Prisma extension narrows each query to the tenant of the code that runs now. Services read and write as before and never pass a tenant around. The code lives in src/lib/tenant/ and src/services/tenant/, the user side in Tenants, Shared Databases and Shared Destinations and Channels.
The Model
| Model or field | Holds |
|---|---|
Tenant | A customer, with a unique name. HOME_TENANT_ID = "default" in src/lib/tenant/home.ts is the home tenant, which the migration creates as Default and which is never deleted |
Tenant.sharesWithCustomers | Whether the tenant gives databases of its servers, its destinations and its channels to other tenants, a tenant of the MSP. False by default, true for the home tenant from the migration 20261016120000_sharing_tenants, renamed from sharesDatabases in 20261017090000_shared_connections. sharesWithCustomers(tenantId) in src/services/tenant/shared-servers.ts reads it, see Shared Databases and Shared Destinations and Channels |
Tenant.addsDatabases, addsDirectorySources, addsDestinations, addsChannels, addsApiKeys | What the tenant adds itself, true by default, see Tenant Allowances |
Tenant.blocksLocalNetwork, allowedNetworks | Whether its connections reach the local network of the server, true for a new tenant, and the CIDR ranges they reach anyway, see The Local Network |
TenantMembership | { userId, tenantId, groupId }, the group (role) of an account in one tenant. The group is one of that tenant |
TenantInvitation | A link into a tenant with a group, kept as the SHA-256 of its token |
User.platformRole | "ADMIN" (PLATFORM_ADMIN_ROLE) for a platform admin, else null |
Group.fullAccess | The group with every permission, which cannot be edited or deleted. Nothing checks a group by its name |
Session.activeTenantId | The tenant a session picked with the switcher |
tenantId on every model in TENANT_MODELS | The tenant of the row. Its default "default" only fills the rows of the migration |
DatabaseAssignment, DatabaseAssignmentRule | The databases of a shared server given to another tenant. tenantId names the customer without a relation, null on an assignment that took a database back, and a rule ends with validUntil, see Shared Databases |
ConnectionShare, ConnectionShareTenant, ChannelRecipients | A destination or a channel shared with other tenants, the customers it names and the recipients a customer names for a shared email channel, see Shared Destinations and Channels |
Users, sessions and the system settings belong to no tenant. A group belongs to one tenant, Group is in TENANT_MODELS. A sign-in provider belongs to the platform or names one tenant in SsoProvider.tenantId, outside the filter. TenantMembership and TenantInvitation name a tenant too but stay out of TENANT_MODELS, the platform's to read through membershipService and invitationService, which name the tenantId in every query of one tenant. An invitation is found by the hash of its link while whoever opens it is no member of its tenant yet, which the filter of their request would hide. tests/unit/tenant/tenant-isolation.test.ts lists every such model. Names of groups, saved logins and the five template kinds are unique per tenant (@@unique([tenantId, name])), so services look them up with findFirst({ where: { name } }).
The Scope
src/lib/tenant/context.ts keeps the scope of the code that runs now in an AsyncLocalStorage, one per process on globalThis, since Next.js builds several module graphs that would otherwise each have their own.
type TenantScope = { kind: "tenant"; tenantId: string } | { kind: "system" } | { kind: "all" };
runAsTenant(tenantId, () => jobService.list()); // reads and writes one tenant
runAsSystem(() => scheduler.refresh()); // reads every tenant, unfiltered
explicitScope(); // the scope set around this code, if anyA request sets no scope. The extension asks the request instead, through requestScope() in src/lib/tenant/current.ts, which reads the headers with requestHeaders() and calls getAuthContext(headers). getAuthContext is memoised per Headers object in a WeakMap on globalThis, so a request resolves its session or key once however many queries ask. forgetAuthContext(headers) drops the entry, which switchTenant does before it reads on.
The tenant of a request comes from:
- A session:
pickTenantinaccess-control.tstakesSession.activeTenantIdwhile the account is still a member there (a platform admin any tenant that exists, a platform group any tenant of its scope), else the home tenant for a member of it, else the first membership. Null for an account of no tenant, whose Overview showsNoTenantNoteand whose dashboard reads nothing of a tenant. A platform admin, and a platform group that opens All tenants, starts in All tenants while there is more than one tenant. Removing someone from a tenant clears it from their sessions, so they pick another one on their next request. - All tenants: a platform admin's session that stores
ALL_TENANTS("*", fromsrc/lib/tenant/home.ts) getsallTenants: trueand notenantId. Its scope isall, see below. - An API key:
apiKeyService.validateruns inrunAsSystem, since a key is found by its hash before its tenant is known, and returnstenantId, the tenant the key was made in.
currentTenantId() answers the same question for code that has to name a tenant itself, like the audit log, which writes in the system scope: the tenant of runAsTenant, null in the system scope, else the one of the request.
tenantOf(ctx) from access-control.ts returns the tenant of a context or throws PermissionError for an account of none and in All tenants.
Every tab of a browser shares the session and so its tenant. TenantSync in src/components/tenant/tenant-sync.tsx reloads the other tabs when one switches, and the actions of the tenant settings take the tenant the page shows and refuse when the session works in another one.
A link of a notification names the tenant of its record with tenant= (TENANT_PARAM, paths in src/lib/notifications/templates/format.ts, from TemplateOptions.tenantId). TenantFromLink in src/components/tenant/tenant-from-link.tsx, mounted once by the dashboard layout, switches the session into that tenant and loads the same page there, or only drops the parameter in All tenants, in the open tenant and for a tenant the viewer may not open. useOpenFromLink waits while the switch is pending (useSwitchFromLink), or it would look for the record in the wrong tenant.
All Tenants
All tenants is the view of a platform admin, or of a platform group of all tenants, over every tenant at once. The scope all reads every tenant unfiltered and refuses every create, update and delete of a tenant model or a child row with TenantScopeError, except the caches a read fills on the way (StorageListCache, DatabaseListCache, HealthCheckLog, DbVersionHistory). A change belongs to one tenant, and only there assertOwnRecords can check what it links. The UI opens a record in its tenant before it changes it, through useOpenInTenant in src/components/tenant/tenant-view.tsx, and asks for the tenant of a new record with TenantPickDialog. The pick lists only the tenants where the viewer may add that kind of record, from the writes that tenantService.switchable returns with each tenant and newRecordPermission in src/components/tenant/new-record.ts, and New stays hidden without one.
The permissions in All tenants are ALL_TENANTS_PERMISSIONS: every *:read and *:view, the platform permissions, the profile and users:write, since accounts, memberships and settings are global. Groups belong to tenants and change in their tenant. A platform admin passes no other check there, so the UI hides every write by the permissions it already reads.
The Prisma Extension
tenantScopeExtension in src/lib/tenant/prisma-scope.ts is added to the client in src/lib/prisma.ts. For a model in TENANT_MODELS it does per operation:
| Operation | Tenant scope | System scope |
|---|---|---|
findUnique, findFirst, findMany, count, aggregate, groupBy and their OrThrow forms | where gets AND: [{ tenantId }] | As it is |
update, updateMany, updateManyAndReturn | The same, and data.tenantId may only name the same tenant | As it is |
delete, deleteMany | where narrowed | As it is |
create, createMany, createManyAndReturn | Every row stamped with the tenant, a row naming another one throws | Every row must name its tenantId |
upsert | where narrowed, create stamped, update checked | create must name its tenantId |
| Anything else | Throws | As it is |
The filter is one more entry of AND, so the unique field of a findUnique stays where Prisma looks for it. An id of another tenant reads the same as one that does not exist.
The system scope must name the tenant of a row it creates, since the column default would otherwise put a row of a customer into the home tenant without a word. Moving a row between tenants is for the system scope only.
CHILD_MODELS maps the rows that belong to a tenant through their parent, like JobDestination through job or HealthCheckLog through adapterConfig, to that relation. Their reads, changes and deletes are narrowed with { [relation]: { tenantId } }. A create passes, since it names its parent by an id, which the write checks itself (see Writes).
Without a scope and without a request that names a tenant, a query on a tenant model throws TenantScopeError from src/lib/logging/errors.ts. A forgotten scope is an error, never a leak of every row.
Prisma Queries Are Lazy
A Prisma query is a PrismaPromise, which only runs once something calls its then. Handed back unawaited from the function of runAsTenant, it would run after store.run returned, outside the scope, and fail or read another tenant. within() in context.ts therefore starts a returned thenable inside the scope and hands back a plain Promise:
// Fine: within() calls then() inside the scope, the query runs there.
const jobs = await runAsTenant(id, () => prisma.job.findMany());
// Wrong: the query is handed out wrapped and only awaited after the scope ended. It runs in the
// tenant of the request, or throws TenantScopeError outside one.
const { count } = runAsTenant(id, () => ({ count: prisma.job.count() }));
await count;Await a query inside the function, or return it as it is, never inside another object.
A transaction keeps the scope of the code that opened it, the interactive form and the batch form alike.
Raw SQL Skips the Filter
$queryRaw and $executeRaw are no model operations, so the extension never sees them. Raw SQL over tenant models may only find ids or numbers, and the rows themselves are read through the client afterwards, which filters them again.
Writes Check What They Link
The filter narrows queries, it does not read the ids inside a payload. A job of one tenant that names the source of another by its id would back up a database of a stranger. A write that links records checks the ids first with assertOwnRecords(client, { adapterConfig: [...], encryptionProfile: [...] }) from src/lib/tenant/references.ts, with the client or transaction it writes with. A row of another tenant counts as missing, and the ValidationError reads the same as for one that never existed. jobService and the notification templates use it, a connection checks its logins through validateCredentialAssignments, which reads them through the filtered client.
Code Outside a Request
Everything that runs without a request names its scope:
| Entry point | Scope |
|---|---|
src/instrumentation.ts | startServer in runAsSystem: recovery of stale runs, the scheduler, the checks at start |
Scheduler, src/lib/server/scheduler.ts | refresh() reads the jobs of every tenant in runAsSystem, a due job runs in runAsTenant(job.tenantId), a system task in runAsSystem |
Queue, processQueue() | Reads the pending runs of every tenant in runAsSystem, starts each in runAsTenant(execution.tenantId). Only the runs it starts (QUEUED_RUNS, a run of a job or a backup) fill its slots, so a restore or a check of copies of one tenant holds no backup of another |
System tasks, systemTaskService.runTask | runAsSystem, also when someone starts one from a page. The integrity check records one run per tenant, each in runAsTenant of that tenant with its own copies, the home tenant first (system-task-runs.ts, integrityService.tenantsToCheck). A check of backup copies someone starts from a page records its run in their own tenant. Execution retention keeps the newest runs per tenant, and the permission sync writes only AVAILABLE_PERMISSIONS into a full access group, never a right of the platform |
| Counts across tenants | The numbers of Data retention and the guards that wait for no running run, before a VACUUM, the database download and the restore of a database copy, count in runAsSystem, so the runs of every tenant count whichever tenant the admin has open. The queue is one for every tenant, so the Timeline, Upcoming and the schedule picker of a tenant play the jobs and live runs of the others through too, read by otherTenantQueue in src/services/jobs/other-tenant-queue.ts and named only OTHER_TENANT_JOB, never by name or id. It reads one snapshot every tenant shares, src/services/jobs/queue-snapshot.ts (each scheduled job with its usual length, a job saved reads the list again, a run that ended its length), planned once per schedule by queue-schedules.ts at the fixed offset of its time zone where the window holds no change of it (src/lib/core/cron-window.ts) |
notify(), the system notifications | runAsSystem, the channels of Settings → Notifications belong to the platform. recordNotificationLog writes into the tenant the entry names, else the tenant of the channel, else the current one, else the home tenant. A message of a customer through a channel another tenant shares names the customer, so its row lands in the customer's log and never in the tenant that shares the channel. The links of a message name the tenant of its records from recordTenant, while the log of the platform's channels keeps the executionId only of a run of the home tenant (platformRun in system-notification-service.ts), since the home tenant cannot open the run of another one |
refreshStorageStatsCache | runAsSystem for every destination, one entry per destination and tenant. getStorageVolume filters the cached entries to the backups the caller sees, see Caches and Numbers per Tenant |
auditService.log | Writes in runAsSystem into origin.tenantId, else currentTenantId(), else the home tenant. The sign-in hooks name the home tenant themselves, so a sign-out lands there too and not in the tenant the session looked at. A change of the platform names it with PLATFORM_LOG or platformLogFor(ctx), see Audit Logging |
| Restore of a shared database | restoreService.restoreShared in runAsSystem, the run recorded in the customer's tenant and a second run in the tenant of the server |
| Configuration backup and its restore | The settings point at a destination and a key of the home tenant, so restore-flow.ts and restore-pipeline.ts read them in runAsTenant(HOME_TENANT_ID), also on the sign-up page, which has no tenant of its own |
Caches and Tokens
cached()insrc/services/dashboard/cache.tsprefixes every key with the scope: the tenant,*for the system scope and All tenants, which read every tenant alike, and-without one, like a request of an account of no tenant. The numbers of one tenant never answer another, and code of no tenant never gets those of every tenant.- A run that ended clears the scope of its tenant and
*alone (invalidateTenantCaches), a storage refresh those of the tenants of its destinations, so the pages of the other tenants stay warm. An entry that reads other tenants too, like the shared servers of a customer, says so withreadsOtherTenants: trueand goes with every clear.invalidateDashboardCache()clears everything, for a change of the platform. - A download link is fetched without a session, so its token carries the tenant it was made in and its maker (
generateLinkToken({ tenantId, userId, ... }),kind: "link").GET /api/storage/public-downloadserves only such a link, insiderunAsTenantof its tenant, whilemakerMayDownloadinsrc/lib/auth/download-maker.tsfinds that its maker, and the API key it was made with (apiKeyId), may still download there, and reads any other token as spent. A file prepared for the browser and a pick of a seekable archive carry their tenant and API key too, and only a request of the same tenant and key collects them (madeBy), the pick served inrunAsTenantof its tenant. The dump of a shared database goes to its customer throughGET /api/shared/download?token=, whose address never names the destination of the server. See Download Tokens.
Shared Databases
DatabaseAssignment and DatabaseAssignmentRule name the customer in tenantId, but belong to the tenant of their server and sit in CHILD_MODELS through server, the SharedServer that outlives its connection. Their tenantId has no relation, so the id of a deleted tenant stays. The tenant of the server manages them with databaseAssignmentService in src/services/tenant/database-assignments.ts, under the normal filter. Changing them needs platform-sharing:write (SHARE_PERMISSION in src/lib/tenant/sharing.ts), seeing them platform-sharing:read (SEE_SHARING_PERMISSION), never sources:*.
Every change takes the PlatformReach of the person as its last argument, which the action or the route reads with platformGroupService.reachOf(userId), of the owner for an API key. src/services/tenant/assignment-reach.ts refuses a database given to or taken from a tenant beyond it, a move or a change of backups on demand of a database such a tenant holds, a change of its rules, a new rule that would rank above one of its rules that can match the same names (patternsMayOverlap in database-owner.ts, decided from the rules, since the list DBackup last read from the server misses a database made since), and the end of a rule above such a rule of it (assertRuleEndReached), with the sentences of OUTSIDE_REACH in src/lib/tenant/sharing-reach.ts, which never name a tenant beyond the reach. Such a tenant reads as gone: forSource lists the reached tenants under customers and tenants, and sends a holder, an assignment and a rule of a tenant beyond the reach as TENANT_BEYOND_REACH, one id and one name for all of them, which tenants lists too while it holds or held a database there. The card locks the same through assignment-reach.ts beside it in components/dashboard/explorer/. getServerCustomers and getCustomerDatabases name only the reached customers. Every audit entry of a change of who holds a database goes through assignment-audit.ts, which names the server in details.name. "all" changes nothing.
Only a tenant with Tenant.sharesWithCustomers gives databases away, so a customer's own servers never share, whoever holds platform-sharing:write. assertCustomer(tenantId, server) in shared-servers.ts refuses a server of a tenant that does not share (NOT_SHARING), a server of an engine that gives no database alone, the tenant of the server itself and a tenant that is gone. assign refuses a server of a tenant that does not share before anything else, taking back included, so it never records a SharedServer there. A tenant that shares may get databases too, a database it gets is no server of its own, so it never passes one on. forSource offers every other tenant the viewer reaches as customers, moveDatabase refuses a server of a tenant that does not share, and serverSharingOf in server-sharing.ts shows the card only in a tenant that shares, while there is more than one tenant.
Whether an engine gives one database alone is sharesSingleDatabases of its definition in src/lib/adapters/definitions/index.ts, which every database adapter declares (tests/unit/adapters/shares-single-databases.test.ts). Redis and Valkey do not, since their backup is one snapshot of every logical database. assertSharesSingleDatabases refuses giving, a rule and a move there, the card of the server hides by the same flag, and tenantPlaces leaves out every place on such a server, so an assignment made before the check hands its customer nothing, while taking it back still works.
tenantService.setSharing(id, on) switches it, through the action setTenantSharing in actions/tenant/tenant.ts, which needs platform-tenants:write and a tenant the viewer manages (managesTenant) and writes the audit entry into the home tenant. It refuses to switch off while its servers give databases away, an open assignment with a tenant or an active rule on a SharedServer of the tenant whose connection still exists, or while it has a ConnectionShare, since without the switch nobody could manage them. What a server whose connection was deleted gave away nobody can take back, so the switch ends it. createTenant(name, sharesWithCustomers) sets it from the start.
Who held a database at a time is plain data, holderAt() and ownerAt() in src/lib/tenant/database-owner.ts: the assignment of the name that held then, else a move away, which keeps every rule off it, else the matching rule of the highest priority and, of equal ones, the oldest. It is settled by what was given by then, by createdAt: an assignment or a rule made later with a start in the past only reaches times nobody else held, so backdating or a new rule of a higher priority never takes the backups of another customer's time. A backup is judged by its createdAt, never by the owner now, and since the stamp of the sidecar comes after every dump of the run, sharedBackupsOf gives it to a customer only when the same customer held the database at dumpsStartedAt of the sidecar (written by dumpDatabases and stepUpload), at createdAt and at every start, end and createdAt of an assignment or a rule between them. A sidecar without dumpsStartedAt is judged by createdAt alone.
assign()ends an assignment rather than deleting it, and moves a backdated start up to the end of the previous one.- Taking a database back,
tenantId: null, is recorded as an assignment without a tenant, so it belongs to the server's tenant alone from then on, also when a rule matches its name. Taking back what nobody holds changes nothing. removeRule()ends a rule withvalidUntilinstead of deleting it. The backups made while it gave a database stay with its customer, like after an ended assignment. Someone of a platform group of some tenants cannot end it while a rule of a tenant beyond the reach ranks below it and can match the same names, listed by the server or not.tenantService.deleterefuses while a run of the tenant waits or runs (assertNoActiveRun, asked again inside its transaction), and while a run of a customer writes to a destination it shares or waits to (assertNoCustomerRun). It ends the assignments and rules of the deleted tenant and keeps them with its id, so the backups of its time belong to nobody and never fall to another tenant's rule, and takes a database it held by name back with an assignment without a tenant, like Nobody else.
src/services/tenant/shared-databases.ts is the one path that reads across tenants on purpose. In runAsSystem it reads the server, its jobs, the destinations of the server's tenant and those other tenants share with it (providedTo in serverBackupFiles), and their cached listings as the server's tenant sees them, and keeps the archives with an index (hasFileIndex) that hold the database and were made while the customer held it. A copy counts only with a proof that DBackup made it, never by its sidecar alone (shared-copy-proof.ts): a backup run of the server's tenant that recorded the path (recorded-backups.ts), whose index has to be the one the run recorded by its SHA-256, on a new storage too, or for an encrypted backup the seal of a key of the server's tenant, which no customer can make. The time window judges a recorded copy by the times of its run and a sealed one by the time in its index, never by the sidecar. The proof is checked where the download and the restore open the archive anyway (assertCopyProven), and the restore reads the index whose proof was checked alone (SharedRestore.indexSha256). A plain backup no run recorded, or moved by a run of a version that recorded no digest, is shared with nobody. A backup is opened in runAsTenant of the server's tenant, so a backup without a named key is matched against that tenant's keys alone, and the key never leaves the module. A download and a restore try the next copy when a destination does not answer. Every download and restore checks the backup again from its reference, never from what the browser sends along.
A restore of a customer passes every line of its run through sharedLogScrubber in src/services/restore/shared-restore-log.ts. The run names the database alone, never a host of the host field or of a list, another host of its domain, the master of a Sentinel, a replica set, the port, user, archive, destination or another database of the backup or of what the server lists (listedDatabases), also at the start of a longer name, and keeps no details. It keeps the version of the server by its number (versionShown), also in the record of the run, and a line of the provider alone, like the data folder of SQL Server, goes through split or SERVER_DIAGNOSTICS. Its notifications name the database instead of the archive, without the destination and with a generic error.
The tenant of the server gets a run of the same restore with the whole log. restoreSharedBackup passes both tenants in RestoreInput.shared.provider, RestoreService creates the customer's run and the provider's run in one transaction, and createRestoreLogs in src/services/restore/run-logs.ts writes both: log for a line both read, split for a line the customer reads in other words or not at all, and sinks for archive-restore.ts and archive-restore-databases.ts, which hand the raw line to the provider's run and the scrubbed one to the customer's. Both runs share their progress, end with the same status, and are cancelled through one AbortController registered under both ids. The provider's run follows the customer's, see below. A failed run of the customer ends on providerHint. The action writes the restore into both audit logs, in the provider's as shared_restore with the database, the server and the customer, and downloadSharedBackup a download the same way as shared_download, with the names prepareSharedDownload hands back in provider. A restore the preflight refuses still gets both runs, failed at once, from recordRefusedRestore in src/services/restore/refused-restore.ts: the customer's with the reason through the scrubber, the provider's with the whole reason. It throws SharedRestoreRefused, which the action answers with the reason and the id of the customer's run.
The dump of a customer's restore is the customer's content, written by the provider's login. archive-restore-databases.ts sets sharedRestore: true on the config restoreOne gets, and an adapter that can keep the customer's code from the rights of that login reads it: PostgreSQL runs pg_restore --role with the owner of the target database (datdba), and since code of the dump can RESET ROLE to the login while the data loads, sharedRestoreRole refuses the restore when the owner is the login or a superuser, or the login is a superuser, has CREATEROLE or BYPASSRLS, or is a member of another role than the owner beside the pg_ roles. SQL Server moves every file into the default folders of the instance and switches off TRUSTWORTHY and DB_CHAINING afterwards. MySQL needs nothing, its dump keeps the DEFINER of every object. An engine that cannot contain it sets customerRestores: false on its definition, today Firebird and Azure SQL, and customerRestoreRefusal in src/services/tenant/customer-restores.ts refuses a customer's restore of it in restoreTarget and again in archive-restore-databases.ts before anything is staged. A new adapter whose restore runs code of the dump does one of the two and says in the user guide of shared databases what a customer's restore does.
An ordinary restore by the tenant of the server over databases customers hold now is refused without RestoreInput.overwriteCustomerDatabases, see assertCustomerDatabasesConfirmed in src/services/restore/customer-databases.ts. The restore page reads those databases through getCustomerDatabases and asks for a tick, and the route names the customers in its audit entry under customers.
Every restore keeps what it read and every database and folder it restored under restore in the metadata of its run, from createRestoreTracker in src/services/restore/restore-record.ts, which the summary of the run reads. The customer's run keeps view(true), without from and archive, the destination and the path of the server's archive, and the provider's run view(false), the whole record. SharedRestore.copyTenantId in src/services/restore/types.ts names the tenant in whose view of the destination the path of the copy lies, the tenant of the server, so preflight.ts, pipeline.ts, archive-restore.ts and archive-restore-databases.ts open the copy as that tenant through openDestination(id, copyTenantId) or findDestination. A copy on a destination another tenant provides to the server's tenant lies in that tenant's folder there, which the view of the customer, the tenant of the request, would never reach.
How many backups a day a customer may start of a database is effectiveOnDemand in src/lib/tenant/sharing.ts: the onDemandPerDay of the assignment or the rule that holds it, none for ON_DEMAND_OFF (0), and for null the Tenant.onDemandPerDay of the customer, which the platform sets with setTenantOnDemand (platform-tenants:write, a tenant the viewer reaches). tenantPlaces, holdingNow and the rows of the provided servers resolve it, so a new reader of the number goes through it too. tenantOnDemandService in src/services/tenant/tenant-on-demand.ts lists the assignments and rules of a customer that set their own and lets one follow the customer again, both with the PlatformReach of the viewer: a tenant that shares beyond it shows as PROVIDER_BEYOND_REACH without its id, and what it gives never follows from there.
A backup a customer starts of a shared database (requestSharedBackup in shared-on-demand.ts) is a run of the job of the server's tenant with onlyDatabases. The customer gets a run of its own from src/services/tenant/shared-backup-runs.ts: createCustomerBackupRun records it, waiting, before the real run is queued under an id picked for it (RunJobOptions.executionId). When the queue takes the real run, performExecution finds it through customerBackupFollower and tells it its start and each stage, and stepFinalize ends it right after the real run's status. It writes its own lines and never copies one of the runner, so a new log line of the runner can never reach a customer. A Partial run reads as Success there, the size is the one of the dump of the database. CUSTOMER_BACKUP_STAGES are its steps on the page of the run.
Both are followers (src/lib/execution/follower-runs.ts): their metadata names the run they follow under followsRun, which every query matches with its colon, "followsRun":, so a destination or a database of that name makes no follower. LEADING_RUNS keeps them out of the queue, pending or running, and out of the watchdog. Whatever ends a run without its own steps, the watchdog or a cancel before the start or of a run of another process, calls endFollowers. A follower whose run is gone, like one of a deleted tenant, or ended without it, like a restore whose write of the provider's run failed, is ended by endOrphanedFollowers, which sweepStuckExecutions calls on every round of the watchdog while it is on. It waits ORPHAN_GRACE_MS (10 minutes) from the end of that run, or from the start of the oldest follower when the run is gone, and then ends the followers with the status of the run, or Failed when it is gone. Cancel of a follower cancels the run it follows, in runAsSystem, since that run belongs to another tenant. The startup recovery keeps the metadata of a run, so a follower keeps its name.
Shared Destinations and Channels
A tenant with sharesWithCustomers also shares its destinations and notification channels with other tenants. The code lives in src/services/sharing/, the view of a destination in src/lib/tenant/storage-view.ts.
| Model | Holds |
|---|---|
ConnectionShare | One per shared connection, its id is the id of the AdapterConfig and cascades with it. tenantId is the tenant that shares it, in TENANT_MODELS. allTenants (every tenant, a new one too), encryptedOnly (a destination, false by default) and ownRecipients (an email channel, true by default) |
ConnectionShareTenant | { shareId, tenantId }, a picked customer while allTenants is false. tenantId names the customer without a relation and the row sits in CHILD_MODELS through share, so the tenant that shares manages it. tenantService.delete removes the rows of a deleted customer |
ChannelRecipients | The addresses a customer names for a shared email channel, a JSON array, unique per tenantId and configId. tenantId is the customer, in TENANT_MODELS |
connectionShareService in connection-shares.ts runs in the scope of the tenant that shares, so it finds only its own connections, and sharedKindOf lets only a destination or a channel through. save refuses a tenant without sharesWithCustomers, refuses to share a destination while a job of the tenant called tenants (TENANTS_FOLDER), or whose name starts with tenants/ (isTenantsFolderName), writes to it, refuses the first share while tenants/ holds a file (customerFolderFiles), and wants confirmed when a tenant loses a destination or a channel (UNCONFIRMED_LOSS). It takes the connection away first with forgetConnection and writes the share only then, so a storage that does not answer leaves the share as it was. view and losing say how each customer uses it, from its jobs and templates and from storageEntriesOf.
The actions in actions/tenant/connection-share.ts check SHARE_PERMISSION or SEE_SHARING_PERMISSION, refuse All tenants and write share and unshare into the log of the tenant that shares, one entry for each tenant that got or lost the connection with its tenantId in the details (logShareChange in share-audit.ts), so the log of the home tenant hides the entries about a tenant beyond the reach of its reader. A share with every tenant writes one entry that names none, and only when it was not shared with every tenant before. A save that changes encryptedOnly or ownRecipients of a share that stays writes one shareTerms entry with its changes from diffFields (shareTermChanges), and a save that changes nothing writes nothing. They hand the service the reach of the viewer, and boundToReach in share-reach.ts keeps a change inside it: every tenant, and any change of a share with every tenant, needs "all", a picked tenant beyond the reach is refused, the tenants beyond it that the share names stay on it, and the terms stay while one of them gets it. stopFor and deleteBackupsOf refuse such a tenant. view lists the reached tenants as candidates, in settings.tenantIds and in uses, and what stays locked in reach (ShareReachView), whose beyond says only that tenants beyond the reach get the connection, never which or how many. summaries names the reached tenants alike for the list of connections, with ListAdaptersOptions.shareReach. Each customer that lost the connection gets an unshare entry with its lost jobs, templates and backups in its own log (origin.tenantId), which withdrawnConnections in withdrawn.ts reads back for the note of the Connections page during WITHDRAWN_DAYS. The recipients a customer names (setChannelRecipients) need notifications:write in its tenant.
Reading Across Tenants
A provided connection stays in the tenant that shares it, so the scope of the customer never finds it. providedTo(tenantId, { kind, ids }) in provided.ts reads across tenants on purpose, in runAsSystem, so it answers the same in every scope. A connection counts as provided while its tenant has sharesWithCustomers and its share names the customer or every tenant. Every reader of a provided connection asks it, or providedConnection and providedIds for given ids:
providedIteminconnection-options.tsturns one into a list row withconfig: "{}", no secret status, nolastErrorandprovidedwith the tenant that shares it and its terms.listAdaptersappendsprovidedItems(type, role), the pickers useconnectionOptions(type).- The job service accepts a provided destination and refuses a job without a key on one with
encryptedOnly(assertEncryptionAsked). The notification templates and the tenant notifications accept a provided channel. - Everything else, an edit, test, clone or delete of a provided connection, finds nothing, since the tenant filter keeps it out.
Opening a Destination
destination-access.ts is the one way to open storage:
| Function | Returns |
|---|---|
findDestination(id, tenantId?, lookup?) | The row and the StorageViewMode of the tenant given, else of the request, else of the tenant of the destination, for the system scope and All tenants. A destination of another tenant that is not shared with it throws NotFoundError, and so does a directory source unless lookup.roles is ANY_STORAGE_ROLE |
openRow(row, mode) | The adapter wrapped in storageView(adapter, mode), with the resolved config. In the view of a customer a config that cannot be resolved throws SHARED_STORAGE_FAILURE, never the message, which names the logins of the tenant that shares it |
openDestination(id, tenantId?, lookup?) | Both at once |
destinationAccess(row, tenantId) | The mode with the ProvidedConnection, whose encryptedOnly the runner reads |
A lookup takes destinations alone by default, since list, download, delete, lock, verify and analyze act on backups, which lie on a destination. A directory source holds the files of a customer and no backups, so the routes under /api/storage/[id] answer 404 for one. Only the target of a restore of files takes a source as well, since it writes files back into it: findRestoreTarget in src/services/restore/restore-target.ts and checkRestorePath, the badge of its folder, pass ANY_STORAGE_ROLE.
StorageViewMode is own for a destination nobody shares, the plain adapter, provider for the tenant of a shared destination, which sees everything but tenants/ and gets a ValidationError for a path into it, and customer with its tenant id, which sees tenants/<id>/ alone with every path relative to it. cleanPath refuses .. and every other segment of dots and spaces alone, like .. or ..., which Windows reads as .., also once its % escapes are read and split at an escaped / or \, like a%2F..%2Fb, and the folder of the customers is found without the dots and spaces at the end of its name, in lower and in upper case. A listed file the view could never name is left out of a listing. The view maps the path of upload, download, list, delete, read, verifyChecksum, downloadRange and openSession, and leaves out listTree, downloadDirectory, createSymlink and browseDirectories, since a destination never serves as a directory source.
Rule for new code
Code that reads or writes backups opens the destination through openDestination, or findDestination with openRow when it needs the row first. Never registry.get with resolveAdapterConfig itself, which reaches the folder of every customer. A lookup that should also reach a directory source passes { roles: ANY_STORAGE_ROLE } and says why.
A path a caller names for a backup goes through assertBackupPath in storage-view.ts before a service deletes or locks it, whatever the view: the view of a destination nobody shares hands the path to the storage as it is. It refuses an empty path, one that starts with / or \ and a .. segment, also between backslashes or behind percent escapes, with a ValidationError. storageService.deleteFile, toggleLock, setLocked, setLockedWith, deleteBackupsBulk and setBackupsLocked ask it, and a new service that writes or deletes a backup by its path asks it too.
The exceptions know why: 01-initialize.ts builds the view from destinationAccess with the tenant of the job and records a refusal for the upload instead of throwing, SHARED_STORAGE_FAILURE for a provided destination whose config cannot be resolved, refreshStorageStatsCache lists a whole destination in the system scope and splits it by tenant, and the configuration backup uses a destination of the home tenant as it is. Its file there holds the keys of the instance, so whichever tenant looks at the destination, every download, analysis and restore of it is for a platform admin with a session alone, see Who Downloads It.
Caches and Numbers per Tenant
StorageListCacheis keyed byadapterConfigIdandtenantIdand sits inTENANT_MODELS. A shared destination keeps a listing per tenant, each with the backups of that tenant alone.storageService.listingTenantpicks the tenant of a call likefindDestinationdoes.refreshStorageStatsCachewrites oneStorageVolumeEntryper destination and tenant (entriesOf), countingtenants/<id>/for that customer while it gets the destination, the folder of any other tenant for nobody, and everything else for the tenant of the destination, and saves theStorageSnapshotrows per tenant.getStorageVolumeshows a tenant its own backups on its own and on provided destinations, All tenants one entry per destination.storageEntriesOf(configId)gives the tenant that shares the space of each customer. The storage limit and the usage spike of a shared destination watch its whole storage, the backups of its customers included, while the missing backup alert watches its own tenant's runs.
Channels
channelTarget(channel, tenantId) in channel-recipients.ts decides where a message of a tenant through a channel goes: an own channel sends as it is set, a provided channel to the recipients the customer named when ownRecipients, else as it is set. It refuses once the channel is no longer shared with the tenant and while the customer named nobody, so a message of a customer never reaches the recipients of the tenant that shares by mistake. sendThroughChannel and 04-completion.ts ask it before every send and record a refusal as a failed notification. setRecipients keeps at most MAX_RECIPIENTS (20), and sendRecipientsTest sends a plain notification, never the SMTP test of the channel, whose mail would name the server and sender.
Taking It Back
forgetConnection(configId, kind, tenantIds, deleteBackups = true) in share-cleanup.ts takes every link of each tenant to the connection away, in the system scope. On a destination it deletes the backups of the tenant first with deleteTenantBackups, which opens the destination in the view of that customer so nothing outside its folder is touched, then its JobDestination rows and its listing. A channel leaves the template channels of the tenant, the notifications of its jobs, its ChannelRecipients and its tenant notification settings. Only tenantService.delete of the tenant that shares passes false, since its connection goes and the storage stays, and passes its transaction as tx, so its customers keep every link when the delete fails. Deleting a customer deletes its backups on provided destinations after the delete, in the background (clearBorrowedDestination). A share left with nobody is dropped only after sweepCustomerFolder emptied tenants/, and kept with nobody when the storage does not answer, like save does, and the numbers of the destination are refreshed either way.
removeAdapter refuses a connection with a ConnectionShare with a ConflictError, and tenantService.setSharing refuses to switch off while the tenant has one.
No Second Way In
The view of the tenant that shares hides tenants/ of a shared destination, which a second, unshared connection to the same place would show. assertOffSharedPlaces(adapterId, config, logins, exceptId) in src/services/adapters/shared-place.ts refuses a storage connection of either role whose place equals, lies inside or holds the place of a destination the tenant that runs now shares with customers. createAdapter, cloneAdapter and updateAdapter (when the config or a login changes, leaving the connection itself out) call it. The place is storagePlaceOf(adapterId, config) in src/lib/adapters/storage-place.ts, the storage (a server, a bucket on its endpoint, a share, the app of a cloud drive) and the folder in it, with the logins laid over the config by layLoginsOver, since a relative folder of SFTP lies in the home of its account. SFTP and rsync on one host are one storage, Amazon S3 and a generic S3 at an endpoint of Amazon too. It compares the address as typed, without the dot at the end of a host and with every name of the loopback as one, so a name and its address still count as two storages, and a folder of Google Drive is an id, so only the same folder and the top of the drive are found there. A connection that overlapped before the destination was shared stays as it is.
The spelling of an address never opens the folder with a saved login of the shared destination: src/services/adapters/shared-login.ts keeps such a login to that destination. assertOffSharedLogins(logins, heldBy) refuses it for a connection of any kind in createAdapter, cloneAdapter, updateAdapter (when a login changes, the logins the connection holds already stay) and a restore from Recently deleted, and for the forms that list folders, browseLocation and listRemoteDirectory. assertSharedLoginsAlone refuses the first share, and a new login of a shared destination, while another connection of the tenant signs in with it. Someone who types the secret itself is not held back, like a second spelling of the address.
Tenant Allowances
Tenant.addsDatabases, addsDirectorySources, addsDestinations, addsChannels and addsApiKeys say what a tenant adds itself, ALLOWANCE_KINDS in src/lib/tenant/allowances.ts. assertMayAddKind(kind) in src/services/tenant/tenant-allowances.ts refuses something of a kind the tenant that runs now may not add with a ValidationError, and passes outside a tenant, like the configuration restore. assertMayAdd(type, storageRole) asks it for a connection. createAdapter, cloneAdapter, a role change in updateAdapter, a connection back from Recently deleted in restoreFromTrash and apiKeyService.create call it, and so does any new path that creates one of these. setAllowances refuses the home tenant. The action setTenantAllowances needs platform-tenants:write and a tenant the viewer manages, and writes the change into the log of the home tenant and of the tenant. The pages read the allowances from tenantService.switchable through useMayAdd(kind), and tenantsThatCreate leaves a tenant out of the pick of New in All tenants.
Connections to the Server DBackup Runs On
A connection that reaches the server DBackup runs on, its files, its Docker, its sockets, the tools it runs with options of the connection, a SQL Server shared folder (mssqlUsesLocalFolder) and its SSH keys or agent, is added, changed, copied, tested, browsed and restored from Recently deleted by a platform admin with a session in the home tenant alone. hostLocalReason(adapterId, config) in src/lib/adapters/host-local.ts says why one does, and mayReachHost() in config-resolver.ts whether the code that runs now may: a platform admin with a session in the home tenant, also inside a system or home scope the request set, and code outside a request, like a script or the lab seed, unless its scope is a customer or all. A member of the home tenant, an API key, All tenants and every customer may not.
createAdapter,cloneAdapterand a change of the place inupdateAdaptergo throughassertMayReachHost, which in the home tenant callsassertMayManageHostLocaland outside it refuseshostLocalReasonfor everyone.updateAdapterasksassertMayManageHostLocalof the stored config too, so a rename or a switch back to SSH of such a connection is a platform admin's as well, and so doupdateAdapterFlagsandrestoreFromTrashfor a connection. They lay the saved logins over leniently, a login that cannot be read leaves the config alone.restoreFromTrashalso asks the checks of a save of a connection,validateCredentialAssignments,savedLoginRefusalwith the caller the action passes andassertOffSharedPlacesfor a storage connection, where a login deleted since the snapshot counts as none.- Every config of a form that a route runs before it is saved, a test,
access-check,check-snapshot,test-ssh,browse-location, the SSH browse ofsystem/filesystem/remoteand the reads of the Database Explorer, goes throughoverlayCredentialsOnConfigorassertConfigOffHost. GET /api/system/filesystem, the browser of a local path, answerscheckPlatformAdmin()in the home tenant alone.- An SSH key profile with
authType: "agent"reaches every connection that logs in with it without a save of the connection, socreateCredentialProfileandupdateCredentialProfilerefuse it unlessmayReachHost()(loginHostReasoninhost-local.ts). A profile saved that way before stays usable, a run cannot tell who set it. - The folders and volumes of a saved connection that reaches the server (
adapters/[id]/browse,adapters/[id]/volumesand itssizes) takejobs:writeorstorage:restorebesidedestinations:read(HOST_FOLDER_PICKERS,savedConnectionReachesHostinlocation-browse-service.ts), the rights of whoever picks them in a job or a restore. A member who may only see the connections lists none. - A restore into Docker Volumes without SSH empties volumes of the Docker of the server, so the file restore and the directory mappings of a full restore refuse it unless
callerReachesHost(writeTargetRefusalinsrc/services/restore/restore-targets.ts, with the caller the route hands over). Over SSH the volume the directory source read goes back for whoever may restore, another one needsdestinations:writein the tenant of the connection. resolveAdapterConfignever asks, a run and a read of a saved connection keep working for the members who pick it in their jobs. It refuses a stored one of a customer at run time, also when a login it names changed later.- The list DTO carries
hostLocal, the reason from the stored config without its logins, which the connection form (useMayReachServer,formHostLocalReasonincomponents/adapter/host-local-access.ts) uses to open such a connection read-only, to block a save or a test that turns a config into one and to mark its modes Platform admin only. The picker of types leaves outisAlwaysHostLocalfor anyone but a platform admin.
A new adapter that touches the server itself, or a new option that a tool on the server reads, gets a rule in hostLocalReason, and a new path that changes or tries a connection asks assertMayReachHost, assertMayManageHostLocal or assertConfigOffHost. The MySQL restore runs its client with lockedDownClientArgs (mysql/client-lock.ts), so a dump runs no shell command and reads, writes or sends no local file, and in direct mode a client that cannot be locked down is refused before the restore starts.
At run time src/lib/adapters/own-data.ts stays a second line for everyone, a platform admin too: ownDataRefusal refuses a local path that reaches the folder of the database, of the TLS certificate, the temporary folder (getTempDir(), which holds the plain copy of the configuration backup and decrypted restores, left out for SQL Server, whose shared folder sees only names it made up) or the folder of the configuration backup. SQL Server checks its shared folder before a backup or restore moves a file through it (mssql/shared-folder.ts), like SQLite its file. 01-initialize.ts checks every destination at its folder and at the folder of the job (jobFolderRefusal in steps/job-folder-check.ts), 03-upload.ts the folder of an incremental chain, and the warmup of the storage cache, the storage statistics and the integrity check in destinations mode leave out a destination scanRefusal refuses, unless it is the destination of the configuration backup. The Local Filesystem adapter compares the real path of every path it opens, and of the folder of every path it creates or removes, with the real base folder, so no link below the base leads out of it. A file restore lays no link whose target is absolute or climbs out of the folder it writes into (linkLeavesTarget in src/lib/archive/link-target.ts), and names it in its log instead. The folder a restore writes into never leaves its connection either, also not by a part a storage reads as .. (assertRestoreFolder in src/lib/archive/remote-paths.ts, with cleanPath), checked by the route, resolveTargets, openDirectoryTarget and once more by safeRemoteJoin for every file.
A job writes into the folder its name gives, so isUnsafeJobFolderName in src/lib/core/job-folder.ts refuses a name with a . or .. folder or of slashes alone on create, update, clone and a restore from Recently deleted, pauses one an import brings and stops its run in 01-initialize.ts. isConfigBackupFolderName and isTenantsFolderName read the first folder as written and as a path resolves it, so x/../config-backups counts.
The Local Network
Tenant.blocksLocalNetwork says whether the connections of a tenant reach the local network of the server DBackup runs on, and Tenant.allowedNetworks (a JSON array of CIDR ranges) what they reach anyway. The migration 20261018120000_tenant_network sets the column to false for every tenant it finds, a new tenant starts with true. The home tenant is never blocked, whatever its column says, networkOfRow reads it as false.
| Module | Holds |
|---|---|
src/lib/network/local-network.ts | What counts as local (LOCAL_NETWORK: loopback, this network, private, link-local, the shared space 100.64.0.0/10 of VPN overlays, multicast, reserved and broadcast, and for IPv6 ::1, ::, fc00::/7, fe80::/10, fec0::/10, ff00::/8), an IPv4 inside IPv6 (mapped or NAT64) checked as its IPv4, the address parser and parseCidr, which refuses anything malformed and bits after the prefix. No Node.js module, so the Edit dialog checks a range as it is typed with the same code. The tests compare its parser with isIP of node:net |
src/lib/adapters/dialed-hosts.ts | DIALED_HOSTS, per adapter id the hosts DBackup itself connects to for a config. tests/unit/adapters/dialed-hosts.test.ts fails for an adapter without an entry |
src/lib/network/reach-check.ts | firstBlockedAddress(hosts, allowed): an IP literal as it is, a name through dns.promises.lookup with all: true, a host that does not resolve within five seconds passes |
src/services/tenant/tenant-network.ts | assertTenantReach(tenantId, adapterId, config), assertReachFromHere for the tenant that runs now, assertHostsReachable for hosts of its own, tenantBlocksLocalNetwork for the mark of a resolved config, and setNetwork. The refusal is a ValidationError like Acme reaches no address of the local network, db.internal is 10.0.0.5. The platform allows ranges for it under Platform, Tenants. |
A database in direct mode dials its host and every host of a list (PostgreSQL's a,b, the sentinels of Redis, the hosts of a MongoDB URI), in SSH mode the SSH host alone, since whatever lies behind it is reached from that server. SQL Server tunnels to its SSH host, which falls back to the database host, and its older file transfer over SSH dials both. A storage dials the server of its address, an S3 endpoint and the host of the bucket on it unless the bucket goes in the path (the endpoint of R2 and Hetzner is built from a field the person types, so it counts too). A channel dials every URL or host a person types. Amazon S3, the cloud drives, Telegram and Twilio have endpoints of their own and dial nothing to check. An agent will be like SSH mode: DBackup dials the agent, the agent the database, so only the address of the agent counts.
Each entry of DIALED_HOSTS reads its field the way the client or tool of that adapter reads it, so a spelling the client takes for another host than a plain reading never passes unchecked. tests/unit/adapters/dialed-hosts.test.ts has a case for each spelling:
| Adapter | What counts |
|---|---|
| MySQL, MariaDB, PostgreSQL, MongoDB, Firebird | An empty host as 127.0.0.1, the fallback of their clients |
| PostgreSQL | Every entry of a libpq host list. An entry that starts with / or @, or an empty entry of a list, is a Unix socket, which host-local.ts keeps to the platform |
| MongoDB | The hosts parseMongoTarget in mongodb/args.ts reads for the tools and the driver: after the user info before @, before a path or a query, every entry of a list, 127.0.0.1 when nothing is left, an SRV record by its name. A host that ends in .sock or holds %2F is a socket |
| SQL Server, Azure SQL | The host part of a server name: without tcp:, a named instance host\INSTANCE, a port after a comma, and . or (local) as the server itself |
| Firebird | The host of host/port and [v6]/port, as gbak and isql read the connection string |
| rsync | The part after the last @, as rsync and OpenSSH read user@host |
| SMB | Only //server/share or \\server\share with a server smbclient and rpcclient dial as written (SMB_SERVER in smb-address.ts: a name or IPv4 address, or a bare IPv6 address), which the form schema and the adapter enforce too, so no NetBIOS suffix #20, binding string of rpcclient, port or brackets. For a connection of a tenant that blocks the local network both tools get name resolve order=host (SMB_RESOLVE_OPTIONS through smbResolveOptions), so they look the server up in DNS like the check and never by a NetBIOS broadcast, see below. A user or domain with @ names a Kerberos realm whose KDC smbclient finds itself. Any of these throws HostFormatError, which assertTenantReach turns into a refusal for a tenant that blocks the local network |
Beside the hosts, the values a form, a job or a restore hands on are taken only as the kind of value they are:
- The SSH test of a form (
api/adapters/test-ssh) checksspec.ssh.host, the host the transport resolved for the login, right before it connects, for every adapter id, and refuses an adapter without an SSH mode (hasSshModeinsrc/lib/adapters/ssh-mode.ts). - An rsync username is a login name without
:,/, whitespace, control characters or a leading-(rsync-user.ts). It may hold@, likealice@corp.local, since rsync and OpenSSH read the host after the last@and the adapter always puts the host of the connection there. The form schema, the assignment of a saved login to an rsync connection, the update of a saved login an rsync connection uses, and the adapter before it builds a command check it. - A database name never starts with
-(database/common/database-name.ts). A job checks its list on save, a restore its target and the selected entries of its mapping, and the MySQL and MongoDB adapters every name they pass as an argument. The MySQL client gets the restore target as--database=<name>. A Redis database is a whole number, and the Firebird connection string never starts with-. - The webhook style channels (Discord, Slack, Teams, Generic Webhook, Gotify, ntfy) send through
webhookFetch(notification/http.ts), which follows a redirect only to the host of the URL, with any scheme or port, up to five times, and hands back a redirect to another host as a failed send. WebDAV gets HTTP agents that open a socket only to the host of its URL (webdav-agents.ts), so a redirect to another host fails and one on the same host still works. - The share a file server exposes a shadow copy under is held to the form
//server/shareinsmb-vss.tsand checked like the configured connection inacquireGroupSnapshot(collect-directories.ts), in the tenant of the connection, before anything reads it. - MongoDB with one host and no SRV record connects with
directConnection: trueindriver-meta.ts, so the driver never contacts the members a server announces. The tools connect directly to a single--hoston their own. A seed list or an SRV name still discovers its replica set.
The check runs wherever a config of a tenant is used:
resolveAdapterConfigchecks a saved connection with the tenant of its row, besidehostLocalReason, for every run, test, health check and notification that resolves one. A destination or a channel another tenant shares follows the setting of that tenant, never the one of the customer whose run uses it.assertConfigOffHost, whichoverlayCredentialsOnConfigcalls, checks every config of a form a route runs before it is saved, in the tenant that runs now: Test connection (connection-test.ts), the access check, the reads of databases by config, the browse of a location and the SSH test.listRemoteDirectorychecks the host it dials itself, whatever the mode of the form.assertMayReachHostincreateAdapter,updateAdapterandcloneAdapter, so a person learns it on save.layLoginsOverlays the logins over a config without any check, for the callers that check or only compare the result.
A tool that finds a server on its own beyond what the check resolved needs to know the setting too. smbclient and rpcclient look a name up in lmhosts, WINS, DNS and then by a NetBIOS broadcast into the local network, so smb.ts and smb-vss.ts add SMB_RESOLVE_OPTIONS for a connection of a tenant that blocks the local network, and the home tenant and a tenant without the block keep the lookup of Samba with NetBIOS names. The fact travels on the resolved config under a symbol, markLocalNetwork and blocksLocalNetwork in src/lib/adapters/network-fact.ts:
resolveAdapterConfigmarks a saved connection withtenantBlocksLocalNetworkof the tenant of its row,overlayCredentialsOnConfiga config of a form with the tenant that runs now. A spread like the folder of a browse or the share of a shadow copy keeps the mark.- A stored config and a config a form or the API sends are JSON, which holds no symbol, so no field of a config switches it, and no schema needs to strip one.
- It fails closed. A config without the mark, one that never went through the resolver or lost it through JSON or a schema, and a tenant that is unknown or gone count as blocked and get the DNS-only lookup. So does a form tried in the system scope, which runs for no tenant.
tests/unit/adapters/storage/smb-name-resolution.test.ts covers both tools for each case.
The action setTenantNetwork in actions/tenant/tenant.ts needs platform-tenants:write and a tenant the viewer manages, refuses the home tenant and All tenants, and writes diffFields of the block and the ranges into the log of the home tenant and of the tenant. TenantRow.network carries both to the Tenants page.
A guard against mistakes and plain misuse
The check reads the answer of the resolver when it runs, and the adapter resolves the name again when it connects. A hostile DNS server that answers a public address first and a private one after gets past it, so is a host whose lookup takes longer than five seconds. The firewall of the server keeps out what must never be reached.
What only the network of the server can enforce, since the client follows the server or the network without DBackup seeing it:
- A DNS answer that changes between the check and the connect, as above, and the hosts an SRV record of MongoDB names, which the driver and the tools look up themselves.
- SQL Server sending a client on to another server, like the read-only routing of an availability group or the redirection of Azure SQL.
- DFS referrals of an SMB server, which send smbclient on to another file server.
Tenant Notifications
The channels of Settings → Notifications belong to the platform and hear about every tenant. A tenant other than the home tenant also picks channels of its own for the events about its own data, under the Notifications part of its tenant settings.
| Module | Holds |
|---|---|
src/lib/notifications/tenant-events.ts | TENANT_EVENT_DEFINITIONS, the events a tenant may hear about: the three runs of a job, which the platform only hears about through the job, and the restores, storage alerts, connections and the integrity check in the words of the platform. getTenantEventDefinition is undefined for an event of the platform alone, like a sign-in or a new version |
src/services/notifications/tenant-notification-settings.ts | What a tenant stored, in SystemSetting under notifications.tenant.<id> (tenantNotificationKey), with default channels and channels per event like the platform, without reminders and mails to a person. A save checks every channel against the own ones and those another tenant shares (providedTo). tenantHearsAbout tells a caller whether gathering the event is worth it |
src/services/notifications/tenant-notification-delivery.ts | recordTenant, the tenant an event is about: the one a caller names, else the connection, run or job it names, else the tenant that runs now. deliverToTenant sends to the picked channels in runAsTenant of that tenant, the shared ones read in the system scope, and never for the home tenant. sendTenantEventTest is Send a test |
src/services/notifications/channel-send.ts | sendThroughChannel, shared by the platform and the tenants. It asks channelTarget before every send and records the send in the notification log of the tenant it was for, also through a shared channel. A customer reads SHARED_FAILURE instead of the error of a channel another tenant shares |
src/services/notifications/tenant-integrity.ts | notifyTenantsOfIntegrity, after the integrity check: each tenant whose copies failed hears about its own copies alone, by the tenant whose run checked them, never by the tenant of the destination. The message of the platform (reportIntegrityFailures in system-task-runs.ts) and its row in the notification log of the home tenant name the files and destinations of the home tenant alone, and of each other tenant only how many of its copies failed (tenants). The run of another tenant names Platform as who started it, never the person who pressed Run now |
notify() finds the tenant with recordTenant before it enters the system scope of the platform, which would hide the tenant of a run, and then sends to the platform and to the tenant. A run of a job sends with platform: false and skipChannelIds from 04-completion.ts, so a channel the job told already gets it once. The actions in actions/tenant/tenant-settings.ts need tenant-settings:write in the tenant they change and refuse the home tenant, which reports through the notifications of the platform.
The Logo
TenantLogo keeps the logo of a tenant in the database, one row per tenant, like Avatar keeps the pictures of the people, so a configuration backup brings it back. src/services/tenant/tenant-logo-service.ts stores at most 1 MB (TENANT_LOGO_MAX_BYTES) and reads the type from the first bytes with imageTypeOf, PNG, JPG, GIF or WebP, never SVG, which could carry a script. GET /api/tenants/[id]/logo serves it with nosniff to whoever mayViewTenantLogo allows, a platform admin, the tenant a request acts in, a member or a platform group that reaches the tenant, and answers anyone else like a tenant without a logo. Its address carries the time of the logo in ?v= (tenantLogoUrl), so it is cached for good. POST and DELETE need tenant-settings:write in the tenant the request acts in and write into its audit log.
Invitations
TenantInvitation keeps a link into a tenant with a group, an optional note, its maker and when it runs out, and only the SHA-256 of its token (tokenHash). invitationService in src/services/tenant/invitation-service.ts makes a token of 32 random bytes and hands it back once. A link works from 1 to 30 days, 7 by default, a tenant has at most 100 open ones (OPEN_INVITATIONS_MAX), and one person makes at most INVITATIONS_PER_HOUR within an hour. A revoke sets expiresAt instead of deleting the row, so revoked links count too. openInvitation finds a link by the hash and treats a link that is unknown, used, run out, or whose maker may no longer give its group (creatorMayGive through roleRefusal) the same.
accept refuses the account that made the link (OWN_INVITATION), a key of it included, since its maker is the owner of the key, so nobody gives themselves a group past the rule of setMemberAs. acceptWithNewAccount marks the link used before it makes the account, so two requests at once make one account, and frees it again when the account fails. A refusal counts against the link in a map of the process by the hash, and the fifth (MAX_FAILED_SIGN_UPS) spends the link. An email with an account and one whose domain the provider of another tenant claims answer the same INVITATION_EMAIL_REFUSED, after the password rules, so whoever opens a link never learns which. An email with an account also counts against the maker of the link in src/services/user/email-tries.ts, the count takenEmailRefusal keeps, unless the maker is a platform admin, and once it is spent every sign-up through their links answers INVITATION_SIGN_UPS_PAUSED. The counts live in memory, so a restart resets them.
membershipEndRefusal in src/services/tenant/membership-end.ts guards every place a membership ends by the hand of someone: setMemberAs, tenantGroupRefusal (no group), moveUsersToGroup to none, deleteGroup without a group for the members, and a save or delete of a group for tenants that removes its group from a tenant without a group for its members (membersLeaving with ends). An account belongs to the admin of a tenant once it works there alone, so it refuses an end after which the account works only in tenants where the caller holds users:write or that their platform-accounts:write reaches, unless accountRefusalFor lets the caller change the account already. The last membership moves the account to its place of no tenant (placelessTenantOf), so before that it is refused unless the place is the tenant left or the caller manages the users of the place (MEMBERSHIP_END_MOVES_ACCOUNT), whoever may change the account. Deleting a tenant ends memberships too, a right of the platform that leaves no tenant behind for the account to work in alone, and is not guarded by it. The actions in actions/tenant/invitation.ts write the creation and the revoke with the note, and the accept, into the audit log of the tenant. An account also never reaches its place of no tenant by another road with the sign-in a customer chose: tenantService.delete takes the sessions, credentials and second factors of the accounts that worked in the tenant alone and marks their email unverified (disarmAccountsOnlyOf in tenant-leftover-accounts.ts), a restore from Recently deleted that would lose every membership is for a platform admin unless the place is the tenant it left (assertKeepsItsPlace in services/trash/account-return.ts), a sign-up through an invitation whose link went meanwhile deletes the account it made, and the import of a file of an older version merges a user only into an account that is the platform's own (isPlatformsOwn in services/config/import-users.ts). A new path that takes the last membership of an account, or makes an account before its membership, follows one of them.
Permissions
src/lib/auth/owner-permissions.ts decides what someone may do while working in a tenant:
| Function | Returns |
|---|---|
groupPermissions(group) | Every permission for a fullAccess group, else the stored ones DBackup knows |
permissionsIn(memberships, tenantId) | The group in the tenant without the platform permissions, plus the platform permissions from the group in the home tenant, plus the profile permissions from any group |
ownerPermissionsIn(owner, tenantId, tenantShares) | The ceiling of an API key in its tenant: every permission of a tenant for a platform admin, else permissionsIn without what a platform group gives on the platform, and the settings of the instance only in the home tenant (keyGroupPermissions). Of the rights of the platform only those keyPlatformPermissions(tenantShares, homeTenant) allows and the owner holds there: platform-sharing:* in a tenant that shares with customers and that the owner's platform group reaches, platform-tenants:* and platform-accounts:* in the home tenant, see The API of the Platform |
capToOwner(key, owner) | What the key holds and its owner may do now |
PLATFORM_PERMISSIONS are settings:*, which act on the whole instance. Groups belong to a tenant, so groups:* counts in each tenant, and tenant-settings:* covers the tenant's own settings and its Recently deleted (TRASH_ADMIN_PERMISSION). PROFILE_PERMISSIONS are profile:*, which change the own account, which belongs to no tenant. With one tenant permissionsIn is exactly what the group gives.
AuthContext carries tenantId, isPlatformAdmin and allTenants. A platform admin passes every check of checkPermission and checkPermissionWithContext inside a tenant, never through an API key, and in All tenants only what ALL_TENANTS_PERMISSIONS holds. The tenants and their members check platform-tenants:* (actions/tenant/tenant.ts, members.ts). checkPlatformAdmin() guards the platform admin role and the keys of the home tenant a configuration restore picks from (getPlatformEncryptionProfiles). getViewer() returns the user with isPlatformAdmin, tenantId, permissions and role, their group in the tenant.
Platform Groups
A PlatformGroup gives the people of the MSP rights without the platform admin role, which stays User.platformRole and shows in the pages as the built-in group Platform admin. A user holds at most one through User.platformGroupId. Its platformPermissions are the settings of the instance and the rights of PLATFORM_ONLY_PERMISSIONS (platform-tenants:*, platform-accounts:*, platform-sign-in:read, platform-sharing:*), which are kept apart from AVAILABLE_PERMISSIONS, so no group of a tenant ever holds them. Its tenantPermissions count in every tenant when allTenants, else in those of PlatformGroupTenant.
permissionsIn(memberships, tenantId, platformGroup) adds the tenant rights of the group in a tenant of its scope (platformTenantRightsIn) and its platform rights (platformRightsIn), and ctx.permissions carries both, so a platform right is checked like any other. platform-sharing:* acts on the servers, destinations and channels of the tenant someone works in, so platformRightsIn adds it only in a tenant the reach covers (in All tenants for a group of every tenant), for a session and the key of an owner alike. The tenants and the accounts count wherever someone works, their checks pick what the reach covers themselves. pickTenant opens a tenant of the scope without a membership, and All tenants for a group that reaches every tenant and reads there (opensAllTenants), with allTenantsPermissionsOf. ownerPermissionsIn gives an API key the tenant rights of its owner's platform group, and of its platform rights only platform-sharing:* in a key of a tenant that shares with customers and platform-tenants:* with platform-accounts:* in a key of the home tenant. The settings of the instance from the owner's group in the home tenant still count for a key of the home tenant, as before tenants, and never for a key of another tenant (keyGroupPermissions). platform-sign-in:write is in no list a group draws from, so only a platform admin changes a sign-in provider.
Two rights of the platform follow the scope as well, through platformGroupService.reachOf, which returns "all" for a platform admin and a platform group of all tenants and the tenants of the scope otherwise (PlatformReach, reachCovers in platform-group-caps.ts). platform-sharing:* gives to and takes from the reached tenants only, see Shared Databases and Shared Destinations and Channels. platform-sign-in:read shows the providers of the platform and of the reached tenants, with the groups they place people in and the accounts that work there, getSsoProvidersModel with a SsoProvidersScope, while settings:read still shows every provider.
Someone of a platform group looks after a tenant on Platform > Tenants, its members, its name, its sharing and its delete, only when their platform group reaches it (platformGroupService.reaches). A membership there gives no right on the platform. Creating a tenant needs a platform admin or a platform group of all tenants (reachesAll), since a group of some tenants would never reach the new one.
platform-accounts:* follow the reach as well. accountsSeenReach in src/services/user/account-visibility.ts is the reach whose people someone sees through them, "all" also in All tenants. For a group of some tenants maySeeAccount, maySeePicture, getAllUsersModel and getPlatformGroupsModel keep to the accounts with a membership in a reached tenant (membersOfAny), an account in no tenant counting as one of the home tenant like in accountRefusal, their own account, only the reached tenants of each and the groups of those tenants. The members of the platform groups follow the same rule, and GET /api/sso-providers lists beside the providers only the people of accountsSeenReach and the own account (SsoProvidersScope.peopleReach), none of another tenant without platform-accounts:*, and hands the groups new people can start in (manage) to a platform admin alone. accountRefusal reads the reach of the actor (AccountActor.reach, from userService.accountsReachOf), and the Users tab marks a row it does not cover beyondReach, which accountLock reads. New user offers newUserTenants with newUserGroups from viewerReach, as createUser checks them: a reached tenant for platform-accounts:write, or one where the viewer holds users:write. Someone of a group of some tenants gives a platform group with setUserPlatformGroup only to an account accountRefusal lets them change, and createPlatformUser needs a reach that covers the home tenant, since the new account is in none and an account of no tenant counts as one of the home tenant, the same as New user without a group in the home tenant followed by setUserPlatformGroup. emailClaimScope lets a group of some tenants give the emails the providers of its tenants claim.
Groups for Tenants
A TenantGroupTemplate is a group the platform makes once (src/services/user/tenant-group-templates.ts): a name, unique among them and never Admin, the permissions of a group of a tenant without the settings of the instance (templateRights), and allTenants or the tenants of TenantGroupTemplateTenant. Each tenant it reaches has a real Group with templateId, so the checks of a tenant read it like any group of theirs, and @@unique([tenantId, templateId]) keeps one per tenant. groupService.update, delete and deleteMany refuse a linked group with fromPlatform, and the actions say so first.
preview lists what a save asks: a tenant to reach with an own group by the name (ReachClash, with its members and what they gain and lose) and a tenant it leaves where people hold it (ReachLeave, with the other groups there). save and remove take the answers as a ReachPlan and do all of it in one transaction in runAsSystem, refusing a missing answer. A group taken out moves its members and the sign-in providers that add people to it like groupService.delete, through settleProviders, and both return those providers as ssoProviders, which the actions write into the log of the platform with providerPlacementChanges and never hand to the browser. Unless by is a platform admin, assertManagersStay of group-service.ts refuses, in the transaction, a removal, a move into a group without users:write, a change of the permissions and a take-over that would leave a tenant without anyone who manages its members. seedTenantGroups gives a new tenant the groups for all tenants and its full access Admin, nothing else. The presets of New group for tenants are TENANT_GROUP_TEMPLATES in src/lib/auth/group-templates.ts, led by Client, and the editor starts from them through draftOf in tenant-group-template-model.ts, which leaves out what the viewer may not give.
The actions in actions/auth/tenant-group-template.ts need platform-accounts:write and cap a group for tenants like a platform group without rights on the platform (platformGroupRefusal), a move of members into a group by lockedTenantRight, and a take-over of a group of a tenant by takeOverRefusal: someone of a platform group takes over no group with fullAccess or with a permission their platform group does not give there. previewTenantGroupTemplate marks such a clash beyondYou, which the dialog offers only to leave out. A save or delete that removes the group from a tenant without a group for its members asks membershipEndRefusal for each of them first (membershipEndsRefusal), a move to another group keeps the membership and only needs moveRefusal. membershipEndsRefusal also hands the service the members of every group that moves or ends as checkedEnds, and removeGroup stops with MEMBERS_CHANGED (assertMembersUnchanged) when someone holds the group there who was not checked, also on a move, so a platform admin added after platformAdminRefusal is never moved by someone else. They write the group to the log of the home tenant with forTenants, and per tenant what happened to its group with fromPlatform.
src/lib/auth/account-access.ts guards the people:
accountRefusal(actor, target): a platform admin may change any account, nobody else the one of a platform admin. An account of a platform group is changed only by someone withplatform-accounts:writewho may give that platform group (platformGroupRefusalinplatform-group-caps.ts), never by the admin of a tenant. Whoever holdsplatform-accounts:writechanges any other account whose tenants their reach all covers (reachCoversAccount), every one for a platform group of all tenants, and an account of a platform group only within it too. Anyone else, and someone of a platform group beyond their reach, changes as a tenant admin withusers:writeonly an account whose memberships are all in their tenant. An account of no tenant counts as one of the home tenant, unless no platform role or group holds it and the sign-in provider of another tenant claims its email:userService.accountOfthen names that tenant (foreignClaimsOfinsrc/services/sso/email-domain-claims.ts), since the person of the email signs in there, andmembershipEndRefusalreads the same tenant throughplacelessTenantOf. The Users tab of the home tenant marks such a rowelsewhere, the lists of a platform group of some tenantsbeyondReachunless it reaches that tenant, andplacementRefusalinmember-rules.tsgives it no membership in another tenant, unless the actor is a platform admin or holdsplatform-tenants:writeorplatform-accounts:writewith a platform group that reaches that tenant. Every path that gives someone else a membership asks it:setMemberAs,tenantGroupRefusalandmoveUsersToGroup. After all that, nobody but a platform admin changes an account whose group in one of its tenants may do more than they may there (heldGroupBeyond, theroleRefusalof each group ofAccountTarget.groupsagainst the session in the tenant the actor works in andAccountActor.permissionsByTenantin the others), and the lists of users mark such a rowgroupBeyond(groupsBeyondViewer), whichaccountLockreads.userService.accountRefusalForreads both sides, the groups of the account, what the actor may in each of its tenants (permissionsIn) and the reach.roleRefusal(actor, group): a group is given only by someone who holds every permission it gives in that tenant, fromroleGrantsIn. On Platform > Tenants it is checked against what the viewer may do in that tenant (actorIninservices/tenant/member-admin.ts), for the group given and for the one taken.updateUserGroupandmoveUsersToGroupgive a group of the tenant only to its people,membershipService.isPersonOfwithpeopleOf: its members, and in the home tenant the accounts of no tenant. Only a platform admin gives one to a platform admin or to anyone else. The others join by an invitation or through Platform > Tenants.
A change of an account writes its audit entry into the log accountLogOrigin in src/services/audit/account-log.ts picks, read before the change: the tenant the viewer works in while every account is one of its people, else the home tenant (PLATFORM_LOG), see Audit Logging.
Recently deleted keeps an account where someone may bring it back, never by the tenant it was deleted from. keepInTrash writes its DeletedRecord in the system scope into the tenant of the snapshot: the one tenant of an account with exactly one membership, no platform role, no platform group and no API key in another tenant, else the home tenant with superAdminOnly. Undo of the viewer's own delete reads the newest entry of each account in every tenant with latestAccountEntries, keeps those mayRestoreAccount allows (a platform admin any, anyone else with users:write or platform-accounts:write an entry without superAdminOnly whose account accountRefusal lets them change with their reach), and restores them with restoreFromTrash(ids, { anyTenantAccounts: true }). An account comes back with an email whose domain the provider of another tenant claims only through a platform admin or someone of a platform group that reaches that tenant, claimedAccounts with emailClaimScope in actions/settings/trash.ts, and its API keys stay out of a tenant without addsApiKeys.
The API of the Platform
An MSP creates its customers from a script, so the tenants, their members, their invitations and what a tenant shares have routes beside Platform > Tenants:
| Route | Right | Service |
|---|---|---|
GET, POST /api/tenants, GET, PATCH, DELETE /api/tenants/[id] | platform-tenants:read or :write | src/services/tenant/tenant-admin.ts |
/api/tenants/[id]/members, /members/[userId] | platform-tenants:* and a reached tenant, or users:* in the tenant of the request | member-admin.ts, member-rules.ts |
/api/tenants/[id]/invitations, /invitations/[invitationId] | The same | invitation-admin.ts |
GET, PUT /api/adapters/[id]/share | platform-sharing:read or :write in the tenant of the connection | connectionShareService, share-audit.ts |
GET, POST /api/adapters/[id]/database-assignments/rules, DELETE .../rules/[ruleId] | The same in the tenant of the server | databaseAssignmentService, assignment-audit.ts |
The Server Actions of the pages and the routes call the same functions, which check the reach and write the audit entries, so a rule never lives twice. TenantAdminActor is who acts: the person of a session or the owner of a key, isPlatformAdmin only for a session, the API key the entries name, and the reach. A route reads the reach once with routeActor(ctx), an action leaves it out and managesTenant asks the platform group per check, as before. tenantRouteAccess(ctx, tenantId, write) in src/lib/auth/tenant-route-access.ts decides the way into a tenant's members and invitations: the platform first, with its right and a reached tenant, else the tenant itself with users:* while the request works in it, else 404 for a tenant beyond the reach of someone with the platform right and 403 for anyone else. serviceRefusal in src/lib/server/permission-refusal.ts answers a refusal of a service with 403, 400, 404 or 409 and its sentence.
Why a key gets these rights, and why only like this:
- Only in the home tenant. The home tenant is the platform. A key of a customer acting on the platform would let the admin of one customer reach the others, so
keyPlatformPermissionsoffersplatform-tenants:*andplatform-accounts:*only fortenantId === HOME_TENANT_ID, andcapToOwnerdrops them from any other key at every request, whatever it stores. - Never more than the owner.
ownerPermissionsIntakes them only from the owner's platform group, or from a platform admin, andvalidate()caps the key at every request, so a key whose owner loses the right or the platform group stops at its next request. The actions check a new key and a change against the same ceiling, and the key editor offers the areas only to an owner who holds one of them. - The reach of the owner. A right of the platform is bounded by
platformGroupService.reachOf(ownerId), the same reach the pages read. The key of someone of a platform group of selected tenants manages those tenants only, reads any other as gone and creates none, since it would never reach the new one. The key of a platform admin reaches every tenant. - Still no platform admin.
AuthContext.isPlatformAdminstays false for a key, so a key of a platform admin changes neither its owner's memberships nor those of a platform admin, and passes no check only a platform admin passes.roleRefusalcaps a group given on the platform way by what the owner may in that tenant (actorIn), on the way of the tenant by what the key holds. - Sessions only stay so. The sign-in providers, the configuration restore, the database download and the pages under
/api/platformrefuse every key, whatever it holds, since they decide who signs in as whom or bring back every tenant.
PATCH /api/tenants/[id] saves its fields one after the other with the functions of the Edit dialog and stops at the first refusal, naming the fields it saved under data.saved. DELETE refuses a tenant that holds anything without ?confirmed=true (TenantNotEmptyError, 409 with its tenantContents), like the dialog that lists it first. A key of the tenant itself gives a group only through tenantGroupRefusal, the rule of the Users tab: never its owner's own, only to the people of its tenant, never more than the key holds.
Adding a Model
A new model that holds data of a customer:
- Add
tenantId String @default("default")with an index, or a unique pair with its name, toprisma/schema.prisma, and write the migration - Add the model to
TENANT_MODELS. A model that belongs to a tenant through a parent goes intoCHILD_MODELSwith the relation instead - Delete its rows in
tenantService.delete, which removes a tenant table by table, since the tenant columns carry no relation - Seed a row of it for both tenants in
tests/unit/tenant/tenant-isolation.test.ts. Its first case fails for every model with atenantIdcolumn that is missing fromTENANT_MODELS, and the cases per model then prove that a second tenant neither reads, counts, changes nor deletes the row - Check every write that links the new rows to others with
assertOwnRecords, and every code path outside a request for its scope
Next Steps
- Permission System - the permissions and their checks
- API Keys & Webhooks - how a key finds its tenant
- Audit Logging - which tenant an entry lands in