Skip to content

Commit b710aea

Browse files
authored
Merge pull request #10 from mailsvb/cwdWithHandler
Fix CWD when using handler
2 parents a728310 + 78d41a3 commit b710aea

5 files changed

Lines changed: 51 additions & 60 deletions

File tree

lib/jsftpd.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -342,48 +342,46 @@ class ftpd {
342342
*/
343343
const CWD = function (cmd, arg) {
344344
let newPath = arg
345-
if (main._useHdl === false) {
346-
if (newPath.charAt(0) === '/') {
347-
let folder = path.join(basefolder, newPath)
348-
if (fs.existsSync(folder) === true && fs.statSync(folder).isDirectory() === true && main._beginsWith(basefolder, folder) === true) {
349-
if (folder.charAt(folder.length - 1) !== '/') {
350-
folder += '/'
351-
}
352-
if (newPath.charAt(newPath.length - 1) !== '/') {
353-
newPath += '/'
354-
}
355-
absolutePath = folder
356-
relativePath = newPath
357-
return main._writeToSocket(socket, '250', ' ', `CWD successful. "${relativePath}" is current directory`, connectionInfo, SocketStateAfterWrite.Open)
345+
if (newPath.charAt(0) === '/') {
346+
let folder = path.join(basefolder, newPath)
347+
if (isValidFolder(folder) === true || main._useHdl === true) {
348+
if (folder.charAt(folder.length - 1) !== '/') {
349+
folder += '/'
358350
}
359-
} else if (newPath !== '..') {
360-
let folder = path.join(basefolder, relativePath, newPath)
361-
if (fs.existsSync(folder) === true && fs.statSync(folder).isDirectory() === true && main._beginsWith(basefolder, folder) === true) {
362-
if (folder.charAt(folder.length - 1) !== '/') {
363-
folder += '/'
364-
}
365-
if (newPath.charAt(newPath.length - 1) !== '/') {
366-
newPath += '/'
367-
}
368-
absolutePath = folder
369-
relativePath += newPath
370-
return main._writeToSocket(socket, '250', ' ', `CWD successful. "${relativePath}" is current directory`, connectionInfo, SocketStateAfterWrite.Open)
351+
if (newPath.charAt(newPath.length - 1) !== '/') {
352+
newPath += '/'
371353
}
372-
} else if (newPath === '..') {
373-
if (relativePath !== '/') {
374-
newPath = relativePath.split('/')
375-
newPath.pop()
376-
newPath.pop()
377-
newPath = newPath.join('/') + '/'
378-
const folder = path.join(basefolder, newPath)
379-
if (fs.existsSync(folder) === true && fs.statSync(folder).isDirectory() === true && main._beginsWith(basefolder, folder) === true) {
380-
absolutePath = folder
381-
relativePath = newPath
382-
return main._writeToSocket(socket, '250', ' ', `CWD successful. "${relativePath}" is current directory`, connectionInfo, SocketStateAfterWrite.Open)
383-
}
384-
} else {
354+
absolutePath = folder
355+
relativePath = newPath
356+
return main._writeToSocket(socket, '250', ' ', `CWD successful. "${relativePath}" is current directory`, connectionInfo, SocketStateAfterWrite.Open)
357+
}
358+
} else if (newPath !== '..') {
359+
let folder = path.join(basefolder, relativePath, newPath)
360+
if (isValidFolder(folder) === true || main._useHdl === true) {
361+
if (folder.charAt(folder.length - 1) !== '/') {
362+
folder += '/'
363+
}
364+
if (newPath.charAt(newPath.length - 1) !== '/') {
365+
newPath += '/'
366+
}
367+
absolutePath = folder
368+
relativePath += newPath
369+
return main._writeToSocket(socket, '250', ' ', `CWD successful. "${relativePath}" is current directory`, connectionInfo, SocketStateAfterWrite.Open)
370+
}
371+
} else if (newPath === '..') {
372+
if (relativePath !== '/') {
373+
newPath = relativePath.split('/')
374+
newPath.pop()
375+
newPath.pop()
376+
newPath = newPath.join('/') + '/'
377+
const folder = path.join(basefolder, newPath)
378+
if (isValidFolder(folder) === true || main._useHdl === true) {
379+
absolutePath = folder
380+
relativePath = newPath
385381
return main._writeToSocket(socket, '250', ' ', `CWD successful. "${relativePath}" is current directory`, connectionInfo, SocketStateAfterWrite.Open)
386382
}
383+
} else {
384+
return main._writeToSocket(socket, '250', ' ', `CWD successful. "${relativePath}" is current directory`, connectionInfo, SocketStateAfterWrite.Open)
387385
}
388386
}
389387
return main._writeToSocket(socket, '530', ' ', 'CWD not successful', connectionInfo, SocketStateAfterWrite.Open)
@@ -1019,6 +1017,10 @@ class ftpd {
10191017
main._writeToSocket(socket, '501', ' ', 'Command failed', connectionInfo, SocketStateAfterWrite.Open)
10201018
}
10211019
}
1020+
1021+
const isValidFolder = function (folder) {
1022+
return fs.existsSync(folder) === true && fs.statSync(folder).isDirectory() === true && main._beginsWith(basefolder, folder) === true
1023+
}
10221024
}
10231025

10241026
_informLogin (username, remoteAddr) {

test/LIST.test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ test('test LIST message', async () => {
5757
await dataSocket.connect(dataPort, 'localhost')
5858

5959
await promiseSocket.write('LIST')
60-
content = await promiseSocket.read()
6160

6261
dataContent = await promiseDataSocket.read()
6362
expect(dataContent.toString().trim()).toMatch('dr--r--r--')
@@ -66,7 +65,7 @@ test('test LIST message', async () => {
6665

6766
await sleep(100)
6867

69-
content += await promiseSocket.read()
68+
content = await promiseSocket.read()
7069
expect(content.toString().trim()).toMatch('150 Opening data channel')
7170
expect(content.toString().trim()).toMatch('226 Successfully transferred "/"')
7271

@@ -108,7 +107,6 @@ test('test MLSD message', async () => {
108107
await dataSocket.connect(dataPort, 'localhost')
109108

110109
await promiseSocket.write('MLSD')
111-
content = await promiseSocket.read()
112110

113111
dataContent = await promiseDataSocket.read()
114112
expect(dataContent.toString().trim()).toMatch('type=dir')
@@ -117,7 +115,7 @@ test('test MLSD message', async () => {
117115

118116
await sleep(100)
119117

120-
content += await promiseSocket.read()
118+
content = await promiseSocket.read()
121119
expect(content.toString().trim()).toMatch('150 Opening data channel')
122120
expect(content.toString().trim()).toMatch('226 Successfully transferred "/"')
123121

@@ -219,15 +217,14 @@ test('test MLSD message with handler', async () => {
219217
await dataSocket.connect(dataPort, 'localhost')
220218

221219
await promiseSocket.write('MLSD')
222-
content = await promiseSocket.read()
223220

224221
dataContent = await promiseDataSocket.read()
225222
expect(dataContent.toString().trim()).toBe('')
226223
await promiseDataSocket.end()
227224

228225
await sleep(100)
229226

230-
content += await promiseSocket.read()
227+
content = await promiseSocket.read()
231228
expect(content.toString().trim()).toMatch('150 Opening data channel')
232229
expect(content.toString().trim()).toMatch('226 Successfully transferred "/"')
233230

test/RETR.test.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,14 @@ test('test RETR message', async () => {
123123
expect(content.toString().trim()).toBe('550 File not found')
124124

125125
await promiseSocket.write('RETR mytestfile')
126-
content = await promiseSocket.read()
127126

128127
dataContent = await promiseDataSocket.read()
129128
expect(dataContent.toString().trim()).toMatch('SOMETESTCONTENT')
130129
await promiseDataSocket.end()
131130

132131
await sleep(100)
133132

134-
content += await promiseSocket.read()
133+
content = await promiseSocket.read()
135134
expect(content.toString().trim()).toMatch('150 Opening data channel')
136135
expect(content.toString().trim()).toMatch('226 Successfully transferred "mytestfile"')
137136

@@ -198,15 +197,14 @@ test('test RETR message with ASCII', async () => {
198197
expect(content.toString().trim()).toBe('550 File not found')
199198

200199
await promiseSocket.write('RETR mytestfile')
201-
content = await promiseSocket.read()
202200

203201
dataContent = await promiseDataSocket.read()
204202
expect(dataContent.toString().trim()).toMatch('SOMETESTCONTENT')
205203
await promiseDataSocket.end()
206204

207205
await sleep(100)
208206

209-
content += await promiseSocket.read()
207+
content = await promiseSocket.read()
210208
expect(content.toString().trim()).toMatch('150 Opening data channel')
211209
expect(content.toString().trim()).toMatch('226 Successfully transferred "mytestfile"')
212210

@@ -265,15 +263,14 @@ test('test RETR message with handler', async () => {
265263
await dataSocket.connect(dataPort, 'localhost')
266264

267265
await promiseSocket.write('RETR mytestfile')
268-
content = await promiseSocket.read()
269266

270267
dataContent = await promiseDataSocket.read()
271268
expect(dataContent.toString().trim()).toMatch('SOMETESTCONTENT')
272269
await promiseDataSocket.end()
273270

274271
await sleep(100)
275272

276-
content += await promiseSocket.read()
273+
content = await promiseSocket.read()
277274
expect(content.toString().trim()).toMatch('150 Opening data channel')
278275
expect(content.toString().trim()).toMatch('226 Successfully transferred "mytestfile"')
279276

@@ -335,15 +332,14 @@ test('test RETR message with handler fails', async () => {
335332
await dataSocket.connect(dataPort, 'localhost')
336333

337334
await promiseSocket.write('RETR mytestfile')
338-
content = await promiseSocket.read()
339335

340336
dataContent = await promiseDataSocket.read()
341337
expect(dataContent).toBe(undefined)
342338
await promiseDataSocket.end()
343339

344340
await sleep(100)
345341

346-
content += await promiseSocket.read()
342+
content = await promiseSocket.read()
347343
expect(content.toString().trim()).toMatch('150 Opening data channel')
348344
expect(content.toString().trim()).toMatch('550 Transfer failed "mytestfile"')
349345

test/RN.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ test('test RNFR/RNTO message', async () => {
125125
await dataSocket.connect(dataPort, 'localhost')
126126

127127
await promiseSocket.write('MLSD')
128-
content = await promiseSocket.read()
129128

130129
dataContent = await promiseDataSocket.read()
131130
expect(dataContent.toString().trim()).toMatch('type=file')
@@ -135,7 +134,7 @@ test('test RNFR/RNTO message', async () => {
135134

136135
await sleep(100)
137136

138-
content += await promiseSocket.read()
137+
content = await promiseSocket.read()
139138
expect(content.toString().trim()).toMatch('150 Opening data channel')
140139
expect(content.toString().trim()).toMatch('226 Successfully transferred "/"')
141140

test/STOR.test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ test('test STOR message', async () => {
112112
await dataSocket.connect(dataPort, 'localhost')
113113

114114
await promiseSocket.write('MLSD')
115-
content = await promiseSocket.read()
116115

117116
dataContent = await promiseDataSocket.read()
118117
expect(dataContent.toString().trim()).toMatch('type=file')
@@ -122,7 +121,7 @@ test('test STOR message', async () => {
122121

123122
await sleep(100)
124123

125-
content += await promiseSocket.read()
124+
content = await promiseSocket.read()
126125
expect(content.toString().trim()).toMatch('150 Opening data channel')
127126
expect(content.toString().trim()).toMatch('226 Successfully transferred "/"')
128127

@@ -189,7 +188,6 @@ test('test STOR message with ASCII', async () => {
189188
await dataSocket.connect(dataPort, 'localhost')
190189

191190
await promiseSocket.write('MLSD')
192-
content = await promiseSocket.read()
193191

194192
dataContent = await promiseDataSocket.read()
195193
expect(dataContent.toString().trim()).toMatch('type=file')
@@ -199,7 +197,7 @@ test('test STOR message with ASCII', async () => {
199197

200198
await sleep(100)
201199

202-
content += await promiseSocket.read()
200+
content = await promiseSocket.read()
203201
expect(content.toString().trim()).toMatch('150 Opening data channel')
204202
expect(content.toString().trim()).toMatch('226 Successfully transferred "/"')
205203

@@ -261,7 +259,6 @@ test('test STOR message overwrite not allowed', async () => {
261259
await dataSocket.connect(dataPort, 'localhost')
262260

263261
await promiseSocket.write('MLSD')
264-
content = await promiseSocket.read()
265262

266263
dataContent = await promiseDataSocket.read()
267264
expect(dataContent.toString().trim()).toMatch('type=file')
@@ -271,7 +268,7 @@ test('test STOR message overwrite not allowed', async () => {
271268

272269
await sleep(100)
273270

274-
content += await promiseSocket.read()
271+
content = await promiseSocket.read()
275272
expect(content.toString().trim()).toMatch('150 Opening data channel')
276273
expect(content.toString().trim()).toMatch('226 Successfully transferred "/"')
277274

0 commit comments

Comments
 (0)