Skip to content

Commit

Permalink
fix(chat): add validation (#2866)
Browse files Browse the repository at this point in the history
Co-authored-by: Ilya Bondar <[email protected]>
  • Loading branch information
denys-kolomiitsev and IlyaBondar authored Dec 23, 2024
1 parent 53c7802 commit 69b9dfd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
6 changes: 1 addition & 5 deletions apps/chat/src/pages/api/[entitytype]/[...slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { JWT, getToken } from 'next-auth/jwt';

import { constructPath } from '@/src/utils/app/file';
import { validateServerSession } from '@/src/utils/auth/session';
import { isValidEntityApiType } from '@/src/utils/server/api';
import { getApiHeaders } from '@/src/utils/server/get-headers';
import { logger } from '@/src/utils/server/logger';
import { ServerUtils } from '@/src/utils/server/server';

import { ApiKeys } from '@/src/types/common';
import { DialAIError } from '@/src/types/error';
import { HTTPMethod } from '@/src/types/http';

Expand Down Expand Up @@ -40,10 +40,6 @@ const getEntityUrlFromSlugs = (
);
};

const isValidEntityApiType = (apiKey: string): boolean => {
return Object.values(ApiKeys).includes(apiKey as ApiKeys);
};

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const entityType = ServerUtils.getEntityTypeFromPath(req);
if (!entityType || !isValidEntityApiType(entityType)) {
Expand Down
23 changes: 15 additions & 8 deletions apps/chat/src/pages/api/listing/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getServerSession } from 'next-auth/next';

import { constructPath } from '@/src/utils/app/file';
import { validateServerSession } from '@/src/utils/auth/session';
import { isValidEntityApiType } from '@/src/utils/server/api';
import { getApiHeaders } from '@/src/utils/server/get-headers';
import { logger } from '@/src/utils/server/logger';

Expand All @@ -15,6 +16,7 @@ import { errorsMessages } from '@/src/constants/errors';

import { authOptions } from '@/src/pages/api/auth/[...nextauth]';

import { sanitizeUri } from 'micromark-util-sanitize-uri';
import fetch from 'node-fetch';

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
Expand All @@ -37,14 +39,19 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
};
const token = await getToken({ req });

const apiUrls = body.urls.map(
(url) =>
`${constructPath(
process.env.DIAL_API_HOST,
'v1/metadata',
url,
)}/?limit=${limit}&recursive=${recursive}`,
);
const apiUrls = body.urls
.map((url) => {
const entityType = url.split('/')[0];
if (isValidEntityApiType(entityType)) {
return `${constructPath(
process.env.DIAL_API_HOST,
'v1/metadata',
sanitizeUri(url),
)}/?limit=${limit}&recursive=${recursive}`;
}
return;
})
.filter(Boolean) as string[];

const fetchPromises = apiUrls.map(async (url) => {
const response = await fetch(url, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { getToken } from 'next-auth/jwt';

import { constructPath } from '@/src/utils/app/file';
import { validateServerSession } from '@/src/utils/auth/session';
import { isValidEntityApiType } from '@/src/utils/server/api';
import { getApiHeaders } from '@/src/utils/server/get-headers';
import { logger } from '@/src/utils/server/logger';
import { ServerUtils } from '@/src/utils/server/server';

import { ApiKeys } from '@/src/types/common';
import { DialAIError } from '@/src/types/error';

import { errorsMessages } from '@/src/constants/errors';
Expand Down Expand Up @@ -39,10 +39,6 @@ const getEntityUrlFromSlugs = (
);
};

const isValidEntityApiType = (apiKey: string): boolean => {
return Object.values(ApiKeys).includes(apiKey as ApiKeys);
};

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const entityType = ServerUtils.getEntityTypeFromPath(req);
if (!entityType || !isValidEntityApiType(entityType)) {
Expand Down
5 changes: 5 additions & 0 deletions apps/chat/src/utils/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ServerUtils } from '@/src/utils/server/server';

import { ApplicationInfo } from '@/src/types/applications';
import { Conversation } from '@/src/types/chat';
import { ApiKeys } from '@/src/types/common';
import { HTTPMethod } from '@/src/types/http';
import { PromptInfo } from '@/src/types/prompt';

Expand Down Expand Up @@ -283,3 +284,7 @@ export const getPublicItemIdWithoutVersion = (version: string, id: string) => {

export const addVersionToId = (id: string, version: string) =>
[id, version].join(pathKeySeparator);

export const isValidEntityApiType = (apiKey: string): boolean => {
return Object.values(ApiKeys).includes(apiKey as ApiKeys);
};

0 comments on commit 69b9dfd

Please sign in to comment.