Skip to content

Commit 17fe639

Browse files
committed
fix
1 parent 421fe10 commit 17fe639

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

spec/rest.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,63 @@ describe('read-only masterKey', () => {
13071307
});
13081308
expect(Array.isArray(res.data)).toBe(true);
13091309
});
1310+
1311+
it('should throw when trying to delete a file with readOnlyMasterKey', async () => {
1312+
// Create a file with the real master key
1313+
const uploadRes = await request({
1314+
method: 'POST',
1315+
url: `${Parse.serverURL}/files/readonly-delete-test.txt`,
1316+
headers: {
1317+
'X-Parse-Application-Id': Parse.applicationId,
1318+
'X-Parse-Master-Key': Parse.masterKey,
1319+
'Content-Type': 'text/plain',
1320+
},
1321+
body: 'file content',
1322+
});
1323+
const filename = uploadRes.data.name;
1324+
expect(filename).toBeDefined();
1325+
1326+
// Attempt delete with readOnlyMasterKey — should be rejected
1327+
loggerErrorSpy.calls.reset();
1328+
try {
1329+
await request({
1330+
method: 'DELETE',
1331+
url: `${Parse.serverURL}/files/${filename}`,
1332+
headers: {
1333+
'X-Parse-Application-Id': Parse.applicationId,
1334+
'X-Parse-Master-Key': 'read-only-test',
1335+
},
1336+
});
1337+
fail('should have thrown');
1338+
} catch (res) {
1339+
expect(res.status).toBe(403);
1340+
expect(res.data.error).toBe('Permission denied');
1341+
}
1342+
1343+
// Verify file still exists
1344+
const getRes = await request({ url: uploadRes.data.url });
1345+
expect(getRes.status).toBe(200);
1346+
});
1347+
1348+
it('should throw when trying to create a file with readOnlyMasterKey', async () => {
1349+
loggerErrorSpy.calls.reset();
1350+
try {
1351+
await request({
1352+
method: 'POST',
1353+
url: `${Parse.serverURL}/files/readonly-create-test.txt`,
1354+
headers: {
1355+
'X-Parse-Application-Id': Parse.applicationId,
1356+
'X-Parse-Master-Key': 'read-only-test',
1357+
'Content-Type': 'text/plain',
1358+
},
1359+
body: 'file content',
1360+
});
1361+
fail('should have thrown');
1362+
} catch (res) {
1363+
expect(res.status).toBe(403);
1364+
expect(res.data.error).toBe('Permission denied');
1365+
}
1366+
});
13101367
});
13111368

13121369
describe('rest context', () => {

src/Routers/FilesRouter.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ export class FilesRouter {
300300
}
301301

302302
async createHandler(req, res, next) {
303+
if (req.auth.isReadOnly) {
304+
const error = createSanitizedHttpError(403, "read-only masterKey isn't allowed to create a file.", req.config);
305+
res.status(error.status);
306+
res.end(`{"error":"${error.message}"}`);
307+
return;
308+
}
303309
const config = req.config;
304310
const user = req.auth.user;
305311
const isMaster = req.auth.isMaster;
@@ -667,6 +673,12 @@ export class FilesRouter {
667673
}
668674

669675
async deleteHandler(req, res, next) {
676+
if (req.auth.isReadOnly) {
677+
const error = createSanitizedHttpError(403, "read-only masterKey isn't allowed to delete a file.", req.config);
678+
res.status(error.status);
679+
res.end(`{"error":"${error.message}"}`);
680+
return;
681+
}
670682
try {
671683
const { filesController } = req.config;
672684
const filename = FilesRouter._getFilenameFromParams(req);

0 commit comments

Comments
 (0)