|
1 | 1 | import { test } from 'node:test'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | 3 | import { applyOptions, config } from '../src/config.js'; |
4 | | -import { resolveUpstreamHeaders, stagingTargetIp } from '../src/util/upstream.js'; |
| 4 | +import { resolveUpstreamHeaders, sanitizeOriginResponseHeaders, stagingTargetIp } from '../src/util/upstream.js'; |
5 | 5 |
|
6 | 6 | // Minimal stand-in for the request headers object (only `.get` is used here). |
7 | 7 | const headersWith = (present) => ({ get: (name) => (present.includes(name) ? '1' : null) }); |
@@ -99,6 +99,58 @@ test('resolveUpstreamHeaders matches ignoredHeaders case-insensitively', () => { |
99 | 99 | assert.equal(upstream['x-internal'], undefined); |
100 | 100 | }); |
101 | 101 |
|
| 102 | +test('sanitizeOriginResponseHeaders keeps genuine origin headers', () => { |
| 103 | + const clean = sanitizeOriginResponseHeaders({ |
| 104 | + 'content-type': 'text/html; charset=utf-8', |
| 105 | + 'content-encoding': 'gzip', |
| 106 | + 'content-length': '1234', |
| 107 | + 'cache-control': 'max-age=60', |
| 108 | + 'etag': '"abc"', |
| 109 | + 'last-modified': 'Wed, 02 Jul 2026 00:00:00 GMT', |
| 110 | + 'vary': 'Accept-Encoding', |
| 111 | + 'x-robots-tag': 'noindex', |
| 112 | + }); |
| 113 | + assert.equal(clean['content-type'], 'text/html; charset=utf-8'); |
| 114 | + assert.equal(clean['content-encoding'], 'gzip'); |
| 115 | + assert.equal(clean['content-length'], '1234'); |
| 116 | + assert.equal(clean['cache-control'], 'max-age=60'); |
| 117 | + assert.equal(clean['etag'], '"abc"'); |
| 118 | + assert.equal(clean['vary'], 'Accept-Encoding'); |
| 119 | + assert.equal(clean['x-robots-tag'], 'noindex'); |
| 120 | +}); |
| 121 | + |
| 122 | +test('sanitizeOriginResponseHeaders strips CDN/edge-injected headers (badxform cause)', () => { |
| 123 | + const clean = sanitizeOriginResponseHeaders({ |
| 124 | + 'content-type': 'text/html', |
| 125 | + 'akamai-grn': '0.1234abcd', |
| 126 | + 'x-akamai-staging': 'ESSL', |
| 127 | + 'x-akamai-transformed': '9 0 0', |
| 128 | + 'x-cache': 'TCP_MISS from a1-2-3-4', |
| 129 | + 'x-cache-key': '/L/1/2/3/foo', |
| 130 | + 'x-check-cacheable': 'NO', |
| 131 | + 'via': '1.1 akamai.net', |
| 132 | + 'server-timing': 'cdn-cache; desc=MISS', |
| 133 | + 'set-cookie': 'sid=abc; Path=/', |
| 134 | + 'connection': 'keep-alive', |
| 135 | + // empty duplicated custom origin headers seen in the wild — must not be forwarded |
| 136 | + 'x-origin-cc': '', |
| 137 | + 'x-origin-ttl': '', |
| 138 | + }); |
| 139 | + assert.deepEqual(Object.keys(clean), ['content-type']); |
| 140 | +}); |
| 141 | + |
| 142 | +test('sanitizeOriginResponseHeaders returns {} for null/undefined input', () => { |
| 143 | + assert.deepEqual(sanitizeOriginResponseHeaders(null), {}); |
| 144 | + assert.deepEqual(sanitizeOriginResponseHeaders(undefined), {}); |
| 145 | +}); |
| 146 | + |
| 147 | +test('sanitizeOriginResponseHeaders matches allowlist case-insensitively (normalizes key)', () => { |
| 148 | + const clean = sanitizeOriginResponseHeaders({ 'Content-Type': 'text/html', 'X-Akamai-Staging': 'ESSL' }); |
| 149 | + assert.equal(clean['content-type'], 'text/html'); |
| 150 | + assert.equal(clean['x-akamai-staging'], undefined); |
| 151 | + assert.equal(clean['X-Akamai-Staging'], undefined); |
| 152 | +}); |
| 153 | + |
102 | 154 | test('resolveUpstreamHeaders drops a spoofed token/debug header even when configured mixed-case', () => { |
103 | 155 | // Incoming keys are lowercased, so a mixed-case configured name must still match. |
104 | 156 | applyOptions({ securityToken: { header: 'X-Harper-Token', value: 'real' }, debugHeader: { key: 'X-Harper-Debug' } }); |
|
0 commit comments