Possible path traversal when using an untrusted tenant name
Description
activerecord-tenanted interpolated a tenant name into the SQLite database path without rejecting . or ... With a tenant named .., create_tenant created and migrated a database one directory above the per-tenant directory, tenants did not list it, and destroy_tenant deleted the file at that path. Under a URI database template, SQLite decoded %2e%2e to .., and a ? or # truncated the path, so foo?bar and foo?baz shared one database. The Active Storage disk service also interpolated the name into its root, so with_tenant("../../shared") reached a directory outside the storage tree.
activerecord-tenanted 0.9.0 accepts only tenant names made of letters, digits, -, ., _, and ~ that do not begin with . or ~, and raises BadTenantNameError for any other name.
Severity
The maintainers have evaluated this as medium severity, CVSS 3.1 4.8 (AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:L). Reaching it requires an application that passes an unvalidated, user-chosen string to create_tenant, destroy_tenant, or with_tenant. A request whose subdomain resolved to .. could not create a tenant: the tenant selector returned a 404 unless a database already existed at the escaped path. Two tenants shared a database only when both names contained ? and matched up to it.
Mitigation
Upgrade to activerecord-tenanted 0.9.0 or later. This is a breaking change: tenants whose names the new rule rejects are no longer served, and tenants skips them with a logged warning. Rename them before upgrading.
As a workaround for earlier versions, validate tenant names before passing them to the gem, for example against /\A[A-Za-z0-9_-][A-Za-z0-9._~-]*\z/.
Credit
This issue was responsibly reported independently by @izaal10 and @manus-pi.