Skip to content

Use configurable public host for self-hosted response URLs - #3699

Merged
mogery merged 1 commit into
firecrawl:mainfrom
saivedant169:fix/self-hosted-domain-urls
Jun 23, 2026
Merged

mogery merged 1 commit into
firecrawl:mainfrom
saivedant169:fix/self-hosted-domain-urls

Conversation

@saivedant169

@saivedant169 saivedant169 commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Self-hosted instances behind a reverse proxy build absolute URLs from req.get("host"), which returns the internal bind address. Crawl/batch job URLs and pagination next links come back pointing at http://localhost:3002 and are unreachable by the client.

Fixes #2964

Fix

  • Add getRequestHostname(req) in apps/api/src/lib/host-utils.ts.
  • Use it at every site that builds a response URL: v1/crawl, v1/batch-scrape, v1/crawl-status, v2/crawl, v2/batch-scrape, v2/crawl-status, and v2/monitor checks.
  • Resolution order: SELF_HOSTED_DOMAIN env, then the first X-Forwarded-Host value, then req.get("host"). Cloud behavior is unchanged when neither is set.
  • SELF_HOSTED_DOMAIN is normalized to a bare host[:port] even if an operator includes a scheme or path, and an empty or whitespace value is ignored, so the helper never produces malformed URLs like http://https://example.com.
  • The helper returns a host only and callers keep prefixing req.protocol, so pagination next links stay valid.

Scope is limited to the URL-emitting controllers plus the helper and its test. A prior attempt (#3015) mixed this fix with unrelated PDF, fire-engine, and SDK changes and was abandoned; this PR keeps the change focused and addresses the empty-value and malformed-URL edge cases that review flagged there.

Test plan

New unit test apps/api/src/lib/__tests__/host-utils.test.ts covering the resolution order and normalization:

PASS src/lib/__tests__/host-utils.test.ts
  getRequestHostname
    ✓ falls back to the request host when nothing is configured
    ✓ prefers SELF_HOSTED_DOMAIN over the request host
    ✓ keeps an explicit port in SELF_HOSTED_DOMAIN
    ✓ strips a scheme and path from SELF_HOSTED_DOMAIN
    ✓ ignores an empty or whitespace SELF_HOSTED_DOMAIN
    ✓ uses the first X-Forwarded-Host when no domain is configured
    ✓ prefers SELF_HOSTED_DOMAIN over X-Forwarded-Host
Tests:       7 passed, 7 total

Also ran tsc --noEmit: no new type errors in the changed files. Documented SELF_HOSTED_DOMAIN in apps/api/.env.example.


Summary by cubic

Fixes broken absolute URLs behind reverse proxies by using Express’s req.host. Crawl/batch job URLs, parse-upload links, and pagination now point to the external domain (including port) instead of the internal bind address.

  • Bug Fixes
    • Replaced req.get("host") with req.host in v1/v2 crawl, batch-scrape, crawl-status, monitor checks, and v2 parse-upload; works with trust proxy and needs no extra config.
    • Honors X-Forwarded-Host and preserves ports; pagination next links remain valid.

Written for commit 5d79fea. Summary will update on new commits.

Review in cubic

@saivedant169
saivedant169 requested a review from mogery as a code owner June 3, 2026 00:53

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 10 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/lib/host-utils.ts Resolved
@saivedant169

saivedant169 commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor Author

fixed in c9a0c62. normalizeHost now returns undefined when the value cannot be parsed into a host, and getRequestHostname falls through to X-Forwarded-Host then req host instead of leaking the raw value. Added a test covering an unparseable SELF_HOSTED_DOMAIN (8 passing now).

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 2 files (changes from recent commits).

Re-trigger cubic

@saivedant169

Copy link
Copy Markdown
Contributor Author

Open since early June. The cubic review is addressed and it's mergeable, the only red check is the Vercel preview deploy that needs maintainer auth on fork PRs. @mogery mind giving it a look when you have time?

@iwitw182-glitch

Copy link
Copy Markdown

Suer

@mogery

mogery commented Jun 23, 2026 •

Copy link
Copy Markdown
Member

Not sure why this makes sense. X-Forwarded-Host is already obeyed automatically if the EXPRESS_TRUST_PROXY env is correctly configured:

if (config.EXPRESS_TRUST_PROXY) {
app.set("trust proxy", config.EXPRESS_TRUST_PROXY);
}

@saivedant169

Copy link
Copy Markdown
Contributor Author

Agreed that trust proxy is the right signal to lean on. One wrinkle: this repo is on Express 4.22, where the trust-proxy-aware accessor is req.hostname, and that strips the port. The response URLs need host:port (self-hosted often runs on something like :3002), so req.hostname would drop it, while req.get("host") keeps the port but ignores X-Forwarded-Host, which is the bug here. The 5.x req.host you linked does keep the port, but we're not on 5 yet.

So leaning purely on the framework on 4.x means either losing the port or still ignoring X-Forwarded-Host. I can rework this to read X-Forwarded-Host directly when trust proxy is set (which keeps the port) and leave SELF_HOSTED_DOMAIN only as an explicit override for setups with no forwarding proxy. Or if you'd rather just use req.hostname and accept dropping non-default ports, I can do that. Which way do you prefer?

@mogery

mogery commented Jun 23, 2026

Copy link
Copy Markdown
Member

Agreed that trust proxy is the right signal to lean on. One wrinkle: this repo is on Express 4.22, where the trust-proxy-aware accessor is req.hostname, and that strips the port. The response URLs need host:port (self-hosted often runs on something like :3002), so req.hostname would drop it, while req.get("host") keeps the port but ignores X-Forwarded-Host, which is the bug here. The 5.x req.host you linked does keep the port, but we're not on 5 yet.

So leaning purely on the framework on 4.x means either losing the port or still ignoring X-Forwarded-Host. I can rework this to read X-Forwarded-Host directly when trust proxy is set (which keeps the port) and leave SELF_HOSTED_DOMAIN only as an explicit override for setups with no forwarding proxy. Or if you'd rather just use req.hostname and accept dropping non-default ports, I can do that. Which way do you prefer?

Gah, good catch, that's annoying. I'll port to Express 5.

@mogery

mogery commented Jun 23, 2026

Copy link
Copy Markdown
Member

Agreed that trust proxy is the right signal to lean on. One wrinkle: this repo is on Express 4.22, where the trust-proxy-aware accessor is req.hostname, and that strips the port. The response URLs need host:port (self-hosted often runs on something like :3002), so req.hostname would drop it, while req.get("host") keeps the port but ignores X-Forwarded-Host, which is the bug here. The 5.x req.host you linked does keep the port, but we're not on 5 yet.
So leaning purely on the framework on 4.x means either losing the port or still ignoring X-Forwarded-Host. I can rework this to read X-Forwarded-Host directly when trust proxy is set (which keeps the port) and leave SELF_HOSTED_DOMAIN only as an explicit override for setups with no forwarding proxy. Or if you'd rather just use req.hostname and accept dropping non-default ports, I can do that. Which way do you prefer?

Gah, good catch, that's annoying. I'll port to Express 5.

Ok, we are now on 5! #3860

@saivedant169 Could you please re-test whether it works now on latest, and if not, make the necessary updates?

req.get("host") returns the raw Host header and ignores X-Forwarded-Host, so
behind a reverse proxy the absolute URLs in API responses (crawl/batch job
URLs, pagination links) pointed at the internal bind address instead of the
public host. On Express 5 req.host honors X-Forwarded-Host when trust proxy is
configured and keeps the port, so swapping req.get("host") for req.host makes
the response URLs use the public host without any extra config.
@saivedant169
saivedant169 force-pushed the fix/self-hosted-domain-urls branch from c9a0c62 to 5d79fea Compare June 23, 2026 15:29
@saivedant169

saivedant169 commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor Author

@mogery reworked on latest main. Dropped the SELF_HOSTED_DOMAIN env var and the helper, it just uses req.host now across the crawl/batch/monitor/parse URL builders. On 5.x req.host honors X-Forwarded-Host with trust proxy and keeps the port, so the public host comes through with no extra config. I also caught one more spot still on req.get("host") (parse-upload) and switched that too. Typechecks clean against @types/express 5.

@superagent-security

Copy link
Copy Markdown

Superagent didn't find any vulnerabilities or security issues in this PR.

@mogery
mogery merged commit 2dd97c6 into firecrawl:main Jun 23, 2026
10 checks passed
@mogery

mogery commented Jun 23, 2026

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Self-Host] custom hostname

3 participants