Skip to content

Commit

Permalink
fix: names of cookies with a colon [#587]
Browse files Browse the repository at this point in the history
  • Loading branch information
vitshev committed May 29, 2024
1 parent bc06aec commit 79a4254
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/ui/src/server/components/yt-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {AppConfig} from '@gravity-ui/nodekit';
import type {Response} from 'express';
import {YT_CYPRESS_COOKIE_NAME} from '../../shared/constants';
import {getClustersFromConfig} from './utils';
import {makeAuthClusterCookieName} from '../utils';

export function isYtAuthEnabled(config: AppConfig) {
return Boolean(config.allowPasswordAuth);
Expand All @@ -14,7 +15,7 @@ export function YTAuthLogout(res: Response) {
'set-cookie',
[`${YT_CYPRESS_COOKIE_NAME}=deleted; Path=/; Max-Age=0;`].concat(
Object.keys(clusters).map(
(cluster) => `${cluster}:${YT_CYPRESS_COOKIE_NAME}=deleted; Path=/; Max-Age=0;`,
(cluster) => `${makeAuthClusterCookieName(cluster)}=deleted; Path=/; Max-Age=0;`,
),
),
);
Expand Down
5 changes: 2 additions & 3 deletions packages/ui/src/server/controllers/clusters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {Request, Response} from 'express';
import {YT_CYPRESS_COOKIE_NAME} from '../../shared/constants';
import {sendError, sendResponse} from '../utils';
import {makeAuthClusterCookieName, sendError, sendResponse} from '../utils';
import {getVersions} from '../components/cluster-queries';
import {getClustersFromConfig} from '../components/utils';

Expand All @@ -24,7 +23,7 @@ export function clusterAuthStatus(req: Request, res: Response) {

data = Object.keys(clusters).reduce((ret, cluster) => {
ret[cluster] = {
authorized: Boolean(req.cookies[`${cluster}:${YT_CYPRESS_COOKIE_NAME}`]),
authorized: Boolean(req.cookies[makeAuthClusterCookieName(cluster)]),
};

return ret;
Expand Down
9 changes: 7 additions & 2 deletions packages/ui/src/server/controllers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import axios from 'axios';
import _ from 'lodash';
import {YT_CYPRESS_COOKIE_NAME} from '../../shared/constants';
import {getUserYTApiSetup, getYTApiClusterSetup} from '../components/requestsSetup';
import {UNEXPECTED_PIPE_AXIOS_RESPONSE, pipeAxiosResponse, sendAndLogError} from '../utils';
import {
UNEXPECTED_PIPE_AXIOS_RESPONSE,
makeAuthClusterCookieName,
pipeAxiosResponse,
sendAndLogError,
} from '../utils';
import crypto from 'crypto';

// @ts-ignore
Expand Down Expand Up @@ -50,7 +55,7 @@ export async function handleLogin(req: Request, res: Response) {
ret.push(
item.replace(
YT_CYPRESS_COOKIE_NAME,
`${ytAuthCluster}:${YT_CYPRESS_COOKIE_NAME}`,
makeAuthClusterCookieName(ytAuthCluster),
),
);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/server/middlewares/yt-auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type {Request, Response} from 'express';
import {AppMiddleware} from '@gravity-ui/expresskit';
import {YT_CYPRESS_COOKIE_NAME, YT_UI_CLUSTER_HEADER_NAME} from '../../shared/constants';
import {makeAuthClusterCookieName} from '../utils';

export function createYTAuthorizationResolver(): AppMiddleware {
return async function resolveYTAuthorization(req: Request, res: Response, next) {
const {ytAuthCluster} = req.params;

const secret: string = req.cookies[`${ytAuthCluster}:${YT_CYPRESS_COOKIE_NAME}`];
const secret: string = req.cookies[makeAuthClusterCookieName(ytAuthCluster)];

if (ytAuthCluster) {
res.setHeader(YT_UI_CLUSTER_HEADER_NAME, ytAuthCluster);
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/src/server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {AxiosError, AxiosResponse} from 'axios';
import {AppContext} from '@gravity-ui/nodekit';
import {isYTError} from '../../shared/utils';
import {getApp} from '../ServerFactory';
import {YT_CYPRESS_COOKIE_NAME} from '../../shared/constants';
import path from 'path';

export function isProductionEnv() {
Expand Down Expand Up @@ -200,3 +201,7 @@ export async function sendAndLogError(

return res.status(status || 500).send({message: JSON.stringify(e)});
}

export const makeAuthClusterCookieName = (ytAuthCluster: string) => {
return `${ytAuthCluster}_${YT_CYPRESS_COOKIE_NAME}`;
};

0 comments on commit 79a4254

Please sign in to comment.