Skip to content

Commit

Permalink
Fix incorrect handling of international characters in paths
Browse files Browse the repository at this point in the history
  • Loading branch information
deepkolos authored and StarLederer committed Oct 28, 2023
1 parent 9c16ad0 commit a56d524
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
Empty file.
36 changes: 36 additions & 0 deletions __tests__/src/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ describe('readdir request', () => {
const response = await fetch(`${url}/file?cmd=readdir`);
expect(response.status).toEqual(400);
});

it('should support various UTF-8 characters in path', async () => {
const response = await fetch(`${url}/directory 目录 каталог/file 文件 файл?cmd=readFile`);
expect(response.status).toEqual(200);
});
});

// readFile
Expand Down Expand Up @@ -118,6 +123,11 @@ describe('readFile request', () => {
const response = await fetch(`${url}/directory?cmd=readFile`);
expect(response.status).toEqual(400);
});

it('should support various UTF-8 characters in path', async () => {
const response = await fetch(`${url}/directory 目录 каталог/file 文件 файл?cmd=readFile`);
expect(response.status).toEqual(200);
});
});

// stat
Expand All @@ -141,6 +151,11 @@ describe('stat request', () => {
const response = await fetch(`${url}/notfile?cmd=stat`);
expect(response.status).toEqual(404);
});

it('should support various UTF-8 characters in path', async () => {
const response = await fetch(`${url}/directory 目录 каталог/file 文件 файл?cmd=stat`);
expect(response.status).toEqual(200);
});
});

// writeFile
Expand Down Expand Up @@ -186,6 +201,18 @@ describe('writeFile request', () => {
expect(response.status).toEqual(200);
expect(newdata.buffer).toEqual(testData);
});

it('should support various UTF-8 characters in path', async () => {
const testFilename = 'new file 文件 файл';
const response = await fetch(`${url}/newdirectory/${testFilename}?cmd=writeFile`, {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: '',
});
const statPromise = fs.stat(resolveWithRoot(`newdirectory/${testFilename}`));
expect(response.status).toEqual(200);
await expect(statPromise).resolves.toBeTruthy();
});
});

// rm
Expand Down Expand Up @@ -219,6 +246,15 @@ describe('rm request', () => {
expect(response.status).toEqual(200);
await expect(statPromise).rejects.toBeTruthy();
});

it('should support various UTF-8 characters in path', async () => {
const testFilename = 'auto file 文件 файл';
try { await fs.writeFile(resolve(resolveWithRoot(testFilename)), ''); } catch (err) { /**/ }
const response = await fetch(`${url}/${testFilename}?cmd=rm`, { method: 'DELETE' });
const statPromise = fs.stat(resolveWithRoot(testFilename));
expect(response.status).toEqual(200);
await expect(statPromise).rejects.toBeTruthy();
});
});

export default {};
2 changes: 1 addition & 1 deletion src/plugin/server/requests/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function createRoutes(resolvePath: (path: string) => string): Rou

let path;
try {
path = resolvePath(ctx.path);
path = resolvePath(decodeURIComponent(ctx.path));
} catch (err) {
if (isNodeError(err)) {
ctx.status = 403;
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/server/requests/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function createRoutes(resolvePath: (path: string) => string): Rou

let path;
try {
path = resolvePath(ctx.path);
path = resolvePath(decodeURIComponent(ctx.path));
} catch (err) {
if (isNodeError(err)) {
ctx.status = 403;
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/server/requests/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function createRoutes(resolvePath: (path: string) => string): Rou

let path;
try {
path = resolvePath(ctx.path);
path = resolvePath(decodeURIComponent(ctx.path));
} catch (err) {
if (isNodeError(err)) {
ctx.status = 403;
Expand Down

0 comments on commit a56d524

Please sign in to comment.