DEV Community

Shanthi's Dev Diary
Shanthi's Dev Diary

Posted on

Cube.js + SQL Injection: Just Because It's an Analytics Layer Doesn't Mean Hackers Can't Do Math! 😂

Your dashboard looks beautiful. Your charts are loading. Your KPIs are green. But is your database safe?

Let's talk about something every developer should think about when building analytics dashboards: SQL Injection in Cube.js applications.

Because let's be honest. We spend hours making dashboards look beautiful, but sometimes security gets just 10 minutes before deployment. 😅

First, what exactly is Cube.js?

If you've worked with React dashboards, chances are you've come across Cube.js.

Cube.js is an open-source headless BI and analytics platform that sits between your frontend and database.

A typical architecture looks like this:

React Application
       |
       | API Request
       v
     Cube.js
       |
       | SQL Queries
       v
    Database
Enter fullscreen mode Exit fullscreen mode

Cube.js helps developers:

  • Build reusable data models.
  • Define measures and dimensions.
  • Handle analytical queries.
  • Manage caching and pre-aggregations.
  • Implement data access controls.

Basically, it saves us from writing the same complex analytical SQL queries repeatedly.

Sounds great, right?

But here's the question:

Does Cube.js automatically protect us from SQL injection?

Spoiler: NO!

Wait... Can SQL Injection Actually Happen in Cube.js?

Yes!

Cube.js provides a structured analytics layer, but security still depends on how we configure and use it.

Imagine a developer writing dynamic SQL like this:

const customerId = req.query.customerId;

const sql = `
  SELECT *
  FROM customers
  WHERE customer_id = '${customerId}'
`;
Enter fullscreen mode Exit fullscreen mode

Looks innocent.

Until someone sends malicious input instead of a normal customer ID.

The problem isn't React. The problem isn't the dashboard. The problem is trusting user input and injecting it directly into SQL.

And that's how a normal filter dropdown can become a security incident. 🙃

Developer lesson: Never concatenate untrusted input into SQL.

Where Should We Be Careful in Cube.js?

Let's look at five areas.

1. Dynamic SQL in Cube Data Models

Cube.js supports SQL-based data models.

If you're constructing SQL dynamically, be careful about how user-controlled values are introduced.

Avoid blindly concatenating values into SQL strings.

Use safe parameterization or the appropriate query-building mechanisms supported by your database driver.

2. User-Defined Filters

Suppose your dashboard has filters for:

  • Customer ID
  • Region
  • Date range
  • Product category

Don't assume that because the filter comes through Cube's query API, every possible input is automatically safe.

Validate inputs and restrict which dimensions and measures users can query.

3. Security Context and Row-Level Security

Imagine a multi-tenant application.

Company A should see only Company A's data.

Company B should see only Company B's data.

Cube.js supports security contexts that can help enforce this separation.

For example:

cube(`Orders`, {
  sql: `SELECT * FROM orders`,

  measures: {
    count: {
      type: `count`
    }
  },

  dimensions: {
    companyId: {
      sql: `company_id`,
      type: `string`
    }
  },

  // Apply tenant restrictions through
  // the appropriate security configuration.
});
Enter fullscreen mode Exit fullscreen mode

The important part is ensuring that tenant identity comes from a trusted, verified authentication context — not from a frontend parameter that users can modify.

Remember:

Hiding another tenant's data in the UI is not security.

4. Custom API Endpoints

If you're using Node.js or Express alongside Cube.js, pay attention to custom endpoints.

For example:

app.get('/customers', async (req, res) => {
  // Validate inputs.
  // Authorize the user.
  // Use parameterized database queries.
});
Enter fullscreen mode Exit fullscreen mode

Never assume that having Cube.js in your architecture automatically secures your separate Express APIs.

Each entry point needs its own security checks.

5. Database Permissions

Here's another important one.

Why give your analytics service permission to delete production records if it only needs to read them?

Use least-privilege database credentials.

For read-only analytics workloads, use a read-only database account wherever practical.

Your database shouldn't have superhero-level permissions just because your dashboard needs to display a pie chart. 😂

A Real Cube.js Security Incident: CVE-2022-23510

This isn't just theoretical.

In December 2022, Cube.js had a reported security vulnerability affecting version 0.31.23.

The issue allowed authenticated Cube clients to bypass row-level security and execute arbitrary SQL through the /v1/sql-runner endpoint.

The vulnerability was addressed in version 0.31.24 by reverting the problematic change.

You can read the original advisory here:

This is a useful reminder that even well-designed frameworks can have security vulnerabilities.

Keeping dependencies updated is not optional housekeeping. It's part of engineering.

My Cube.js Security Checklist

Before deploying your next analytics dashboard, check these:

  • Keep Cube.js and dependencies updated.
  • Enable proper authentication.
  • Validate JWT tokens on the server.
  • Implement row-level security where required.
  • Never trust tenant IDs sent directly from the frontend.
  • Avoid unsafe dynamic SQL construction.
  • Restrict access to administrative and SQL execution endpoints.
  • Use least-privilege database credentials.
  • Test access restrictions between different users and tenants.

So, Do We Need Node.js + Express Alongside Cube.js?

Not necessarily!

If your application mainly needs analytics, dashboards, reports, aggregations and filtering, Cube.js can serve as your analytics backend.

But if you need additional business APIs, CRUD operations, integrations or custom workflows, Node.js + Express can complement Cube.js.

Choose the architecture based on your requirements, not because adding another backend framework makes the architecture look more enterprise-ready. 😉

Final Thoughts

Cube.js makes analytics development easier, but it doesn't make security someone else's responsibility.

As developers, we should stop thinking:

"I'm using a framework, so security is handled."

Instead, ask:

"What happens if someone sends something I never expected?"

Because a dashboard showing incorrect numbers is a bug.

A dashboard exposing someone else's confidential data is a much bigger problem.

Build beautiful dashboards. Write clean queries. And please, don't let your database become an open buffet! 😂


Let's Discuss!

Have you used Cube.js in production?

  • Have you implemented row-level security?
  • Do you prefer Cube.js or custom Node.js APIs for analytics?
  • Have you encountered any security challenges while building data-driven applications?

Share your experiences in the comments. Let's learn from each other!

Top comments (7)

Collapse
 
mujtbabbas profile image
Syed Mujtaba •

Good framing — the "analytics is read-only, so it's safe" assumption is exactly how these slip through. One gap I'd add: pre-aggregations. If Cube pre-aggregates across tenants and the cache key doesn't include the tenant, the row-level check in the query never runs for the cached result — tenant isolation has to live in the cache key, not just in the query. The test that catches it is simple: two tenants, same dashboard, same filters, and confirm tenant B can never see tenant A's numbers from a warm cache. Same idea for the database user Cube connects with: read-only, and scoped to the tables the dashboards actually need, so a filter escape still can't reach anything else.

Collapse
 
shantih_palani profile image
Shanthi's Dev Diary •

That's a great addition! I hadn't covered the cache isolation aspect in the blog.

You're absolutely right that tenant isolation shouldn't stop at query-level security. Pre-aggregations and caching introduce another layer that needs to be considered, especially in multi-tenant applications.

The two-tenant, same-dashboard, same-filters test with a warm cache is a really practical way to validate this.

And the least-privilege database user is equally important. Even if an application-level security check fails, restricting database permissions helps limit the impact.

Thanks for bringing up these points! This adds an important dimension to the discussion. Definitely something worth exploring in more detail. 🙌

Collapse
 
mujtbabbas profile image
Syed Mujtaba •

Glad it landed. The cache key part is the one that usually gets missed, since the query level check looks complete on paper while the cached result quietly skips it. If you dig into it further, the two tenant test with a warm cache is the fastest way to prove the gap, and scoping the Cube database user to just the tables the dashboards need is cheap insurance either way. Happy to compare notes if you write the follow-up.

Thread Thread
 
shantih_palani profile image
Shanthi's Dev Diary •

Here is the followup blog: dev.to/shantih_palani/cubejs-your-...

Thread Thread
 
mujtbabbas profile image
Syed Mujtaba •

Thanks for writing it, Shanthi. I read through it and the two tenant test stood out to me, since it makes the warm cache case concrete instead of theoretical. I also liked that you covered shared pre-aggregations and tenant specific schemas side by side, because that choice really depends on tenant count and how strict the isolation needs to be. The least privilege part at the end is a good reminder too. Nice follow up.

Thread Thread
 
shantih_palani profile image
Shanthi's Dev Diary •

Thank you

Thread Thread
 
mujtbabbas profile image
Syed Mujtaba •

You are welcome, Shanthi. Thanks again for the follow up. I enjoyed reading it, and the two tenant test is a useful one to keep in mind.