Skip to content

Commit

Permalink
fix: Parse Server option fileUpload.fileExtensions fails to determi…
Browse files Browse the repository at this point in the history
…ne file extension if filename contains multiple dots (#8754)
  • Loading branch information
MarcDerhammer authored Sep 23, 2023
1 parent b70c2d9 commit 3d6d50e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
68 changes: 68 additions & 0 deletions spec/ParseFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,74 @@ describe('Parse.File testing', () => {
);
});

it('works with a period in the file name', async () => {
await reconfigureServer({
fileUpload: {
enableForPublic: true,
fileExtensions: ['^[^hH][^tT][^mM][^lL]?$'],
},
});
const headers = {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
};

const values = ['file.png.html', 'file.txt.png.html', 'file.png.txt.html'];

for (const value of values) {
await expectAsync(
request({
method: 'POST',
headers: headers,
url: `http://localhost:8378/1/files/${value}`,
body: '<html></html>\n',
}).catch(e => {
throw new Error(e.data.error);
})
).toBeRejectedWith(
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of extension html is disabled.`)
);
}
});

it('works to stop invalid filenames', async () => {
await reconfigureServer({
fileUpload: {
enableForPublic: true,
fileExtensions: ['^[^hH][^tT][^mM][^lL]?$'],
},
});
const headers = {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
};

const values = [
'!invalid.png',
'.png',
'.html',
' .html',
'.png.html',
'~invalid.png',
'-invalid.png',
];

for (const value of values) {
await expectAsync(
request({
method: 'POST',
headers: headers,
url: `http://localhost:8378/1/files/${value}`,
body: '<html></html>\n',
}).catch(e => {
throw new Error(e.data.error);
})
).toBeRejectedWith(
new Parse.Error(Parse.Error.INVALID_FILE_NAME, `Filename contains invalid characters.`)
);
}
});

it('works with array', async () => {
await reconfigureServer({
fileUpload: {
Expand Down
2 changes: 1 addition & 1 deletion src/Routers/FilesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class FilesRouter {
};
let extension = contentType;
if (filename && filename.includes('.')) {
extension = filename.split('.')[1];
extension = filename.substring(filename.lastIndexOf('.') + 1);
} else if (contentType && contentType.includes('/')) {
extension = contentType.split('/')[1];
}
Expand Down

0 comments on commit 3d6d50e

Please sign in to comment.