Skip to content

Commit 317b92f

Browse files
committed
refactor
1 parent 16a9d0a commit 317b92f

File tree

9 files changed

+46
-72
lines changed

9 files changed

+46
-72
lines changed

spec/.eslintrc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
"jequal": true,
3535
"create": true,
3636
"arrayContains": true,
37-
"databaseAdapter": true,
38-
"requestWithExpectedError": true
37+
"databaseAdapter": true
3938
},
4039
"rules": {
4140
"no-console": [0],

spec/ParseFile.spec.js

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,22 +1112,22 @@ describe('Parse.File testing', () => {
11121112
await expectAsync(
11131113
reconfigureServer({
11141114
fileUpload: {
1115-
fileTypes: 1,
1115+
fileExtensions: 1,
11161116
},
11171117
})
1118-
).toBeRejectedWith('fileUpload.fileTypes must be an array or string.');
1118+
).toBeRejectedWith('fileUpload.fileExtensions must be an array.');
11191119
});
11201120
});
1121-
describe('fileTypes', () => {
1121+
describe('fileExtensions', () => {
11221122
it('works with _ContentType', async () => {
11231123
await reconfigureServer({
11241124
fileUpload: {
11251125
enableForPublic: true,
1126-
fileTypes: '(^image)(/)[a-zA-Z0-9_]*',
1126+
fileExtensions: ['png'],
11271127
},
11281128
});
11291129
await expectAsync(
1130-
requestWithExpectedError({
1130+
request({
11311131
method: 'POST',
11321132
url: 'http://localhost:8378/1/files/file',
11331133
body: JSON.stringify({
@@ -1136,44 +1136,47 @@ describe('Parse.File testing', () => {
11361136
_ContentType: 'text/html',
11371137
base64: 'PGh0bWw+PC9odG1sPgo=',
11381138
}),
1139+
}).catch(e => {
1140+
throw new Error(e.data.error);
11391141
})
11401142
).toBeRejectedWith(
1141-
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of type text/html is disabled.`)
1143+
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of type html is disabled.`)
11421144
);
11431145
});
11441146

11451147
it('works without Content-Type', async () => {
11461148
await reconfigureServer({
11471149
fileUpload: {
11481150
enableForPublic: true,
1149-
fileTypes: '(^image)(/)[a-zA-Z0-9_]*',
11501151
},
11511152
});
11521153
const headers = {
11531154
'X-Parse-Application-Id': 'test',
11541155
'X-Parse-REST-API-Key': 'rest',
11551156
};
11561157
await expectAsync(
1157-
requestWithExpectedError({
1158+
request({
11581159
method: 'POST',
11591160
headers: headers,
11601161
url: 'http://localhost:8378/1/files/file.html',
11611162
body: '<html></html>\n',
1163+
}).catch(e => {
1164+
throw new Error(e.data.error);
11621165
})
11631166
).toBeRejectedWith(
1164-
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of type text/html is disabled.`)
1167+
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of type html is disabled.`)
11651168
);
11661169
});
11671170

11681171
it('works with array', async () => {
11691172
await reconfigureServer({
11701173
fileUpload: {
11711174
enableForPublic: true,
1172-
fileTypes: ['image/jpg'],
1175+
fileExtensions: ['jpg'],
11731176
},
11741177
});
11751178
await expectAsync(
1176-
requestWithExpectedError({
1179+
request({
11771180
method: 'POST',
11781181
url: 'http://localhost:8378/1/files/file',
11791182
body: JSON.stringify({
@@ -1182,40 +1185,44 @@ describe('Parse.File testing', () => {
11821185
_ContentType: 'text/html',
11831186
base64: 'PGh0bWw+PC9odG1sPgo=',
11841187
}),
1188+
}).catch(e => {
1189+
throw new Error(e.data.error);
11851190
})
11861191
).toBeRejectedWith(
1187-
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of type text/html is disabled.`)
1192+
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of type html is disabled.`)
11881193
);
11891194
});
11901195

11911196
it('works with array without Content-Type', async () => {
11921197
await reconfigureServer({
11931198
fileUpload: {
11941199
enableForPublic: true,
1195-
fileTypes: ['image/jpg'],
1200+
fileExtensions: ['jpg'],
11961201
},
11971202
});
11981203
const headers = {
11991204
'X-Parse-Application-Id': 'test',
12001205
'X-Parse-REST-API-Key': 'rest',
12011206
};
12021207
await expectAsync(
1203-
requestWithExpectedError({
1208+
request({
12041209
method: 'POST',
12051210
headers: headers,
12061211
url: 'http://localhost:8378/1/files/file.html',
12071212
body: '<html></html>\n',
1213+
}).catch(e => {
1214+
throw new Error(e.data.error);
12081215
})
12091216
).toBeRejectedWith(
1210-
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of type text/html is disabled.`)
1217+
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, `File upload of type html is disabled.`)
12111218
);
12121219
});
12131220

12141221
it('works with array with correct file type', async () => {
12151222
await reconfigureServer({
12161223
fileUpload: {
12171224
enableForPublic: true,
1218-
fileTypes: ['text/html'],
1225+
fileExtensions: ['html'],
12191226
},
12201227
});
12211228
const response = await request({
@@ -1232,27 +1239,5 @@ describe('Parse.File testing', () => {
12321239
expect(b.name).toMatch(/_file.html$/);
12331240
expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/test\/.*file.html$/);
12341241
});
1235-
1236-
it('works with regex with correct file type', async () => {
1237-
await reconfigureServer({
1238-
fileUpload: {
1239-
enableForPublic: true,
1240-
fileTypes: '(^text)(/)[a-zA-Z0-9_]*',
1241-
},
1242-
});
1243-
const headers = {
1244-
'X-Parse-Application-Id': 'test',
1245-
'X-Parse-REST-API-Key': 'rest',
1246-
};
1247-
const response = await request({
1248-
method: 'POST',
1249-
headers: headers,
1250-
url: 'http://localhost:8378/1/files/file.html',
1251-
body: '<html></html>\n',
1252-
});
1253-
const b = response.data;
1254-
expect(b.name).toMatch(/_file.html$/);
1255-
expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/test\/.*file.html$/);
1256-
});
12571242
});
12581243
});

spec/helper.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const semver = require('semver');
44
const CurrentSpecReporter = require('./support/CurrentSpecReporter.js');
55
const { SpecReporter } = require('jasmine-spec-reporter');
66
const SchemaCache = require('../lib/Adapters/Cache/SchemaCache').default;
7-
const request = require('../lib/request');
87

98
// Ensure localhost resolves to ipv4 address first on node v17+
109
if (dns.setDefaultResultOrder) {
@@ -31,13 +30,7 @@ if (global._babelPolyfill) {
3130
console.error('We should not use polyfilled tests');
3231
process.exit(1);
3332
}
34-
global.requestWithExpectedError = async params => {
35-
try {
36-
return await request(params);
37-
} catch (e) {
38-
throw new Error(e.data.error);
39-
}
40-
};
33+
4134
process.noDeprecation = true;
4235

4336
const cache = require('../lib/cache').default;
@@ -117,6 +110,7 @@ const defaultConfiguration = {
117110
enableForPublic: true,
118111
enableForAnonymousUser: true,
119112
enableForAuthenticatedUser: true,
113+
fileExtensions: ['*']
120114
},
121115
push: {
122116
android: {

src/Config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@ export class Config {
421421
} else if (typeof fileUpload.enableForAuthenticatedUser !== 'boolean') {
422422
throw 'fileUpload.enableForAuthenticatedUser must be a boolean value.';
423423
}
424-
if (fileUpload.fileTypes === undefined) {
425-
fileUpload.fileTypes = FileUploadOptions.fileTypes.default;
426-
} else if (typeof fileUpload.fileTypes !== 'string' && !Array.isArray(fileUpload.fileTypes)) {
427-
throw 'fileUpload.fileTypes must be an array or string.';
424+
if (fileUpload.fileExtensions === undefined) {
425+
fileUpload.fileExtensions = FileUploadOptions.fileExtensions.default;
426+
} else if (!Array.isArray(fileUpload.fileExtensions)) {
427+
throw 'fileUpload.fileExtensions must be an array.';
428428
}
429429
}
430430

src/Deprecator/Deprecations.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ module.exports = [
2424
},
2525
{ optionKey: 'enforcePrivateUsers', changeNewDefault: 'true' },
2626
{ optionKey: 'allowClientClassCreation', changeNewDefault: 'false' },
27-
{ optionKey: 'fileUpload.fileTypes', changeNewDefault: '^(.(?!.*html?))*$' },
2827
];

src/Options/Definitions.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,10 +866,11 @@ module.exports.FileUploadOptions = {
866866
action: parsers.booleanParser,
867867
default: false,
868868
},
869-
fileTypes: {
870-
env: 'PARSE_SERVER_FILE_UPLOAD_FILE_TYPES',
871-
help: 'If set, allowed content types of files',
872-
default: '.*',
869+
fileExtensions: {
870+
env: 'PARSE_SERVER_FILE_UPLOAD_FILE_EXTENSIONS',
871+
help: 'Allowed content types of files',
872+
action: parsers.arrayParser,
873+
default: [],
873874
},
874875
};
875876
module.exports.DatabaseOptions = {

src/Options/docs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
* @property {Boolean} enableForAnonymousUser Is true if file upload should be allowed for anonymous users.
203203
* @property {Boolean} enableForAuthenticatedUser Is true if file upload should be allowed for authenticated users.
204204
* @property {Boolean} enableForPublic Is true if file upload should be allowed for anyone, regardless of user authentication.
205-
* @property {String} fileTypes If set, allowed content types of files
205+
* @property {String[]} fileExtensions Allowed content types of files
206206
*/
207207

208208
/**

src/Options/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,9 @@ export interface PasswordPolicyOptions {
490490
}
491491

492492
export interface FileUploadOptions {
493-
/* If set, allowed content types of files
494-
:DEFAULT: .* */
495-
fileTypes: ?string;
493+
/* Allowed content types of files
494+
:DEFAULT: [] */
495+
fileExtensions: ?(string[]);
496496
/* Is true if file upload should be allowed for anonymous users.
497497
:DEFAULT: false */
498498
enableForAnonymousUser: ?boolean;

src/Routers/FilesRouter.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,14 @@ export class FilesRouter {
137137
return;
138138
}
139139

140-
const fileTypes = config.fileUpload && config.fileUpload.fileTypes;
141-
if (!isMaster && fileTypes) {
140+
const fileExtensions = config.fileUpload && config.fileUpload.fileExtensions;
141+
if (!isMaster && fileExtensions && !fileExtensions.includes('*')) {
142142
try {
143-
if (Array.isArray(fileTypes)) {
144-
if (!fileTypes.includes(contentType)) {
145-
throw `File upload of type ${contentType} is disabled.`;
146-
}
147-
} else if (typeof fileTypes === 'string') {
148-
const regex = new RegExp(fileTypes);
149-
if (!regex.test(contentType)) {
150-
throw `File upload of type ${contentType} is disabled.`;
151-
}
143+
const extension = filename.includes('.')
144+
? filename.split('.')[1]
145+
: contentType.split('/')[1];
146+
if (!fileExtensions.includes(extension)) {
147+
throw `File upload of type ${extension} is disabled.`;
152148
}
153149
} catch (e) {
154150
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, e));

0 commit comments

Comments
 (0)