Skip to content

[Feature] Add file upload policy #4822

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ The client keys used with Parse are no longer necessary with Parse Server. If yo
* `masterKeyIps` - The array of ip addresses where masterKey usage will be restricted to only these ips. (Default to [] which means allow all ips). If you're using this feature and have `useMasterKey: true` in cloudcode, make sure that you put your own ip in this list.
* `readOnlyMasterKey` - A masterKey that has full read access to the data, but no write access. This key should be treated the same way as your masterKey, keeping it private.
* `objectIdSize` - The string length of the newly generated object's ids.
* `fileCreationPolicy` - Permissions for file creation. Set `readonly` to prevent anyone including master to create new files. Set `master` to only allow master to create files. Set `user` to allow master and users to create files. By default, anonymous users are allowed to create new files.

##### Logging

Expand Down
89 changes: 89 additions & 0 deletions spec/ParseFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,95 @@ describe('Parse.File testing', () => {
});
});

it("save file with 'readonly' file creation policy", async () => {
await reconfigureServer({
fileCreationPolicy: 'readonly'
})

const file = new Parse.File("hello.txt", data, "text/plain");
try {
await file.save();
fail('file.save() should not be allowed')
} catch (error) {
expect(error.code).toBe(Parse.Error.OPERATION_FORBIDDEN);
}
});

it("save file with 'master' file creation policy", async done => {
await reconfigureServer({
fileCreationPolicy: 'master'
})

// Save as anonymous
let file = new Parse.File("hello.txt", data, "text/plain");
try {
await file.save();
fail('file.save() should not be allowed for anonymous users')
} catch (error) {
expect(error.code).toBe(Parse.Error.OPERATION_FORBIDDEN);
}

// Save as master
file = new Parse.File("hello.txt", data, "text/plain");
await file.save({ useMasterKey: true });

// Save as user
let user = new Parse.User();
user.set('username', `user${Math.floor(Math.random() * 10000)}`);
user.set('password', 'p@ssw0rd');
user = await user.signUp();
request.post({
headers: {
'Content-Type': 'text/html',
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest'
},
url: 'http://localhost:8378/1/files/file',
body: 'fee fi fo',
}, (error, response, body) => {
const b = JSON.parse(body);
expect(b.code).toEqual(Parse.Error.OPERATION_FORBIDDEN);
done();
});
});

it("save file with 'user' file creation policy", async done => {
await reconfigureServer({
fileCreationPolicy: 'user'
})

// Save as anonymous
let file = new Parse.File("hello.txt", data, "text/plain");
try {
await file.save();
fail('file.save() should not be allowed for anonymous users')
} catch (error) {
expect(error.code).toBe(Parse.Error.OPERATION_FORBIDDEN);
}

// Save as master
file = new Parse.File("hello.txt", data, "text/plain");
await file.save({ useMasterKey: true });

// Save as user
let user = new Parse.User();
user.set('username', `user${Math.floor(Math.random() * 10000)}`);
user.set('password', 'p@ssw0rd');
user = await user.signUp();
request.post({
headers: {
'Content-Type': 'text/html',
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest'
},
url: 'http://localhost:8378/1/files/file',
body: 'fee fi fo',
}, (error) => {
expect(error).toBe(null);
done();
});
});

it("file toJSON testing", done => {
const file = new Parse.File("hello.txt", data, "text/plain");
ok(!file.url());
Expand Down
15 changes: 15 additions & 0 deletions src/Routers/FilesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ export class FilesRouter {
}

createHandler(req, res, next) {
if (req.config.fileCreationPolicy === 'readonly') {
next(new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, 'read-only masterKey isn\'t allowed to perform the file create operation.'));
return;
}

if (req.config.fileCreationPolicy === 'master' && !req.auth.isMaster) {
next(new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, 'Only master is allowed to create new files.'));
return;
}

if (req.config.fileCreationPolicy === 'user' && !req.auth.isMaster && !req.auth.user) {
next(new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, 'Only master and registred users aire allowed to create new files.'));
return;
}

if (!req.body || !req.body.length) {
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR,
'Invalid file upload.'));
Expand Down