Skip to content

Commit

Permalink
fix: don't require secret at build-time (nextauthjs#10592)
Browse files Browse the repository at this point in the history
* fix: don't require secret at build-time

* revert tests

* update Dockerfile
  • Loading branch information
balazsorban44 authored and JipSterk committed Aug 26, 2024
1 parent a2531a7 commit 7df986d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apps/dev/nextjs/.env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# https://generate-secret.vercel.app/32 to generate a secret.
# Note: Changing a secret may invalidate existing sessions
# and/or verification tokens.
NEXTAUTH_SECRET=secret
AUTH_SECRET=secret

AUTH_ASGARDEO_CLIENT_ID=
AUTH_ASGARDEO_CLIENT_SECRET=
Expand Down
4 changes: 1 addition & 3 deletions apps/examples/nextjs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ COPY . .
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

# This should be replaced with an actual secret in production.
# REVIEW: Can we make this not required during build?
RUN AUTH_SECRET=dummy npm run build
RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/utils/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function assertConfig(
return new UntrustedHost(`Host must be trusted. URL was: ${request.url}`)
}

if (!options.secret) {
if (!options.secret?.length) {
return new MissingSecret("Please define a `secret`.")
}

Expand Down
6 changes: 0 additions & 6 deletions packages/core/src/lib/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ export function setEnvDefaults(envObject: any, config: AuthConfig) {
}
}

if (!config.secret?.length) {
throw new MissingSecret(
"Missing secret, please set AUTH_SECRET or config.secret"
)
}

config.redirectProxyUrl ??= envObject.AUTH_REDIRECT_PROXY_URL
config.trustHost ??= !!(
envObject.AUTH_URL ??
Expand Down
25 changes: 9 additions & 16 deletions packages/core/test/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ beforeEach(() => {
describe("config is inferred from environment variables", () => {
it("providers (client id, client secret, issuer, api key)", () => {
const env = {
AUTH_SECRET: "asdf",
AUTH_AUTH0_ID: "asdf",
AUTH_AUTH0_SECRET: "fdsa",
AUTH_AUTH0_ISSUER: "https://example.com",
Expand Down Expand Up @@ -64,43 +63,37 @@ describe("config is inferred from environment variables", () => {
})

it("AUTH_REDIRECT_PROXY_URL", () => {
const env = {
AUTH_REDIRECT_PROXY_URL: "http://example.com",
AUTH_SECRET: "asdf",
}
const env = { AUTH_REDIRECT_PROXY_URL: "http://example.com" }
setEnvDefaults(env, authConfig)
expect(authConfig.redirectProxyUrl).toBe(env.AUTH_REDIRECT_PROXY_URL)
})

it("AUTH_URL", () => {
const env = { AUTH_URL: "http://n/api/auth", AUTH_SECRET: "asdf" }
const env = { AUTH_URL: "http://n/api/auth" }
setEnvDefaults(env, authConfig)
expect(authConfig.basePath).toBe("/api/auth")
})

it("AUTH_URL + prefer config", () => {
const env = { AUTH_URL: "http://n/api/auth", AUTH_SECRET: "asdf" }
const env = { AUTH_URL: "http://n/api/auth" }
const fromConfig = "/basepath-from-config"
authConfig.basePath = fromConfig
setEnvDefaults(env, authConfig)
expect(authConfig.basePath).toBe(fromConfig)
})

it("AUTH_URL, but invalid value", () => {
const env = { AUTH_URL: "secret", AUTH_SECRET: "asdf" }
const env = { AUTH_URL: "secret" }
setEnvDefaults(env, authConfig)
expect(authConfig.basePath).toBe("/auth")
})

it.each([
[{ AUTH_TRUST_HOST: "1", AUTH_SECRET: "asdf" }, { trustHost: true }],
[{ VERCEL: "1", AUTH_SECRET: "asdf" }, { trustHost: true }],
[{ NODE_ENV: "development", AUTH_SECRET: "asdf" }, { trustHost: true }],
[{ NODE_ENV: "test", AUTH_SECRET: "asdf" }, { trustHost: true }],
[
{ AUTH_URL: "http://example.com", AUTH_SECRET: "asdf" },
{ trustHost: true },
],
[{ AUTH_TRUST_HOST: "1" }, { trustHost: true }],
[{ VERCEL: "1" }, { trustHost: true }],
[{ NODE_ENV: "development" }, { trustHost: true }],
[{ NODE_ENV: "test" }, { trustHost: true }],
[{ AUTH_URL: "http://example.com" }, { trustHost: true }],
])(`%j`, (env, expected) => {
setEnvDefaults(env, authConfig)
expect(authConfig).toMatchObject(expected)
Expand Down

0 comments on commit 7df986d

Please sign in to comment.