Safety policy

Updated at:
Copy as MD

Safety policies provide global client-side protection for Alibaba Cloud CLI operations. When enabled, the CLI matches each command against configured rules to allow, deny, or require manual confirmation for the operation.

Overview

Each rule has two parts: pattern specifies which commands to match, and action specifies what to do on a match:

  • allow (permit): Explicitly permit the operation.

  • deny (block): Block the operation. The CLI does not send the API request.

  • confirm (require approval): In an interactive terminal, the CLI prompts you to type yes before proceeding.

If no rule matches, the operation is allowed by default.

Safety policies complement server-side access control (RAM) but do not replace RAM permission policies.

Commonly used to intercept destructive operations (such as delete operations) run by AI agents, and to help reduce accidental deletion by operators.

The safety policy is a local configuration: users with access to the local machine, including the agent itself, can modify or disable it.

Prerequisites

  • Alibaba Cloud CLI v3.3.14 or later is installed. Run the following command to check your current version:

    aliyun version
  • CLI credentials are configured. If you have not configured credentials, run the following command:

    aliyun configure

Manage safety policies

Safety policies are managed through the aliyun configure safety-policy command family.

View current policy

View the safety policy configuration (JSON output):

aliyun configure safety-policy show

Enable and disable

# Enable safety policy
aliyun configure safety-policy enable

# Disable safety policy
aliyun configure safety-policy disable

Run aliyun configure safety-policy show to verify the current effective state. Without the ALIBABA_CLOUD_SAFETY_POLICY_ENABLED environment variable, enabled should be true after enabling and false after disabling; if the variable is set, the effective state follows it.

Add rules

Command syntax:

aliyun configure safety-policy add --pattern <pattern> --action <deny|confirm>

Example: Block all delete operations across all products:

aliyun configure safety-policy add --pattern '*:Delete*' --action deny

Example: Require human confirmation for ECS modify operations:

aliyun configure safety-policy add --pattern 'ecs:Modify*' --action confirm

Run aliyun configure safety-policy list to verify that the rule has been added.

Note

If you add a rule with a pattern that already exists, the CLI updates the action for that pattern instead of creating a duplicate entry.

Remove rules

Specify the exact pattern to remove a rule:

aliyun configure safety-policy remove --pattern '*:Delete*'

List rules

Run the following command to view all rules in a formatted list:

aliyun configure safety-policy list

Sample output:

Safety policy: enabled
Config file: /home/user/.aliyun/safety-policy.json

Rules:
  1. *:Delete* -> deny
  2. ecs:Modify* -> confirm

Rule syntax

Pattern format

Write a rule as product:operation, for example ecs:DeleteInstance.

  • * stands for anything. ecs:Delete* covers every ECS operation whose name starts with Delete.

  • Matching is case-insensitive.

  • Without *, a rule matches only commands exactly identical to it: ecs:Delete does not match ecs:DeleteInstance. To cover a class of operations, append * to the operation name.

Common examples:

Pattern

Operations covered

*:Delete*

Operations whose names start with Delete, across all products

ecs:*

All ECS operations

*:*

All operations across all products

Note

Wrap the pattern in single quotes (such as '*:Delete*') so the shell does not expand *.

Note

To match a word anywhere in the operation name, add another * before the operation name, such as *:*delete*. This pattern is broader and may also block read-only operations whose names contain that word.

Operation name spellings

The same operation usually has two spellings:

aliyun ecs DeleteInstance
aliyun ecs delete-instance

The safety policy matches commands as typed, so these are two different commands. A rule that specifies an exact operation name (such as ecs:DeleteInstance) blocks only the first spelling; the second runs as usual.

You do not need to work out the other spelling yourself. When you add an exact rule and no existing rule covers the other spelling, the CLI prompts you to add it and names that spelling. For example, after you add a rule for ecs:DeleteInstance:

Rule saved. This rule does not match "ecs:delete-instance". To apply the same restriction to that command, add a separate rule.

Two approaches cover both spellings:

  • To block a class of operations, use a wildcard rule such as *:Delete*:

    aliyun configure safety-policy add --pattern '*:Delete*' --action deny
  • To target a single operation exactly, add one rule per spelling:

    aliyun configure safety-policy add --pattern 'ecs:DeleteInstance' --action deny
    aliyun configure safety-policy add --pattern 'ecs:delete-instance' --action deny

Evaluation order

Rules are checked top to bottom; the first match takes effect. Put more specific rules first.

For example, given the following two rules:

  1. ecs:Delete* → confirm

  2. *:Delete* → deny

When you run aliyun ecs DeleteInstance, the first rule (ecs:Delete*) matches, and the confirm action is applied — not the second rule's deny.

To exempt an operation, place its allow rule first. For example, ecs:Describe* → allow before ecs:* → deny: Describe operations run as usual, and the rest are blocked.

Important

deny does not automatically take precedence. After configuration, run aliyun configure safety-policy list to check the order.

Verify rules

After configuration, test the commands you want to block. --cli-dry-run builds the request without running it:

aliyun ecs DeleteInstance --cli-dry-run
aliyun ecs delete-instance --cli-dry-run

Read the output:

  • An error containing operation blocked by safety policy → a deny rule matched.

  • An error containing Safety policy requires confirmation for, or a confirmation prompt in an interactive terminal → a confirm rule matched.

  • Any other output (missing-parameter errors, dry-run request previews, and so on) → no rule matched. Check the pattern spelling and the rule order.

Important

Verify both spellings. Do not judge by the exit code: exit codes are not fixed across error types and modes. Use the error text instead.

Non-interactive mode and AI agent scenarios

The --yes flag

When you specify the --yes (or -y) flag, confirm rules are automatically skipped (treated as approved). deny rules are not affected and still block execution.

# confirm rules are skipped by --yes, operation executes directly
aliyun ecs ModifyInstanceAttribute --InstanceId i-bp1xxxxx --yes

# deny rules are not affected by --yes, operation is still blocked
aliyun ecs DeleteInstance --InstanceId i-bp1xxxxx --yes
# Output: ERROR: operation blocked by safety policy: Ecs DeleteInstance (rule: *:Delete*)

Non-interactive terminal behavior

In non-interactive environments (scripts, CI/CD, AI agents), if a confirm rule matches and --yes is not specified, the CLI returns this error:

Safety policy requires confirmation for: Ecs ModifyInstanceAttribute
This confirmation applies only to local CLI operations. It does not grant cloud permissions or verify external approval. Cloud access permissions are controlled by RAM.
This operation cannot run in non-interactive mode without explicit approval. If you are an agent, ask the user whether this operation is allowed; after they confirm (e.g. reply yes or 确认), re-run the same command with --yes.

Recommended AI agent workflow

Recommended setup: deny for destructive operations, confirm for operations that need human review. When the agent hits a confirm rule, it should explain the operation to the user, get approval, and then re-run the command with --yes.

Example configuration: Block delete operations and require human confirmation for update operations:

aliyun configure safety-policy enable
aliyun configure safety-policy add --pattern '*:Delete*' --action deny
aliyun configure safety-policy add --pattern '*:Update*' --action confirm

Environment variables

In addition to command-line configuration, you can control safety policies through environment variables. Environment variables take precedence over the configuration file.

Environment variable

Description

Example

ALIBABA_CLOUD_SAFETY_POLICY_ENABLED

Override the enabled value in the configuration file. Accepts true, false, 1, 0.

export ALIBABA_CLOUD_SAFETY_POLICY_ENABLED=true

ALIBABA_CLOUD_SAFETY_POLICY_RULES

Override the rule list in the configuration file. Use the format pattern=action, with multiple rules separated by commas. When set, configuration file rules are completely replaced, not merged, but the enabled state is unchanged: if enabled is false in the configuration file, you must also set ALIBABA_CLOUD_SAFETY_POLICY_ENABLED=true for the rules to take effect. Set to an empty string to clear all rules. If the format is invalid, the CLI returns an error and refuses to run the command.

export ALIBABA_CLOUD_SAFETY_POLICY_RULES="ecs:Describe*=allow,*:Delete*=deny,ecs:Modify*=confirm"

ALIBABA_CLOUD_SAFETY_SKIP_CONFIRM

Set to 1 or true to skip confirmation prompts, equivalent to --yes.

export ALIBABA_CLOUD_SAFETY_SKIP_CONFIRM=1

Configuration file

Safety policy configuration is stored in ~/.aliyun/safety-policy.json. If you specify a configuration file path with the --config-path option, safety-policy.json is located in the same directory as that configuration file.

File format example:

{
  "enabled": true,
  "rules": [
    {
      "pattern": "ecs:Describe*",
      "action": "allow"
    },
    {
      "pattern": "*:Delete*",
      "action": "deny"
    },
    {
      "pattern": "ecs:Modify*",
      "action": "confirm"
    }
  ]
}
  • If the configuration file does not exist, safety policy is disabled by default and the rule list is empty. The file is automatically created when you first run the enable or add subcommand.

Note

If the policy file is corrupted or its content is invalid, the CLI returns an error and refuses to run the command; it does not skip safety checks. Only when the policy file does not exist is the safety policy treated as disabled.