# Access control

> Restrict anonymous access with Referer, Headers, and IP blocklists and allowlists to prevent hotlinking.

Access control (called **Hotlink protection settings (防盗链配置)** in the console) restricts who can anonymously access public files in a bucket. For example, you can allow only your own website to embed images, or block certain clients and IPs.

To get there, go to **Object Storage (对象存储) › [Bucket list (桶列表)](https://console.bitiful.com/buckets)** › click the bucket name › **Access management (访问管理) › Hotlink protection settings**.

## Scope

| Request                                                                | Checked                                                                                         |
| :--------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| Anonymous access to the bucket default domain or a bound bucket domain | Yes                                                                                             |
| Presigned URLs and requests signed with an AccessKey                   | No                                                                                              |
| Access through a CDN domain                                            | No. Set [access control](https://docs.bitiful.com/en/cdn/handbook/access-control.md) on the CDN |

- When Referer, Headers, and IP are all set, a request must **pass all of them** to be allowed.
- A request that fails returns `403 AccessDenied`.
- Changes take effect immediately after you save.
- These rules don't make private files accessible. Files still need to be [made public](https://docs.bitiful.com/en/bitiful-s4/handbook/public-read.md) first.

> **Info**
>
> Referer and request headers are filled in by the client. They suit preventing hotlinking between websites but can't replace authentication. For strict restrictions, keep files private and use [presigned URLs](https://docs.bitiful.com/en/developer/s3/presigned-url.md), or the CDN's [advanced authentication](https://docs.bitiful.com/en/best-practice/token-link.md).

## Referer

Click **Edit (编辑)** on the **Referer** card.

- `Empty Referer` (single choice, default: On (access allowed)): Whether to allow requests that have no Referer. Opening a file directly in the address bar, and some apps and download tools, send no Referer, and turning this off rejects all of them.

- `Referer hotlink protection type` (single choice, default: Blocklist active): **Allowlist active**: Only sources that match the list can access. **Blocklist active**: Sources that match the list are rejected. You can't use both at the same time, and saving one replaces the other.

- `Referer allowlist / blocklist` (multiline text): One domain or IP per line. The content (including wildcard expansion) can't exceed 1 KiB, and you're warned before saving if it does.

How matching works:

- Matching starts from the **beginning** of the Referer and is case-insensitive.
- If you don't write `http://` or `https://`, both protocols match. If you write one, only that protocol matches.
- `*` matches any number of characters, and `?` matches one character.

| Rule                        | Matches                                                | Doesn't match              |
| :-------------------------- | ------------------------------------------------------ | -------------------------- |
| `www.example.com/`          | `https://www.example.com/page`                         | `https://example.com/`     |
| `example.com/`              | `https://example.com/`, `http://example.com/a`         | `https://www.example.com/` |
| `*.example.com/`            | `https://www.example.com/`, `https://a.b.example.com/` | `https://example.com/`     |
| `https://example.com/blog/` | `https://example.com/blog/1.html`                      | `https://example.com/`     |

> **Tip**
>
> - Rules that contain only a domain (such as `example.com` or `*.example.com`) get a trailing `/` automatically when saved, so they don't match domains like `example.com.evil.com`. For origins with a port, include the port, for example `example.com:8080`.
> - If you really need prefix matching, end the rule with `*`. Rules saved earlier are completed only when you edit and save them again.
> - Write the root domain and `*.` subdomains on separate lines.
> - Many browsers send only the origin domain, without the path, on cross-site requests, so we don't recommend allowlisting by path.

When the allowlist is empty, you see "系统会拒绝携带任何Referer的匿名请求" (the system will reject anonymous requests that carry any Referer) before saving.

**To remove the restriction**: choose **Blocklist active**, clear the list, set **Empty Referer** to **On (access allowed)**, then save.

## Headers

Restrict access by any request header. The most common is User-Agent. Each header is set separately, up to 10 per bucket.

### Add

Click **Add Header (添加 Header)** on the **Headers** card.

### Fill in the rule

Fill in the fields below, then enter a request header value in **Test (测试)** to confirm whether it is allowed or rejected.

### Save

Click **Save (保存)**. Manage existing headers with **Edit** and **Delete (删除)**.

- `Header name` (text, required): Case-insensitive; letters, digits, hyphens, and underscores only; up to 29 characters; can't be renamed after creation. Set Referer on the Referer card. `Host`, `Authorization`, `Transfer-Encoding`, and `Trailer` aren't supported.

- `Type` (single choice, default: Blocklist): **Allowlist**: A request must match at least one entry to access. **Blocklist**: A request that matches any entry is rejected.

- `Empty value` (single choice, default: Allow access): Whether to allow requests that don't carry this header or whose value is empty.

- `Rules` (multiline text): One per line, compared with the **entire value** of the request header, case-insensitive. `*` matches any number of characters and `?` matches one character; escaping isn't supported. Rules for the same header total at most 1024 bytes, and each wildcard counts as 3 bytes. Consecutive `?` aren't allowed.

Unlike Referer, Headers match the entire string: to match "contains Chrome", write `*Chrome*`.

Examples:

| Goal                                                  | Header       |    Type   |  Empty value | Rules                          |
| :---------------------------------------------------- | ------------ | :-------: | :----------: | ------------------------------ |
| Block common command-line downloaders                 | `User-Agent` | Blocklist |  Deny access | `*curl*`, `*wget*`, `*python*` |
| Allow only your own app                               | `User-Agent` | Allowlist |  Deny access | `MyApp/*`                      |
| Allow cross-origin requests only from a specific site | `Origin`     | Allowlist | Allow access | `https://www.example.com`      |

Two special cases:

- An empty allowlist that doesn't allow empty values rejects all anonymous requests, and you must confirm before saving.
- An empty blocklist that allows empty values is the same as no restriction and can't be saved. Add rules or delete this header.

## IP

Click **Edit** on the **IP** card.

- `IP hotlink protection type` (single choice, default: Blocklist active): **Allowlist active**: Only IPs in the list are allowed. **Blocklist active**: IPs in the list are rejected. You can't use both at the same time.

- `IP allowlist / blocklist` (multiline text): One per line. Single IPv4 addresses and CIDR ranges (mask `/1` \~ `/32`) are supported, for example `114.114.114.114` and `114.114.114.0/24`. All content together can't exceed 1024 bytes, about 60 addresses, so merge addresses into ranges where you can.

When the allowlist is empty, you see "系统会拒绝来自任何IP的匿名请求" (the system will reject anonymous requests from any IP) before saving. To remove the restriction, choose **Blocklist active**, clear the list, and save.

## FAQ

<details>
<summary>My own website's images won't load after I set this up</summary>

1. Use the browser's developer tools to check the Referer of the image request and confirm it matches the allowlist.
2. If the page sets `Referrer-Policy: no-referrer` or `rel="noreferrer"`, no Referer is sent, and you need to allow empty Referers.
3. If your site has both `www` and non-`www` domains, add both.

</details>

<details>
<summary>It won't open in WeChat or an app</summary>

These environments may not send a Referer. Allow empty Referers, or switch to signed links.

</details>

<details>
<summary>Saving shows "设置referer失败" or "设置IP失败"</summary>

These mean "failed to set Referer" or "failed to set IP". Most often the content exceeds 1024 bytes. Reduce the entries, or merge them with wildcards or ranges.

</details>

<details>
<summary>I already use a CDN. Do I still need to set this?</summary>

Access through the CDN domain isn't subject to the rules here. Set them on the CDN. Anonymous access to the bucket default domain is still subject to the rules here.

</details>
