Skip to content

Commit 147b493

Browse files
authored
Results invalid session when providing an invalid session token (#2154)
* Results invalid session when providing an invalid session token * Reverts unsafe loggers * Fixes failing tests - The tests were failin when run in sequence as we called done() before the JSSDK had a chance to register the session token, therefore having a proper logout call in afterEach
1 parent a861c4e commit 147b493

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

spec/ParseUser.spec.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ describe('Parse.User testing', () => {
15891589
bob.setPassword('meower');
15901590
return bob.save();
15911591
}).then(() => {
1592-
return Parse.User.logIn('bob', 'meower');
1592+
return Parse.User.logIn('bob', 'meower');
15931593
}).then((bob) => {
15941594
expect(bob.getUsername()).toEqual('bob');
15951595
done();
@@ -2091,7 +2091,7 @@ describe('Parse.User testing', () => {
20912091
fail('Save should have failed.');
20922092
done();
20932093
}, (e) => {
2094-
expect(e.code).toEqual(Parse.Error.SESSION_MISSING);
2094+
expect(e.code).toEqual(Parse.Error.INVALID_SESSION_TOKEN);
20952095
done();
20962096
});
20972097
});
@@ -2124,6 +2124,26 @@ describe('Parse.User testing', () => {
21242124
});
21252125
});
21262126

2127+
it("invalid session tokens are rejected", (done) => {
2128+
Parse.User.signUp("asdf", "zxcv", null, {
2129+
success: function(user) {
2130+
request.get({
2131+
url: 'http://localhost:8378/1/classes/AClass',
2132+
json: true,
2133+
headers: {
2134+
'X-Parse-Application-Id': 'test',
2135+
'X-Parse-Rest-API-Key': 'rest',
2136+
'X-Parse-Session-Token': 'text'
2137+
},
2138+
}, (error, response, body) => {
2139+
expect(body.code).toBe(209);
2140+
expect(body.error).toBe('invalid session token');
2141+
done();
2142+
})
2143+
}
2144+
});
2145+
});
2146+
21272147
it_exclude_dbs(['postgres'])('should cleanup null authData keys (regression test for #935)', (done) => {
21282148
let database = new Config(Parse.applicationId).database;
21292149
database.create('_User', {
@@ -2374,7 +2394,7 @@ describe('Parse.User testing', () => {
23742394
})
23752395
.then(() => obj.fetch())
23762396
.catch(error => {
2377-
expect(error.code).toEqual(Parse.Error.OBJECT_NOT_FOUND);
2397+
expect(error.code).toEqual(Parse.Error.INVALID_SESSION_TOKEN);
23782398
done();
23792399
});
23802400
})

spec/ValidationAndPasswordsReset.spec.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,12 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
304304
});
305305

306306
it_exclude_dbs(['postgres'])('receives the app name and user in the adapter', done => {
307+
var emailSent = false;
307308
var emailAdapter = {
308309
sendVerificationEmail: options => {
309310
expect(options.appName).toEqual('emailing app');
310311
expect(options.user.get('email')).toEqual('[email protected]');
311-
done();
312+
emailSent = true;
312313
},
313314
sendPasswordResetEmail: () => Promise.resolve(),
314315
sendMail: () => {}
@@ -325,7 +326,10 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
325326
user.setUsername("zxcv");
326327
user.set('email', '[email protected]');
327328
user.signUp(null, {
328-
success: () => {},
329+
success: () => {
330+
expect(emailSent).toBe(true);
331+
done();
332+
},
329333
error: function(userAgain, error) {
330334
fail('Failed to save user');
331335
done();
@@ -336,23 +340,10 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
336340

337341
it_exclude_dbs(['postgres'])('when you click the link in the email it sets emailVerified to true and redirects you', done => {
338342
var user = new Parse.User();
343+
var sendEmailOptions;
339344
var emailAdapter = {
340345
sendVerificationEmail: options => {
341-
request.get(options.link, {
342-
followRedirect: false,
343-
}, (error, response, body) => {
344-
expect(response.statusCode).toEqual(302);
345-
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/verify_email_success.html?username=user');
346-
user.fetch()
347-
.then(() => {
348-
expect(user.get('emailVerified')).toEqual(true);
349-
done();
350-
}, (err) => {
351-
console.error(err);
352-
fail("this should not fail");
353-
done();
354-
});
355-
});
346+
sendEmailOptions = options;
356347
},
357348
sendPasswordResetEmail: () => Promise.resolve(),
358349
sendMail: () => {}
@@ -364,10 +355,32 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
364355
publicServerURL: "http://localhost:8378/1"
365356
})
366357
.then(() => {
367-
user.setPassword("asdf");
358+
user.setPassword("other-password");
368359
user.setUsername("user");
369360
user.set('email', '[email protected]');
370-
user.signUp();
361+
return user.signUp();
362+
}).then(() => {
363+
expect(sendEmailOptions).not.toBeUndefined();
364+
request.get(sendEmailOptions.link, {
365+
followRedirect: false,
366+
}, (error, response, body) => {
367+
expect(response.statusCode).toEqual(302);
368+
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/verify_email_success.html?username=user');
369+
user.fetch()
370+
.then(() => {
371+
expect(user.get('emailVerified')).toEqual(true);
372+
done();
373+
}, (err) => {
374+
console.error(err);
375+
fail("this should not fail");
376+
done();
377+
}).catch((err) =>
378+
{
379+
console.error(err);
380+
fail(err);
381+
done();
382+
})
383+
});
371384
});
372385
});
373386

spec/helper.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ global.it_exclude_dbs = excluded => {
329329
}
330330
}
331331

332+
global.fit_exclude_dbs = excluded => {
333+
if (excluded.includes(process.env.PARSE_SERVER_TEST_DB)) {
334+
return xit;
335+
} else {
336+
return fit;
337+
}
338+
}
339+
332340
// LiveQuery test setting
333341
require('../src/LiveQuery/PLog').logLevel = 'NONE';
334342
var libraryCache = {};

src/Auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var getAuthForSessionToken = function({ config, sessionToken, installationId } =
5858
return query.execute().then((response) => {
5959
var results = response.results;
6060
if (results.length !== 1 || !results[0]['user']) {
61-
return nobody(config);
61+
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'invalid session token');
6262
}
6363

6464
var now = new Date(),

src/middlewares.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ function handleParseHeaders(req, res, next) {
130130
return invalidRequest(req, res);
131131
}
132132

133+
if (req.url == "/login") {
134+
delete info.sessionToken;
135+
}
136+
133137
if (!info.sessionToken) {
134138
req.auth = new auth.Auth({ config: req.config, installationId: info.installationId, isMaster: false });
135139
next();
@@ -219,6 +223,7 @@ var allowMethodOverride = function(req, res, next) {
219223
};
220224

221225
var handleParseErrors = function(err, req, res, next) {
226+
// TODO: Add logging as those errors won't make it to the PromiseRouter
222227
if (err instanceof Parse.Error) {
223228
var httpStatus;
224229

0 commit comments

Comments
 (0)