Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit d90a285

Browse files
authored
feat: improve xfh parsing (adobe#594)
fixes adobe#592
1 parent ee0e9f7 commit d90a285

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

.github/workflows/main.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
with:
1717
node-version: '20.x'
1818
- run: npm ci
19+
- run: npm run lint
1920
- run: npm test
2021
- uses: codecov/codecov-action@v4
2122
with:
@@ -25,12 +26,13 @@ jobs:
2526
env:
2627
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2728
NPM_TOKEN: ${{ secrets.ADOBE_BOT_NPM_TOKEN }}
29+
CORALOGIX_TAGGER_API_KEY: ${{ secrets.CORALOGIX_TAGGER_API_KEY }}
2830

2931
release:
3032
name: Release
3133
runs-on: ubuntu-latest
3234
needs: test
33-
if: github.ref == 'refs/heads/main'
35+
if: github.ref == 'refs/heads/5.x' || github.ref == 'refs/heads/main'
3436
steps:
3537
- uses: actions/checkout@v4
3638
- name: Use Node.js 20.x

src/steps/utils.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ const HELIX_URL_REGEXP = /^https:\/\/(?!admin\.|www\.)[^.]+\.(aem|hlx3?)\.(live|
2424
export function getOriginalHost(headers) {
2525
const xfh = headers.get('x-forwarded-host');
2626
if (xfh) {
27-
return xfh.split(',')[0].trim();
27+
const segs = xfh.split(',');
28+
for (const seg of segs) {
29+
const host = seg.trim();
30+
if (host) {
31+
return host;
32+
}
33+
}
2834
}
2935
return headers.get('host');
3036
}

test/steps/utils.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ describe('Get Original Host', () => {
9393
]);
9494
assert.strictEqual(getOriginalHost(headers), 'spark.adobe.com');
9595
});
96+
97+
it('get correct host for multiple plain xfwd header if first segment is empty', () => {
98+
const headers = new Map([
99+
['host', 'blog.adobe.com'],
100+
['x-forwarded-host', ' , spark.adobe.com, cdn1.hlx.page'],
101+
]);
102+
assert.strictEqual(getOriginalHost(headers), 'spark.adobe.com');
103+
});
104+
105+
it('get correct host for multiple plain xfwd header if all segments are empty', () => {
106+
const headers = new Map([
107+
['host', 'blog.adobe.com'],
108+
['x-forwarded-host', ' , ,'],
109+
]);
110+
assert.strictEqual(getOriginalHost(headers), 'blog.adobe.com');
111+
});
96112
});
97113

98114
describe('Rewrite URLs test', () => {

0 commit comments

Comments
 (0)