Continuation of this blog : Cube.js + SQL Injection: Just Because It's an Analytics Layer Doesn't Mean Hackers Can't Do Math!
Multi-tenant security doesn't end when your SQL query is safe. Sometimes, the real problem is hiding in your cache.
Imagine this.
You've built a beautiful analytics dashboard using React and Cube.js.
You have:
- Authentication working.
- JWT validation in place.
- Row-level security configured.
- Tenant-specific filters applied.
- Database access restricted.
Your security checklist looks green. ✅
You deploy to production, and everything works perfectly.
Until one day, a customer reports:
"Why am I seeing another company's sales figures?"
You investigate the SQL queries. Everything looks correct.
You check the authentication. No issues.
You check the tenant ID. Correct.
Then you discover something interesting.
The database query wasn't the problem. The cached result was.
Welcome to one of the less obvious challenges in multi-tenant analytics: cache isolation.
1. Understanding the multi-tenant problem
Suppose you're building an analytics platform for multiple dealerships.
Each dealership is a tenant.
Let's take two dealerships:
| Metric | Dealership A | Dealership B |
|---|---|---|
| Total leads | 1,500 | 850 |
| Converted leads | 300 | 120 |
| Revenue | ₹25 lakh | ₹12 lakh |
Dealership A should never see Dealership B's numbers, and vice versa.
Your application uses Cube.js to process analytical queries.
A simplified architecture looks like this:
React Dashboard
|
v
Authentication
|
v
Cube.js
|
Tenant Security
|
v
Query Engine
|
v
Cache / Pre-aggregations
|
v
Database
Everything looks good.
But there's another question we need to ask:
Does the caching layer understand which tenant owns the data?
2. What happens when caching enters the picture?
Cube.js provides caching and pre-aggregations to improve analytical query performance.
Let's understand the difference.
Query result caching
When an analytical query executes, Cube can cache its result so that equivalent subsequent queries can be served faster.
Pre-aggregations
These are materialized, precomputed query results. Instead of repeatedly aggregating millions of database records, Cube can use an existing pre-aggregation.
For example, imagine calculating daily sales for an entire year.
Instead of repeatedly scanning millions of transactions, Cube can use a pre-aggregated result.
That's great for performance.
But performance optimizations must preserve the same security boundaries as the original query.
3. The cache isolation scenario
Let's walk through a hypothetical misconfiguration.
Both dealerships open the same dashboard.
They select:
- Date range: Last 30 days
- Metric: Total sales
- Region: South
Dealership A opens the dashboard first.
Cube processes the query and caches the result.
First request: Dealership A
Tenant: Dealership A
Dashboard: Sales
Filters: Last 30 days, South
Result: ₹25,00,000
|
v
Cached Result
Now Dealership B opens the same dashboard with identical filters.
If the cache key or pre-aggregation isolation is incorrectly configured and doesn't distinguish the tenant context, the cached result could be reused across tenants.
Potential security failure:
Tenant A requests dashboard
|
v
Query executes
|
v
Result cached: ₹25,00,000
|
|
Tenant B requests same dashboard
|
v
Incorrect cache reuse
|
v
Tenant B receives ₹25,00,000
Dealership B should have received ₹12,00,000.
This is a hypothetical failure scenario, not the default behaviour of Cube.js.
The important lesson is that tenant context must be preserved across query execution, caching and pre-aggregation selection.
4. Your security context should travel with the query
One important feature of Cube.js is its security context.
Typically, the application authenticates a user and issues a signed JWT containing trusted claims such as:
{
"user_id": "user_101",
"tenant_id": "dealership_A",
"role": "dealer_admin"
}
Cube.js validates the token and uses the security context to enforce data access restrictions.
The key principle:
Never trust a tenant ID simply because it came from the frontend.
A user can modify browser requests.
Instead:
- Authenticate the user.
- Determine their tenant on the trusted backend.
- Issue a signed token containing the tenant identity.
- Validate the token in Cube.js.
- Apply tenant restrictions through the security context.
For example, a conceptual row-level restriction could look like:
cube(`Orders`, {
sql: `
SELECT *
FROM orders
WHERE tenant_id = '${SECURITY_CONTEXT.tenantId.value}'
`
});
This is illustrative rather than a copy-paste configuration. Use Cube's supported security-context and query-rewriting mechanisms for your version, and ensure values are safely handled.
The objective is to make tenant isolation part of the data-access layer, rather than relying on frontend filters.
5. What about pre-aggregations?
This is where things get particularly interesting.
Pre-aggregations are materialized results that Cube can reuse to serve analytical queries.
When building a multi-tenant analytics platform, you need to consider how those pre-aggregations are scoped.
There are different approaches depending on your architecture.
Approach A: Shared pre-aggregations
A shared pre-aggregation can contain data for multiple tenants, provided it retains the tenant dimension and every query is correctly scoped by trusted tenant context.
For example:
daily_sales
--------------------------------
tenant_id | date | revenue
--------------------------------
A | 2026-11-01 | 100000
B | 2026-11-01 | 75000
The tenant restriction must still be enforced when the pre-aggregation is queried.
Approach B: Tenant-specific pre-aggregation schemas
For stronger physical separation, Cube supports configuring the pre-aggregation schema based on the security context.
Conceptually:
preAggregationsSchema: ({ securityContext }) => {
return `pre_aggregations_${securityContext.tenantId}`;
}
This is a simplified example. In production, validate and sanitize the tenant identifier before using it in a schema name.
This approach can help isolate pre-aggregation storage between tenants, although it introduces additional storage and operational considerations.
The right choice depends on tenant count, data volume, performance requirements and isolation requirements.
Important: Don't assume that manually adding a tenant ID to an arbitrary cache key is the fix. Cube has its own query caching and multi-tenancy mechanisms. Understand how your deployed Cube version incorporates security context into query execution and cache identity before implementing custom cache logic.
6. How do we test for cache leakage?
Here's a practical test worth adding to your multi-tenant application's security testing strategy.
Don't just test whether Tenant A can access its own data.
Test what happens when Tenant A has already populated the cache and Tenant B requests the same dashboard.
The two-tenant test
- Login as Tenant A.
Open the dashboard and execute the query.
- Populate the cache.
Ensure the query result is cached and the relevant pre-aggregation is available.
- Login as Tenant B.
Open the same dashboard with exactly the same filters and date range.
- Verify the response.
Confirm that Tenant B receives only its own data.
- Reverse the order.
Repeat with Tenant B first and Tenant A second.
Expected result:
Tenant A and Tenant B must always receive their own numbers, regardless of request order or cache state.
Also test:
- Cold cache versus warm cache.
- Query result cache versus pre-aggregation hits.
- Different users within the same tenant.
- Different tenants using identical query parameters.
- Cache refresh and pre-aggregation rebuild scenarios.
- Scheduled reports and exports, not just interactive dashboards.
A successful test with an empty cache isn't enough.
The real test is whether tenant isolation survives when the cache is already populated.
7. Don't forget database permissions
Now let's move one layer deeper.
Suppose your Cube.js service connects to your production database using an account that has full permissions.
It can:
- Read every table.
- Insert records.
- Update customer information.
- Delete records.
But your analytics dashboard only needs to read sales, leads and dealership information.
Why should it have permission to do anything else?
This is where the principle of least privilege comes in.
Create a dedicated database account for Cube.js.
| Permission | Recommended access |
|---|---|
| Read required analytics tables | Allowed |
| Read unrelated sensitive tables | Denied |
| Insert records | Denied |
| Update records | Denied |
| Delete records | Denied |
| Database administration | Denied |
Even if an application-level security control fails, restricted database permissions reduce the potential impact.
Remember, read-only access does not automatically guarantee tenant isolation. It limits what the service can do, not necessarily which tenant's records it can read.
8. Multi-tenant analytics security checklist
Before deploying a Cube.js application, consider these checks.
Authentication and authorization
- Validate signed JWTs.
- Derive tenant identity from trusted claims.
- Never trust frontend-supplied tenant IDs.
- Enforce row-level restrictions.
Caching and pre-aggregations
- Understand how security context affects query cache identity.
- Verify tenant isolation for shared pre-aggregations.
- Consider tenant-specific pre-aggregation schemas where appropriate.
- Test both cold-cache and warm-cache scenarios.
- Validate isolation across API instances and refresh workers.
Database security
- Use dedicated database credentials.
- Prefer read-only access for analytics workloads.
- Restrict access to required schemas and tables.
- Avoid unnecessary database privileges.
Testing and monitoring
- Test with at least two tenants.
- Test identical dashboard queries across tenants.
- Test exports and scheduled reports.
- Monitor unexpected access patterns.
9. Final thoughts
When building multi-tenant applications, we often focus heavily on authentication and SQL query security.
But security doesn't stop when the query is executed.
It continues through caching, pre-aggregations, background refreshes and database permissions.
A secure query with an incorrectly shared cached result can still expose sensitive information.
A read-only database account with unrestricted access to every tenant's data can still create a serious isolation problem.
The goal isn't just to make sure the query is safe.
It's to make sure the entire data journey respects the tenant boundary.
Because in a multi-tenant application, one customer's dashboard should never become another customer's data source.
Let's discuss!
For developers working with Cube.js or other analytics platforms:
- Do you prefer shared pre-aggregations or tenant-specific schemas?
- How do you test tenant isolation when caching is enabled?
- Have you encountered unexpected behaviour with cached analytical results?
I'd love to hear how other teams approach this problem.
Top comments (0)