Skip to content

Commit d38b43b

Browse files
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.
1 parent 590ca13 commit d38b43b

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export class ApolloServerBase<
214214
this.logger = loglevelLogger;
215215
}
216216

217-
this.apolloConfig = determineApolloConfig(apollo);
217+
this.apolloConfig = determineApolloConfig(apollo, this.logger);
218218

219219
if (gateway && (modules || schema || typeDefs || resolvers)) {
220220
throw new Error(

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,43 @@ describe('ApolloServerBase construction', () => {
8484
`"Apollo Server requires either an existing schema, modules or typeDefs"`,
8585
);
8686
});
87+
88+
it('throws when an API key is not a valid header value', () => {
89+
expect(() => {
90+
new ApolloServerBase({
91+
typeDefs,
92+
resolvers,
93+
apollo: {
94+
key: 'bar▒baz▒',
95+
},
96+
});
97+
}).toThrowErrorMatchingInlineSnapshot(
98+
`"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."`,
99+
);
100+
});
101+
102+
it('trims whitespace from incoming API keys and logs a warning', () => {
103+
const logger = {
104+
debug: jest.fn(),
105+
info: jest.fn(),
106+
warn: jest.fn(),
107+
error: jest.fn(),
108+
};
109+
expect(() => {
110+
new ApolloServerBase({
111+
typeDefs,
112+
resolvers,
113+
apollo: {
114+
key: 'barbaz\n',
115+
},
116+
logger,
117+
});
118+
}).not.toThrow();
119+
expect(logger.warn).toHaveBeenCalledWith(
120+
'The provided API key has unexpected leading or trailing whitespace. ' +
121+
'Apollo Server will trim the key value before use.',
122+
);
123+
});
87124
});
88125

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

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import type { ApolloConfig, ApolloConfigInput } from 'apollo-server-types';
1+
import type { ApolloConfig, ApolloConfigInput, Logger } from 'apollo-server-types';
22
import createSHA from './utils/createSHA';
33

44
// This function combines the `apollo` constructor argument and some environment
55
// variables to come up with a full ApolloConfig.
66
export function determineApolloConfig(
77
input: ApolloConfigInput | undefined,
8+
logger: Logger,
89
): ApolloConfig {
910
const apolloConfig: ApolloConfig = {};
1011

@@ -17,9 +18,21 @@ export function determineApolloConfig(
1718

1819
// Determine key.
1920
if (input?.key) {
20-
apolloConfig.key = input.key;
21+
apolloConfig.key = input.key.trim();
2122
} else if (APOLLO_KEY) {
22-
apolloConfig.key = APOLLO_KEY;
23+
apolloConfig.key = APOLLO_KEY.trim();
24+
}
25+
if ((input?.key ?? APOLLO_KEY) !== apolloConfig.key) {
26+
logger.warn(
27+
'The provided API key has unexpected leading or trailing whitespace. ' +
28+
'Apollo Server will trim the key value before use.',
29+
);
30+
}
31+
32+
// Assert API key is a valid header value, since it's going to be used as one
33+
// throughout.
34+
if (apolloConfig.key) {
35+
assertValidHeaderValue(apolloConfig.key);
2336
}
2437

2538
// Determine key hash.
@@ -65,3 +78,17 @@ export function determineApolloConfig(
6578

6679
return apolloConfig;
6780
}
81+
82+
function assertValidHeaderValue(value: string) {
83+
// Ref: node-fetch@2.x `Headers` validation
84+
// https://github.com/node-fetch/node-fetch/blob/9b9d45881e5ca68757077726b3c0ecf8fdca1f29/src/headers.js#L18
85+
const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g;
86+
if (invalidHeaderCharRegex.test(value)) {
87+
const invalidChars = value.match(invalidHeaderCharRegex)!;
88+
throw new Error(
89+
`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(
90+
', ',
91+
)}. Valid header values may only contain ASCII visible characters. If you think there is an issue with your key, please contact Apollo support.`,
92+
);
93+
}
94+
}

0 commit comments

Comments
 (0)