Next.js目录结构和JSON Secrets的安全性

发布于 2025-01-30 18:20:59 字数 383 浏览 2 评论 0 原文

我有一个有关访问next.js目录及其访问要求的安全问题。
我有一个根文件夹,上面有我的页面,公共,SRC,样式,型号文件夹。

在SRC文件夹中,我有一个设置。JSON文件是一个空的JavaScript对象。这个想法是,设置将添加到此文件并由API路由访问,以检查可以在此设置上修改的设置。访问SRC目录并获取settings.json文件。
我想将Secret Key放在这里,以便我可以轻松地更改秘密键,而不必重新启动服务器。因此,我可以实时更新秘密密钥,然后将其应用于settings.json文件。然后,更新将立即进行,我不必更改环境变量并重新启动服务器。
保留和使用SRC目录中的JSON文件以存储机密数据是安全的吗?如果没有,是否有一种方法可以保留和使用JSON文件为此目的? 感谢您的帮助和信息。

I have a security question regarding the access of Next.JS directories, and their access requirements.
I have a root folder that has my pages, public, src, styles, models folders.

In the src folder I have a settings.json file that is a empty JavaScript object. The idea is that settings would be added to this file and accessed by api routes, to check settings that could be modified on this settings.json file...
What I am wondering is if the client can actually somehow just read/access the src directory and get the settings.json file.
I want to put secret key's here that way I can easily change secret keys without having to restart my server. So I could just update the secret key live, and have it applied to the settings.json file. Then the update would be live immediately and I don't have to change the environment variables and restart the server.
Is it safe to keep and use a json file in the src directory to store confidential data? If not, is there a way to keep and use a json file for this purpose?
Thanks for the help and info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乙白 2025-02-06 18:20:59

正如Juliomalves指出的那样,客户代码将无法访问服务器上您在服务器上拥有的目录或文件,但公共目录除外。

接下来,您能够从 [root]/public 中提供静态资产,如在这里

注意:仅在建造时间公共目录中的资产才能由Next.js。

提供

如果此目录已重命名,则这些资产将不再从客户端获得。

注意:不要命名公共目录。 该名称无法更改,并且是用于提供静态资产的唯一目录。

该名称无法更改, 在没有我的情况下下载该设置。JSON文件会将内容/文件本身发送给它们?”

可以从API路由提供信息的 方法是通过明确创建通话 res [ponse] .send()(或 res.json( ))带有从该文件导入的数据。 API路线永远不会捆绑在客户端,只有如所指出的,如

文件夹/API中的任何文件都映射到/api/*,并将视为API端点而不是页面。 它们是服务器端仅捆绑,不会增加您的客户端捆绑包大小。

“我想知道客户端是否实际上可以以某种方式读取/访问SRC目录并获取settings.json文件。”

如上所述,在/public 目录中仅作为文件访问目录中的资产。在接下来的静态资产中,目录永远无法访问。这甚至在

send(req, path)
  .on('directory', () => {
    // We don't allow directories to be read.
    const err: any = new Error('No directory access')
    err.code = 'ENOENT'
    reject(err)
  })

As juliomalves pointed out client code won't be able to access a directory or file that you have on the server with the exception of the public directory.

Next gives you the ability to serve static assets from [root]/public as documented here

Note: Only assets that are in the public directory at build time will be served by Next.js.

If this directory is ever renamed, these assets are no longer available from a client.

Note: Don't name the public directory anything else. The name cannot be changed and is the only directory used to serve static assets.

"I put a settings.json file right next to that .env file and required it in an api route, could the client somehow download that settings.json file without me purposely sending them the contents/file itself?"

The only way information can be served from an api route is by expressly creating a route to call res[ponse].send() (or res.json()) with data imported from that file. Api routes are not ever bundled on the client side and only ever exist on the server as noted here.

Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page. They are server-side only bundles and won't increase your client-side bundle size.

"What I am wondering is if the client can actually somehow just read/access the src directory and get the settings.json file."

As noted above only assets in the /public directory are accessible as files by path. Directories are never accessible in Next as static assets. This is even pointed out in the source code.

send(req, path)
  .on('directory', () => {
    // We don't allow directories to be read.
    const err: any = new Error('No directory access')
    err.code = 'ENOENT'
    reject(err)
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文