fix: proxy token failing because of Host header#202
Conversation
📝 WalkthroughWalkthroughThis PR introduces a new PROXY_HOST environment variable for the API. The variable is defined in the Fastify environment schema, supplied via docker-compose, and utilized in the proxy tokens route to set the Host header in rewritten requests. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
apps/api/src/app/routes/proxies/tokens/index.ts (1)
14-19:⚠️ Potential issue | 🟠 MajorAdd validation guard for
PROXY_HOSTto matchPROXY_UPSTREAMpattern.Currently
PROXY_UPSTREAMis guarded with an early return, butPROXY_HOSTis not. While@fastify/http-proxywith the default undici backend silently omitsundefinedheader values, inconsistent validation creates maintenance risk. If the proxy target requires theHostheader or if the deployment uses the Nodehttp.requestbackend (not undici), an undefinedPROXY_HOSTwill either fail silently or throwERR_HTTP_INVALID_HEADER_VALUEat runtime. For consistency and defense-in-depth, add an early return guard forPROXY_HOST(and considerPROXY_ORIGINas well if it's expected to be mandatory).Suggested fix
const proxy: FastifyPluginAsync = async (fastify, opts): Promise<void> => { const upstream = fastify.config.PROXY_UPSTREAM; + const proxyHost = fastify.config.PROXY_HOST; if (!upstream) { fastify.log.warn('PROXY_UPSTREAM is not set. Skipping proxy.'); return; } + if (!proxyHost) { + fastify.log.warn('PROXY_HOST is not set. Skipping proxy.'); + return; + } fastify.register(httpProxy, { upstream, replyOptions: { rewriteRequestHeaders: (originalRequest: any, headers: any) => { return { ...headers, Origin: fastify.config.PROXY_ORIGIN, - Host: fastify.config.PROXY_HOST, + Host: proxyHost, }; }, }, }); };🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/api/src/app/routes/proxies/tokens/index.ts` around lines 14 - 19, The rewriteRequestHeaders function currently sets Origin and Host without validating PROXY_HOST (and optionally PROXY_ORIGIN); add the same early-return guard used for PROXY_UPSTREAM so the handler exits if PROXY_HOST is not defined (and treat PROXY_ORIGIN as required if intended). Locate rewriteRequestHeaders in the tokens route and before returning headers, check fastify.config.PROXY_HOST (and fastify.config.PROXY_ORIGIN if mandatory) and return early (no proxy) when missing to avoid injecting undefined headers and potential ERR_HTTP_INVALID_HEADER_VALUE.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@apps/api/src/app/routes/proxies/tokens/index.ts`:
- Around line 14-19: The rewriteRequestHeaders function currently sets Origin
and Host without validating PROXY_HOST (and optionally PROXY_ORIGIN); add the
same early-return guard used for PROXY_UPSTREAM so the handler exits if
PROXY_HOST is not defined (and treat PROXY_ORIGIN as required if intended).
Locate rewriteRequestHeaders in the tokens route and before returning headers,
check fastify.config.PROXY_HOST (and fastify.config.PROXY_ORIGIN if mandatory)
and return early (no proxy) when missing to avoid injecting undefined headers
and potential ERR_HTTP_INVALID_HEADER_VALUE.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
apps/api/src/app/plugins/env.tsapps/api/src/app/routes/proxies/tokens/index.tsdocker-compose.yml
Fixes a problem causing our proxy to fail affecting token search (in cowswap) and token price graph (in cow-fi):
Context: https://nomevlabs.slack.com/archives/C036PMLUPQF/p1771413120954169
Summary by CodeRabbit
Improvements
Chores