Skip to content

feat(core): Add support for x-forwarded-host and x-forwarded-proto headers #16687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ export function httpRequestToRequestData(request: {
};
}): RequestEventData {
const headers = request.headers || {};
const host = typeof headers.host === 'string' ? headers.host : undefined;
const protocol = request.protocol || (request.socket?.encrypted ? 'https' : 'http');

// Check for x-forwarded-host first, then fall back to host header
const forwardedHost = typeof headers['x-forwarded-host'] === 'string' ? headers['x-forwarded-host'] : undefined;
const host = forwardedHost || (typeof headers.host === 'string' ? headers.host : undefined);

// Check for x-forwarded-proto first, then fall back to existing protocol detection
const forwardedProto = typeof headers['x-forwarded-proto'] === 'string' ? headers['x-forwarded-proto'] : undefined;
const protocol = forwardedProto || request.protocol || (request.socket?.encrypted ? 'https' : 'http');

const url = request.url || '';

const absoluteUrl = getAbsoluteUrl({
Expand Down
209 changes: 209 additions & 0 deletions packages/core/test/lib/utils/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,215 @@ describe('request utils', () => {
data: { xx: 'a', yy: 'z' },
});
});

describe('x-forwarded headers support', () => {
it('should prioritize x-forwarded-proto header over explicit protocol parameter', () => {
const actual = httpRequestToRequestData({
url: '/test',
headers: {
host: 'example.com',
'x-forwarded-proto': 'https',
},
protocol: 'http',
Comment on lines +208 to +210
Copy link
Member

Choose a reason for hiding this comment

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

l: Could you also add one test case for the reverse (protocol: https but forwarded: http) just to test that this isn't just accidentally passing because we always have https?

});

expect(actual).toEqual({
url: 'https://example.com/test',
headers: {
host: 'example.com',
'x-forwarded-proto': 'https',
},
});
});

it('should prioritize x-forwarded-proto header even when downgrading from https to http', () => {
const actual = httpRequestToRequestData({
url: '/test',
headers: {
host: 'example.com',
'x-forwarded-proto': 'http',
},
protocol: 'https',
});

expect(actual).toEqual({
url: 'http://example.com/test',
headers: {
host: 'example.com',
'x-forwarded-proto': 'http',
},
});
});

it('should prioritize x-forwarded-proto header over socket encryption detection', () => {
const actual = httpRequestToRequestData({
url: '/test',
headers: {
host: 'example.com',
'x-forwarded-proto': 'https',
},
socket: {
encrypted: false,
},
});

expect(actual).toEqual({
url: 'https://example.com/test',
headers: {
host: 'example.com',
'x-forwarded-proto': 'https',
},
});
});

it('should prioritize x-forwarded-host header over standard host header', () => {
const actual = httpRequestToRequestData({
url: '/test',
headers: {
host: 'localhost:3000',
'x-forwarded-host': 'example.com',
'x-forwarded-proto': 'https',
},
});

expect(actual).toEqual({
url: 'https://example.com/test',
headers: {
host: 'localhost:3000',
'x-forwarded-host': 'example.com',
'x-forwarded-proto': 'https',
},
});
});

it('should construct URL correctly when both x-forwarded-proto and x-forwarded-host are present', () => {
const actual = httpRequestToRequestData({
method: 'POST',
url: '/api/test?param=value',
headers: {
host: 'localhost:3000',
'x-forwarded-host': 'api.example.com',
'x-forwarded-proto': 'https',
'content-type': 'application/json',
},
protocol: 'http',
});

expect(actual).toEqual({
method: 'POST',
url: 'https://api.example.com/api/test?param=value',
query_string: 'param=value',
headers: {
host: 'localhost:3000',
'x-forwarded-host': 'api.example.com',
'x-forwarded-proto': 'https',
'content-type': 'application/json',
},
});
});

it('should fall back to standard headers when x-forwarded headers are not present', () => {
const actual = httpRequestToRequestData({
url: '/test',
headers: {
host: 'example.com',
},
protocol: 'https',
});

expect(actual).toEqual({
url: 'https://example.com/test',
headers: {
host: 'example.com',
},
});
});

it('should ignore x-forwarded headers when they contain non-string values', () => {
const actual = httpRequestToRequestData({
url: '/test',
headers: {
host: 'example.com',
'x-forwarded-host': ['forwarded.example.com'] as any,
'x-forwarded-proto': ['https'] as any,
},
protocol: 'http',
});

expect(actual).toEqual({
url: 'http://example.com/test',
headers: {
host: 'example.com',
},
});
});

it('should correctly transform localhost request to public URL using x-forwarded headers', () => {
const actual = httpRequestToRequestData({
method: 'GET',
url: '/',
headers: {
host: 'localhost:3000',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'example.com',
},
});

expect(actual).toEqual({
method: 'GET',
url: 'https://example.com/',
headers: {
host: 'localhost:3000',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'example.com',
},
});
});

it('should respect x-forwarded-proto even when it downgrades from encrypted socket', () => {
const actual = httpRequestToRequestData({
url: '/test',
headers: {
host: 'example.com',
'x-forwarded-proto': 'http',
},
socket: {
encrypted: true,
},
});

expect(actual).toEqual({
url: 'http://example.com/test',
headers: {
host: 'example.com',
'x-forwarded-proto': 'http',
},
});
});

it('should preserve query parameters when constructing URL with x-forwarded headers', () => {
const actual = httpRequestToRequestData({
method: 'GET',
url: '/search?q=test&category=api',
headers: {
host: 'localhost:8080',
'x-forwarded-host': 'search.example.com',
'x-forwarded-proto': 'https',
},
});

expect(actual).toEqual({
method: 'GET',
url: 'https://search.example.com/search?q=test&category=api',
query_string: 'q=test&category=api',
headers: {
host: 'localhost:8080',
'x-forwarded-host': 'search.example.com',
'x-forwarded-proto': 'https',
},
});
});
});
});

describe('extractQueryParamsFromUrl', () => {
Expand Down
Loading