Skip to content

Commit

Permalink
chore: expose type and more fixes (#5268)
Browse files Browse the repository at this point in the history
Expose new interface while also getting rid of unneeded compiler ignores

None of the changes should add new security risks, despite this report:
> Code scanning results / CodeQL Failing after 4s — 2 new alerts
including 2 high severity security vulnerabilities

Not sure what that means, maybe a removed ignore...
  • Loading branch information
gastonfournier authored Nov 3, 2023
1 parent 1d9a671 commit 6f8f21f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
18 changes: 7 additions & 11 deletions src/lib/middleware/demo-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import { IUnleashServices } from '../types/services';
import { IUnleashConfig } from '../types/option';
import ApiUser from '../types/api-user';
import { ApiTokenType } from '../types/models/api-token';
import { IAuthRequest } from 'lib/server-impl';
import { IApiRequest } from 'lib/routes/unleash-types';

function demoAuthentication(
app: Application,
basePath: string, // eslint-disable-line
basePath: string,
{ userService }: Pick<IUnleashServices, 'userService'>,
{ authentication }: Pick<IUnleashConfig, 'authentication'>,
): void {
app.post(`${basePath}/auth/demo/login`, async (req, res) => {
app.post(`${basePath}/auth/demo/login`, async (req: IAuthRequest, res) => {
const { email } = req.body;
try {
const user = await userService.loginUserWithoutPassword(
email,
true,
);
// @ts-expect-error
req.session.user = user;
return res.status(200).json(user);
} catch (e) {
Expand All @@ -28,19 +29,15 @@ function demoAuthentication(
}
});

app.use(`${basePath}/api/admin/`, (req, res, next) => {
// @ts-expect-error
app.use(`${basePath}/api/admin/`, (req: IAuthRequest, res, next) => {
if (req.session.user?.email) {
// @ts-expect-error
req.user = req.session.user;
}
next();
});

app.use(`${basePath}/api/client`, (req, res, next) => {
// @ts-expect-error
app.use(`${basePath}/api/client`, (req: IApiRequest, res, next) => {
if (!authentication.enableApiToken && !req.user) {
// @ts-expect-error
req.user = new ApiUser({
tokenName: 'unauthed-default-client',
permissions: [],
Expand All @@ -53,8 +50,7 @@ function demoAuthentication(
next();
});

app.use(`${basePath}/api`, (req, res, next) => {
// @ts-expect-error
app.use(`${basePath}/api`, (req: IAuthRequest, res, next) => {
if (req.user) {
return next();
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/routes/proxy-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NONE,
} from '../../types';
import { Logger } from '../../logger';
import ApiUser from '../../types/api-user';
import { IApiUser } from '../../types/api-user';
import {
ClientMetricsSchema,
createRequestSchema,
Expand All @@ -32,7 +32,7 @@ interface ApiUserRequest<
ReqBody = any,
ReqQuery = any,
> extends Request<PARAM, ResBody, ReqBody, ReqQuery> {
user: ApiUser;
user: IApiUser;
}

type Services = Pick<
Expand Down
3 changes: 2 additions & 1 deletion src/lib/server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import ApiUser from './types/api-user';
import { Logger, LogLevel } from './logger';
import AuthenticationRequired from './types/authentication-required';
import Controller from './routes/controller';
import { IAuthRequest } from './routes/unleash-types';
import { IApiRequest, IAuthRequest } from './routes/unleash-types';
import { SimpleAuthSettings } from './types/settings/simple-auth-settings';
import { Knex } from 'knex';
import * as permissions from './types/permissions';
Expand Down Expand Up @@ -209,5 +209,6 @@ export type {
IUser,
IUnleashServices,
IAuthRequest,
IApiRequest,
SimpleAuthSettings,
};
2 changes: 1 addition & 1 deletion src/lib/services/client-metrics/metrics-service-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
secondsToMilliseconds,
} from 'date-fns';
import { CLIENT_METRICS } from '../../types/events';
import ApiUser from '../../types/api-user';
import ApiUser, { IApiUser } from '../../types/api-user';
import { ALL } from '../../types/models/api-token';
import User from '../../types/user';
import { collapseHourlyMetrics } from '../../util/collapseHourlyMetrics';
Expand Down
14 changes: 7 additions & 7 deletions src/lib/services/proxy-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IUnleashConfig, IUnleashServices, IUnleashStores } from '../types';
import { Logger } from '../logger';
import { ClientMetricsSchema, ProxyFeatureSchema } from '../openapi';
import ApiUser from '../types/api-user';
import ApiUser, { IApiUser } from '../types/api-user';
import {
Context,
InMemStorageProvider,
Expand Down Expand Up @@ -61,7 +61,7 @@ export class ProxyService {
}

async getProxyFeatures(
token: ApiUser,
token: IApiUser,
context: Context,
): Promise<ProxyFeatureSchema[]> {
const client = await this.clientForProxyToken(token);
Expand All @@ -85,15 +85,15 @@ export class ProxyService {
}

async registerProxyMetrics(
token: ApiUser,
token: IApiUser,
metrics: ClientMetricsSchema,
ip: string,
): Promise<void> {
ProxyService.assertExpectedTokenType(token);

const environment =
this.services.clientMetricsServiceV2.resolveMetricsEnvironment(
token,
token as ApiUser,
metrics,
);

Expand All @@ -103,7 +103,7 @@ export class ProxyService {
);
}

private async clientForProxyToken(token: ApiUser): Promise<Unleash> {
private async clientForProxyToken(token: IApiUser): Promise<Unleash> {
ProxyService.assertExpectedTokenType(token);

let client = this.clients.get(token.secret);
Expand All @@ -115,7 +115,7 @@ export class ProxyService {
return client;
}

private async createClientForProxyToken(token: ApiUser): Promise<Unleash> {
private async createClientForProxyToken(token: IApiUser): Promise<Unleash> {
const repository = new ProxyRepository(
this.config,
this.stores,
Expand Down Expand Up @@ -153,7 +153,7 @@ export class ProxyService {
this.clients.forEach((promise) => promise.then((c) => c.destroy()));
}

private static assertExpectedTokenType({ type }: ApiUser) {
private static assertExpectedTokenType({ type }: IApiUser) {
if (!(type === ApiTokenType.FRONTEND || type === ApiTokenType.ADMIN)) {
throw new InvalidTokenError();
}
Expand Down

0 comments on commit 6f8f21f

Please sign in to comment.