Skip to content

Commit f0c84d4

Browse files
Merge pull request from 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.
1 parent 755770e commit f0c84d4

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

packages/apollo-server-core/src/__tests__/ApolloServerBase.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,43 @@ describe('ApolloServerBase construction', () => {
103103
`"Apollo Server requires either an existing schema, modules or typeDefs"`,
104104
);
105105
});
106+
107+
it('throws when an API key is not a valid header value', () => {
108+
expect(() => {
109+
new ApolloServerBase({
110+
typeDefs,
111+
resolvers,
112+
apollo: {
113+
key: 'bar▒baz▒',
114+
},
115+
});
116+
}).toThrowErrorMatchingInlineSnapshot(
117+
`"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: ▒, ▒. Valid header values may only contain ASCII visible characters. If you think there is an issue with your key, please contact Apollo support."`,
118+
);
119+
});
120+
121+
it('trims whitespace from incoming API keys and logs a warning', () => {
122+
const logger = {
123+
debug: jest.fn(),
124+
info: jest.fn(),
125+
warn: jest.fn(),
126+
error: jest.fn(),
127+
};
128+
expect(() => {
129+
new ApolloServerBase({
130+
typeDefs,
131+
resolvers,
132+
apollo: {
133+
key: 'barbaz\n',
134+
},
135+
logger,
136+
});
137+
}).not.toThrow();
138+
expect(logger.warn).toHaveBeenCalledWith(
139+
'The provided API key has unexpected leading or trailing whitespace. ' +
140+
'Apollo Server will trim the key value before use.',
141+
);
142+
});
106143
});
107144

108145
describe('ApolloServerBase start', () => {

packages/apollo-server-core/src/determineApolloConfig.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,34 @@ export function determineApolloConfig(
3434

3535
// Determine key.
3636
if (input?.key) {
37-
apolloConfig.key = input.key;
37+
apolloConfig.key = input.key.trim();
3838
} else if (typeof engine === 'object' && engine.apiKey) {
39-
apolloConfig.key = engine.apiKey;
39+
apolloConfig.key = engine.apiKey.trim();
4040
} else if (APOLLO_KEY) {
4141
if (ENGINE_API_KEY) {
4242
logger.warn(
4343
'Using `APOLLO_KEY` since `ENGINE_API_KEY` (deprecated) is also set in the environment.',
4444
);
4545
}
46-
apolloConfig.key = APOLLO_KEY;
46+
apolloConfig.key = APOLLO_KEY.trim();
4747
} else if (ENGINE_API_KEY) {
4848
logger.warn(
4949
'[deprecated] The `ENGINE_API_KEY` environment variable has been renamed to `APOLLO_KEY`.',
5050
);
51-
apolloConfig.key = ENGINE_API_KEY;
51+
apolloConfig.key = ENGINE_API_KEY.trim();
52+
}
53+
54+
if ((input?.key ?? APOLLO_KEY ?? ENGINE_API_KEY) !== apolloConfig.key) {
55+
logger.warn(
56+
'The provided API key has unexpected leading or trailing whitespace. ' +
57+
'Apollo Server will trim the key value before use.',
58+
);
59+
}
60+
61+
// Assert API key is a valid header value, since it's going to be used as one
62+
// throughout.
63+
if (apolloConfig.key) {
64+
assertValidHeaderValue(apolloConfig.key);
5265
}
5366

5467
// Determine key hash.
@@ -155,3 +168,17 @@ export function determineApolloConfig(
155168

156169
return apolloConfig as ApolloConfig; // can remove cast in AS3
157170
}
171+
172+
function assertValidHeaderValue(value: string) {
173+
// Ref: [email protected] `Headers` validation
174+
// https://github.com/node-fetch/node-fetch/blob/9b9d45881e5ca68757077726b3c0ecf8fdca1f29/src/headers.js#L18
175+
const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g;
176+
if (invalidHeaderCharRegex.test(value)) {
177+
const invalidChars = value.match(invalidHeaderCharRegex)!;
178+
throw new Error(
179+
`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(
180+
', ',
181+
)}. Valid header values may only contain ASCII visible characters. If you think there is an issue with your key, please contact Apollo support.`,
182+
);
183+
}
184+
}

0 commit comments

Comments
 (0)