Skip to content

Commit 93301f3

Browse files
committed
fix(console): safely inspect zen request fields
1 parent a1feb53 commit 93301f3

1 file changed

Lines changed: 82 additions & 7 deletions

File tree

  • packages/console/app/src/routes/zen/util

‎packages/console/app/src/routes/zen/util/handler.ts‎

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,76 @@ function resolve(text: string, params?: Record<string, string | number>) {
7070
})
7171
}
7272

73+
function inspectRequestBody(text: string) {
74+
const nextToken = (start: number) => {
75+
for (let i = start; i < text.length; i++) {
76+
if (!/\s/.test(text[i])) return i
77+
}
78+
}
79+
const endString = (start: number) => {
80+
for (let i = start + 1; i < text.length; i++) {
81+
if (text[i] === "\\") {
82+
i++
83+
continue
84+
}
85+
if (text[i] === '"') return i
86+
}
87+
}
88+
89+
const stack: string[] = []
90+
let expectKey = false
91+
let model: { value: string; start: number; end: number } | undefined
92+
let stream: boolean | undefined
93+
let streamOptions = false
94+
for (let i = 0; i < text.length; i++) {
95+
const char = text[i]
96+
if (char === '"') {
97+
const end = endString(i)
98+
if (end === undefined) return
99+
if (stack.length === 1 && expectKey) {
100+
const key = JSON.parse(text.slice(i, end + 1))
101+
const colon = nextToken(end + 1)
102+
if (colon === undefined || text[colon] !== ":") return
103+
const start = nextToken(colon + 1)
104+
if (start === undefined) return
105+
if (key === "model") {
106+
if (model || text[start] !== '"') return
107+
const valueEnd = endString(start)
108+
if (valueEnd === undefined) return
109+
model = { value: JSON.parse(text.slice(start, valueEnd + 1)), start, end: valueEnd + 1 }
110+
}
111+
if (key === "stream") {
112+
if (stream !== undefined) return
113+
if (text.startsWith("true", start)) stream = true
114+
else if (text.startsWith("false", start)) stream = false
115+
else return
116+
}
117+
if (key === "stream_options") {
118+
if (streamOptions) return
119+
streamOptions = true
120+
}
121+
expectKey = false
122+
}
123+
i = end
124+
continue
125+
}
126+
if (char === "{" || char === "[") {
127+
if (stack.length === 0 && char !== "{") return
128+
stack.push(char)
129+
if (stack.length === 1) expectKey = true
130+
continue
131+
}
132+
if (char === "}" || char === "]") {
133+
if (stack.pop() !== (char === "}" ? "{" : "[")) return
134+
continue
135+
}
136+
if (char === "," && stack.length === 1) expectKey = true
137+
if (stack.length === 0 && !/\s/.test(char)) return
138+
}
139+
if (stack.length || !model) return
140+
return { model, isStream: stream ?? false, streamOptions }
141+
}
142+
73143
export async function handler(
74144
input: APIEvent,
75145
opts: {
@@ -99,12 +169,14 @@ export async function handler(
99169
try {
100170
const url = input.request.url
101171
const body = await input.request.text()
172+
const inspected = opts.format === "google" ? undefined : inspectRequestBody(body)
173+
const parsed = opts.format === "google" || inspected ? undefined : JSON.parse(body)
102174
const model =
103175
opts.format === "google"
104176
? opts.parseModel(url, undefined)
105-
: body.match(/"model"\s*:\s*"([^"]+)"/)?.[1] ?? ""
177+
: inspected?.model.value ?? opts.parseModel(url, parsed)
106178
const isStream =
107-
opts.format === "google" ? opts.parseIsStream(url, undefined) : /"stream"\s*:\s*true/.test(body)
179+
opts.format === "google" ? opts.parseIsStream(url, undefined) : inspected?.isStream ?? opts.parseIsStream(url, parsed)
108180
const rawIp = input.request.headers.get("x-real-ip") ?? ""
109181
const ip = rawIp.includes(":") ? rawIp.split(":").slice(0, 4).join(":") : rawIp
110182
const rawZenApiKey = opts.parseApiKey(input.request.headers)
@@ -209,18 +281,21 @@ export async function handler(
209281
providerInfo.model.startsWith("global.anthropic.") ||
210282
providerInfo.model.startsWith("databricks-claude-"))
211283
if (providerInfo.format === opts.format && !providerInfo.payloadModifier && !specialAnthropic) {
212-
const patched = body.replace(
213-
/"model"\s*:\s*"[^"]+"/,
214-
`"model":${JSON.stringify(providerInfo.model)}`,
215-
)
284+
if (providerInfo.format === "google") return body
285+
if (!inspected) return
286+
const patched =
287+
body.slice(0, inspected.model.start) +
288+
JSON.stringify(providerInfo.model) +
289+
body.slice(inspected.model.end)
216290
if (providerInfo.format !== "oa-compat" || !isStream) return patched
291+
if (inspected.streamOptions) return
217292
return patched.replace(/}\s*$/, ',"stream_options":{"include_usage":true}}')
218293
}
219294
return undefined
220295
})()
221296
const reqBody = directBody ?? JSON.stringify(
222297
providerInfo.modifyBody({
223-
...createBodyConverter(opts.format, providerInfo.format)(JSON.parse(body)),
298+
...createBodyConverter(opts.format, providerInfo.format)(parsed ?? JSON.parse(body)),
224299
model: providerInfo.model,
225300
...(() => {
226301
const replacer = (obj: Record<string, any>): Record<string, any> =>

0 commit comments

Comments
 (0)