Skip to content

Unnecessary read restriction #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/query_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var step = require('step');
var assert = require('assert');
var PSQL = require('cartodb-psql');
var CachedQueryTables = require('../services/cached-query-tables');
const pgEntitiesAccessValidator = require('../services/pg-entities-access-validator');
const PGEntitiesAccessValidator = require('../services/pg-entities-access-validator');
const pgEntitiesAccessValidator = new PGEntitiesAccessValidator();
var queryMayWrite = require('../utils/query_may_write');

var formats = require('../models/formats');
Expand Down
46 changes: 29 additions & 17 deletions app/services/pg-entities-access-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,60 @@ const FORBIDDEN_ENTITIES = {
]
};

const Validator = {
function isForbiddenTable (schema, table) {
return FORBIDDEN_ENTITIES[schema] && FORBIDDEN_ENTITIES[schema].includes(table);
}

function isForbiddenSchema (schema) {
return FORBIDDEN_ENTITIES[schema] && FORBIDDEN_ENTITIES[schema][0] === '*';
}

function isForbiddenEntity (entity) {
const { schema_name: schema, table_name: table } = entity;

return isForbiddenSchema(schema) || isForbiddenTable(schema, table);
}

function isSystemEntity (entity) {
const { table_name: table } = entity;
return table.match(/\bpg_/);
}

module.exports = class PGEntitiesAccessValidator {
validate(affectedTables, authorizationLevel) {
let hardValidationResult = true;
let softValidationResult = true;

if (!!affectedTables && affectedTables.tables) {
if (global.settings.validatePGEntitiesAccess) {
hardValidationResult = this.hardValidation(affectedTables.tables);
hardValidationResult = this._hardValidation(affectedTables.tables);
}

if (authorizationLevel !== 'master') {
softValidationResult = this.softValidation(affectedTables.tables);
softValidationResult = this._softValidation(affectedTables.tables);
}
}

return hardValidationResult && softValidationResult;
},
}

hardValidation(tables) {
_hardValidation(tables) {
for (let table of tables) {
if (FORBIDDEN_ENTITIES[table.schema_name] && FORBIDDEN_ENTITIES[table.schema_name].length &&
(
FORBIDDEN_ENTITIES[table.schema_name][0] === '*' ||
FORBIDDEN_ENTITIES[table.schema_name].includes(table.table_name)
)
) {
if (isForbiddenEntity(table)) {
return false;
}
}

return true;
},
}

softValidation(tables) {
_softValidation(tables) {
for (let table of tables) {
if (table.table_name.match(/\bpg_/)) {
if (isSystemEntity(table)) {
return false;
}
}

return true;
}
};

module.exports = Validator;
};
101 changes: 101 additions & 0 deletions test/acceptance/export/involved-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require('../../helper');

const server = require('../../../app/server')();
const assert = require('../../support/assert');
const querystring = require('querystring');

describe('Read restriction on "geometry_columns"', function() {
describe('OGR formats', function () {
it('GET /api/v1/sql downloads KML file from a private table', function (done){
const query = querystring.stringify({
api_key: 1234,
q: "SELECT * FROM private_table LIMIT 1",
format: "kml",
filename: 'private_table'
});
assert.response(server, {
url: '/api/v1/sql?' + query,
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
},{ }, function(err, res){
assert.equal(res.statusCode, 200, res.body);

const cd = res.headers['content-disposition'];

assert.ok(/filename=private_table.kml/gi.test(cd), cd);
assert.equal(res.headers['content-type'], 'application/kml; charset=utf-8');

done();
});
});

it('GET /api/v1/sql downloads a KML file from a public table', function (done){
const query = querystring.stringify({
q: "SELECT * FROM populated_places_simple_reduced LIMIT 1",
format: "kml",
filename: 'populated_places_simple_reduced'
});
assert.response(server, {
url: '/api/v1/sql?' + query,
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
},{ }, function(err, res){
assert.equal(res.statusCode, 200, res.body);

const cd = res.headers['content-disposition'];

assert.ok(/filename=populated_places_simple_reduced.kml/gi.test(cd), cd);
assert.equal(res.headers['content-type'], 'application/kml; charset=utf-8');

done();
});
});

it('GET /api/v1/sql downloads a KML file from "geometry_columns" view', function (done){
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @pramsey!

I've started fixing #513, but I realized that I'm not sure which is the real use case that you want to be tackled. OGR driver only works when you want to download specific formats, such as: kml, csv, shp, etc... When you try to export a kml file of a private table w/o api_key it fails. If you send the request w/ your api_key it works. And if you want to export data from a public table you can request it with and without the master api_key. So far so good, no system tables are involved for public tables when you use the OGR driver.

When I try to send the folowing query w/o api_key (see this tests ^^): SELECT * FROM geometry_columns LIMIT 1 it fails with system tables are forbidden. I understand that this is the use case that raises this issue, doesn't it?

If not, please help to me to understand better this issue. I've implemented a few tests with the examples that I just descibed.

Thanks in advance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For folks using OGR in a "not CARTO" context, the first thing OGR will do is ask CARTO, "hey, what tables are there?" (this is what happens when an OGRDataSource is instantiated). There's even a super ugly hack in OGR where it tries to sneak a peak at the system tables without letting on to CARTO that it is doing so.

So, running EXPORT CARTO_API_KEY=dfadfa ogrinfo CARTO:pramsey should return to e a list of tables I can read from, for example. Similarly, if I'm using an OGR driver to feed QGIS, when I pop open the layer selector I should see all the layers I have read access to.

Now, as it stands we're not going to re-open access to the pg_* tables, but the geometry_columns view is already a way to get a list of layers that are visible to the current user, so that's not a bad thing to open up.

Anyways, the use case is "given this key, what can I see?" and the apps that use it are things like ogrinfo and any other "select the thing you want" style application. Otherwise you're expecting the user to arrive with an a priori list of things she has access to, which may not be the case.

const query = querystring.stringify({
q: "SELECT * FROM geometry_columns LIMIT 1",
format: "kml",
filename: 'geometry_columns'
});

assert.response(server, {
url: '/api/v1/sql?' + query,
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
},{ }, function(err, res){
assert.equal(res.statusCode, 200, res.body);

const cd = res.headers['content-disposition'];

assert.ok(/filename=geometry_columns.kml/gi.test(cd), cd);
assert.equal(res.headers['content-type'], 'application/kml; charset=utf-8');

done();
});
});

it('GET /api/v1/sql downloads a KML file with master api_key from "geometry_columns" view', function (done){
const query = querystring.stringify({
api_key: 1234,
q: "SELECT * FROM geometry_columns LIMIT 1",
format: "kml",
filename: 'geometry_columns'
});

assert.response(server, {
url: '/api/v1/sql?' + query,
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
},{ }, function(err, res){
assert.equal(res.statusCode, 200, res.body);

const cd = res.headers['content-disposition'];

assert.ok(/filename=geometry_columns.kml/gi.test(cd), cd);
assert.equal(res.headers['content-type'], 'application/kml; charset=utf-8');

done();
});
});
});
});
60 changes: 31 additions & 29 deletions test/unit/pg-entities-access-validator.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const pgEntitiesAccessValidator = require('../../app/services/pg-entities-access-validator');
const PGEntitiesAccessValidator = require('../../app/services/pg-entities-access-validator');

const fakeAffectedTables = [{
schema_name: 'schema',
Expand Down Expand Up @@ -72,6 +72,8 @@ const fakeAffectedTablesTopologyKO = [


describe('pg entities access validator with validatePGEntitiesAccess enabled', function () {
const pgEntitiesAccessValidator = new PGEntitiesAccessValidator();

before(function() {
global.settings.validatePGEntitiesAccess = true;
});
Expand Down Expand Up @@ -99,75 +101,75 @@ describe('pg entities access validator with validatePGEntitiesAccess enabled', f
it('validate function: should not be validated', function () {
let authorizationLevel = 'master';
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesCarto }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesCarto }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesCartodbKO }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesCartodbKO }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesPgcatalog }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesPgcatalog }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesInfo }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesInfo }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesPublicKO }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesPublicKO }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesTopologyKO }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesTopologyKO }, authorizationLevel),
false
);


authorizationLevel = 'regular';
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesCarto }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesCarto }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesCartodbKO }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesCartodbKO }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesPgcatalog }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesPgcatalog }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesInfo }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesInfo }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesPublicKO }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesPublicKO }, authorizationLevel),
false
);
assert.strictEqual(
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesTopologyKO }, authorizationLevel),
pgEntitiesAccessValidator.validate({ tables: fakeAffectedTablesTopologyKO }, authorizationLevel),
false
);
});

it('hardValidation function', function () {
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTables), true);
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesCartodbOK), true);
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesPublicOK), true);
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesTopologyOK), true);

assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesCarto), false);
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesCartodbKO), false);
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesPgcatalog), false);
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesInfo), false);
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesPublicKO), false);
assert.strictEqual(pgEntitiesAccessValidator.hardValidation(fakeAffectedTablesTopologyKO), false);
it('_hardValidation function', function () {
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTables), true);
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesCartodbOK), true);
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesPublicOK), true);
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesTopologyOK), true);

assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesCarto), false);
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesCartodbKO), false);
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesPgcatalog), false);
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesInfo), false);
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesPublicKO), false);
assert.strictEqual(pgEntitiesAccessValidator._hardValidation(fakeAffectedTablesTopologyKO), false);
});

it('softValidation function', function () {
assert.strictEqual(pgEntitiesAccessValidator.softValidation(fakeAffectedTablesCartodbKO), true);
assert.strictEqual(pgEntitiesAccessValidator.softValidation(fakeAffectedTablesPgcatalog), false);
it('_softValidation function', function () {
assert.strictEqual(pgEntitiesAccessValidator._softValidation(fakeAffectedTablesCartodbKO), true);
assert.strictEqual(pgEntitiesAccessValidator._softValidation(fakeAffectedTablesPgcatalog), false);
});

});