使用 SetEnv 或在 php.ini 中存储数据库变量的安全性如何?
我不喜欢在 document_root 下存储站点范围的加密密钥和数据库访问信息,因此我在 conf.d 下使用 Apache 的 SetEnv 和 php.ini 文件将它们与代码库分开。最大的问题是,哪一个更好? apache vhost 文件下的环境变量 (SetEnv SITEKEY 'oinkoink!'
) 或 conf.d/xxx.ini 文件 (db_pass="oink?"
) 内?也许还有别的事?
优点 缺点:
SetEnv:
+存储在 DOCUMENT_ROOT 外部
+只有给定的虚拟主机才能访问
- 通过 PHPINFO() 可见 - 黑客需要直接访问/上传文件
get_cfg_var:
+存储在 DOCUMENT_ROOT 外部
+对于 PHPINFO() 不可见
-(非常糟糕)所有定义的 ini 变量都包含在内,因此每个虚拟主机都可以通过(ini_get_all)查询它们,因此在共享虚拟主机环境中不可用
I don't like storing sitewide crypto keys and DB access information under document_root, so I was using Apache's SetEnv and php.ini files under conf.d to separate these from the codebase. The big question is, which one is better? Inside environment variables under apache vhost files (SetEnv SITEKEY 'oinkoink!'
) or inside conf.d/xxx.ini files (db_pass="oink?"
)? Maybe something else?
PROS n CONS:
SetEnv:
+Stored outside DOCUMENT_ROOT
+Only the given vhost has access
-Visible with PHPINFO() - Hacker needs direct access/upload exploit to files
get_cfg_var:
+Stored outside DOCUMENT_ROOT
+Not visible with PHPINFO()
-(VERY BAD) All the defined ini variables are included, so each vhost can query them via (ini_get_all), so not usable in a shared vhost environment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要 *.ini 和 SetEnv 位于 Web 根目录(文档根目录)之外,无论哪种方式都没有关系。只需选择您喜欢的即可。我喜欢 SetEnv,但这实际上只是个人喜好。对我来说使用 SetEnv 更有意义,因为变量被放入
_SERVER
中。对于 .ini,我认为将其留给特定于代码工作方式的初始化设置更有意义。不存储在文档根目录下是防止访问可能安全的数据的好主意。
请注意,
phpinfo()
将列出所有已设置的服务器变量,因此请务必小心。最后,如果您要包含文件,请确保不允许用户以某种方式设置无偿的
../../
,否则他们将有权访问潜在的安全文件(甚至包括/etc/passwd
!)我认为您的主要问题是“安全性如何”。好吧,这可能是您在不引起严重头痛的情况下所能获得的最大安全性。 php 代码可以访问这些变量,因此如果您将它们打印出来,它们很容易可见,因此这取决于您的代码库的安全性。也许可以将 LDAP 与 MySQL 一起使用,但这听起来很痛苦。
As long as *.ini and SetEnv are outside of the web root (document root) it doesn't matter either way. Just choose whichever you prefer. I like SetEnv, but it's really just personal preference. It makes more sense to me to use SetEnv since the variables are put into
_SERVER
. With the .ini, I think it makes more sense to leave it for initialization settings specific to how the code works.Not storing under the document root is a good idea to prevent access to possibly secure data.
Note that
phpinfo()
will list any server variables that are set, so be very careful about that.Finally, if you are including files, make sure that you don't allow gratuitous
../../
set by the user somehow or they will have access to potentially secure files (even including/etc/passwd
!)I think your main question is "how secure." Well, this probably about as secure as you can get without causing major headaches. The php code has access to these variables, so if you print them out they are easily visible, so it depends on how secure your code base is. It might be possible to use LDAP with MySQL, but that sounds like a huge pain.
在 document_root 之外使用存储非公共文件是常见的做法。典型的布局可能是这样的:
将 PHP 内容存储在 documentRoot 中,将所有非公共内容存储在 nonPublicFiles 中。 documentRoot 将是 vHost 的 Apache document_root。由于 nonPublicFiles 位于外部,Apache 将不会响应请求。
考虑到安全性,SetEnv 或 *.ini 往往是等效的:如果有人获得执行任意 PHP 代码的权限,两种方式都会向该代码提供敏感信息。
我更喜欢 SetEnv 和 *.ini 方法,因为 Apache 本身不会披露这些详细信息。需要一个脚本。
即使没有脚本,配置错误也可能会泄露nonPublicFiles的内容。
如果您要使用nonPublicFiles,请预先准备一个脚本,该脚本会检查一切是否设置正常,如果发现问题,则转发电子邮件。可能使用 CRON 来调用它。
It's common practice to use store non-public files outside of document_root. A typical layout could be this:
Store your PHP stuff in documentRoot and all non-public stuff in nonPublicFiles. documentRoot would be the Apache document_root of the vHost. Since nonPublicFiles is outside, Apache won't answer request.
Recarding security, SetEnv or *.ini tend to be equivalent: In case someone gains rights to execute arbitrary PHP-Code, both ways provide the sensible information to this code.
I'd prefer the SetEnv and *.ini method, since Apache won't disclose these details itself. A script is required.
Misconfiguration may disclose the contents of nonPublicFiles even without a script.
If case you are going to use nonPublicFiles, prepare upfront a script, which checks if everything is set up fine and forward an email, if it found problems. Probably call it using CRON.
我更喜欢将它们存储在只能由 apache 访问的非公共文件夹中,或者存储在 document_root 之外。
I prefer storing them in either non-public folders, which can be accessed only by apache, or outside the document_root.