Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
fix(serve): fix a small bug in request header validation
Browse files Browse the repository at this point in the history
In the case that an incoming request header was an array, it always would have been seen as invalid
due to a typo
  • Loading branch information
tamj0rd2 committed May 16, 2020
1 parent c8a4671 commit 92dfa3f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
3 changes: 1 addition & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"plugins": ["@typescript-eslint", "jest", "prettier"],
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
"prettier/@typescript-eslint"
],
"rules": {
"no-unused-expressions": "off",
Expand Down
17 changes: 0 additions & 17 deletions codecov.yml

This file was deleted.

20 changes: 17 additions & 3 deletions src/commands/serve/server/header-validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('areHeadersValid', () => {
const result = areHeadersValid(expected, {
'X-Hello': 'world',
'what-is': 'love',
baby: ['dont', 'hurt', 'me'],
baby: ['dont', 'me', 'hurt'],
})

expect(result).toMatchObject<HeaderValidationResult>({ success: true })
Expand All @@ -28,8 +28,22 @@ describe('areHeadersValid', () => {

const result = areHeadersValid(expected, {
Hello: 'world',
'what-is': 'Love',
baby: ['dont', 'hurt', 'me'],
'what-is': 'love',
baby: ['dont', 'hurt'],
})

expect(result).toMatchObject<HeaderValidationResult>({
success: false,
})
})

it('returns false if there are multiple received header values that do not match', () => {
const expected = {
hello: 'world',
}

const result = areHeadersValid(expected, {
hello: ['werld', 'peace'],
})

expect(result).toMatchObject<HeaderValidationResult>({
Expand Down
12 changes: 6 additions & 6 deletions src/commands/serve/server/header-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ export const areHeadersValid = (

for (const key in expectedHeaders) {
const expected = expectedHeaders[key]
const actual = receivedHeaders[key]
const received = receivedHeaders[key]
const badResult = { success: false }

if (expected.includes(',')) {
if (!Array.isArray(actual)) return badResult
if (!Array.isArray(received)) return badResult
for (const item of expected.split(',')) {
if (!actual.includes(item)) return badResult
if (!received.includes(item)) return badResult
}
break
}

if (Array.isArray(receivedHeaders)) {
if (!actual?.includes(expected)) return badResult
if (Array.isArray(received)) {
if (!received?.includes(expected)) return badResult
} else {
if (actual !== expected) return badResult
if (received !== expected) return badResult
}
}

Expand Down

0 comments on commit 92dfa3f

Please sign in to comment.