Skip to content

Commit c194261

Browse files
authoredJun 27, 2022
fix(core): respect NEXTAUTH_SECRET in unstable_getServerSession (#4774)
* fix(core): respect `NEXTAUTH_SECRET` in `unstable_getServerSession` * add `secret` tests * add `@types/jest` * fix tests
1 parent 5fdd848 commit c194261

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed
 

‎packages/next-auth/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"@testing-library/react": "^13.3.0",
104104
"@testing-library/react-hooks": "^8.0.0",
105105
"@testing-library/user-event": "^14.2.0",
106+
"@types/jest": "^28.1.3",
106107
"@types/node": "^17.0.42",
107108
"@types/nodemailer": "^6.4.4",
108109
"@types/oauth": "^0.9.1",

‎packages/next-auth/src/next/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ export async function unstable_getServerSession(
9696
)
9797

9898
const [req, res, options] = args;
99+
100+
options.secret = options.secret ?? process.env.NEXTAUTH_SECRET
101+
99102
const session = await NextAuthHandler<Session | {}>({
100103
options,
101104
req: {

‎packages/next-auth/tests/assert.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { InvalidCallbackUrl, MissingSecret } from "../src/core/errors"
12
import { handler } from "./lib"
23

34
it("Show error page if secret is not defined", async () => {
@@ -10,7 +11,7 @@ it("Show error page if secret is not defined", async () => {
1011
expect(res.html).toMatch(/there is a problem with the server configuration./i)
1112
expect(res.html).toMatch(/check the server logs for more information./i)
1213

13-
expect(log.error).toBeCalledWith("NO_SECRET", expect.anything())
14+
expect(log.error).toBeCalledWith("NO_SECRET", expect.any(MissingSecret))
1415
})
1516

1617
it("Should show configuration error page on invalid `callbackUrl`", async () => {
@@ -25,7 +26,7 @@ it("Should show configuration error page on invalid `callbackUrl`", async () =>
2526

2627
expect(log.error).toBeCalledWith(
2728
"INVALID_CALLBACK_URL_ERROR",
28-
expect.anything()
29+
expect.any(InvalidCallbackUrl)
2930
)
3031
})
3132

@@ -38,6 +39,6 @@ it("Allow relative `callbackUrl`", async () => {
3839
expect(res.status).not.toBe(500)
3940
expect(log.error).not.toBeCalledWith(
4041
"INVALID_CALLBACK_URL_ERROR",
41-
expect.anything()
42+
expect.any(InvalidCallbackUrl)
4243
)
4344
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { NextApiRequest } from "next"
2+
import { MissingSecret } from "../src/core/errors"
3+
import { unstable_getServerSession } from "../src/next"
4+
import { mockLogger } from "./lib"
5+
6+
let originalWarn = console.warn
7+
let logger = mockLogger()
8+
9+
beforeEach(() => {
10+
process.env.NODE_ENV = "production"
11+
process.env.NEXTAUTH_URL = "http://localhost"
12+
console.warn = jest.fn()
13+
})
14+
15+
afterEach(() => {
16+
logger = mockLogger()
17+
process.env.NODE_ENV = "test"
18+
delete process.env.NEXTAUTH_URL
19+
console.warn = originalWarn
20+
})
21+
22+
describe("Treat secret correctly", () => {
23+
const req: any = { headers: {} }
24+
const res: any = { setHeader: jest.fn(), getHeader: jest.fn() }
25+
26+
it("Read from NEXTAUTH_SECRET", async () => {
27+
process.env.NEXTAUTH_SECRET = "secret"
28+
await unstable_getServerSession(req, res, { providers: [], logger })
29+
30+
expect(logger.error).toBeCalledTimes(0)
31+
expect(logger.error).not.toBeCalledWith("NO_SECRET")
32+
33+
delete process.env.NEXTAUTH_SECRET
34+
})
35+
36+
it("Read from options.secret", async () => {
37+
await unstable_getServerSession(req, res, {
38+
providers: [],
39+
logger,
40+
secret: "secret",
41+
})
42+
43+
expect(logger.error).toBeCalledTimes(0)
44+
expect(logger.error).not.toBeCalledWith("NO_SECRET")
45+
})
46+
47+
it("Error if missing NEXTAUTH_SECRET and secret", async () => {
48+
await unstable_getServerSession(req, res, { providers: [], logger })
49+
50+
expect(logger.error).toBeCalledTimes(1)
51+
expect(logger.error).toBeCalledWith("NO_SECRET", expect.any(MissingSecret))
52+
})
53+
})

‎packages/next-auth/tests/lib.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { LoggerInstance, NextAuthOptions } from "../src"
22
import { NextAuthHandler } from "../src/core"
33

4+
export const mockLogger: () => LoggerInstance = () => ({
5+
error: jest.fn(() => {}),
6+
warn: jest.fn(() => {}),
7+
debug: jest.fn(() => {}),
8+
})
9+
410
export async function handler(
511
options: NextAuthOptions,
612
{
@@ -16,11 +22,6 @@ export async function handler(
1622
// @ts-ignore
1723
if (prod) process.env.NODE_ENV = "production"
1824

19-
const mockLogger: LoggerInstance = {
20-
error: jest.fn(),
21-
warn: jest.fn(),
22-
debug: jest.fn(),
23-
}
2425
const url = new URL(
2526
`http://localhost/api/auth/${path ?? "signin"}?${new URLSearchParams(
2627
params ?? {}
@@ -31,9 +32,10 @@ export async function handler(
3132
host: "",
3233
},
3334
})
35+
const logger = mockLogger()
3436
const response = await NextAuthHandler({
3537
req,
36-
options: { secret: "secret", ...options, logger: mockLogger },
38+
options: { secret: "secret", ...options, logger },
3739
})
3840
// @ts-ignore
3941
if (prod) process.env.NODE_ENV = "test"
@@ -44,6 +46,6 @@ export async function handler(
4446
html:
4547
response.headers?.[0].value === "text/html" ? response.body : undefined,
4648
},
47-
log: mockLogger,
49+
log: logger,
4850
}
4951
}

‎pnpm-lock.yaml

+5-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.