-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate header names and values (#69)
- Loading branch information
1 parent
e970f59
commit ad5ead7
Showing
6 changed files
with
202 additions
and
32 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
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,47 @@ | ||
/** | ||
* Validate the given header name. | ||
* @see https://fetch.spec.whatwg.org/#header-name | ||
*/ | ||
export function isValidHeaderName(value: unknown) { | ||
if (typeof value !== 'string') { | ||
return false | ||
} | ||
|
||
if (value.length === 0) { | ||
return false | ||
} | ||
|
||
for (let i = 0; i < value.length; i++) { | ||
const character = value.charCodeAt(i) | ||
|
||
if (character > 0x7f || !isToken(character)) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
function isToken(value: string | number): boolean { | ||
return ![ | ||
0x7f, | ||
0x20, | ||
'(', | ||
')', | ||
'<', | ||
'>', | ||
'@', | ||
',', | ||
';', | ||
':', | ||
'\\', | ||
'"', | ||
'/', | ||
'[', | ||
']', | ||
'?', | ||
'=', | ||
'{', | ||
'}', | ||
].includes(value) | ||
} |
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,29 @@ | ||
/** | ||
* Validate the given header value. | ||
* @see https://fetch.spec.whatwg.org/#header-value | ||
*/ | ||
export function isValidHeaderValue(value: unknown): boolean { | ||
if (typeof value !== 'string') { | ||
return false | ||
} | ||
|
||
if (value.trim() !== value) { | ||
return false | ||
} | ||
|
||
for (let i = 0; i < value.length; i++) { | ||
const character = value.charCodeAt(i) | ||
|
||
if ( | ||
// NUL. | ||
character === 0x00 || | ||
// HTTP newline bytes. | ||
character === 0x0a || | ||
character === 0x0d | ||
) { | ||
return false | ||
} | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
const HEADERS_INVALID_CHARACTERS = /[^a-z0-9\-#$%&'*+.^_`|~]/i | ||
|
||
export function normalizeHeaderName(name: string): string { | ||
if (typeof name !== 'string') { | ||
name = String(name) | ||
} | ||
|
||
if (HEADERS_INVALID_CHARACTERS.test(name) || name.trim() === '') { | ||
throw new TypeError('Invalid character in header field name') | ||
} | ||
|
||
return name.toLowerCase() | ||
return name.trim().toLowerCase() | ||
} |
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,7 +1,20 @@ | ||
export function normalizeHeaderValue(value: string): string { | ||
if (typeof value !== 'string') { | ||
value = String(value) | ||
} | ||
const charCodesToRemove = [ | ||
String.fromCharCode(0x0a), | ||
String.fromCharCode(0x0d), | ||
String.fromCharCode(0x09), | ||
String.fromCharCode(0x20), | ||
] | ||
|
||
const HEADER_VALUE_REMOVE_REGEXP = new RegExp( | ||
`(^[${charCodesToRemove.join('')}]|$[${charCodesToRemove.join('')}])`, | ||
'g' | ||
) | ||
|
||
return value | ||
/** | ||
* Normalize the given header value. | ||
* @see https://fetch.spec.whatwg.org/#concept-header-value-normalize | ||
*/ | ||
export function normalizeHeaderValue(value: string): string { | ||
const nextValue = value.replace(HEADER_VALUE_REMOVE_REGEXP, '') | ||
return nextValue | ||
} |