Skip to content

Always clear sessions when user password is updated #3821

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

Merged
merged 3 commits into from
May 16, 2017
Merged
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
5 changes: 1 addition & 4 deletions spec/ParseServerRESTController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ describe('ParseServerRESTController', () => {
}).then(sessions => {
expect(sessions.length).toBe(0);
done();
}, (err) => {
jfail(err);
done();
});
}, done.fail);
});

it('ensures a session token is created when passing installationId != cloud', (done) => {
Expand Down
17 changes: 17 additions & 0 deletions spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2935,4 +2935,21 @@ describe('Parse.User testing', () => {
done();
});
});

it('should revoke sessions when setting paswword with masterKey (#3289)', (done) => {
let user;
Parse.User.signUp('username', 'password')
.then((newUser) => {
user = newUser;
user.set('password', 'newPassword');
return user.save(null, {useMasterKey: true});
}).then(() => {
const query = new Parse.Query('_Session');
query.equalTo('user', user);
return query.find({useMasterKey: true});
}).then((results) => {
expect(results.length).toBe(0);
done();
}, done.fail);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, I've adopted a new pattern in my promise shiz.

I used to do this:

async.do()
  .then(done)
  .then(null, done.fail)

the reason for the second then was to prevent an error in the first then block from getting swallowed.

But then I started using bluebird and adopted this:

async.do()
  .then(done)
  .catch(done.fail)

So testing it out a bit....

const Parse = require('parse/node');

const p = new Parse.Promise((resolve, reject) => {
  setTimeout(resolve, 100);
});

p
  .then(() => console.log('boo'))
  .catch(() => console.error('bingo'));

prints 'boo' as you'd expect. If I then mangle the then to create an error:

const Parse = require('parse/node');

const p = new Parse.Promise((resolve, reject) => {
  setTimeout(resolve, 100);
});

p
  .then(() => console.bogusFunction('boo'))
  .catch((e) => console.error(e, 'bingo'));

I get:

TypeError: console.bogusFunction is not a function
    at ParsePromise.p.then (/Users/arthur/code/followstyle-bot/testing.js:8:23)
    at ParsePromise.wrappedResolvedCallback (/Users/arthur/code/followstyle-bot/node_modules/parse/lib/node/ParsePromise.js:153:43)
    at ParsePromise.value (/Users/arthur/code/followstyle-bot/node_modules/parse/lib/node/ParsePromise.js:89:36)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5) 'bingo'

so far so good! now to demonstrate why we bother, if i change it to:

const Parse = require('parse/node');

const p = new Parse.Promise((resolve, reject) => {
  setTimeout(resolve, 100);
});

p
  .then(() => console.bogusFunction('boo'), (e) => console.error(e, 'bingo'));

then it just silently fails! no bueno.

and just to complete the circle, here's how the old style I used worked:

const Parse = require('parse/node');

const p = new Parse.Promise((resolve, reject) => {
  setTimeout(resolve, 100);
});

p
  .then(() => console.bogusFunction('boo'))
  .then(null, (e) => console.error(e, 'bingo'));

which will print out the exact same error as when using the catch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's interesting, having last handler like that, me likey :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case it isn't clear, my current preferred is:

async.do()
  .then(done)
  .catch(done.fail);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acinader makes sense, do you want me to update the code for that style? Or we'll do it at a later time?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to change this. just an fyi :)

});
});
7 changes: 5 additions & 2 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,12 @@ RestWrite.prototype.transformUser = function() {
return Promise.resolve();
}

if (this.query && !this.auth.isMaster) {
if (this.query) {
this.storage['clearSessions'] = true;
this.storage['generateNewSession'] = true;
// Generate a new session only if the user requested
if (!this.auth.isMaster) {
this.storage['generateNewSession'] = true;
}
}

return this._validatePasswordPolicy().then(() => {
Expand Down