

# 防止跨服务混淆代理
<a name="cross-service-confused-deputy-prevention"></a>

混淆代理问题是一个安全性问题，即不具有某操作执行权限的实体可能会迫使具有更高权限的实体执行该操作。在 AWS 中，跨服务模拟可能会导致混淆代理问题。一个服务（*呼叫服务*) 调用另一项服务（*所谓的服务*)时，可能会发生跨服务模拟。可以操纵调用服务以使用其权限对另一个客户的资源进行操作，否则该服务不应有访问权限。为了防止这种情况，AWS 提供可帮助您保护所有服务的服务委托人数据的工具，这些服务委托人有权限访问账户中的资源。

建议在资源策略中使用 [https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourcearn](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourcearn) 和 [https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourceaccount](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourceaccount) 全局条件上下文键，以限制 CloudFormation 为其他服务提供的对特定资源（例如 CloudFormation 扩展）的访问权限。如果您只希望将一个资源与跨服务访问相关联，请使用。`aws:SourceArn`如果您想允许该账户中的任何资源与跨服务使用操作相关联，请使用。`aws:SourceAccount`

确保 `aws:SourceArn` 的值为 CloudFormation 存储的资源的 ARN。

防范混淆代理问题最有效的方法是使用 `aws:SourceArn` 全局条件上下文键和资源的完整 ARN。如果不知道资源的完整 ARN，或者正在指定多个资源，请针对 ARN 未知部分使用带有通配符（`*`）的 `aws:SourceArn` 全局上下文条件键。例如 `arn:aws:cloudformation:*:123456789012:*`。

如果 `aws:SourceArn` 值不包含账户 ID，则您必须使用两个全局条件上下文键来限制权限。

以下示例演示如何使用 CloudFormation 中的 `aws:SourceArn` 和 `aws:SourceAccount` 全局条件上下文键来防范混淆代理问题。

## 使用 `aws:SourceArn` 和 `aws:SourceAccount` 条件键的信任策略示例
<a name="cross-service-confused-deputy-prevention-example"></a>

对于注册表服务，CloudFormation 调用 AWS Security Token Service（AWS STS）以在您的账户中担任服务角色。此角色是为 [https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_RegisterType.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_RegisterType.html) 操作中的 `ExecutionRoleArn` 以及 [https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_LoggingConfig.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_LoggingConfig.html) 操作中的 `LogRoleArn` 集而配置。有关更多信息，请参阅 [配置具有 IAM 权限的执行角色和公有扩展访问权限的信任策略](registry-public.md#registry-public-enable-execution-role)。

此示例角色信任策略使用条件语句，将服务角色的 `AssumeRole` 功能限制为只能对指定账户中的指定 CloudFormation 扩展执行操作。`aws:SourceArn` 和 `aws:SourceAccount` 条件会得到独立评估。使用服务角色的任何请求都必须满足这两个条件。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "resources.cloudformation.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole",
      "Condition": {
        "StringEquals": {
          "aws:SourceAccount": "123456789012"
        },
        "ArnLike": {
          "aws:SourceArn": "arn:aws:cloudformation:us-east-1:123456789012:type/resource/Organization-Service-Resource"
        }
      }
    }
  ]
}
```

------

## 附加信息
<a name="cross-service-confused-deputy-prevention-additional-information"></a>

有关对 StackSets 使用的服务角色使用 `aws:SourceArn` 和 `aws:SourceAccount` 全局条件上下文键的策略示例，请参阅[设置全局键以缓解混淆代理问题](stacksets-prereqs-self-managed.md#confused-deputy-mitigation)。

有关更多信息，请参阅《IAM 用户指南》**中[更新角色信任策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_update-role-trust-policy.html)。