CVE-2022-23510: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (CWE-89)
value: cube-js is a headless business intelligence platform. In version 0.31.23 all authenticated Cube clients could bypass SQL row-level security and run arbitrary SQL via the newly introduced /v1/sql-runner endpoint. This issue has been resolved in version 0.31.24. Users are advised to either upgrade to 0.31.24 or to downgrade to 0.31.22. There are no known workarounds for this vulnerability.
CWE description: The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
CVSS v3 base score: 8.8
Published: 2022-12-09T23:15Z
Repository: https://github.com/cube-js/cube.js
Language: TypeScript
Files changed: packages/cubejs-api-gateway/src/gateway.ts, packages/cubejs-server-core/src/core/CompilerApi.js, packages/cubejs-server-core/test/unit/index.test.ts
Commit date: 2022-12-09 10:53:23 -0800
Commit message: Revert "feat(api-gateway, server-core): Added dataSources list to extended meta (#5743)" (#5785)

This reverts commit 2c5db32f2ded074ebe5e83668eee8c024101240b.
Diff: diff --git a/packages/cubejs-api-gateway/src/gateway.ts b/packages/cubejs-api-gateway/src/gateway.ts
index 56caf82fd..498d333ea 100644
--- a/packages/cubejs-api-gateway/src/gateway.ts
+++ b/packages/cubejs-api-gateway/src/gateway.ts
@@ -446,11 +446,11 @@ class ApiGateway {
 
     try {
       const metaConfigExtended = await this.getCompilerApi(context).metaConfigExtended({
         requestId: context.requestId,
       });
-      const { metaConfig, cubeDefinitions, dataSources } = metaConfigExtended;
+      const { metaConfig, cubeDefinitions } = metaConfigExtended;
 
       const cubes = this.filterVisibleItemsInMeta(context, metaConfig)
         .map((meta) => meta.config)
         .map((cube) => ({
           ...transformCube(cube, cubeDefinitions),
@@ -464,11 +464,11 @@ class ApiGateway {
             ...transformSegment(segment, cubeDefinitions),
           })),
           joins: transformJoins(cubeDefinitions[cube.name]?.joins),
           preAggregations: transformPreAggregations(cubeDefinitions[cube.name]?.preAggregations),
         }));
-      res({ cubes, dataSources });
+      res({ cubes });
     } catch (e) {
       this.handleError({
         e,
         context,
         res,
diff --git a/packages/cubejs-server-core/src/core/CompilerApi.js b/packages/cubejs-server-core/src/core/CompilerApi.js
index 3dc47e3b2..63069b13a 100644
--- a/packages/cubejs-server-core/src/core/CompilerApi.js
+++ b/packages/cubejs-server-core/src/core/CompilerApi.js
@@ -64,14 +64,10 @@ export class CompilerApi {
     }
 
     return this.compilers;
   }
 
-  async getDataSources(cubeEvaluator) {
-    return Promise.all(cubeEvaluator.cubeNames().map(async cube => cubeEvaluator.cubeFromPath(cube).dataSource ?? 'default'));
-  }
-
   async createQueryFactory(compilers) {
     const { cubeEvaluator } = compilers;
 
     const cubeToQueryClass = R.fromPairs(
       await Promise.all(
@@ -182,14 +178,13 @@ export class CompilerApi {
   async metaConfig(options) {
     return (await this.getCompilers(options)).metaTransformer.cubes;
   }
 
   async metaConfigExtended(options) {
-    const { metaTransformer, cubeEvaluator } = await this.getCompilers(options);
+    const { metaTransformer } = await this.getCompilers(options);
     return {
       metaConfig: metaTransformer?.cubes,
-      dataSources: await this.getDataSources(cubeEvaluator),
       cubeDefinitions: metaTransformer?.cubeEvaluator?.cubeDefinitions,
     };
   }
 
   canUsePreAggregationForTransformedQuery(transformedQuery, refs) {
diff --git a/packages/cubejs-server-core/test/unit/index.test.ts b/packages/cubejs-server-core/test/unit/index.test.ts
index 7f803d76a..7b49cdb52 100644
--- a/packages/cubejs-server-core/test/unit/index.test.ts
+++ b/packages/cubejs-server-core/test/unit/index.test.ts
@@ -50,38 +50,10 @@ cube('Bar', {
 `,
     },
   ]),
 };
 
-const repositoryWithDataSource: SchemaFileRepository = {
-  localPath: () => __dirname,
-  dataSchemaFiles: () => Promise.resolve([
-    {
-      fileName: 'main.js', content: `
-cube('Bar', {
-  sql: 'select * from bar',
-  
-  measures: {
-    count: {
-      type: 'count'
-    }
-  },
-
-  dataSource: 'dataSource',
-
-  dimensions: {
-    time: {
-      sql: 'timestamp',
-      type: 'time'
-    }
-  }
-});
-`,
-    },
-  ]),
-};
-
 const repositoryWithoutContent: SchemaFileRepository = {
   localPath: () => __dirname,
   dataSchemaFiles: () => Promise.resolve([{ fileName: 'main.js', content: '' }]),
 };
 
@@ -361,33 +333,10 @@ describe('index.test', () => {
     test('CompilerApi metaConfigExtended', async () => {
       const metaConfigExtended = await compilerApi.metaConfigExtended({ requestId: 'XXX' });
       expect(metaConfigExtended).toHaveProperty('metaConfig');
       expect(metaConfigExtended.metaConfig.length).toBeGreaterThan(0);
       expect(metaConfigExtended).toHaveProperty('cubeDefinitions');
-      expect(metaConfigExtended).toHaveProperty('dataSources');
-      expect(metaConfigExtended.dataSources).toEqual(['default']);
-      expect(metaConfigExtendedSpy).toHaveBeenCalled();
-      metaConfigExtendedSpy.mockClear();
-    });
-  });
-  
-  describe('CompilerApi with dataSource', () => {
-    const logger = jest.fn(() => {});
-    const compilerApi = new CompilerApi(
-      repositoryWithDataSource,
-      async () => 'mysql',
-      { logger }
-    );
-    const metaConfigExtendedSpy = jest.spyOn(compilerApi, 'metaConfigExtended');
-
-    test('CompilerApi metaConfigExtended', async () => {
-      const metaConfigExtended = await compilerApi.metaConfigExtended({ requestId: 'XXX' });
-      expect(metaConfigExtended).toHaveProperty('metaConfig');
-      expect(metaConfigExtended.metaConfig.length).toBeGreaterThan(0);
-      expect(metaConfigExtended).toHaveProperty('cubeDefinitions');
-      expect(metaConfigExtended).toHaveProperty('dataSources');
-      expect(metaConfigExtended.dataSources).toEqual(['dataSource']);
       expect(metaConfigExtendedSpy).toHaveBeenCalled();
       metaConfigExtendedSpy.mockClear();
     });
   });
 

Vulnerable code:       const { metaConfig, cubeDefinitions, dataSources } = metaConfigExtended;
      res({ cubes, dataSources });
  async getDataSources(cubeEvaluator) {
    return Promise.all(cubeEvaluator.cubeNames().map(async cube => cubeEvaluator.cubeFromPath(cube).dataSource ?? 'default'));
  }

    const { metaTransformer, cubeEvaluator } = await this.getCompilers(options);
      dataSources: await this.getDataSources(cubeEvaluator),
const repositoryWithDataSource: SchemaFileRepository = {
  localPath: () => __dirname,
  dataSchemaFiles: () => Promise.resolve([
    {
      fileName: 'main.js', content: `
cube('Bar', {
  sql: 'select * from bar',
  
  measures: {
    count: {
      type: 'count'
    }
  },

  dataSource: 'dataSource',

  dimensions: {
    time: {
      sql: 'timestamp',
      type: 'time'
    }
  }
});
`,
    },
  ]),
};

      expect(metaConfigExtended).toHaveProperty('dataSources');
      expect(metaConfigExtended.dataSources).toEqual(['default']);
      expect(metaConfigExtendedSpy).toHaveBeenCalled();
      metaConfigExtendedSpy.mockClear();
    });
  });
  
  describe('CompilerApi with dataSource', () => {
    const logger = jest.fn(() => {});
    const compilerApi = new CompilerApi(
      repositoryWithDataSource,
      async () => 'mysql',
      { logger }
    );
    const metaConfigExtendedSpy = jest.spyOn(compilerApi, 'metaConfigExtended');

    test('CompilerApi metaConfigExtended', async () => {
      const metaConfigExtended = await compilerApi.metaConfigExtended({ requestId: 'XXX' });
      expect(metaConfigExtended).toHaveProperty('metaConfig');
      expect(metaConfigExtended.metaConfig.length).toBeGreaterThan(0);
      expect(metaConfigExtended).toHaveProperty('cubeDefinitions');
      expect(metaConfigExtended).toHaveProperty('dataSources');
      expect(metaConfigExtended.dataSources).toEqual(['dataSource']);
Fixed code:       const { metaConfig, cubeDefinitions } = metaConfigExtended;
      res({ cubes });
    const { metaTransformer } = await this.getCompilers(options);