typescript-virtual-container 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/SSHMimic/exec.d.ts.map +1 -1
- package/dist/SSHMimic/exec.js +8 -2
- package/dist/SSHMimic/index.d.ts +1 -0
- package/dist/SSHMimic/index.d.ts.map +1 -1
- package/dist/SSHMimic/index.js +9 -3
- package/dist/SSHMimic/sftp.d.ts +46 -0
- package/dist/SSHMimic/sftp.d.ts.map +1 -0
- package/dist/SSHMimic/sftp.js +576 -0
- package/dist/VirtualFileSystem/index.d.ts +6 -4
- package/dist/VirtualFileSystem/index.d.ts.map +1 -1
- package/dist/VirtualFileSystem/index.js +144 -153
- package/dist/VirtualShell/index.d.ts +6 -0
- package/dist/VirtualShell/index.d.ts.map +1 -1
- package/dist/VirtualShell/index.js +16 -4
- package/dist/VirtualShell/shell.d.ts.map +1 -1
- package/dist/VirtualShell/shell.js +7 -0
- package/dist/VirtualUserManager/index.d.ts +8 -0
- package/dist/VirtualUserManager/index.d.ts.map +1 -1
- package/dist/VirtualUserManager/index.js +30 -0
- package/dist/commands/exit.d.ts.map +1 -1
- package/dist/commands/exit.js +1 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +2 -0
- package/dist/commands/passwd.d.ts +3 -0
- package/dist/commands/passwd.d.ts.map +1 -0
- package/dist/commands/passwd.js +21 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/modules/neofetch.d.ts.map +1 -1
- package/dist/modules/neofetch.js +0 -1
- package/dist/standalone.js +10 -1
- package/package.json +1 -1
- package/src/SSHMimic/exec.ts +18 -12
- package/src/SSHMimic/index.ts +16 -7
- package/src/SSHMimic/sftp.ts +833 -0
- package/src/VirtualFileSystem/index.ts +158 -188
- package/src/VirtualShell/index.ts +19 -8
- package/src/VirtualShell/shell.ts +7 -0
- package/src/VirtualUserManager/index.ts +38 -0
- package/src/commands/exit.ts +1 -0
- package/src/commands/index.ts +2 -0
- package/src/commands/passwd.ts +25 -0
- package/src/index.ts +2 -1
- package/src/modules/neofetch.ts +0 -2
- package/src/standalone.ts +11 -1
- package/tests/sftp.test.ts +319 -0
- package/tests/ssh-exec.test.ts +45 -0
- package/tests/users.test.ts +13 -0
|
@@ -0,0 +1,833 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/useNamingConvention: const as enum */
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type { AuthenticationType, KeyboardAuthContext } from "ssh2";
|
|
4
|
+
import { Server as SshServer } from "ssh2";
|
|
5
|
+
import type VirtualFileSystem from "../VirtualFileSystem";
|
|
6
|
+
import { VirtualShell } from "../VirtualShell";
|
|
7
|
+
import type { VirtualUserManager } from "../VirtualUserManager";
|
|
8
|
+
import type { VfsNodeStats } from "../types/vfs";
|
|
9
|
+
import { loadOrCreateHostKey } from "./hostKey";
|
|
10
|
+
|
|
11
|
+
const SFTP_STATUS_CODE = {
|
|
12
|
+
OK: 0,
|
|
13
|
+
EOF: 1,
|
|
14
|
+
NO_SUCH_FILE: 2,
|
|
15
|
+
PERMISSION_DENIED: 3,
|
|
16
|
+
FAILURE: 4,
|
|
17
|
+
BAD_MESSAGE: 5,
|
|
18
|
+
NO_CONNECTION: 6,
|
|
19
|
+
CONNECTION_LOST: 7,
|
|
20
|
+
OP_UNSUPPORTED: 8,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const OPEN_MODE = {
|
|
24
|
+
READ: 0x00000001,
|
|
25
|
+
WRITE: 0x00000002,
|
|
26
|
+
APPEND: 0x00000004,
|
|
27
|
+
CREAT: 0x00000008,
|
|
28
|
+
TRUNC: 0x00000010,
|
|
29
|
+
EXCL: 0x00000020,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
interface SftpFileHandle {
|
|
33
|
+
type: "file";
|
|
34
|
+
path: string;
|
|
35
|
+
flags: number;
|
|
36
|
+
buffer: Buffer;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface SftpDirHandle {
|
|
40
|
+
type: "dir";
|
|
41
|
+
path: string;
|
|
42
|
+
entries: string[];
|
|
43
|
+
index: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type SftpHandle = SftpFileHandle | SftpDirHandle;
|
|
47
|
+
|
|
48
|
+
interface SftpAttributes {
|
|
49
|
+
mode: number;
|
|
50
|
+
uid: number;
|
|
51
|
+
gid: number;
|
|
52
|
+
size: number;
|
|
53
|
+
atime: number | Date;
|
|
54
|
+
mtime: number | Date;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface SftpServerStream {
|
|
58
|
+
on(
|
|
59
|
+
event: "OPEN",
|
|
60
|
+
listener: (reqid: number, filename: string, flags: number) => void,
|
|
61
|
+
): this;
|
|
62
|
+
on(
|
|
63
|
+
event: "READ",
|
|
64
|
+
listener: (
|
|
65
|
+
reqid: number,
|
|
66
|
+
handle: Buffer,
|
|
67
|
+
offset: number,
|
|
68
|
+
length: number,
|
|
69
|
+
) => void,
|
|
70
|
+
): this;
|
|
71
|
+
on(
|
|
72
|
+
event: "WRITE",
|
|
73
|
+
listener: (
|
|
74
|
+
reqid: number,
|
|
75
|
+
handle: Buffer,
|
|
76
|
+
offset: number,
|
|
77
|
+
data: Buffer,
|
|
78
|
+
) => void,
|
|
79
|
+
): this;
|
|
80
|
+
on(event: "FSTAT", listener: (reqid: number, handle: Buffer) => void): this;
|
|
81
|
+
on(event: "CLOSE", listener: (reqid: number, handle: Buffer) => void): this;
|
|
82
|
+
on(event: "OPENDIR", listener: (reqid: number, path: string) => void): this;
|
|
83
|
+
on(event: "READDIR", listener: (reqid: number, handle: Buffer) => void): this;
|
|
84
|
+
on(event: "STAT", listener: (reqid: number, path: string) => void): this;
|
|
85
|
+
on(event: "LSTAT", listener: (reqid: number, path: string) => void): this;
|
|
86
|
+
on(
|
|
87
|
+
event: "FSETSTAT",
|
|
88
|
+
listener: (
|
|
89
|
+
reqid: number,
|
|
90
|
+
handle: Buffer,
|
|
91
|
+
attrs: Partial<SftpAttributes>,
|
|
92
|
+
) => void,
|
|
93
|
+
): this;
|
|
94
|
+
on(
|
|
95
|
+
event: "SETSTAT",
|
|
96
|
+
listener: (
|
|
97
|
+
reqid: number,
|
|
98
|
+
path: string,
|
|
99
|
+
attrs: Partial<SftpAttributes>,
|
|
100
|
+
) => void,
|
|
101
|
+
): this;
|
|
102
|
+
on(event: "REALPATH", listener: (reqid: number, path: string) => void): this;
|
|
103
|
+
on(event: "MKDIR", listener: (reqid: number, path: string) => void): this;
|
|
104
|
+
on(event: "RMDIR", listener: (reqid: number, path: string) => void): this;
|
|
105
|
+
on(event: "REMOVE", listener: (reqid: number, path: string) => void): this;
|
|
106
|
+
on(
|
|
107
|
+
event: "RENAME",
|
|
108
|
+
listener: (reqid: number, oldPath: string, newPath: string) => void,
|
|
109
|
+
): this;
|
|
110
|
+
on(event: "READLINK", listener: (reqid: number) => void): this;
|
|
111
|
+
on(event: "SYMLINK", listener: (reqid: number) => void): this;
|
|
112
|
+
on(event: "END", listener: () => void): this;
|
|
113
|
+
on(event: "end", listener: () => void): this;
|
|
114
|
+
on(event: "error", listener: (error: Error) => void): this;
|
|
115
|
+
on(event: "close", listener: () => void): this;
|
|
116
|
+
status(reqid: number, code: number): void;
|
|
117
|
+
attrs(reqid: number, attrs: SftpAttributes): void;
|
|
118
|
+
handle(reqid: number, handle: Buffer): void;
|
|
119
|
+
data(reqid: number, data: Buffer): void;
|
|
120
|
+
name(
|
|
121
|
+
reqid: number,
|
|
122
|
+
entries: Array<{
|
|
123
|
+
filename: string;
|
|
124
|
+
longname: string;
|
|
125
|
+
attrs: SftpAttributes;
|
|
126
|
+
}>,
|
|
127
|
+
): void;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface SftpMimicOptions {
|
|
131
|
+
port: number;
|
|
132
|
+
hostname?: string;
|
|
133
|
+
shell?: VirtualShell;
|
|
134
|
+
vfs?: VirtualFileSystem;
|
|
135
|
+
users?: VirtualUserManager;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export class SftpMimic {
|
|
139
|
+
port: number;
|
|
140
|
+
server: SshServer | null;
|
|
141
|
+
private readonly hostname: string;
|
|
142
|
+
private readonly shell: VirtualShell | null;
|
|
143
|
+
private readonly vfs: VirtualFileSystem;
|
|
144
|
+
private readonly users: VirtualUserManager;
|
|
145
|
+
private nextHandleId = 0;
|
|
146
|
+
private handles = new Map<string, SftpHandle>();
|
|
147
|
+
|
|
148
|
+
constructor({
|
|
149
|
+
port,
|
|
150
|
+
hostname = "typescript-vm",
|
|
151
|
+
shell,
|
|
152
|
+
vfs,
|
|
153
|
+
users,
|
|
154
|
+
}: SftpMimicOptions) {
|
|
155
|
+
this.port = port;
|
|
156
|
+
this.server = null;
|
|
157
|
+
this.hostname = hostname;
|
|
158
|
+
this.shell = null;
|
|
159
|
+
|
|
160
|
+
if (shell) {
|
|
161
|
+
this.vfs = shell.vfs;
|
|
162
|
+
this.users = shell.users;
|
|
163
|
+
this.hostname = shell.hostname;
|
|
164
|
+
this.shell = shell;
|
|
165
|
+
} else if (vfs && users) {
|
|
166
|
+
this.vfs = vfs;
|
|
167
|
+
this.users = users;
|
|
168
|
+
} else {
|
|
169
|
+
const defaultShell = new VirtualShell(hostname);
|
|
170
|
+
this.vfs = defaultShell.vfs;
|
|
171
|
+
this.users = defaultShell.users;
|
|
172
|
+
this.shell = defaultShell;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private getVfs(): VirtualFileSystem {
|
|
177
|
+
return this.shell?.vfs ?? this.vfs;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private getUsers(): VirtualUserManager {
|
|
181
|
+
return this.shell?.users ?? this.users;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
public async start(): Promise<number> {
|
|
185
|
+
const privateKey = loadOrCreateHostKey();
|
|
186
|
+
|
|
187
|
+
// Ensure VirtualShell is fully initialized before accepting connections
|
|
188
|
+
if (this.shell) {
|
|
189
|
+
await this.shell.ensureInitialized();
|
|
190
|
+
} else {
|
|
191
|
+
// If using standalone VFS+Users, initialize users now
|
|
192
|
+
await this.users.initialize();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
this.server = new SshServer(
|
|
196
|
+
{
|
|
197
|
+
hostKeys: [privateKey],
|
|
198
|
+
ident: `SSH-2.0-${this.hostname}`,
|
|
199
|
+
},
|
|
200
|
+
(client) => {
|
|
201
|
+
const allowedAuthMethods: AuthenticationType[] = [
|
|
202
|
+
"password",
|
|
203
|
+
"keyboard-interactive",
|
|
204
|
+
];
|
|
205
|
+
let authUser = "root";
|
|
206
|
+
let sessionId: string | null = null;
|
|
207
|
+
let remoteAddress = "unknown";
|
|
208
|
+
|
|
209
|
+
// Add error handling for the client
|
|
210
|
+
client.on("error", (error: unknown) => {
|
|
211
|
+
console.error(`[SFTP] Client error:`, error);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const acceptSession = (username: string): void => {
|
|
215
|
+
authUser = username;
|
|
216
|
+
sessionId = this.getUsers().registerSession(
|
|
217
|
+
authUser,
|
|
218
|
+
remoteAddress,
|
|
219
|
+
).id;
|
|
220
|
+
|
|
221
|
+
const homeRoot = "/home";
|
|
222
|
+
if (!this.getVfs().exists(homeRoot)) {
|
|
223
|
+
this.getVfs().mkdir(homeRoot, 0o755);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const homePath = `/home/${authUser}`;
|
|
227
|
+
if (!this.getVfs().exists(homePath)) {
|
|
228
|
+
this.getVfs().mkdir(homePath, 0o755);
|
|
229
|
+
this.getVfs().writeFile(
|
|
230
|
+
`${homePath}/README.txt`,
|
|
231
|
+
`Welcome to ${this.hostname}`,
|
|
232
|
+
);
|
|
233
|
+
void this.getVfs().flushMirror();
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
client.on("authentication", (ctx) => {
|
|
238
|
+
const candidateUser = ctx.username || "root";
|
|
239
|
+
remoteAddress = (ctx as { ip?: string }).ip ?? remoteAddress;
|
|
240
|
+
|
|
241
|
+
console.log(
|
|
242
|
+
`[SFTP] Auth attempt: user=${candidateUser}, method=${ctx.method}, ip=${remoteAddress}`,
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
if (ctx.method === "password") {
|
|
246
|
+
if (
|
|
247
|
+
!this.getUsers().verifyPassword(candidateUser, ctx.password ?? "")
|
|
248
|
+
) {
|
|
249
|
+
ctx.reject(allowedAuthMethods);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
acceptSession(candidateUser);
|
|
254
|
+
ctx.accept();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (ctx.method === "keyboard-interactive") {
|
|
259
|
+
const keyboardCtx = ctx as KeyboardAuthContext;
|
|
260
|
+
keyboardCtx.prompt(
|
|
261
|
+
[{ prompt: "Password: ", echo: false }],
|
|
262
|
+
(answers) => {
|
|
263
|
+
const password = answers[0] ?? "";
|
|
264
|
+
if (!this.getUsers().verifyPassword(candidateUser, password)) {
|
|
265
|
+
keyboardCtx.reject(allowedAuthMethods);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
acceptSession(candidateUser);
|
|
270
|
+
keyboardCtx.accept();
|
|
271
|
+
},
|
|
272
|
+
);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
ctx.reject(allowedAuthMethods);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
client.on("close", () => {
|
|
280
|
+
this.getUsers().unregisterSession(sessionId);
|
|
281
|
+
sessionId = null;
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
client.on("ready", () => {
|
|
285
|
+
client.on("session", (accept, _reject) => {
|
|
286
|
+
const session = accept();
|
|
287
|
+
|
|
288
|
+
// Add error handling for the session
|
|
289
|
+
session.on("error", (error: unknown) => {
|
|
290
|
+
console.error(
|
|
291
|
+
`[SFTP] Session error for user=${authUser}:`,
|
|
292
|
+
error,
|
|
293
|
+
);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
session.on("sftp", (acceptSftp) => {
|
|
297
|
+
const sftp = acceptSftp();
|
|
298
|
+
this.attachSftpHandlers(sftp, authUser);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
},
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
return new Promise<number>((resolve, reject) => {
|
|
306
|
+
this.server?.once("error", (err: unknown) => reject(err));
|
|
307
|
+
this.server?.listen(this.port, "0.0.0.0", () => {
|
|
308
|
+
const address = this.server?.address();
|
|
309
|
+
const actualPort =
|
|
310
|
+
address && typeof address === "object" && "port" in address
|
|
311
|
+
? address.port
|
|
312
|
+
: this.port;
|
|
313
|
+
console.log(`SFTP Mimic listening on port ${actualPort}`);
|
|
314
|
+
resolve(actualPort as number);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
public stop(): void {
|
|
320
|
+
if (this.server) {
|
|
321
|
+
this.server.close(() => {
|
|
322
|
+
console.log("SFTP Mimic stopped");
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Resolves SFTP request paths with proper handling of relative paths.
|
|
329
|
+
* Relative paths (including ".") are resolved relative to the user's home directory.
|
|
330
|
+
* This is standard SFTP behavior where the "working directory" is always the home.
|
|
331
|
+
*/
|
|
332
|
+
private resolveRequestPath(requestPath: string, authUser: string): string {
|
|
333
|
+
const homePath = `/home/${authUser}`;
|
|
334
|
+
|
|
335
|
+
// Empty path or "." → resolve to home directory
|
|
336
|
+
if (!requestPath || requestPath === ".") {
|
|
337
|
+
return homePath;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Relative path (doesn't start with "/") → resolve relative to home
|
|
341
|
+
if (!requestPath.startsWith("/")) {
|
|
342
|
+
const joined = path.posix.join(homePath, requestPath);
|
|
343
|
+
return path.posix.normalize(joined);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Absolute path → just normalize it
|
|
347
|
+
return path.posix.normalize(requestPath);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Verifies that a target path is confined within the user's home directory.
|
|
351
|
+
* This implements chroot-like behavior for security.
|
|
352
|
+
* @param targetPath - The normalized target path
|
|
353
|
+
* @param authUser - The authenticated username
|
|
354
|
+
* @returns true if path is within home, false if traversal attempt detected
|
|
355
|
+
*/
|
|
356
|
+
private isPathWithinHome(targetPath: string, authUser: string): boolean {
|
|
357
|
+
const homePath = `/home/${authUser}`;
|
|
358
|
+
const normalized = path.posix.normalize(targetPath);
|
|
359
|
+
|
|
360
|
+
// Allow access to home directory itself
|
|
361
|
+
if (normalized === homePath) {
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Check if path is within home directory (starts with /home/username/)
|
|
366
|
+
if (normalized.startsWith(`${homePath}/`)) {
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Reject any attempt to escape home directory
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private createAttrs(node: VfsNodeStats): SftpAttributes {
|
|
375
|
+
const permissions = node.mode & 0o777;
|
|
376
|
+
const fileType = node.type === "directory" ? 0o040000 : 0o100000;
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
mode: fileType | permissions,
|
|
380
|
+
size: node.type === "file" ? node.size : 0,
|
|
381
|
+
uid: 0,
|
|
382
|
+
gid: 0,
|
|
383
|
+
atime: Math.floor(node.createdAt.getTime() / 1000),
|
|
384
|
+
mtime: Math.floor(node.updatedAt.getTime() / 1000),
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private openHandle(handleValue: SftpHandle): Buffer {
|
|
389
|
+
const handleId = ++this.nextHandleId;
|
|
390
|
+
const handle = Buffer.alloc(4);
|
|
391
|
+
handle.writeUInt32BE(handleId, 0);
|
|
392
|
+
this.handles.set(handle.toString("hex"), handleValue);
|
|
393
|
+
return handle;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
private getHandle(handle: Buffer): SftpHandle | undefined {
|
|
397
|
+
return this.handles.get(handle.toString("hex")) as SftpHandle | undefined;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
private closeHandle(handle: Buffer): void {
|
|
401
|
+
this.handles.delete(handle.toString("hex"));
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
private attachSftpHandlers(sftp: SftpServerStream, authUser: string): void {
|
|
405
|
+
const getVfs = () => this.getVfs();
|
|
406
|
+
const getUsers = () => this.getUsers();
|
|
407
|
+
|
|
408
|
+
sftp.on("OPEN", (reqid: number, filename: string, flags: number) => {
|
|
409
|
+
const targetPath = this.resolveRequestPath(filename, authUser);
|
|
410
|
+
|
|
411
|
+
// Security: Confine to home directory
|
|
412
|
+
if (!this.isPathWithinHome(targetPath, authUser)) {
|
|
413
|
+
console.warn(
|
|
414
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${targetPath}`,
|
|
415
|
+
);
|
|
416
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const openMode = flags;
|
|
421
|
+
const _canRead = Boolean(openMode & OPEN_MODE.READ);
|
|
422
|
+
const _canWrite = Boolean(
|
|
423
|
+
openMode & OPEN_MODE.WRITE || openMode & OPEN_MODE.APPEND,
|
|
424
|
+
);
|
|
425
|
+
const canCreate = Boolean(openMode & OPEN_MODE.CREAT);
|
|
426
|
+
const shouldTruncate = Boolean(openMode & OPEN_MODE.TRUNC);
|
|
427
|
+
|
|
428
|
+
try {
|
|
429
|
+
if (!getVfs().exists(targetPath)) {
|
|
430
|
+
if (!canCreate) {
|
|
431
|
+
sftp.status(reqid, SFTP_STATUS_CODE.NO_SUCH_FILE);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
getVfs().writeFile(targetPath, Buffer.alloc(0));
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const stats = getVfs().stat(targetPath);
|
|
439
|
+
if (stats.type === "directory") {
|
|
440
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
let buffer = Buffer.from(getVfs().readFile(targetPath), "utf8");
|
|
445
|
+
if (shouldTruncate) {
|
|
446
|
+
buffer = Buffer.alloc(0);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (openMode & OPEN_MODE.APPEND) {
|
|
450
|
+
const handle = this.openHandle({
|
|
451
|
+
type: "file",
|
|
452
|
+
path: targetPath,
|
|
453
|
+
flags: openMode,
|
|
454
|
+
buffer,
|
|
455
|
+
});
|
|
456
|
+
sftp.handle(reqid, handle);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const handle = this.openHandle({
|
|
461
|
+
type: "file",
|
|
462
|
+
path: targetPath,
|
|
463
|
+
flags: openMode,
|
|
464
|
+
buffer,
|
|
465
|
+
});
|
|
466
|
+
sftp.handle(reqid, handle);
|
|
467
|
+
} catch (error) {
|
|
468
|
+
console.error("SFTP OPEN error:", error);
|
|
469
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
sftp.on(
|
|
474
|
+
"READ",
|
|
475
|
+
(reqid: number, handle: Buffer, offset: number, length: number) => {
|
|
476
|
+
const entry = this.getHandle(handle);
|
|
477
|
+
if (!entry || entry.type !== "file") {
|
|
478
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
if (offset >= entry.buffer.length) {
|
|
483
|
+
sftp.status(reqid, SFTP_STATUS_CODE.EOF);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const chunk = entry.buffer.slice(offset, offset + length);
|
|
488
|
+
sftp.data(reqid, chunk);
|
|
489
|
+
},
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
sftp.on(
|
|
493
|
+
"WRITE",
|
|
494
|
+
async (reqid: number, handle: Buffer, offset: number, data: Buffer) => {
|
|
495
|
+
const entry = this.getHandle(handle);
|
|
496
|
+
if (!entry || entry.type !== "file") {
|
|
497
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const end = offset + data.length;
|
|
502
|
+
if (end > entry.buffer.length) {
|
|
503
|
+
const nextBuffer = Buffer.alloc(end);
|
|
504
|
+
entry.buffer.copy(nextBuffer, 0, 0, entry.buffer.length);
|
|
505
|
+
entry.buffer = nextBuffer;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
data.copy(entry.buffer, offset);
|
|
509
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OK);
|
|
510
|
+
},
|
|
511
|
+
);
|
|
512
|
+
|
|
513
|
+
sftp.on("FSTAT", (reqid: number, handle: Buffer) => {
|
|
514
|
+
const entry = this.getHandle(handle);
|
|
515
|
+
if (!entry) {
|
|
516
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
try {
|
|
521
|
+
const stats = getVfs().stat(entry.path);
|
|
522
|
+
sftp.attrs(reqid, this.createAttrs(stats));
|
|
523
|
+
} catch {
|
|
524
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
sftp.on("CLOSE", async (reqid: number, handle: Buffer) => {
|
|
529
|
+
const entry = this.getHandle(handle);
|
|
530
|
+
if (!entry) {
|
|
531
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (entry.type === "file") {
|
|
536
|
+
try {
|
|
537
|
+
getUsers().assertWriteWithinQuota(authUser, entry.path, entry.buffer);
|
|
538
|
+
getVfs().writeFile(entry.path, entry.buffer);
|
|
539
|
+
void getVfs().flushMirror();
|
|
540
|
+
} catch (error) {
|
|
541
|
+
console.error("SFTP CLOSE write error:", error);
|
|
542
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
543
|
+
this.closeHandle(handle);
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
this.closeHandle(handle);
|
|
549
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OK);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
sftp.on("OPENDIR", (reqid: number, requestPath: string) => {
|
|
553
|
+
const targetPath = this.resolveRequestPath(requestPath, authUser);
|
|
554
|
+
|
|
555
|
+
// Security: Confine to home directory
|
|
556
|
+
if (!this.isPathWithinHome(targetPath, authUser)) {
|
|
557
|
+
console.warn(
|
|
558
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${targetPath}`,
|
|
559
|
+
);
|
|
560
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
try {
|
|
565
|
+
const stats = getVfs().stat(targetPath);
|
|
566
|
+
if (stats.type !== "directory") {
|
|
567
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const entries = getVfs().list(targetPath);
|
|
572
|
+
const handle = this.openHandle({
|
|
573
|
+
type: "dir",
|
|
574
|
+
path: targetPath,
|
|
575
|
+
entries,
|
|
576
|
+
index: 0,
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
sftp.handle(reqid, handle);
|
|
580
|
+
} catch {
|
|
581
|
+
sftp.status(reqid, SFTP_STATUS_CODE.NO_SUCH_FILE);
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
sftp.on("READDIR", (reqid: number, handle: Buffer) => {
|
|
586
|
+
const entry = this.getHandle(handle);
|
|
587
|
+
if (!entry || entry.type !== "dir") {
|
|
588
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
if (entry.index >= entry.entries.length) {
|
|
593
|
+
sftp.status(reqid, SFTP_STATUS_CODE.EOF);
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
const filename = entry.entries[entry.index++]!;
|
|
598
|
+
const filePath = path.posix.join(entry.path, filename);
|
|
599
|
+
const stats = getVfs().stat(filePath);
|
|
600
|
+
const attrs = this.createAttrs(stats);
|
|
601
|
+
const longname = `${stats.type === "directory" ? "d" : "-"}${(stats.mode & 0o777).toString(8)} ${filename}`;
|
|
602
|
+
return sftp.name(reqid, [{ filename, longname, attrs }]);
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
sftp.on("STAT", (reqid: number, requestPath: string) => {
|
|
606
|
+
const targetPath = this.resolveRequestPath(requestPath, authUser);
|
|
607
|
+
|
|
608
|
+
// Security: Confine to home directory
|
|
609
|
+
if (!this.isPathWithinHome(targetPath, authUser)) {
|
|
610
|
+
console.warn(
|
|
611
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${targetPath}`,
|
|
612
|
+
);
|
|
613
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
try {
|
|
618
|
+
const stats = getVfs().stat(targetPath);
|
|
619
|
+
sftp.attrs(reqid, this.createAttrs(stats));
|
|
620
|
+
} catch {
|
|
621
|
+
sftp.status(reqid, SFTP_STATUS_CODE.NO_SUCH_FILE);
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
sftp.on("LSTAT", (reqid: number, requestPath: string) => {
|
|
626
|
+
const targetPath = this.resolveRequestPath(requestPath, authUser);
|
|
627
|
+
|
|
628
|
+
// Security: Confine to home directory
|
|
629
|
+
if (!this.isPathWithinHome(targetPath, authUser)) {
|
|
630
|
+
console.warn(
|
|
631
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${targetPath}`,
|
|
632
|
+
);
|
|
633
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
try {
|
|
638
|
+
const stats = getVfs().stat(targetPath);
|
|
639
|
+
sftp.attrs(reqid, this.createAttrs(stats));
|
|
640
|
+
} catch {
|
|
641
|
+
sftp.status(reqid, SFTP_STATUS_CODE.NO_SUCH_FILE);
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
sftp.on(
|
|
646
|
+
"FSETSTAT",
|
|
647
|
+
(reqid: number, handle: Buffer, attrs: { mode?: number }) => {
|
|
648
|
+
const entry = this.getHandle(handle);
|
|
649
|
+
if (!entry) {
|
|
650
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
try {
|
|
655
|
+
if (attrs.mode !== undefined) {
|
|
656
|
+
getVfs().chmod(entry.path, attrs.mode);
|
|
657
|
+
}
|
|
658
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OK);
|
|
659
|
+
} catch {
|
|
660
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
661
|
+
}
|
|
662
|
+
},
|
|
663
|
+
);
|
|
664
|
+
|
|
665
|
+
sftp.on(
|
|
666
|
+
"SETSTAT",
|
|
667
|
+
(reqid: number, requestPath: string, attrs: { mode?: number }) => {
|
|
668
|
+
const targetPath = this.resolveRequestPath(requestPath, authUser);
|
|
669
|
+
|
|
670
|
+
// Security: Confine to home directory
|
|
671
|
+
if (!this.isPathWithinHome(targetPath, authUser)) {
|
|
672
|
+
console.warn(
|
|
673
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${targetPath}`,
|
|
674
|
+
);
|
|
675
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
try {
|
|
680
|
+
if (attrs.mode !== undefined) {
|
|
681
|
+
getVfs().chmod(targetPath, attrs.mode);
|
|
682
|
+
}
|
|
683
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OK);
|
|
684
|
+
} catch {
|
|
685
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
686
|
+
}
|
|
687
|
+
},
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
sftp.on("REALPATH", (reqid: number, requestPath: string) => {
|
|
691
|
+
const normalized = this.resolveRequestPath(requestPath, authUser);
|
|
692
|
+
|
|
693
|
+
// Security: Confine to home directory
|
|
694
|
+
if (!this.isPathWithinHome(normalized, authUser)) {
|
|
695
|
+
console.warn(
|
|
696
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${normalized}`,
|
|
697
|
+
);
|
|
698
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
sftp.name(reqid, [
|
|
703
|
+
{
|
|
704
|
+
filename: normalized,
|
|
705
|
+
longname: normalized,
|
|
706
|
+
attrs: {
|
|
707
|
+
mode: 0o040755,
|
|
708
|
+
uid: 0,
|
|
709
|
+
gid: 0,
|
|
710
|
+
size: 0,
|
|
711
|
+
atime: 0,
|
|
712
|
+
mtime: 0,
|
|
713
|
+
},
|
|
714
|
+
},
|
|
715
|
+
]);
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
sftp.on("MKDIR", (reqid: number, requestPath: string) => {
|
|
719
|
+
const targetPath = this.resolveRequestPath(requestPath, authUser);
|
|
720
|
+
|
|
721
|
+
// Security: Confine to home directory
|
|
722
|
+
if (!this.isPathWithinHome(targetPath, authUser)) {
|
|
723
|
+
console.warn(
|
|
724
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${targetPath}`,
|
|
725
|
+
);
|
|
726
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
try {
|
|
731
|
+
getVfs().mkdir(targetPath, 0o755);
|
|
732
|
+
void getVfs().flushMirror();
|
|
733
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OK);
|
|
734
|
+
} catch {
|
|
735
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
sftp.on("RMDIR", (reqid: number, requestPath: string) => {
|
|
740
|
+
const targetPath = this.resolveRequestPath(requestPath, authUser);
|
|
741
|
+
|
|
742
|
+
// Security: Confine to home directory
|
|
743
|
+
if (!this.isPathWithinHome(targetPath, authUser)) {
|
|
744
|
+
console.warn(
|
|
745
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${targetPath}`,
|
|
746
|
+
);
|
|
747
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
try {
|
|
752
|
+
getVfs().remove(targetPath, { recursive: false });
|
|
753
|
+
void getVfs().flushMirror();
|
|
754
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OK);
|
|
755
|
+
} catch {
|
|
756
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
sftp.on("REMOVE", (reqid: number, requestPath: string) => {
|
|
761
|
+
const targetPath = this.resolveRequestPath(requestPath, authUser);
|
|
762
|
+
|
|
763
|
+
// Security: Confine to home directory
|
|
764
|
+
if (!this.isPathWithinHome(targetPath, authUser)) {
|
|
765
|
+
console.warn(
|
|
766
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, path=${targetPath}`,
|
|
767
|
+
);
|
|
768
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
try {
|
|
773
|
+
getVfs().remove(targetPath, { recursive: false });
|
|
774
|
+
void getVfs().flushMirror();
|
|
775
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OK);
|
|
776
|
+
} catch {
|
|
777
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
778
|
+
}
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
sftp.on("RENAME", (reqid: number, oldPath: string, newPath: string) => {
|
|
782
|
+
const fromPath = this.resolveRequestPath(oldPath, authUser);
|
|
783
|
+
const toPath = this.resolveRequestPath(newPath, authUser);
|
|
784
|
+
|
|
785
|
+
// Security: Confine both source and destination to home directory
|
|
786
|
+
if (
|
|
787
|
+
!this.isPathWithinHome(fromPath, authUser) ||
|
|
788
|
+
!this.isPathWithinHome(toPath, authUser)
|
|
789
|
+
) {
|
|
790
|
+
console.warn(
|
|
791
|
+
`[SFTP] Path traversal attempt blocked: user=${authUser}, from=${fromPath}, to=${toPath}`,
|
|
792
|
+
);
|
|
793
|
+
sftp.status(reqid, SFTP_STATUS_CODE.PERMISSION_DENIED);
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
try {
|
|
798
|
+
getVfs().move(fromPath, toPath);
|
|
799
|
+
void getVfs().flushMirror();
|
|
800
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OK);
|
|
801
|
+
} catch {
|
|
802
|
+
sftp.status(reqid, SFTP_STATUS_CODE.FAILURE);
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
sftp.on("READLINK", (reqid: number) => {
|
|
807
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OP_UNSUPPORTED);
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
sftp.on("SYMLINK", (reqid: number) => {
|
|
811
|
+
sftp.status(reqid, SFTP_STATUS_CODE.OP_UNSUPPORTED);
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
sftp.on("error", (error: Error) => {
|
|
815
|
+
console.error(`[SFTP] Stream error for user=${authUser}:`, error);
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
sftp.on("close", () => {
|
|
819
|
+
console.log(`[SFTP] Stream closed for user=${authUser}`);
|
|
820
|
+
this.handles.clear();
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
sftp.on("end", () => {
|
|
824
|
+
console.log(`[SFTP] end event for user=${authUser}`);
|
|
825
|
+
this.handles.clear();
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
sftp.on("END", () => {
|
|
829
|
+
console.log(`[SFTP] END event for user=${authUser}`);
|
|
830
|
+
this.handles.clear();
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
}
|