Skip to content
Merged
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
10 changes: 8 additions & 2 deletions lib/methods/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function parseTableDefinition(def, verb, moduleName) {

// Validate "columns" property
let columns = def.columns;
if (!Array.isArray(columns) || !(columns = [...columns]).every(x => typeof x === 'string')) {
if (!Array.isArray(columns) || !isStringArray(columns = [...columns])) {
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "columns" property (should be an array of strings)`);
}
if (columns.length !== new Set(columns).size) {
Expand All @@ -69,7 +69,7 @@ function parseTableDefinition(def, verb, moduleName) {
let parameters;
if (hasOwnProperty.call(def, 'parameters')) {
parameters = def.parameters;
if (!Array.isArray(parameters) || !(parameters = [...parameters]).every(x => typeof x === 'string')) {
if (!Array.isArray(parameters) || !isStringArray(parameters = [...parameters])) {
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "parameters" property (should be an array of strings)`);
}
} else {
Expand Down Expand Up @@ -187,3 +187,9 @@ const { apply } = Function.prototype;
const GeneratorFunctionPrototype = Object.getPrototypeOf(function*(){});
const identifier = str => `"${str.replace(/"/g, '""')}"`;
const defer = x => () => x;
const isStringArray = (arr) => {
for (let i = 0; i < arr.length; ++i) {
if (typeof arr[i] !== 'string') return false;
}
return true;
};
13 changes: 13 additions & 0 deletions test/34.database.table.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ describe('Database#table()', function () {
expect(() => this.db.table('g', { parameters: ['x'], columns: ['x'], rows: function*(){} })).to.throw(TypeError);
expect(() => this.db.table('h', { parameters: [...Array(33)].map((_, i) => `p${i}`), columns: ['foo'], rows: function*(){} })).to.throw(RangeError);
});
it('should reject non-string parameters even if Array.prototype.every is polluted', function () {
const original = Array.prototype.every;
Array.prototype.every = () => true;
let thrown;
try {
this.db.table('a', { parameters: [new String('x')], columns: ['foo'], rows: function*(){} });
} catch (err) {
thrown = err;
} finally {
Array.prototype.every = original;
}
expect(thrown).to.be.an.instanceof(TypeError);
});
it('should throw an exception if the "rows" option is invalid', function () {
expect(() => this.db.table('a', { columns: ['x'] })).to.throw(TypeError);
expect(() => this.db.table('b', { columns: ['x'], rows: undefined })).to.throw(TypeError);
Expand Down