Skip to content

Commit cf0d6f1

Browse files
committed
fix master key access
1 parent b85a9cc commit cf0d6f1

File tree

2 files changed

+72
-23
lines changed

2 files changed

+72
-23
lines changed

spec/FileDownload.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,47 @@ describe('fileDownload', () => {
236236
expect(e.data.code).toBe(Parse.Error.OPERATION_FORBIDDEN);
237237
}
238238
});
239+
240+
it('should allow maintenance key to bypass download restrictions', async () => {
241+
await reconfigureServer({
242+
fileDownload: {
243+
enableForAnonymousUser: false,
244+
enableForAuthenticatedUser: false,
245+
enableForPublic: false,
246+
},
247+
});
248+
const file = await uploadTestFile();
249+
const request = require('../lib/request');
250+
const res = await request({
251+
headers: {
252+
'X-Parse-Maintenance-Key': 'testing',
253+
},
254+
method: 'GET',
255+
url: file.url,
256+
});
257+
expect(res.status).toBe(200);
258+
});
259+
260+
it('should allow maintenance key to bypass upload restrictions', async () => {
261+
await reconfigureServer({
262+
fileUpload: {
263+
enableForAnonymousUser: false,
264+
enableForAuthenticatedUser: false,
265+
enableForPublic: false,
266+
},
267+
});
268+
const request = require('../lib/request');
269+
const res = await request({
270+
headers: {
271+
'Content-Type': 'text/plain',
272+
'X-Parse-Application-Id': 'test',
273+
'X-Parse-Maintenance-Key': 'testing',
274+
},
275+
method: 'POST',
276+
url: 'http://localhost:8378/1/files/test.txt',
277+
body: 'hello world',
278+
});
279+
expect(res.data.url).toBeDefined();
280+
});
239281
});
240282
});

src/Routers/FilesRouter.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,25 @@ export class FilesRouter {
182182

183183
static _validateFileDownload(req, config) {
184184
const isMaster = req.auth?.isMaster;
185+
const isMaintenance = req.auth?.isMaintenance;
186+
if (isMaster || isMaintenance) {
187+
return;
188+
}
185189
const user = req.auth?.user;
186190
const isLinked = user && Parse.AnonymousUtils.isLinked(user);
187-
if (!isMaster && !config.fileDownload.enableForAnonymousUser && isLinked) {
191+
if (!config.fileDownload.enableForAnonymousUser && isLinked) {
188192
throw new Parse.Error(
189193
Parse.Error.OPERATION_FORBIDDEN,
190194
'File download by anonymous user is disabled.'
191195
);
192196
}
193-
if (!isMaster && !config.fileDownload.enableForAuthenticatedUser && !isLinked && user) {
197+
if (!config.fileDownload.enableForAuthenticatedUser && !isLinked && user) {
194198
throw new Parse.Error(
195199
Parse.Error.OPERATION_FORBIDDEN,
196200
'File download by authenticated user is disabled.'
197201
);
198202
}
199-
if (!isMaster && !config.fileDownload.enableForPublic && !user) {
203+
if (!config.fileDownload.enableForPublic && !user) {
200204
throw new Parse.Error(
201205
Parse.Error.OPERATION_FORBIDDEN,
202206
'File download by public is disabled.'
@@ -371,27 +375,30 @@ export class FilesRouter {
371375
return;
372376
}
373377
const config = req.config;
374-
const user = req.auth.user;
375378
const isMaster = req.auth.isMaster;
376-
const isLinked = user && Parse.AnonymousUtils.isLinked(user);
377-
if (!isMaster && !config.fileUpload.enableForAnonymousUser && isLinked) {
378-
next(
379-
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'File upload by anonymous user is disabled.')
380-
);
381-
return;
382-
}
383-
if (!isMaster && !config.fileUpload.enableForAuthenticatedUser && !isLinked && user) {
384-
next(
385-
new Parse.Error(
386-
Parse.Error.FILE_SAVE_ERROR,
387-
'File upload by authenticated user is disabled.'
388-
)
389-
);
390-
return;
391-
}
392-
if (!isMaster && !config.fileUpload.enableForPublic && !user) {
393-
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'File upload by public is disabled.'));
394-
return;
379+
const isMaintenance = req.auth.isMaintenance;
380+
if (!isMaster && !isMaintenance) {
381+
const user = req.auth.user;
382+
const isLinked = user && Parse.AnonymousUtils.isLinked(user);
383+
if (!config.fileUpload.enableForAnonymousUser && isLinked) {
384+
next(
385+
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'File upload by anonymous user is disabled.')
386+
);
387+
return;
388+
}
389+
if (!config.fileUpload.enableForAuthenticatedUser && !isLinked && user) {
390+
next(
391+
new Parse.Error(
392+
Parse.Error.FILE_SAVE_ERROR,
393+
'File upload by authenticated user is disabled.'
394+
)
395+
);
396+
return;
397+
}
398+
if (!config.fileUpload.enableForPublic && !user) {
399+
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'File upload by public is disabled.'));
400+
return;
401+
}
395402
}
396403
const filesController = config.filesController;
397404
const { filename } = req.params;

0 commit comments

Comments
 (0)