# 代码所有者

## 概述

GitHub 允许您定义负责仓库中代码的个人或团队，即[代码所有者](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners)。为此，您应该将 `CODEOWNERS` 文件添加到以下位置之一：

* `.github/`
* `/`
* `docs/`

之后，您可以设置受保护分支的规则，并要求代码所有者进行强制批准。

参考资料：

* [GitHub 文档：关于代码所有者](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners)

## 代码所有权接管

代码所有者的文档[说明](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners)`CODEOWNERS` 文件可以在 `/`、`docs/` 或 `.github/` 目录中定义：

```
要使用 CODEOWNERS 文件，请在仓库的根目录、docs/ 或 .github/ 目录中创建一个名为 CODEOWNERS 的新文件，在您想要添加代码所有者的分支中。
```

然而，如果一个仓库包含多个 CODEOWNERS 文件会发生什么？实际上，在允许的路径中存在以下优先级：

* `.github/`
* `/`
* `docs/`

因此，如果 GitHub 在 `.github/` 中找到 `CODEOWNERS` 文件，它将忽略 `/` 和 `docs/` 中的 `CODEOWNERS` 文件。换句话说，如果 `CODEOWNERS` 文件已在 `/` 或 `docs/` 中创建，具有写入权限的攻击者能够将 `CODEOWNERS` 文件添加到 `.github/`，接管代码所有权，并绕过分支保护规则。现在攻击者是整个仓库代码的所有者，可以批准任何更改。

假设有一个仓库，其中 `.github/` 有单独的所有者负责对该目录的更改，而 `CODEOWNERS` 文件存储在 `/` 中。在这种情况下，`CODEOWNERS` 文件可能如下所示：

```
* @owner-team
.github/ @dev-team
```

`@dev-team` 团队的成员，或获得该成员账户访问权限的攻击者，可以使用以下步骤在此仓库中提升其权限：

1. 使用个人 GitHub 账户或其他被入侵的账户 fork 仓库。
2. 添加具有以下内容的 `.github/CODEOWNERS` 文件：

   ```
   * @dev-team
   ```
3. 向目标仓库创建 PR。
4. 批准 PR（由于攻击者有权访问 `.github/` 的代码所有者账户，他们可以在 `.github/` 内批准任何更改）。
5. 合并更改。
6. 现在攻击者是整个仓库的代码所有者，他们能够批准任何更改，包括 `.github/` 之外的更改。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.cdxiaodong.life/cicd-an-quan/github-an-quan/codeowners.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
