Skip to content

Commit 5deb43b

Browse files
dac09Josh-Walker-GM
authored andcommitted
fix(auth): Handle when authorization header is lowercased (#10442)
In one of the PRs I did for last release, I switched getting the header using the new `getEventHeaders` function. This function will check for two cases: ``` getEventHeaders('Authorization') => a) header['authorization'] b) header['Authorization'] ``` **BUT** if you passed it a lowercase header in the first place: ``` getEventHeaders('authorization') => a) header['authorization'] b) header['authorization'] ``` I actually didn't change the logic it's the same as before, but in`parseAuthorizationHeader`, we used to call it with the capital case. I know the _full_ solution is to grab the headers, and convert them all to lower-case, but I'm intentionally avoiding this because I don't want to slow down handling of every request by looping over all the headers. --- This PR makes a minor change, and adds some extra tests. 🤞 we'll move everything to Fetch API soon and won't have to deal with this sillyness!
1 parent 357efac commit 5deb43b

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

.changesets/10442.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- fix(auth): Handle when authorization header is lowercased (#10442) by @dac09
2+
Handles when 'authorization' header is lowercased, and adds some extra tests.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { APIGatewayProxyEvent } from 'aws-lambda'
2+
import { test, expect, describe } from 'vitest'
3+
4+
import { parseAuthorizationHeader } from '../index'
5+
6+
describe('parseAuthorizationHeader', () => {
7+
test('throws error if Authorization header is not valid', () => {
8+
const invalidHeaders = [
9+
undefined,
10+
null,
11+
'',
12+
'Bearer',
13+
'Bearer ',
14+
'Bearer token with spaces',
15+
'Token',
16+
'Token ',
17+
'Token token with spaces',
18+
]
19+
20+
invalidHeaders.forEach((header) => {
21+
expect(() =>
22+
// @ts-expect-error That's what we're testing
23+
parseAuthorizationHeader({ headers: { Authorization: header } }),
24+
).toThrowError('The `Authorization` header is not valid.')
25+
})
26+
})
27+
28+
test('returns the schema and token from valid Authorization header', () => {
29+
const validHeaders = [
30+
'Bearer token',
31+
'Bearer 12345',
32+
'Token token',
33+
'Token 12345',
34+
]
35+
36+
validHeaders.forEach((header) => {
37+
// We only care about the headers in the event
38+
const result = parseAuthorizationHeader({
39+
headers: { Authorization: header },
40+
} as unknown as APIGatewayProxyEvent)
41+
42+
expect(result).toEqual({
43+
schema: header.split(' ')[0],
44+
token: header.split(' ')[1],
45+
})
46+
})
47+
})
48+
49+
test('Handles different lower-casing of the authorization header', () => {
50+
const result = parseAuthorizationHeader({
51+
headers: { authorization: 'Bearer bazinga' },
52+
} as unknown as APIGatewayProxyEvent)
53+
54+
expect(result).toEqual({
55+
schema: 'Bearer',
56+
token: 'bazinga',
57+
})
58+
})
59+
60+
test('Handles different capital-casing of the Authorization header', () => {
61+
const result = parseAuthorizationHeader({
62+
headers: { Authorization: 'Bearer bazinga' },
63+
} as unknown as APIGatewayProxyEvent)
64+
65+
expect(result).toEqual({
66+
schema: 'Bearer',
67+
token: 'bazinga',
68+
})
69+
})
70+
})

packages/api/src/auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface AuthorizationHeader {
3333
export const parseAuthorizationHeader = (
3434
event: APIGatewayProxyEvent | Request
3535
): AuthorizationHeader => {
36-
const parts = getEventHeader(event, 'authorization')?.split(' ')
36+
const parts = getEventHeader(event, 'Authorization')?.split(' ')
3737
if (parts?.length !== 2) {
3838
throw new Error('The `Authorization` header is not valid.')
3939
}

0 commit comments

Comments
 (0)