Skip to content

Commit a39d045

Browse files
authored
Fixes issue #4150: Session management (#4152)
* Adds tests * Provide fix * Fix post sessions (#4167) * add tests * provide fix * remove console.log
1 parent ba0a51d commit a39d045

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

spec/rest.spec.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Config = require('../src/Config');
55
var Parse = require('parse/node').Parse;
66
var rest = require('../src/rest');
77
var request = require('request');
8+
var rp = require('request-promise');
89

910
let config;
1011
let database;
@@ -498,6 +499,99 @@ describe('rest create', () => {
498499
expect(error.code).toEqual(119);
499500
done();
500501
})
502+
});
503+
504+
it ('locks down session', (done) => {
505+
let currentUser;
506+
Parse.User.signUp('foo', 'bar').then((user) => {
507+
currentUser = user;
508+
const sessionToken = user.getSessionToken();
509+
var headers = {
510+
'Content-Type': 'application/octet-stream',
511+
'X-Parse-Application-Id': 'test',
512+
'X-Parse-REST-API-Key': 'rest',
513+
'X-Parse-Session-Token': sessionToken,
514+
};
515+
let sessionId;
516+
return rp.get({
517+
headers: headers,
518+
url: 'http://localhost:8378/1/sessions/me',
519+
json: true,
520+
}).then(body => {
521+
sessionId = body.objectId;
522+
return rp.put({
523+
headers,
524+
url: 'http://localhost:8378/1/sessions/' + sessionId,
525+
json: {
526+
installationId: 'yolo'
527+
}
528+
})
529+
}).then(done.fail, (res) => {
530+
expect(res.statusCode).toBe(400);
531+
expect(res.error.code).toBe(105);
532+
return rp.put({
533+
headers,
534+
url: 'http://localhost:8378/1/sessions/' + sessionId,
535+
json: {
536+
sessionToken: 'yolo'
537+
}
538+
})
539+
}).then(done.fail, (res) => {
540+
expect(res.statusCode).toBe(400);
541+
expect(res.error.code).toBe(105);
542+
return Parse.User.signUp('other', 'user');
543+
}).then((otherUser) => {
544+
const user = new Parse.User();
545+
user.id = otherUser.id;
546+
return rp.put({
547+
headers,
548+
url: 'http://localhost:8378/1/sessions/' + sessionId,
549+
json: {
550+
user: Parse._encode(user)
551+
}
552+
})
553+
}).then(done.fail, (res) => {
554+
expect(res.statusCode).toBe(400);
555+
expect(res.error.code).toBe(105);
556+
const user = new Parse.User();
557+
user.id = currentUser.id;
558+
return rp.put({
559+
headers,
560+
url: 'http://localhost:8378/1/sessions/' + sessionId,
561+
json: {
562+
user: Parse._encode(user)
563+
}
564+
})
565+
}).then(done).catch(done.fail);
566+
}).catch(done.fail);
567+
});
568+
569+
it ('sets current user in new sessions', (done) => {
570+
let currentUser;
571+
Parse.User.signUp('foo', 'bar')
572+
.then((user) => {
573+
currentUser = user;
574+
const sessionToken = user.getSessionToken();
575+
const headers = {
576+
'X-Parse-Application-Id': 'test',
577+
'X-Parse-REST-API-Key': 'rest',
578+
'X-Parse-Session-Token': sessionToken,
579+
};
580+
return rp.post({
581+
headers,
582+
url: 'http://localhost:8378/1/sessions',
583+
json: true,
584+
body: { 'user': { '__type': 'Pointer', 'className':'_User', 'objectId': 'fakeId' } },
585+
})
586+
})
587+
.then((body) => {
588+
if (body.user.objectId === currentUser.id) {
589+
return done();
590+
} else {
591+
return done.fail();
592+
}
593+
})
594+
.catch(done.fail);
501595
})
502596
});
503597

src/RestWrite.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ RestWrite.prototype.handleFollowup = function() {
627627
};
628628

629629
// Handles the _Session class specialness.
630-
// Does nothing if this isn't an installation object.
630+
// Does nothing if this isn't an _Session object.
631631
RestWrite.prototype.handleSession = function() {
632632
if (this.response || this.className !== '_Session') {
633633
return;
@@ -644,6 +644,16 @@ RestWrite.prototype.handleSession = function() {
644644
'ACL on a Session.');
645645
}
646646

647+
if (this.query) {
648+
if (this.data.user && !this.auth.isMaster && this.data.user.objectId != this.auth.user.id) {
649+
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME);
650+
} else if (this.data.installationId) {
651+
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME);
652+
} else if (this.data.sessionToken) {
653+
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME);
654+
}
655+
}
656+
647657
if (!this.query && !this.auth.isMaster) {
648658
var token = 'r:' + cryptoUtils.newToken();
649659
var expiresAt = this.config.generateSessionExpiresAt();
@@ -661,7 +671,7 @@ RestWrite.prototype.handleSession = function() {
661671
expiresAt: Parse._encode(expiresAt)
662672
};
663673
for (var key in this.data) {
664-
if (key == 'objectId') {
674+
if (key === 'objectId' || key === 'user') {
665675
continue;
666676
}
667677
sessionData[key] = this.data[key];

0 commit comments

Comments
 (0)