-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-j5g3-5c8r-7qfx
Ref: GHSA-j5g3-5c8r-7qfx If a provided API key had characters that were invalid as header values, usage reporting and schema reporting requests would fail and log the API key. This change implements two fixes to improve this: * Trim the API key of any whitespace and log a warning * Throw an error on startup if the key contains invalid characters after being `.trim()`med.
- Loading branch information
1 parent
590ca13
commit d38b43b
Showing
3 changed files
with
68 additions
and
4 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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import type { ApolloConfig, ApolloConfigInput } from 'apollo-server-types'; | ||
import type { ApolloConfig, ApolloConfigInput, Logger } from 'apollo-server-types'; | ||
import createSHA from './utils/createSHA'; | ||
|
||
// This function combines the `apollo` constructor argument and some environment | ||
// variables to come up with a full ApolloConfig. | ||
export function determineApolloConfig( | ||
input: ApolloConfigInput | undefined, | ||
logger: Logger, | ||
): ApolloConfig { | ||
const apolloConfig: ApolloConfig = {}; | ||
|
||
|
@@ -17,9 +18,21 @@ export function determineApolloConfig( | |
|
||
// Determine key. | ||
if (input?.key) { | ||
apolloConfig.key = input.key; | ||
apolloConfig.key = input.key.trim(); | ||
} else if (APOLLO_KEY) { | ||
apolloConfig.key = APOLLO_KEY; | ||
apolloConfig.key = APOLLO_KEY.trim(); | ||
} | ||
if ((input?.key ?? APOLLO_KEY) !== apolloConfig.key) { | ||
logger.warn( | ||
'The provided API key has unexpected leading or trailing whitespace. ' + | ||
'Apollo Server will trim the key value before use.', | ||
); | ||
} | ||
|
||
// Assert API key is a valid header value, since it's going to be used as one | ||
// throughout. | ||
if (apolloConfig.key) { | ||
assertValidHeaderValue(apolloConfig.key); | ||
} | ||
|
||
// Determine key hash. | ||
|
@@ -65,3 +78,17 @@ export function determineApolloConfig( | |
|
||
return apolloConfig; | ||
} | ||
|
||
function assertValidHeaderValue(value: string) { | ||
// Ref: [email protected] `Headers` validation | ||
// https://github.com/node-fetch/node-fetch/blob/9b9d45881e5ca68757077726b3c0ecf8fdca1f29/src/headers.js#L18 | ||
const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g; | ||
if (invalidHeaderCharRegex.test(value)) { | ||
const invalidChars = value.match(invalidHeaderCharRegex)!; | ||
throw new Error( | ||
`The API key provided to Apollo Server contains characters which are invalid as HTTP header values. The following characters found in the key are invalid: ${invalidChars.join( | ||
', ', | ||
)}. Valid header values may only contain ASCII visible characters. If you think there is an issue with your key, please contact Apollo support.`, | ||
); | ||
} | ||
} |