Skip to content

Proxy handler breaks compressed analytics payloads #618

@zizzfizzix

Description

@zizzfizzix

🐛 The bug

In src/runtime/server/proxy-handler.ts, proxied request bodies are always read and may be transformed, even for compressed binary payloads. This can corrupt or stall analytics requests (e.g. PostHog by default uses gzip-js with compression=gzip-js), leading to repeated proxy timeouts (504 Upstream timeout).

Relevant code (current main)

From src/runtime/server/proxy-handler.ts:

  • Body is always read for write methods:
    • if (method === 'POST' || method === 'PUT' || method === 'PATCH') (~208)
    • rawBody = await readBody(event) (~209)
  • Privacy branch transforms based on parsed/string body:
    • if (anyPrivacy && rawBody) (~211)
  • Forwarded request body is rebuilt:
    • body: body ? (typeof body === 'string' ? body : JSON.stringify(body)) : undefined (~292)
  • Timeout is hardcoded:
    • const timeoutId = setTimeout(() => controller.abort(), 15000) (~285)
  • On fetch error:
    • returns 204 only for /collect, /tr, /events (~303)
    • otherwise throws message: 'Failed to reach upstream' (~312)

🛠️ To reproduce

https://stackblitz.com/edit/nuxt-starter-bcwumula?file=nuxt.config.ts

🌈 Expected behavior

Proxy should preserve raw request bytes for endpoints/content-types that are not explicitly safe to parse and rewrite, and only apply body privacy transforms where parsing is guaranteed valid.

ℹ️ Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions