Skip to main content

Security Rules

Security Rules are CloudBase cloud storage's file-level permission control feature, allowing precise control over read and write permissions for each file through custom expressions.

Configuration Entry

In CloudBase Console/Cloud Storage, click "Permission Settings" and select "Custom Security Rules" to configure security rules.

⚠️ Notes:

  • CloudBase console and server-side always have read and write permissions for all files. Security rules only apply to requests from clients (mini programs, web, etc.)
  • After modifying security rules, it takes 1-3 minutes for permissions to take effect. Please be patient
  • Before publishing, be sure to evaluate the security of the rules to avoid data leakage caused by public rules

Basic Syntax

{
"read": "expression", // Read permission control expression
"write": "expression" // Write permission control expression
}

Common Templates

{
"read": true,
"write": "resource.openid == auth.openid || resource.openid == auth.uid"
}

Expression Syntax

Built-in Variables

VariableDescriptionExample
authUser authentication infoauth.openid, auth.uid
resourceFile resource inforesource.openid

auth Object Properties

PropertyDescriptionTypeExample
uidUser unique identifier (Web)string"abc123"
openidUser unique identifier (Mini Program)string"oABC123"
loginTypeLogin methodstring"WECHAT"
LoginType Enum Values
Enum ValueLogin Method
WECHAT_PUBLICWeChat Official Account
WECHAT_OPENWeChat Open Platform
ANONYMOUSAnonymous Login
EMAILEmail Login
CUSTOMCustom Login

resource Object Properties

PropertyDescriptionTypeExample
openidFile creator unique identifierstring"oABC123"
pathFile relative path in cloud storage (excluding bucket name), format: path/filename.jpgstringphotos/photo.jpg

Operators

OperatorDescriptionExample
==Equalauth.uid == resource.openid
!=Not equalauth.loginType != 'ANONYMOUS'
>Greater thannow > resource.metadata.expireTime
<Less thannow < resource.metadata.publishTime
>=Greater or equalauth.vipLevel >= 3
<=Less or equalresource.size <= 10485760
&&Logical ANDauth != null && auth.loginType == 'WECHAT'
||Logical ORauth.uid == resource.openid \|\| auth.isAdmin

Operation Types

OperationDescriptionDefault
readRead file (download, get URL)false
writeWrite file (upload, delete)false

Regular Expression Support

Security rules support regular expressions to match file paths, but only the .test() method is supported. Common JavaScript string methods are not supported.

Supported Syntax

{
"read": "/test/.test(resource.path)",
"write": "false"
}

Matches all files containing test in the path.

Unsupported Methods

The following methods are not available in security rules:

MethodExampleAlternative
startsWith()resource.path.startsWith('test/')Use /^test\\//.test(resource.path)
includes()resource.path.includes('test/')Use /test/.test(resource.path)
indexOf()resource.path.indexOf('test/') === 0Use /^test\\//.test(resource.path)
match()resource.path.match(/^test\//)Use /^test\\//.test(resource.path)
substr()resource.path.substr(0, 5) == 'test/'Use regex or string equality check

Regular Expression Syntax Rules

RuleDescriptionExample
Must use .test()Regex must be called via .test() method/^test/.test(resource.path)
Slash escapingPath separator / must be written as \\//^uploads\\//.test(resource.path)
Special char escapeRegex special characters need escaping, e.g., . as \\./.*\\.jpg$/.test(resource.path)
Supports ORCan use pipe for simple OR operations/test\|uploads/.test(resource.path)
No groupingCannot use parentheses for complex grouping/^(test\|uploads)\\//.test(...) or /\.(png\|jpg)$/.test(...) not supported, use /^test\\//.test(...) \|\| /^uploads\\//.test(...)
Supports anchorsSupports ^ (start) and $ (end)/^public/.test(...) or /\\.png$/.test(...)

Practical Examples

Multiple Public Directories

{
"read": "/^test\\//.test(resource.path) || /^uploads\\//.test(resource.path) || /^public\\//.test(resource.path)",
"write": "resource.openid == auth.openid"
}

Allows all users to read files in test/, uploads/, public/ directories, but only creators can write.

Specific File Types Public

{
"read": "/\\.png$/.test(resource.path) || /\\.jpg$/.test(resource.path) || /\\.jpeg$/.test(resource.path) || /\\.gif$/.test(resource.path)",
"write": "auth != null"
}

Public read access for all image files, authenticated users can upload.

💡 Note: Since parentheses grouping is not supported, multiple regex expressions need to be connected with ||.

Hierarchical Permission Control

{
"read": "/^public\\//.test(resource.path) || (auth != null && /^private\\//.test(resource.path) && resource.openid == auth.uid)",
"write": "resource.openid == auth.openid || resource.openid == auth.uid"
}
  • public/ directory is readable by everyone
  • private/ directory is only readable by file creators
  • All files are only writable by creators

Application Scenarios

Personal Photo Album

Requirement: Users can only view and manage photos they uploaded.

{
"read": "resource.openid == auth.openid || resource.openid == auth.uid",
"write": "resource.openid == auth.openid || resource.openid == auth.uid"
}
// Mini Program - Upload photo
wx.cloud.uploadFile({
cloudPath: `photos/${Date.now()}.jpg`,
filePath: tempFilePath
})

// Web SDK - Get my photo list (requires cloud function query)
app.callFunction({
name: 'getMyPhotos'
})

Public Photo Album

Requirement: Only read files in the public folder

{
"read": "/^public\\//.test(resource.path)",
"write": "auth.uid == resource.openid || auth.openid == resource.openid"
}

Cross-platform Compatibility

Since mini programs use auth.openid and web uses auth.uid, it's recommended to check both in security rules:

{
"read": "resource.openid == auth.openid || resource.openid == auth.uid",
"write": "resource.openid == auth.openid || resource.openid == auth.uid"
}