feat: check for invalid conversationId and parentMessageId

pull/403/head
Travis Fischer 2023-02-28 04:21:30 -06:00
rodzic 17b81456d2
commit 539aa6d45c
3 zmienionych plików z 32 dodań i 1 usunięć

Wyświetl plik

@ -327,7 +327,7 @@ These libraries work with email + password accounts (e.g., they do not support a
Alternatively, you can manually get an `accessToken` by logging in to the ChatGPT webapp and then opening `https://chat.openai.com/api/auth/session`, which will return a JSON object containing your `accessToken` string.
Access tokens last for ~8 hours.
Access tokens last for days.
**Note**: using a reverse proxy will expose your access token to a third-party. There shouldn't be any adverse effects possible from this, but please consider the risks before using this method.

Wyświetl plik

@ -4,6 +4,7 @@ import { v4 as uuidv4 } from 'uuid'
import * as types from './types'
import { fetch as globalFetch } from './fetch'
import { fetchSSE } from './fetch-sse'
import { isValidUUIDv4 } from './utils'
export class ChatGPTUnofficialProxyAPI {
protected _accessToken: string
@ -97,6 +98,30 @@ export class ChatGPTUnofficialProxyAPI {
text: string,
opts: types.SendMessageBrowserOptions = {}
): Promise<types.ChatMessage> {
if (!!opts.conversationId !== !!opts.parentMessageId) {
throw new Error(
'ChatGPTUnofficialProxyAPI.sendMessage: conversationId and parentMessageId must both be set or both be undefined'
)
}
if (opts.conversationId && !isValidUUIDv4(opts.conversationId)) {
throw new Error(
'ChatGPTUnofficialProxyAPI.sendMessage: conversationId is not a valid v4 UUID'
)
}
if (opts.parentMessageId && !isValidUUIDv4(opts.parentMessageId)) {
throw new Error(
'ChatGPTUnofficialProxyAPI.sendMessage: parentMessageId is not a valid v4 UUID'
)
}
if (opts.messageId && !isValidUUIDv4(opts.messageId)) {
throw new Error(
'ChatGPTUnofficialProxyAPI.sendMessage: messageId is not a valid v4 UUID'
)
}
const {
conversationId,
parentMessageId = uuidv4(),

6
src/utils.ts 100644
Wyświetl plik

@ -0,0 +1,6 @@
const uuidv4Re =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
export function isValidUUIDv4(str: string): boolean {
return str && uuidv4Re.test(str)
}