Skip to content
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

Fix a bug where additional cookies set by beforeRedirection are overwritten #1485

Merged
merged 1 commit into from May 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 deletions packages/oauth/src/install-provider.spec.js
Expand Up @@ -259,7 +259,11 @@ describe('InstallProvider', async () => {
});
const req = {};
const headers = {};
const res = { setHeader(n, v) { headers[n] = v; }, writeHead: () => {}, end: () => {}, };
const res = {
getHeader(n) { return headers[n]; },
setHeader(n, v) { headers[n] = v; },
writeHead: () => {}, end: () => {},
};
try {
await installer.handleInstallPath(req, res);
assert.fail('Exception should be thrown')
Expand Down Expand Up @@ -291,7 +295,12 @@ describe('InstallProvider', async () => {
});
const req = {};
const headers = {};
const res = { setHeader(n, v) { headers[n] = v; }, writeHead: () => {}, end: () => {}, };
const res = {
getHeader(n) { return headers[n]; },
setHeader(n, v) { headers[n] = v; },
writeHead: () => {}, end: () => {},
end: () => {},
};
await installer.handleInstallPath(req, res);

assert.equal(headers['Location'], 'https://slack.com/oauth/v2/authorize?scope=channels%3Aread&state=fakeState&client_id=MY_ID&redirect_uri=https%3A%2F%2Fmysite.com%2Fslack%2Fredirect&team=T12345&user_scope=chat%3Awrite%3Auser');
Expand All @@ -312,11 +321,10 @@ describe('InstallProvider', async () => {
const req = {};
const headers = {};
const res = {
setHeader(n, v) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This mock object's behavior was incorrect.

if (headers[n] === undefined) headers[n] = [];
headers[n].push(v);
},
writeHead: () => {}, end: () => {},
getHeader(n) { return headers[n]; },
setHeader(n, v) { headers[n] = v; },
writeHead: () => {},
end: () => {},
};
const installPathOptions = {
beforeRedirection: async (_, res) => {
Expand Down
19 changes: 18 additions & 1 deletion packages/oauth/src/install-provider.ts
Expand Up @@ -347,7 +347,24 @@ export class InstallProvider {
return;
}
const state = await this.stateStore.generateStateParam(_installOptions, new Date());
res.setHeader('Set-Cookie', this.buildSetCookieHeaderForNewState(state));
const stateCookie: string = this.buildSetCookieHeaderForNewState(state);
if (res.getHeader('Set-Cookie')) {
// If the cookies already exist
const existingCookies = res.getHeader('Set-Cookie') || [];
const allCookies: string[] = [];
if (Array.isArray(existingCookies)) {
allCookies.push(...existingCookies);
} else if (typeof existingCookies === 'string') {
allCookies.push(existingCookies);
} else {
allCookies.push(existingCookies.toString());
}
// Append the state cookie
allCookies.push(stateCookie);
res.setHeader('Set-Cookie', allCookies);
Copy link
Member Author

Choose a reason for hiding this comment

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

setHeader accepts either an array or a primitive value (string or number)

Copy link
Member

Choose a reason for hiding this comment

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

Good catch!

} else {
res.setHeader('Set-Cookie', stateCookie);
}
const url = await this.generateInstallUrl(_installOptions, this.stateVerification, state);
this.logger.debug(`Generated authorize URL: ${url}`);

Expand Down