Skip to content

fix: ignore hop-by-hop headers #822

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 1 commit into from
Feb 21, 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
18 changes: 18 additions & 0 deletions src/steps/set-custom-response-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/**
* Hop-by-hop headers (see https://www.freesoft.org/CIE/RFC/2068/143.htm) that should
* be ignored as custom header.
*/
const HOP_BY_HOP_HEADERS = [
'connection',
'keep-alive',
'public',
'proxy-authenticate',
'content-encoding',
'transfer-encoding',
'upgrade',
];

function cleanupHeaderValue(value) {
return value
.replace(/[^\t\u0020-\u007E\u0080-\u00FF]/g, '')
Expand Down Expand Up @@ -57,6 +72,9 @@ function getACAOriginValue(req, value) {
*/
export default function setCustomResponseHeaders(state, req, res) {
Object.entries(state.headers.getModifiers(state.info.path)).forEach(([name, value]) => {
if (HOP_BY_HOP_HEADERS.includes(name)) {
return;
}
// only use `link` header for extensionless pipeline
if (name !== 'link' || (state.type === 'html' && state.info.selector === '')) {
let val = cleanupHeaderValue(value);
Expand Down
4 changes: 4 additions & 0 deletions test/steps/set-custom-headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ const TEST_HEADERS = {
key: 'Link',
value: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
},
{
key: 'Content-Encoding',
value: 'gzip',
},
],
};

Expand Down