forked from pfrazee/scoped-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (154 loc) · 5.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const path = require('path')
const fs = require('fs')
const watch = require('recursive-watch')
const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/
class ScopedFS {
constructor (basepath) {
this.base = basepath
this._filterFn = false
}
setFilter (fn) {
this._filterFn = fn || false
}
_filter (filepath) {
if (!this._filterFn) return true
return this._filterFn(unjoin(this.base, filepath))
}
createReadStream (name, opts) {
name = join(this.base, name)
if (!name) throw new Error('Invalid path')
if (!this._filter(name)) throw createNotFoundError()
return fs.createReadStream(name, opts)
}
readFile (name, ...args) {
name = join(this.base, name)
var cb = args[args.length - 1]
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createNotFoundError())
return fs.readFile(name, ...args)
}
readFileSync (name, ...args) {
name = join(this.base, name)
if (!name) throw new Error('Invalid path')
if (!this._filter(name)) throw createNotFoundError()
return fs.readFileSync(name, ...args)
}
createWriteStream (name, opts) {
name = join(this.base, name)
if (!name) throw new Error('Invalid path')
if (!this._filter(name)) throw createPermError()
return fs.createWriteStream(name, opts)
}
writeFile (name, ...args) {
name = join(this.base, name)
var cb = args[args.length - 1]
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createPermError())
return fs.writeFile(name, ...args)
}
writeFileSync (name, ...args) {
name = join(this.base, name)
if (!name) throw new Error('Invalid path')
if (!this._filter(name)) throw createPermError()
return fs.writeFileSync(name, ...args)
}
mkdir (name, ...args) {
name = join(this.base, name)
var cb = args[args.length - 1]
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createPermError())
return fs.mkdir(name, ...args)
}
symlink (name1, name2, ...args) {
name1 = join(this.base, name1)
name2 = join(this.base, name2)
var cb = args[args.length - 1]
if (!name1) return cb(new Error('Invalid path'))
if (!name2) return cb(new Error('Invalid path'))
if (!this._filter(name1)) return cb(createPermError())
if (!this._filter(name2)) return cb(createPermError())
return fs.symlink(name1, name2, ...args)
}
access (name, cb) {
name = join(this.base, name)
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createNotFoundError())
return fs.access(name, cb)
}
exists (name, cb) {
name = join(this.base, name)
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(false)
return fs.exists(name, cb)
}
lstat (name, cb) {
name = join(this.base, name)
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createNotFoundError())
return fs.lstat(name, cb)
}
stat (name, cb) {
name = join(this.base, name)
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createNotFoundError())
return fs.stat(name, cb)
}
readdir (name, cb) {
name = join(this.base, name)
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createNotFoundError())
return fs.readdir(name, (err, childNames) => {
if (err) return cb(err)
childNames = childNames.filter(childName => this._filter(path.join(name, childName)))
cb(null, childNames)
})
}
unlink (name, cb) {
name = join(this.base, name)
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createNotFoundError())
return fs.unlink(name, cb)
}
rmdir (name, cb) {
name = join(this.base, name)
if (!name) return cb(new Error('Invalid path'))
if (!this._filter(name)) return cb(createNotFoundError())
return fs.rmdir(name, cb)
}
watch (name, fn) {
name = join(this.base, name)
if (!name) throw new Error('Invalid path')
if (!this._filter(name)) throw createNotFoundError()
return watch(name, changedPath => {
if (!this._filter(changedPath)) return
changedPath = unjoin(this.base, changedPath)
fn(changedPath)
})
}
}
module.exports = ScopedFS
function join (rootpath, subpath) {
// make sure they're not using '..' to get outside of rootpath
if (UP_PATH_REGEXP.test(path.normalize('.' + path.sep + subpath))) {
return false
}
return path.normalize(path.join(rootpath, subpath))
}
function unjoin (rootpath, subpath) {
subpath = subpath.slice(rootpath.length)
return (subpath.charAt(0) === path.sep) ? subpath : (path.sep + subpath)
}
function createNotFoundError () {
var err = new Error('File not found')
err.code = 'ENOENT'
err.notFound = true
err.causedByFiltering = true
return err
}
function createPermError () {
var err = new Error('Not allowed')
err.code = 'EPERM'
err.notAllowed = true
err.causedByFiltering = true
return err
}