This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(serve): validating incoming headers in serve mode
Previously they were not being checked fix #4
- Loading branch information
1 parent
ee43925
commit bcfc762
Showing
9 changed files
with
142 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { areHeadersValid, HeaderValidationResult } from './header-validator' | ||
|
||
jest.disableAutomock() | ||
|
||
describe('areHeadersValid', () => { | ||
it('returns true if the headers match the config', () => { | ||
const expected = { | ||
'x-hello': 'world', | ||
'what-is': 'love', | ||
baby: 'dont,hurt,me', | ||
} | ||
|
||
const result = areHeadersValid(expected, { | ||
'X-Hello': 'world', | ||
'what-is': 'love', | ||
baby: ['dont', 'hurt', 'me'], | ||
}) | ||
|
||
expect(result).toMatchObject<HeaderValidationResult>({ success: true }) | ||
}) | ||
|
||
it('returns false if headers are missing', () => { | ||
const expected = { | ||
hello: 'world', | ||
'what-is': 'love', | ||
baby: 'dont,hurt,me', | ||
} | ||
|
||
const result = areHeadersValid(expected, { | ||
Hello: 'world', | ||
'what-is': 'Love', | ||
baby: ['dont', 'hurt', 'me'], | ||
}) | ||
|
||
expect(result).toMatchObject<HeaderValidationResult>({ | ||
success: false, | ||
}) | ||
}) | ||
|
||
it('returns false if header values do not match case', () => { | ||
const expected = { | ||
hello: 'world', | ||
'what-is': 'love', | ||
baby: 'dont,hurt,me', | ||
} | ||
|
||
const result = areHeadersValid(expected, { | ||
hello: 'world', | ||
'what-is': 'love', | ||
baby: ['dont', 'Hurt', 'me'], | ||
}) | ||
|
||
expect(result).toMatchObject<HeaderValidationResult>({ success: false }) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { IncomingHttpHeaders } from 'http2' | ||
|
||
export type HeaderValidationResult = | ||
| { | ||
success: true | ||
} | ||
| { | ||
success: false | ||
} | ||
|
||
export const areHeadersValid = ( | ||
origExpectedHeaders: NcdcHeaders, | ||
origReceivedHeaders: IncomingHttpHeaders, | ||
): HeaderValidationResult => { | ||
const expectedHeaders: NcdcHeaders = {} | ||
for (const key in origExpectedHeaders) { | ||
expectedHeaders[key.toLowerCase()] = origExpectedHeaders[key] | ||
} | ||
|
||
const receivedHeaders: IncomingHttpHeaders = {} | ||
for (const key in origReceivedHeaders) { | ||
receivedHeaders[key.toLowerCase()] = origReceivedHeaders[key] | ||
} | ||
|
||
for (const key in expectedHeaders) { | ||
const expected = expectedHeaders[key] | ||
const actual = receivedHeaders[key] | ||
const badResult = { success: false } | ||
|
||
if (expected.includes(',')) { | ||
if (!Array.isArray(actual)) return badResult | ||
for (const item of expected.split(',')) { | ||
if (!actual.includes(item)) return badResult | ||
} | ||
break | ||
} | ||
|
||
if (Array.isArray(receivedHeaders)) { | ||
if (!actual?.includes(expected)) return badResult | ||
} else { | ||
if (actual !== expected) return badResult | ||
} | ||
} | ||
|
||
return { success: true } | ||
} |
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
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
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