Skip to content

Commit 45dc51a

Browse files
committed
fix: drop duplicate oauth query params
1 parent 6999099 commit 45dc51a

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎src/oauth/request.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ const normalizeQuery = (query: ExtendedContext['query']): Record<string, string>
1717
return [];
1818
}
1919

20-
return [ [ key, Array.isArray(value) ? value[0] ?? '' : value ] ];
20+
if (Array.isArray(value)) {
21+
return [];
22+
}
23+
24+
return [ [ key, value ] ];
2125
}));
2226
};
2327

‎src/oauth/route/authorize.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const handle = (options: OAuthRouteOptions) => {
1313
}
1414

1515
const user = ctx.state.user;
16-
const state = (ctx.request.body as StateObject)?.state || (ctx.query as StateObject)?.state;
1716
const request = createOAuthRequest(ctx);
1817
const response = new OAuthResponse(ctx.response);
18+
const state = (request.body as StateObject)?.state || (request.query as StateObject)?.state;
1919

2020
await oAuthServer.handle(ctx, response, () => {
2121
return oAuthServer.authorize(request, response, {

‎test/tests/integration/oauth.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,24 @@ describe('OAuth', () => {
219219
expect(res.body).to.have.property('error_description').that.includes('client is invalid');
220220
});
221221

222+
it('should drop all authorization query parameter values when duplicates are present', async () => {
223+
const res1 = await defaultAuthorizationRequest(client1, {}, {
224+
state: [ 'someRandomState', 'otherRandomState' ],
225+
});
226+
227+
expect(res1.status).to.equal(302);
228+
expect(res1.headers['location']).to.include(`https://dash.globalping.io/authorize/`);
229+
230+
const res2 = await requestAgent
231+
.post(getApprovalUrl(res1.headers['location']!))
232+
.set('Cookie', `dash_session_token=${user1Cookie}`)
233+
.send({ approved: 1 });
234+
235+
expect(res2.status).to.equal(302);
236+
expect(res2.headers['location']).to.include(`code=`);
237+
expect(res2.headers['location']).not.to.include(`state=`);
238+
});
239+
222240
it('should fail authorization with mismatched redirect_uri', async () => {
223241
const res = await defaultAuthorizationRequest(client1, {}, {
224242
redirect_uri: 'https://wrongurl.com/callback',

0 commit comments

Comments
 (0)