使用 git 作为部署实用程序时的安全问题

发布于 2025-01-05 22:16:49 字数 352 浏览 3 评论 0原文

git 是一个非常强大的工具,但不是那么容易使用

例如,我很高兴使用 git 作为从我的 git 存储库更新我的远程站点的工具。问题是我的网站根目录有一个 .git 目录,外部用户可以简单地使用 site/.git 访问它,这很糟糕,因为他们可以轻松获取我的代码历史记录,基本上是当前代码,他们甚至可以从配置文件中获取密码和私人信息。

那么怎样才是正确的git使用方式,既能充分利用git,又不会引入这些威胁呢?

使用 git clone git://repo site_root 初始化网站,并使用 git pull 获取更改非常方便,但也带来了巨大的安全问题。

有没有什么方法可以像上面所示的步骤一样方便,但没有安全隐患?

git is a very powerful tool, but not that easy to use

For example, I am glad to use git as a tool to update my remote site from my git repository. The problem is there is a .git directory at the root of my website, external users can simply visit it using site/.git, that is terrible, since they can easily get my code history, and basically the current code, they can even get passwords and private informations from the configuration files.

So what is the right way to use git which can make full use of git, but without introducing these threats?

Using git clone git://repo site_root to initialize web site, and git pull to get changes is of great convenience, but also brings huge security problems.

Is there any methods that can be as convenient as the steps shown above, but without security pitfalls?

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

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

发布评论

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

评论(6

櫻之舞 2025-01-12 22:16:49

Apache 至少默认其配置为禁止 Web 访问任何以 . 开头的文件,并且这也可以在任何其他 Web 服务器中完成。

此外,最好将敏感文件保留在 Web 根目录之外,即:

.git/
config.file
public/
public/index.html

并将网站的文档根目录设为 public/ 目录。

Apache, at least, defaults its configuration to forbidding web access to any file starting with a ., and this could be done in any other webserver as well.

Additionally, it's best to keep sensitive files outside the web root, i.e.:

.git/
config.file
public/
public/index.html

and have the document root for the site be the public/ directory.

天暗了我发光 2025-01-12 22:16:49

好吧,我不太喜欢直接使用 git 自动部署代码的最新版本,但这是另一个问题。

关于您的安全问题,一个真正基本的解决方案是删除对 .git 文件的访问(使用 htaccess 文件?)。

另一件事是从 git 存储库中删除您的密码,在您的版本控制系统中可能没有使用密码。

Well, I'm not really fond of using directly git to automatically deploy the last version of your code, but that's another question.

Regarding your security issue, a really basic solution would be to just remove access to your .git file (with htaccess files?).

Another thing would be to remove your passwords from the git repository, there is probably no use of then in your version control system.

孤蝉 2025-01-12 22:16:49

请参阅 http://www.clientcide .com/best-practices/exporting-files-from-git-similar-to-svn-export/

从上面:

这是我的一行命令,用于获取存档并将其发送到
不同位置:

git 存档头 | (cd ~/path/where/I/want/it/ && tar -xvf -)

这会将整个库提取到指定路径(不包含
.git 文件等等)。

但是,有时我只想取出库的一部分。
git archive 总是给你完整的 enchilada 这有点糟糕,
在本例中,我使用 rsync,如下所示:

rsync路径/我/想要/到/导出/ -ri --del -m --排除“.*”
~/path/where/I/want/it/ |grep sT

最后一点 – grep sT 将限制我所看到的输出
我只能看到已更新的文件。我用它只是为了保持理智
检查我的出口。如果我看到一大堆东西去更新路径
已经有了这个库,我知道我只更改了一个文件,然后我
知道我走错了路。

See http://www.clientcide.com/best-practices/exporting-files-from-git-similar-to-svn-export/

From the above:

Here’s my one-line command for taking the archive and sending it to a
different location:

git archive HEAD | (cd ~/path/where/I/want/it/ && tar -xvf -)

This will extract the ENTIRE library to the specified path (without
the .git files and whatnot).

Sometimes, however, I want to pull out just a portion of the library.
git archive always gives you the whole enchilada which kinda sucks,
and in this case I use rsync, like this:

rsync path/I/want/to/export/ -ri --del -m --exclude ".*"
~/path/where/I/want/it/ |grep sT

That last bit – the grep sT will limit the output of what I see so
that I only see the files that are updated. I use that just to sanity
check my export. If I see a TON of stuff go to update a path that
already has the library and I know I only changed one file, then I
know I got the path wrong.

甜警司 2025-01-12 22:16:49

@JaredPar 的评论和@Chris Shain 上面的答案的组合(+1 表示 git 存档)。

我还使用 git archive,然后使用 Chef 进行实际部署。再简单不过了。

  1. git 存档; | gzip > rc.tar.gz
  2. mv rc.tar.gz 到我的食谱
  3. 上传食谱

在我的服务器上运行的厨师客户端运行配方以将压缩文件复制到 chache 位置并将其解压缩进入我的网络服务器目录。

自我提醒:在 github 上发布食谱

A combination of @JaredPar's comment, and @Chris Shain's answer above (+1 for the git archive).

I use git archive also, and then use Chef for the actual deployment. Couldn't be simpler.

  1. git archive <tag_name> | gzip > rc.tar.gz
  2. mv rc.tar.gz to my cookbook
  3. upload cookbook

The chef-client running on my server runs the recipe to copy the zipped file to a chache location and extract it into my web server directory.

Note to self: publish the recipe on github

不必了 2025-01-12 22:16:49

如果需要在生产服务器中拥有完整的存储库,那么就有一个解决方案。

您可以在文件系统中的某个位置拥有整个历史记录,并让 git “指向”它。

$ cd /public                        # go to the public directory
$ git clone git://repo              # to initialize the web site
$ mv .git /private/repo.git         # move sensible information to a private place
$ export GIT_DIR=/private/repo.git  # make git "point" to the history
$ git pull                          # update changes

如果这样做,您的历史记录将位于无法通过网络访问的 /private/repo.git 中,并且您的工作目录将位于 /public 中,仅提供您指定的文件版本。

如需了解更多信息,您可以阅读 progit

If having the complete repo in the production server is a requirement, then there is a solution.

You can have the whole history somewhere in the file system and have git "point" to it.

$ cd /public                        # go to the public directory
$ git clone git://repo              # to initialize the web site
$ mv .git /private/repo.git         # move sensible information to a private place
$ export GIT_DIR=/private/repo.git  # make git "point" to the history
$ git pull                          # update changes

If you do that, your history will be in /private/repo.git that is not accessible through the web, and your working directory will be in /public, serving only the version of the files you specify.

For more information, you can read progit.

爱冒险 2025-01-12 22:16:49

我会再次提出这个问题,因为我也在研究安全问题。

您应该使用独立的工作树,如下所述 此处。您可以在 Web 根目录之外创建 git 存储库,并使 git 指向不同目录中的源代码。

I'll resurrect this question because I'm also looking into security issues.

You should use a detached work tree, as explained here. You can create your git repository outside the web root, and have git point to the source code at a different directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文