Skip to content

Commit

Permalink
Merge pull request mongo-express#339 from dozoisch/dash_in_db_name
Browse files Browse the repository at this point in the history
Changed Database Name Validation Process
  • Loading branch information
dozoisch authored May 2, 2017
2 parents 9a4163e + e2a7f0c commit ef516ee
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
13 changes: 2 additions & 11 deletions lib/routes/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,8 @@ var routes = function () {
exp.addDatabase = function (req, res) {

var name = req.body.database;

if (name === undefined || name.length === 0) {
//TODO: handle error
console.error('That database name is invalid.');
req.session.error = 'That database name is invalid.';
return res.redirect('back');
}

//Database names must begin with a letter or underscore, and can contain only letters, underscores, numbers or dots
if (!name.match(/^[a-zA-Z_][a-zA-Z0-9._]*$/)) {
//TODO: handle error
if (!utils.isValidDatabaseName(name)) {
// TODO: handle error
console.error('That database name is invalid.');
req.session.error = 'That database name is invalid.';
return res.redirect('back');
Expand Down
2 changes: 1 addition & 1 deletion lib/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import $ from 'jquery';

$(document).ready(() => {
$('#database').popover({
content: 'Database names must begin with a letter or underscore, and can contain only letters, numbers, underscores and dots.',
content: 'Database names cannot be empty, must have fewer than 64 characters and must not contain /. "$*<>:|?',
});

const $deleteButton = $('.deleteButton');
Expand Down
13 changes: 13 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,16 @@ exports.roughSizeOfObject = function (object) {
exports.buildCollectionURL = function (base, dbName, collectionName) {
return base + 'db/' + dbName + '/' + encodeURIComponent(collectionName);
};

exports.isValidDatabaseName = function (name) {
if (!name || name.length > 63) {
return false;
}

// https://docs.mongodb.com/manual/reference/limits/#naming-restrictions
if (name.match(/[/. "$*<>:|?]/)) {
return false;
}

return true;
};
1 change: 0 additions & 1 deletion test/lib/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const expect = require('chai').expect;
const filters = require('../../lib/filters');


describe('filters', function () {
describe('to_display', function () {
it('should escape properly a string', () => {
Expand Down
35 changes: 35 additions & 0 deletions test/lib/utilsSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const expect = require('chai').expect;
const utils = require('../../lib/utils');

describe('utils', function () {
describe('isValidDatabaseName', function () {
it('should be valid', () => {
const validNames = [
'somedb_123123',
'somedb-123123',
'SOME_DB-1231',
'SOMEDB&1231',
];
validNames.forEach((n) => {
expect(utils.isValidDatabaseName(n)).to.equal(true, `Expected "${n}" to be a valid name`);
});
});

it('should be invalid', () => {
const invalidNames = [
'',
'somedb 123123',
'SOME$DB1231',
'SOMEDB<1231',
'SOMEDB>1231',
'1234567890123456789012345678901234567890123456789012345678901234',
'SOMEDB"123',
];
invalidNames.forEach((n) => {
expect(utils.isValidDatabaseName(n)).to.equal(false, `Expected "${n}" to be an invalid name`);
});
});
});
});

0 comments on commit ef516ee

Please sign in to comment.