admin管理员组文章数量:1122832
I'm implementing a "Contact Us" email functionality where I need environment variables for sensitive information such as a username and password for email authentication PRIVATE_KEY and a public API key PUBLIC_KEY.
Locally, my .env file contains the following:
#.env
PUBLIC_KEY=PUBLIC
PRIVATE_KEY=PRIVATE
This works perfectly in my local development environment. However, when I deploy to platforms like Vercel or AWS Amplify, these environment variables are undefined, even though I’ve correctly defined them in the platforms' environment variable settings.
To work around this issue, I added the following configuration to next.config.js
module.exports = {
env: {
PUBLIC_KEY: 'PUBLIC',
PRIVATE_KEY: 'PRIVATE',
},
};
This resolves the issue, but I have concerns about security because, according to the Next.js documentation, any variables added to the env key in next.config.js will be included in the JavaScript bundle and sent to the client. Here's the key excerpt:
Environment variables specified this way will always be included in the JavaScript bundle. Prefixing the environment variable name with NEXT_PUBLIC_ only has an effect when specifying them through the environment or .env files.
My use case involves:
- Using PUBLIC_KEY on client-side pages (e.g., in use client components). This is intentional and expected since it's safe to expose public API keys.
- Using PRIVATE_KEY only in dynamic server-side code (e.g., API routes), which must remain secure and never be included in client-side bundles.
However, based on the documentation, adding PRIVATE_KEY to the env key in next.config.js would expose it in the client-side JavaScript bundle, which is a serious security concern. This makes me wonder if my implementation is truly secure or if my sensitive keys, like PRIVATE_KEY, are inadvertently being leaked to the client.
Could you clarify:
Why defining environment variables in the deployment platform doesn’t work? Whether there’s a secure way to use sensitive environment variables PRIVATE_KEY only on the server-side without risk of exposure?
I tried adding PUBLIC_KEY and PRIVATE_KEY to .env for local development and the deployment platform’s environment variables. Locally, it worked as expected, but in deployment, they were undefined. Adding them to next.config.js fixed it, but I expected the platform environment variables to work without exposing sensitive keys.
本文标签: nextjsEnsure Private Key Environment Variables NextJS are private nextconfigjsStack Overflow
版权声明:本文标题:next.js - Ensure Private Key Environment Variables NextJS are private next.config.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283520a1926991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论