Skip to content

Commit

Permalink
feat(service): listFolderContentsFromPath returns empty list for bad …
Browse files Browse the repository at this point in the history
…paths
  • Loading branch information
jezhiggins committed May 29, 2020
1 parent 4b418aa commit 1b90b09
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
37 changes: 25 additions & 12 deletions lib/components/services/localfilestorage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,38 @@ class LocalFilestorage {
get rootPath () { return this.rootPath_ }

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}`)
}
const rootedPath = this.resolvePath(folderPath)
if (!rootedPath) throw new Error(`Bad folder path - ${folderPath}`)

await fsp.mkdir(rootedPath, { recursive: true })
} // ensureFolderPath

async listFolderContentsFromPath (folderPath) {
const rootedPath = path.join(this.rootPath_, folderPath)
const rootedPath = this.resolvePath(folderPath)
if (!rootedPath) return []

try {
const contents = (await fsp.readdir(rootedPath, {withFileTypes: true}))
.filter(content => content.isFile() || content.isDirectory())
.map(content => {
return {Name: content.name}
})

return contents
} catch (e) {
if (e.code === 'ENOENT') return [] // path doesn't exist
throw e
}
} // listFolderContentsFromPath

const contents = (await fsp.readdir(rootedPath, { withFileTypes: true }))
.filter(content => content.isFile() || content.isDirectory())
.map(content => { return { Name: content.name } })
resolvePath (folderPath) {
const rootedPath = path.join(this.rootPath_, folderPath)

return contents
} // listFolderContentsFromPath
const isRooted = path.relative(this.rootPath_, rootedPath)
return !(isRooted.includes('.') || isRooted === '')
? rootedPath
: null
} // rootPath
} // class LocalFilestorage

function cloudstorageService (options) {
Expand Down
21 changes: 16 additions & 5 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,21 @@ describe('exercise methods', () => {
})
})

/*
async listFolderContentsFromPath (path) {
return this.provider_.listFolderContentsFromPath(path)
} // listFolderContentsFromPath
*/
describe('return nothing for bad paths', () => {
const badPaths = [
'..',
'../..',
'../../../poop',
'start/out/ok/but/../../../../../../../oh',
'good/path/but/which/does/not/exist'
]

for (const p of badPaths) {
it(p, async () => {
const list = await localstorage.listFolderContentsFromPath(p)
expect(list).to.have.length(0)
})
}
})
})
})

0 comments on commit 1b90b09

Please sign in to comment.