Skip to content

Commit 5688b5b

Browse files
committed
chore: validate custom at multiple places
1 parent caee0ef commit 5688b5b

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/handlers/services/providerContext.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Providers from '../../providers';
99
import { RequestContext } from './requestContext';
1010
import { ANTHROPIC, AZURE_OPEN_AI } from '../../globals';
1111
import { GatewayError } from '../../errors/GatewayError';
12+
import { isValidCustomHost } from '../../middlewares/requestValidator';
1213

1314
export class ProviderContext {
1415
constructor(private provider: string) {
@@ -94,6 +95,10 @@ export class ProviderContext {
9495
}
9596

9697
async getFullURL(context: RequestContext): Promise<string> {
98+
const customHost = context.customHost;
99+
if (customHost && !isValidCustomHost(customHost, context.honoContext)) {
100+
throw new GatewayError('Invalid custom host');
101+
}
97102
const baseURL = context.customHost || (await this.getBaseURL(context));
98103
let url: string;
99104
if (context.endpoint === 'proxy') {

src/middlewares/requestValidator/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export const requestValidator = (c: Context, next: any) => {
141141
}
142142

143143
const customHostHeader = requestHeaders[`x-${POWERED_BY}-custom-host`];
144-
if (customHostHeader && !isValidCustomHost(c, customHostHeader)) {
144+
if (customHostHeader && !isValidCustomHost(customHostHeader, c)) {
145145
return new Response(
146146
JSON.stringify({
147147
status: 'failure',
@@ -228,7 +228,8 @@ export const requestValidator = (c: Context, next: any) => {
228228
}
229229
return next();
230230
};
231-
function isValidCustomHost(c: Context, customHost: string) {
231+
232+
export function isValidCustomHost(customHost: string, c?: Context) {
232233
try {
233234
const value = customHost.trim().toLowerCase();
234235

src/middlewares/requestValidator/schema/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GOOGLE_VERTEX_AI,
66
TRITON,
77
} from '../../../globals';
8+
import { isValidCustomHost } from '..';
89

910
export const configSchema: any = z
1011
.object({
@@ -149,7 +150,7 @@ export const configSchema: any = z
149150
.refine(
150151
(value) => {
151152
const customHost = value.custom_host;
152-
if (customHost && customHost.indexOf('api.portkey') > -1) {
153+
if (customHost && !isValidCustomHost(customHost)) {
153154
return false;
154155
}
155156
return true;

0 commit comments

Comments
 (0)