Skip to content

Commit 8a8045c

Browse files
authored
feat: sanitise filename (#49)
1 parent 8637cda commit 8a8045c

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import queueMicrotask from 'queue-microtask'
77
import RAF from 'random-access-file'
88
import randombytes from 'randombytes'
99
import thunky from 'thunky'
10+
import getFileRegex from 'filename-reserved-regex'
11+
12+
const RESERVED_FILENAME_REGEX = getFileRegex()
1013

1114
let TMP
1215
try {
@@ -38,10 +41,12 @@ export default class Storage {
3841
file.offset = prevFile.offset + prevFile.length
3942
}
4043
}
41-
let newPath = file.path
44+
let newPath = path.dirname(file.path)
45+
const filename = path.basename(file.path)
4246
if (this.path) {
43-
newPath = this.addUID ? path.resolve(path.join(this.path, this.name, file.path)) : path.resolve(path.join(this.path, file.path))
47+
newPath = this.addUID ? path.resolve(path.join(this.path, this.name, newPath)) : path.resolve(path.join(this.path, newPath))
4448
}
49+
newPath = path.join(newPath, filename.replace(RESERVED_FILENAME_REGEX, ''))
4550
return { path: newPath, length: file.length, offset: file.offset }
4651
})
4752
this.length = this.files.reduce((sum, file) => { return sum + file.length }, 0)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"type": "module",
1414
"dependencies": {
15+
"filename-reserved-regex": "^3.0.0",
1516
"queue-microtask": "^1.2.2",
1617
"random-access-file": "^4.0.0",
1718
"randombytes": "^2.0.3",

0 commit comments

Comments
 (0)