Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw MissingSecret when secret missing #10305

Merged
merged 4 commits into from Mar 25, 2024
Merged

Conversation

ndom91
Copy link
Member

@ndom91 ndom91 commented Mar 13, 2024

☕️ Reasoning

  • We had thrown MissingSecret if config.secret wasn't set, but not early enough in the flow, so a missing AUTH_SECRET got through in early signin and threw a mysterious error when passed to hkdf
  • Added another throw of this error if AUTH_SECRET isn't set in our core setEnvDefaults fn, since that's the only core place where one of config.secret or AUTH_SECRET should definitely have been set already. If you know a better place for it I'm all ears though 👍

Previous error msg on missing AUTH_SECRET:

image

New error msg:

image

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

📌 Resources

@ndom91 ndom91 requested a review from ThangHuuVu as a code owner March 13, 2024 21:28
Copy link

vercel bot commented Mar 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 25, 2024 1:16pm
2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Mar 25, 2024 1:16pm
nextra-docs ⬜️ Ignored (Inspect) Visit Preview Mar 25, 2024 1:16pm

@github-actions github-actions bot added the core Refers to `@auth/core` label Mar 13, 2024
Copy link

codecov bot commented Mar 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.77%. Comparing base (5ea8b7b) to head (24c5ea3).

Additional details and impacted files
@@             Coverage Diff             @@
##             main   #10305       +/-   ##
===========================================
+ Coverage   39.37%   80.77%   +41.39%     
===========================================
  Files         171       12      -159     
  Lines       27278     2445    -24833     
  Branches     1165      160     -1005     
===========================================
- Hits        10742     1975     -8767     
+ Misses      16536      470    -16066     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ndom91 ndom91 merged commit aafa6dd into main Mar 25, 2024
14 of 15 checks passed
@ndom91 ndom91 deleted the ndom91/throw-missing-secret branch March 25, 2024 13:28
@DonikaV
Copy link

DonikaV commented Mar 27, 2024

I have AUTH_SECRET in my .yaml file and it works in beta.5 but in beta.16 I am getting
Missing secret, please set AUTH_SECRET or config.secret.

@DuarteMartinho
Copy link

Bump! I m also getting "Missing secret, please set AUTH_SECRET or config.secret. Read more at https://errors.authjs.dev#missingsecret"

in my .env i have tried

AUTH_SECRET and NEXTAUTH_SECRET

Also tried setting it to SECRET and using that in auth.js in config and setting config.secret to that...

Also localhost works, but in CI or in Production seems to break

Screenshots from CI
image

Screenshot from production environment (only shows error on client console there is no logs)
image

@ThangHuuVu @ndom91

@ndom91
Copy link
Member Author

ndom91 commented Mar 27, 2024

Hey folks, yeah so setting the secret option on your main config or the AUTH_SECRET env var should be enough to not trigger this. Looking into it..

@DuarteMartinho
Copy link

Checked my production container for environment variable and AUTH_SECRET is set

image

@ndom91 Let me know what to do as this is kind of urgent, thanks!

@DuarteMartinho
Copy link

@DonikaV Found a fix for now

Use beta15 instead of beta16

@LoisDuplain
Copy link

Please revert this merge.

Copy link
Member Author

ndom91 commented Mar 30, 2024

I can't reproduce this in dev and prod builds of SvelteKit apps with Auth.js nor in dev / prod Next.js apps 🤔

Can you all provide some more details about your setups?

@adamspotlite
Copy link

@ndom91 we're build a container with nextjs and then we ship the .env at runtime so the secrets aren't exposed in the container.. what's the suggested work around here?

@DonikaV
Copy link

DonikaV commented Apr 2, 2024

I am not ops specialist. Only knows that we could containers in GitHub CI and using a Docker file along with the yaml files for each of environments. And the beta15 version also has errors in the production build. So the last working version is beta.5 😀

@bpo-impact
Copy link

Having same issue as @adamspotlite we keep our secrets in aws secret manager and they are not available at build step only at runtime. We need to be able to build the code and then at runtime pass the secret.

@jdmnk
Copy link

jdmnk commented Apr 2, 2024

Facing the same issue where secret is not known at build time and this change breaks the build if the secret is only known at runtime.

JipSterk pushed a commit to JipSterk/next-auth that referenced this pull request Apr 3, 2024
* feat: throw for missing secret

* fix: env tests for missing secret

---------

Co-authored-by: Thang Vu <hi@thvu.dev>
JipSterk pushed a commit to JipSterk/next-auth that referenced this pull request Apr 3, 2024
* feat: throw for missing secret

* fix: env tests for missing secret

---------

Co-authored-by: Thang Vu <hi@thvu.dev>
@laeo
Copy link

laeo commented Apr 10, 2024

Can't preview or deploy to Cloudflare Pages, when trying preview it reports same ERROR message.

@matthawk60

This comment was marked as spam.

@jacogasp
Copy link

Hello everyone,

ump! I m also getting "Missing secret, please set AUTH_SECRET or config.secret. Read more at

Same behaviour here. I'm receiving the error message even if I set up AUTH_SECRET in .env file.
I tried the following cases:

  1. Simply set AUTH_SECRET as env variable: not working
  2. Set AUTH_SECRET env variable and in auth config I set secret: process.env.AUTH_SECRET: not working
  3. Hardcoding the secret secret: "foo" into the auth config: it works.

What is really wired to me is that if I do a console.log(process.env.AUTH_SECRET) it correctly prints the secret:

// auth.config.ts

console.log("secret:", process.env.AUTH_SECRET);

export const authConfig: NextAuthConfig = {
  providers: [...],
  secret: process.env.AUTH_SECRET,
  ...
}

and the output is

> next dev -p 8080

  ▲ Next.js 14.2.1
  - Local:        http://localhost:8080
  - Environments: .env

 ✓ Starting...
 ✓ Ready in 5.6s
 ○ Compiling /src/middleware ...
 ✓ Compiled /src/middleware in 928ms (220 modules)
secret: foo
 ○ Compiling / ...
 ✓ Compiled / in 3s (1894 modules)
secret: foo
secret: foo
 GET / 200 in 3956ms
 GET / 200 in 3673ms

proving that the secret is valid each time the config is invoked. But still I receive the missing AUTH_SECRET error.

Unhandled Runtime Error

MissingSecret: Missing secret, please set AUTH_SECRET or config.secret. Read more at https://errors.authjs.dev#missingsecret


src/auth.ts (4:61) @ authConfig

  2 | import { authConfig } from "./auth.config";
  3 |
> 4 | export const { auth, handlers, signIn, signOut } = NextAuth(authConfig);
    |                                                             ^

Tried also with NEXTAUTH_SECRET but same result.

Apparently hardcoding the secret in the auth config is the only working method.
I'm currently testing v5 beta 16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Refers to `@auth/core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet