我有一个有关访问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.
发布评论
评论(1)
正如Juliomalves指出的那样,客户代码将无法访问服务器上您在服务器上拥有的目录或文件,但公共目录除外。
接下来,您能够从
[root]/public
中提供静态资产,如在这里如果此目录已重命名,则这些资产将不再从客户端获得。
该名称无法更改, 在没有我的情况下下载该设置。JSON文件会将内容/文件本身发送给它们?”
可以从API路由提供信息的 方法是通过明确创建通话
res [ponse] .send()
(或 res.json( ))带有从该文件导入的数据。 API路线永远不会捆绑在客户端,只有如所指出的,如。“我想知道客户端是否实际上可以以某种方式读取/访问SRC目录并获取settings.json文件。”
如上所述,在
/public
目录中仅作为文件访问目录中的资产。在接下来的静态资产中,目录永远无法访问。这甚至在。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 hereIf this directory is ever renamed, these assets are no longer available from a client.
"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()
(orres.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."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.