Skip to content

Commit 89e5172

Browse files
committed
fix: Validate session in middleware for non-GET requests to /sessions/me
1 parent 9c48765 commit 89e5172

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

spec/ParseSession.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,64 @@ describe('Parse.Session', () => {
256256
expect(newSession.createdWith.action).toBe('create');
257257
expect(newSession.createdWith.authProvider).toBeUndefined();
258258
});
259+
260+
describe('PUT /sessions/me', () => {
261+
it('should return error with invalid session token', async () => {
262+
const response = await request({
263+
method: 'PUT',
264+
url: 'http://localhost:8378/1/sessions/me',
265+
headers: {
266+
'X-Parse-Application-Id': 'test',
267+
'X-Parse-REST-API-Key': 'rest',
268+
'X-Parse-Session-Token': 'r:invalid-session-token',
269+
'Content-Type': 'application/json',
270+
},
271+
body: JSON.stringify({}),
272+
}).catch(e => e);
273+
expect(response.status).not.toBe(500);
274+
expect(response.data.code).toBe(Parse.Error.INVALID_SESSION_TOKEN);
275+
});
276+
277+
it('should return error without session token', async () => {
278+
const response = await request({
279+
method: 'PUT',
280+
url: 'http://localhost:8378/1/sessions/me',
281+
headers: {
282+
'X-Parse-Application-Id': 'test',
283+
'X-Parse-REST-API-Key': 'rest',
284+
'Content-Type': 'application/json',
285+
},
286+
body: JSON.stringify({}),
287+
}).catch(e => e);
288+
expect(response.status).not.toBe(500);
289+
});
290+
});
291+
292+
describe('DELETE /sessions/me', () => {
293+
it('should return error with invalid session token', async () => {
294+
const response = await request({
295+
method: 'DELETE',
296+
url: 'http://localhost:8378/1/sessions/me',
297+
headers: {
298+
'X-Parse-Application-Id': 'test',
299+
'X-Parse-REST-API-Key': 'rest',
300+
'X-Parse-Session-Token': 'r:invalid-session-token',
301+
},
302+
}).catch(e => e);
303+
expect(response.status).not.toBe(500);
304+
expect(response.data.code).toBe(Parse.Error.INVALID_SESSION_TOKEN);
305+
});
306+
307+
it('should return error without session token', async () => {
308+
const response = await request({
309+
method: 'DELETE',
310+
url: 'http://localhost:8378/1/sessions/me',
311+
headers: {
312+
'X-Parse-Application-Id': 'test',
313+
'X-Parse-REST-API-Key': 'rest',
314+
},
315+
}).catch(e => e);
316+
expect(response.status).not.toBe(500);
317+
});
318+
});
259319
});

src/middlewares.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ const handleRateLimit = async (req, res, next) => {
377377
export const handleParseSession = async (req, res, next) => {
378378
try {
379379
const info = req.info;
380-
if (req.auth || req.url === '/sessions/me') {
380+
if (req.auth || (req.url === '/sessions/me' && req.method === 'GET')) {
381381
next();
382382
return;
383383
}

0 commit comments

Comments
 (0)