Stop a non-string filter code from crashing the nfproxy page

`genericapi` ran every response body through `JSON.parse`, falling back to
the raw text only when that threw. The nfproxy filter source is served as a
PlainTextResponse, so a filter file that happens to be valid JSON came back
as a number/array/object. Mantine's CodeHighlight then calls `code.trim()`
on it, which throws and takes the whole page down with it. `42`, `"abc"`,
`[1,2]` and `{"a":1}` are all both valid JSON and valid Python, so they
pass the upload validation and leave the service page unreachable.

Responses declaring text/plain are no longer parsed, and the render site
type-checks the code before handing it to CodeHighlight.
This commit is contained in:
Domingo Dirutigliano 2026-09-03 18:42:28 +02:00
parent 090d117115
commit ba2703d4bf
No known key found for this signature in database
2 changed files with 5 additions and 1 deletions

View File

@ -111,7 +111,11 @@ export async function genericapi(method:string,path:string,data:any = undefined,
const errorDefault = res.statusText
return res.json().then( res => reject(getErrorMessageFromServerResponse(res, errorDefault)) ).catch( _err => reject(errorDefault))
}
// text/plain bodies (the nfproxy filter source) stay strings: a source file that
// happens to be valid JSON must not be parsed into a number/array/object
const isPlainText = (res.headers.get("content-type") ?? "").startsWith("text/plain")
res.text().then(t => {
if (isPlainText) return resolve(t)
try{
resolve(JSON.parse(t))
}catch(e){

View File

@ -207,7 +207,7 @@ export default function ServiceDetailsNFProxy() {
</Group>
</Card>
{filterCode.data ? <>
{typeof filterCode.data === "string" && filterCode.data ? <>
<Group justify="center" gap="xs" mt="xl">
<ThemeIcon radius="md" size="md" variant="light" color="yellow"><FaPython size={16} /></ThemeIcon>
<Title order={3}>Filter code</Title>