Skip to content

Commit

Permalink
Rotate cookie signing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
kmjennison committed Jul 11, 2022
1 parent 4482164 commit 980a2dc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ NEXT_PUBLIC_SERVICE_WORKER_ENABLED=false
########## Session Options ##########

COOKIE_SECURE_SAME_SITE_NONE=false
# COOKIE_SECRET_20220711=secret-value
# COOKIE_SECRET_CURRENT=secret-value
# COOKIE_SECRET_PREVIOUS=secret-value


########## URL/Path Configuration ##########
Expand Down
4 changes: 3 additions & 1 deletion .env.local.info
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

########## Session Options ##########

### COOKIE_SECRET_20220711 is the actual current secret, despite
### the naming of "COOKIE_SECRET_CURRENT".
# COOKIE_SECRET_20220711=secret-value
# COOKIE_SECRET_CURRENT=secret-value
# COOKIE_SECRET_PREVIOUS=secret-value


##########################################
Expand Down
2 changes: 1 addition & 1 deletion .env.preview.info
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ NEXT_PUBLIC_SERVICE_WORKER_ENABLED=true
########## Session Options ##########

COOKIE_SECURE_SAME_SITE_NONE=true
# COOKIE_SECRET_20220711=secret-value
# COOKIE_SECRET_CURRENT=secret-value
# COOKIE_SECRET_PREVIOUS=secret-value


########## URL/Path Configuration ##########
Expand Down
5 changes: 4 additions & 1 deletion .env.production.info
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ NEXT_PUBLIC_SERVICE_WORKER_ENABLED=true
########## Session Options ##########

COOKIE_SECURE_SAME_SITE_NONE=true

### COOKIE_SECRET_20220711 is the actual current secret, despite
### the naming of "COOKIE_SECRET_CURRENT".
# COOKIE_SECRET_20220711=secret-value
# COOKIE_SECRET_CURRENT=secret-value
# COOKIE_SECRET_PREVIOUS=secret-value


########## URL/Path Configuration ##########
Expand Down
2 changes: 1 addition & 1 deletion src/utils/auth/initAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ const initAuth = () => {
cookies: {
name: 'TabAuth',
keys: [
process.env.COOKIE_SECRET_20220711,
process.env.COOKIE_SECRET_CURRENT,
process.env.COOKIE_SECRET_PREVIOUS,
],
httpOnly: true,
maxAge: 12 * 60 * 60 * 24 * 1000, // twelve days
Expand Down
26 changes: 17 additions & 9 deletions src/utils/middleware/__tests__/cookies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const mockCookie = {
jest.mock('cookies', () => jest.fn(() => mockCookie))

beforeEach(() => {
process.env.COOKIE_SECRET_20220711 = 'xyz'
process.env.COOKIE_SECRET_CURRENT = 'abc'
process.env.COOKIE_SECRET_PREVIOUS = 'def'
process.env.COOKIE_SECURE_SAME_SITE_NONE = 'true'
})

Expand All @@ -32,8 +32,8 @@ describe('cookies middleware: withCookies', () => {

it('passes the request and response objects to Cookies', () => {
expect.assertions(1)
process.env.COOKIE_SECRET_20220711 = 'secret-2022-07-11'
process.env.COOKIE_SECRET_CURRENT = 'thing'
process.env.COOKIE_SECRET_PREVIOUS = 'anotherThing'
const mockReq = getMockReq()
const mockRes = getMockRes()
withCookies(mockReq, mockRes)
Expand All @@ -42,14 +42,14 @@ describe('cookies middleware: withCookies', () => {

it('passes the expected secrets to Cookies', () => {
expect.assertions(1)
process.env.COOKIE_SECRET_20220711 = 'secret-2022-07-11'
process.env.COOKIE_SECRET_CURRENT = 'thing'
process.env.COOKIE_SECRET_PREVIOUS = 'anotherThing'
const mockReq = getMockReq()
const mockRes = getMockRes()
withCookies(mockReq, mockRes)
const optionsForCookies = Cookies.mock.calls[0][2]
expect(optionsForCookies).toMatchObject({
keys: ['thing', 'anotherThing'],
keys: ['secret-2022-07-11', 'thing'],
})
})

Expand All @@ -63,24 +63,32 @@ describe('cookies middleware: withCookies', () => {
)
})

it('throws if the session key COOKIE_SECRET_20220711 is not defined', () => {
expect.assertions(1)
delete process.env.COOKIE_SECRET_20220711
expect(() => {
withCookies(getMockReq(), getMockRes())
}).toThrow(
'Session secrets must be set as env vars `COOKIE_SECRET_20220711` and `COOKIE_SECRET_CURRENT`.'
)
})

it('throws if the session key COOKIE_SECRET_CURRENT is not defined', () => {
expect.assertions(1)
delete process.env.COOKIE_SECRET_CURRENT
expect(() => {
withCookies(getMockReq(), getMockRes())
}).toThrow(
'Session secrets must be set as env vars `COOKIE_SECRET_CURRENT` and `COOKIE_SECRET_PREVIOUS`.'
'Session secrets must be set as env vars `COOKIE_SECRET_20220711` and `COOKIE_SECRET_CURRENT`.'
)
})

it('throws if the session key COOKIE_SECRET_PREVIOUS is not defined', () => {
it('does not throw if the session key COOKIE_SECRET_PREVIOUS is not defined', () => {
expect.assertions(1)
delete process.env.COOKIE_SECRET_PREVIOUS
expect(() => {
withCookies(getMockReq(), getMockRes())
}).toThrow(
'Session secrets must be set as env vars `COOKIE_SECRET_CURRENT` and `COOKIE_SECRET_PREVIOUS`.'
)
}).not.toThrow()
})

it('sets the "secure" option to true in Cookies when secure cookies are configured', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/middleware/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ export const withCookies = (req, res) => {
)
}
if (
!(process.env.COOKIE_SECRET_CURRENT && process.env.COOKIE_SECRET_PREVIOUS)
!(process.env.COOKIE_SECRET_20220711 && process.env.COOKIE_SECRET_CURRENT)
) {
throw new Error(
'Session secrets must be set as env vars `COOKIE_SECRET_CURRENT` and `COOKIE_SECRET_PREVIOUS`.'
'Session secrets must be set as env vars `COOKIE_SECRET_20220711` and `COOKIE_SECRET_CURRENT`.'
)
}

// An array is useful for rotating secrets without invalidating old sessions.
// The first will be used to sign cookies, and the rest to validate them.
// https://github.com/expressjs/cookie-session#keys
const sessionSecrets = [
process.env.COOKIE_SECRET_20220711,
process.env.COOKIE_SECRET_CURRENT,
process.env.COOKIE_SECRET_PREVIOUS,
]

const useSecureSameSiteNone =
Expand Down

0 comments on commit 980a2dc

Please sign in to comment.