Skip to content

Commit d9f36b0

Browse files
Klaitosdplewis
authored andcommitted
Add missing encodeURIComponent on username password reset (#6278)
* Add missing encodeURIComponent on username * Add new unit test on encoded username password reset redirect
1 parent 2d665c9 commit d9f36b0

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

spec/ValidationAndPasswordsReset.spec.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ describe('Custom Pages, Email Verification, Password Reset', () => {
463463
});
464464
});
465465

466-
it('succeeds sending a password reset email if appName, publicServerURL, and email adapter are prodvided', done => {
466+
it('succeeds sending a password reset email if appName, publicServerURL, and email adapter are provided', done => {
467467
reconfigureServer({
468468
appName: 'coolapp',
469469
publicServerURL: 'http://localhost:1337/1',
@@ -910,6 +910,65 @@ describe('Custom Pages, Email Verification, Password Reset', () => {
910910
});
911911
});
912912

913+
it('should redirect with username encoded on success page', done => {
914+
const user = new Parse.User();
915+
const emailAdapter = {
916+
sendVerificationEmail: () => Promise.resolve(),
917+
sendPasswordResetEmail: options => {
918+
request({
919+
url: options.link,
920+
followRedirects: false,
921+
}).then(response => {
922+
expect(response.status).toEqual(302);
923+
const re = /http:\/\/localhost:8378\/1\/apps\/choose_password\?token=([a-zA-Z0-9]+)\&id=test\&username=zxcv%2B1/;
924+
const match = response.text.match(re);
925+
if (!match) {
926+
fail('should have a token');
927+
done();
928+
return;
929+
}
930+
const token = match[1];
931+
932+
request({
933+
url: 'http://localhost:8378/1/apps/test/request_password_reset',
934+
method: 'POST',
935+
body: { new_password: 'hello', token, username: 'zxcv+1' },
936+
headers: {
937+
'Content-Type': 'application/x-www-form-urlencoded',
938+
},
939+
followRedirects: false,
940+
}).then(response => {
941+
expect(response.status).toEqual(302);
942+
expect(response.text).toEqual(
943+
'Found. Redirecting to http://localhost:8378/1/apps/password_reset_success.html?username=zxcv%2B1'
944+
);
945+
done();
946+
});
947+
});
948+
},
949+
sendMail: () => {},
950+
};
951+
reconfigureServer({
952+
appName: 'emailing app',
953+
verifyUserEmails: true,
954+
emailAdapter: emailAdapter,
955+
publicServerURL: 'http://localhost:8378/1',
956+
}).then(() => {
957+
user.setPassword('asdf');
958+
user.setUsername('zxcv+1');
959+
user.set('email', '[email protected]');
960+
user.signUp().then(() => {
961+
Parse.User.requestPasswordReset('[email protected]', {
962+
error: err => {
963+
jfail(err);
964+
fail('Should not fail');
965+
done();
966+
},
967+
});
968+
});
969+
});
970+
});
971+
913972
it('should programmatically reset password on ajax request', async done => {
914973
const user = new Parse.User();
915974
const emailAdapter = {

src/Routers/PublicAPIRouter.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ export class PublicAPIRouter extends PromiseRouter {
212212
}
213213
}
214214

215+
const encodedUsername = encodeURIComponent(username);
216+
const location = result.success
217+
? `${config.passwordResetSuccessURL}?username=${encodedUsername}`
218+
: `${config.choosePasswordURL}?${params}`;
219+
215220
return Promise.resolve({
216221
status: 302,
217-
location: `${
218-
result.success
219-
? `${config.passwordResetSuccessURL}?username=${username}`
220-
: `${config.choosePasswordURL}?${params}`
221-
}`,
222+
location,
222223
});
223224
});
224225
}

0 commit comments

Comments
 (0)