-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove content headers for unsupported HTTP methods (#204)
- Loading branch information
1 parent
ccdd079
commit cdcac35
Showing
3 changed files
with
100 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,116 @@ | ||
import type { FastifyRequest, HTTPMethods } from 'fastify' | ||
|
||
import { config, initConfig } from '../config.js' | ||
|
||
import { rewriteRequestHeaders } from './headers.js' | ||
|
||
describe('HTTP server', () => { | ||
const ip = '192.168.1.1' | ||
const host = 'example.com' | ||
|
||
function getMockRequest(method?: HTTPMethods): FastifyRequest { | ||
return { | ||
ip, | ||
...(method && { method }), | ||
} as FastifyRequest | ||
} | ||
|
||
beforeEach(() => { | ||
initConfig() | ||
}) | ||
|
||
it('Should keep existing headers', () => { | ||
const ip = '192.168.1.1' | ||
const originalHeaders = { | ||
referer: 'https://www.test.com', | ||
} | ||
|
||
const headers = rewriteRequestHeaders( | ||
{ | ||
referer: 'https://www.test.com', | ||
}, | ||
ip, | ||
'example.com' | ||
) | ||
const headers = rewriteRequestHeaders({ | ||
headers: originalHeaders, | ||
request: getMockRequest(), | ||
host, | ||
}) | ||
|
||
expect(headers).toEqual({ | ||
referer: 'https://www.test.com', | ||
...originalHeaders, | ||
'x-forwarded-for': ip, | ||
'x-forwarded-by': config.appName, | ||
'x-forwarded-host': 'example.com', | ||
host: 'example.com', | ||
host, | ||
}) | ||
}) | ||
|
||
it('Should delete forbidden headers', () => { | ||
const ip = '192.168.1.1' | ||
it('Should delete forbidden HTTP2 headers', () => { | ||
const originalHeaders = { | ||
referer: 'https://www.test.com', | ||
} | ||
|
||
const headers = rewriteRequestHeaders( | ||
{ | ||
referer: 'https://www.test.com', | ||
const headers = rewriteRequestHeaders({ | ||
headers: { | ||
...originalHeaders, | ||
connection: 'keep-alive', | ||
upgrade: 'websocket', | ||
}, | ||
ip, | ||
'example.com' | ||
) | ||
request: getMockRequest(), | ||
host, | ||
}) | ||
|
||
expect(headers).toEqual({ | ||
referer: 'https://www.test.com', | ||
...originalHeaders, | ||
'x-forwarded-for': ip, | ||
'x-forwarded-by': config.appName, | ||
'x-forwarded-host': 'example.com', | ||
host: 'example.com', | ||
host, | ||
}) | ||
}) | ||
|
||
it('Should respect x-forwarded-for', () => { | ||
const ip = '192.168.1.1' | ||
const originalHeaders = { | ||
referer: 'https://www.test.com', | ||
} | ||
|
||
const headers = rewriteRequestHeaders( | ||
{ | ||
referer: 'https://www.test.com', | ||
const headers = rewriteRequestHeaders({ | ||
headers: { | ||
...originalHeaders, | ||
'x-forwarded-for': '127.0.0.1', | ||
}, | ||
ip, | ||
'example.com' | ||
) | ||
request: getMockRequest(), | ||
host, | ||
}) | ||
|
||
expect(headers).toEqual({ | ||
referer: 'https://www.test.com', | ||
...originalHeaders, | ||
'x-forwarded-for': '127.0.0.1', | ||
'x-forwarded-by': config.appName, | ||
'x-forwarded-host': 'example.com', | ||
host: 'example.com', | ||
host, | ||
}) | ||
}) | ||
|
||
describe('Should remove content headers if method is not PUT, PATCH or POST', () => { | ||
for (const method of ['GET', 'HEAD', 'OPTIONS', 'DELETE']) { | ||
it(`Should remove content headers if method is ${method}`, () => { | ||
const originalHeaders = { | ||
referer: 'https://www.test.com', | ||
'x-forwarded-for': '127.0.0.1', | ||
'content-type': 'application/json', | ||
'content-length': '123', | ||
'transfer-encoding': 'chunked', | ||
} | ||
|
||
const headers = rewriteRequestHeaders({ | ||
headers: originalHeaders, | ||
request: { ip, method } as FastifyRequest, | ||
host, | ||
}) | ||
|
||
expect(headers).toEqual({ | ||
referer: originalHeaders.referer, | ||
'x-forwarded-for': '127.0.0.1', | ||
'x-forwarded-by': config.appName, | ||
'x-forwarded-host': 'example.com', | ||
host, | ||
}) | ||
}) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters