Skip to content

Add enforceMasterKeyAccess middleware. #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions spec/ParseFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ describe('Parse.File testing', () => {
}, (error, response, body) => {
expect(error).toBe(null);
var del_b = JSON.parse(body);
expect(response.statusCode).toEqual(400);
expect(del_b.code).toEqual(119);
expect(response.statusCode).toEqual(403);
expect(del_b.error).toMatch(/unauthorized/);
// incorrect X-Parse-Master-Key header
request.del({
headers: {
Expand All @@ -114,8 +114,8 @@ describe('Parse.File testing', () => {
}, (error, response, body) => {
expect(error).toBe(null);
var del_b2 = JSON.parse(body);
expect(response.statusCode).toEqual(400);
expect(del_b2.code).toEqual(119);
expect(response.statusCode).toEqual(403);
expect(del_b2.error).toMatch(/unauthorized/);
done();
});
});
Expand Down
8 changes: 1 addition & 7 deletions src/Controllers/FilesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ export class FilesController {

deleteHandler() {
return (req, res, next) => {
// enforce use of master key for file deletions
if(!req.auth.isMaster){
next(new Parse.Error(Parse.Error.OPERATION_FORBIDDEN,
'Master key required for file deletion.'));
return;
}

this._filesAdapter.deleteFile(req.config, req.params.filename).then(() => {
res.status(200);
// TODO: return useful JSON here?
Expand Down Expand Up @@ -142,6 +135,7 @@ export class FilesController {
router.delete('/files/:filename',
Middlewares.allowCrossDomain,
Middlewares.handleParseHeaders,
Middlewares.enforceMasterKeyAccess,
this.deleteHandler()
);

Expand Down
13 changes: 11 additions & 2 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,24 @@ var handleParseErrors = function(err, req, res, next) {
}
};

function enforceMasterKeyAccess(req, res, next) {
if (!req.auth.isMaster) {
res.status(403);
res.end('{"error":"unauthorized: master key is required"}');
return;
}
next();
}

function invalidRequest(req, res) {
res.status(403);
res.end('{"error":"unauthorized"}');
}


module.exports = {
allowCrossDomain: allowCrossDomain,
allowMethodOverride: allowMethodOverride,
handleParseErrors: handleParseErrors,
handleParseHeaders: handleParseHeaders
handleParseHeaders: handleParseHeaders,
enforceMasterKeyAccess: enforceMasterKeyAccess
};