Skip to content

Commit

Permalink
feat(service): ensureFolderPath
Browse files Browse the repository at this point in the history
  • Loading branch information
jezhiggins committed May 29, 2020
1 parent 304a3ac commit 12a0aea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
5 changes: 5 additions & 0 deletions lib/components/services/localfilestorage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class LocalFilestorage {
async ensureFolderPath (folderPath) {
const rootedPath = path.join(this.rootPath_, folderPath)

const isRooted = path.relative(this.rootPath_, rootedPath)
if (isRooted.includes('.') || isRooted === '') {
throw new Error(`Bad folder path - ${folderPath}`)
}

await fsp.mkdir(rootedPath, { recursive: true })
}
} // class LocalFilestorage
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@semantic-release/git": "9.0.0",
"@wmfs/tymly": "1.140.0",
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"chai-string": "1.5.0",
"codecov": "3.7.0",
"conventional-changelog-metahub": "4.0.1",
Expand Down
50 changes: 34 additions & 16 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'))
const expect = chai.expect

const path = require('path')
Expand Down Expand Up @@ -36,27 +37,44 @@ describe('exercise methods', () => {
}) // beforeEach

describe('ensureFolderPath', () => {
it('absolute paths are rooted in root path', async () => {
await localstorage.ensureFolderPath('/absolute')
describe('create good paths', () => {
it('absolute paths are rooted in root path', async () => {
await localstorage.ensureFolderPath('/absolute')

expect(fs.existsSync(path.join(rootPath, 'absolute'))).to.be.true()
})
expect(fs.existsSync(path.join(rootPath, 'absolute'))).to.be.true()
})

it('relative paths are resolved relative to root path', async () => {
await localstorage.ensureFolderPath('relative')
it('relative paths are resolved relative to root path', async () => {
await localstorage.ensureFolderPath('relative')

expect(fs.existsSync(path.join(rootPath, 'relative'))).to.be.true()
})
expect(fs.existsSync(path.join(rootPath, 'relative'))).to.be.true()
})

it('can create nested directories', async () => {
const folderPath = 'one/two/three/deep'
await localstorage.ensureFolderPath(folderPath)
it('can create nested directories', async () => {
const folderPath = 'one/two/three/deep'
await localstorage.ensureFolderPath(folderPath)

let checkPath = rootPath
for (const p of folderPath.split('/')) {
checkPath = path.join(checkPath, p)
expect(fs.existsSync(checkPath)).to.be.true()
}
let checkPath = rootPath
for (const p of folderPath.split('/')) {
checkPath = path.join(checkPath, p)
expect(fs.existsSync(checkPath)).to.be.true()
}
})
}) // good paths

describe('reject naughty paths that try to escape rootPath', () => {
const badPaths = [
'.',
'..',
'../../../poop',
'start/out/ok/but/../../../../../../../oh'
]

for (const p of badPaths) {
it(p, () => {
return expect(localstorage.ensureFolderPath(p)).to.eventually.be.rejectedWith(Error)
})
} // for ...
})
})
})

0 comments on commit 12a0aea

Please sign in to comment.