Skip to content

Commit

Permalink
some security enhancements to better prevent path traversing
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Oct 4, 2019
1 parent fbdbfbd commit f6e292c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/objects/objectsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,17 @@ function sanitizePath(id, name, callback) {
}

if (id) {
id = id.replace(/\.\./g, ''); // do not allow to write in parent directories
id = id.replace(/[\]\[*,;'"`<>\\?\/]/g, ''); // remove all invalid characters from states plus slashes
}

if (name.indexOf('..') !== -1) {
name = path.normalize(name);
if (name.includes('..')) {
name = path.normalize('/' + name);
name = name.replace(/\\/g, '/');
}
if (name.includes('..')) {
// Also after normalization we still have .. in it - should not happen if normalize worked correctly
name = name.replace(/\.\./g, '');
name = path.normalize('/' + name);
name = name.replace(/\\/g, '/');
}
if (name[0] === '/') name = name.substring(1); // do not allow absolute paths
Expand Down
33 changes: 33 additions & 0 deletions test/lib/testObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,39 @@ function register(it, expect, context) {
});
});

it(textName + 'should read file and prevent path traversing', done => {
const objects = context.objects;
objects.readFile(testId, '../../myFile/abc1.txt', err => {
expect(err).to.be.not.ok;
expect(data).to.be.equal('dataInFile');
objects.readFile(testId, '/myFile/abc1.txt', err => {
expect(err).to.be.not.ok;
expect(data).to.be.equal('dataInFile');
objects.readFile(testId, '/../../myFile/abc1.txt', err => {
expect(err).to.be.not.ok;
expect(data).to.be.equal('dataInFile');
objects.readFile(testId, 'myFile/../blubb/../myFile/abc1.txt', err => {
expect(err).to.be.not.ok;
expect(data).to.be.equal('dataInFile');
objects.readFile(testId, '/myFile/../blubb/../myFile/abc1.txt', err => {
expect(err).to.be.not.ok;
expect(data).to.be.equal('dataInFile');
objects.readFile(testId, '../blubb/../myFile/abc1.txt', err => {
expect(err).to.be.not.ok;
expect(data).to.be.equal('dataInFile');
objects.readFile(testId, '/../blubb/../myFile/abc1.txt', err => {
expect(err).to.be.not.ok;
expect(data).to.be.equal('dataInFile');
done();
});
});
});
});
});
});
});
});

it(textName + 'should unlink file', done => {
const objects = context.objects;
objects.unlink(testId, 'myFile/abc1.txt', err => {
Expand Down

0 comments on commit f6e292c

Please sign in to comment.