Skip to content

Commit

Permalink
feat: white url list for openai security
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Jun 12, 2023
1 parent bdb03e0 commit 0d46110
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
18 changes: 18 additions & 0 deletions app/api/openai/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { OpenaiPath } from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { requestOpenai } from "../../common";

const ALLOWD_PATH = new Set(Object.values(OpenaiPath));

async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
console.log("[OpenAI Route] params ", params);

const subpath = params.path.join("/");

if (!ALLOWD_PATH.has(subpath)) {
console.log("[OpenAI Route] forbidden path ", subpath);
return NextResponse.json(
{
error: true,
msg: "you are not allowed to request " + subpath,
},
{
status: 403,
},
);
}

const authResult = auth(req);
if (authResult.error) {
return NextResponse.json(authResult, {
Expand Down
13 changes: 5 additions & 8 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { REQUEST_TIMEOUT_MS } from "@/app/constant";
import { OpenaiPath, REQUEST_TIMEOUT_MS } from "@/app/constant";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";

import { ChatOptions, getHeaders, LLMApi, LLMUsage } from "../api";
Expand All @@ -10,10 +10,6 @@ import {
import { prettyObject } from "@/app/utils/format";

export class ChatGPTApi implements LLMApi {
public ChatPath = "v1/chat/completions";
public UsagePath = "dashboard/billing/usage";
public SubsPath = "dashboard/billing/subscription";

path(path: string): string {
let openaiUrl = useAccessStore.getState().openaiUrl;
if (openaiUrl.endsWith("/")) {
Expand Down Expand Up @@ -55,7 +51,7 @@ export class ChatGPTApi implements LLMApi {
options.onController?.(controller);

try {
const chatPath = this.path(this.ChatPath);
const chatPath = this.path(OpenaiPath.ChatPath);
const chatPayload = {
method: "POST",
body: JSON.stringify(requestPayload),
Expand Down Expand Up @@ -177,14 +173,14 @@ export class ChatGPTApi implements LLMApi {
const [used, subs] = await Promise.all([
fetch(
this.path(
`${this.UsagePath}?start_date=${startDate}&end_date=${endDate}`,
`${OpenaiPath.UsagePath}?start_date=${startDate}&end_date=${endDate}`,
),
{
method: "GET",
headers: getHeaders(),
},
),
fetch(this.path(this.SubsPath), {
fetch(this.path(OpenaiPath.SubsPath), {
method: "GET",
headers: getHeaders(),
}),
Expand Down Expand Up @@ -228,3 +224,4 @@ export class ChatGPTApi implements LLMApi {
} as LLMUsage;
}
}
export { OpenaiPath };

This comment has been minimized.

Copy link
@dvtate

dvtate Oct 25, 2023

why export this? it's never used anywhere else

6 changes: 6 additions & 0 deletions app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ export const LAST_INPUT_KEY = "last-input";
export const REQUEST_TIMEOUT_MS = 60000;

export const EXPORT_MESSAGE_CLASS_NAME = "export-markdown";

export const OpenaiPath = {
ChatPath: "v1/chat/completions",
UsagePath: "dashboard/billing/usage",
SubsPath: "dashboard/billing/subscription",
};

0 comments on commit 0d46110

Please sign in to comment.