> Portal Navigation:
>
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt
## Resource: Security Best Practices
## Article: Security Best Practices
## Article Link: https://dev.wix.com/docs/develop-websites/articles/best-practices/security-best-practices.md
## Article Content:
# Security Best Practices
In general, a site is secure without having to do anything. Wix takes care of that. However, there are certain situations where you have to take some precautions so that you don't expose sensitive data to a site's visitors.
## Collection permissions
If you are using custom collections you should always set the permissions of your database collections to be as restrictive as possible. If the collection contains sensitive data such as PII or secrets, the **Read** permissions should be granted for the **Admin** role only, unless there is a reason to grant a specific permission for additional roles. Even when there is a reason to grant a permission for more roles, you should only grant it for the roles that need it.
Also, to prevent anyone from removing or updating the data, you should set the **Update** and **Remove** permissions to the **Admin** role. Or, if there is a UX flow for users to update or remove the data, then you can set those permissions to **Site member author**.
Here are some examples:
|Example |Recommended permission settings |
|---|---|
|**Scenario: Form submission**
An input form that you want anyone to be able to use.
**Solution**
On the collection that the form is connected to, set the **create** permissions to the **Anyone** role. Keep all the other permissions restricted to the **Admin** role.|Read: Admin
Create: Anyone
Update: Admin
Delete: Admin
|**Scenario: Site content**
A page that displays content from a collection to anyone.
**Solution**
Set the **read** permission of that collection to the **Anyone** role. Keep all the other permissions restricted to the **Admin** role.|Read: Anyone
Create: Admin
Update: Admin
Delete: Admin
|**Scenario: Member-generated content**
A member-generated comments section where members can post comments that anyone can see, but only the poster can update or delete the comment.
**Solution**
Set each permission based on who needs to access it.|Read: Anyone
Create: Site member
Update: Site member author
Delete: Site member author
## Unused permissions
Be careful when granting a permission to a collection even if you don't expose that collection’s functionality in your site.
Here are some examples:
* **Unused create permission**
If your site doesn’t contain a form for site visitors to create content for a specific collection, you may think you can safely set the **create** permission for that collection to **Anyone**. That is not the case. A malicious site visitor can inject data into your collection without a form. Make sure your collection is protected by restricting the **create** permission to the **Admin** role.
* **Unused read permission**
If you have a collection for internal use that you don't use on any of your site's pages, you may think you can safely set the **read** permission for that collection to **Anyone**. That is not the case. A malicious site visitor can still read the data from this collection. Make sure your collection is protected by restricting the **read** permission to the **Admin** role.
## Temporarily bypass collection permissions
Sometimes, you may need to grant access to collection data only in a specific situation or only to a specific user. You may need to expose some of the data in a collection, while keeping the rest private. In these cases, extending the permissions of the collection to more users is a security risk. Doing this exposes all the collection data to users with the permitted roles all the time. Instead, set the collection permissions as appropriate for most situations. Then, when you need to grant access to the collection to a specific user or for a specific use case, perform that operation in backend code.
You can grant temporary access to collections using the `suppressAuth` parameter that’s available as an option for many [Wix Data](https://www.wix.com/velo/reference/wix-data) functions. Setting `suppressAuth` to true allows a data request in backend code to interact with a collection even if the site visitor requesting the data doesn’t have permission to access that collection. To grant temporary access to a collection, define a function in your backend code that makes a data request using `suppressAuth`. Export this function for use in your frontend code.
See [About Collection Permissions](https://support.wix.com/en/article/collection-permissions-an-overview) for more information.
>**Note**
> * A backend function that suppresses Wix Data's permissions checks must do at least one of the following:
> (a) Perform its own checks before accessing a collection.
> (b) Filter out sensitive collection data before passing anything back to frontend.
> Anyone can call exported backend code, as described [below](https://dev.wix.com/docs/develop-websites/articles/best-practices/security-best-practices.md). This code therefore presents a serious security risk.
> * Only suppress permission checks when absolutely necessary. Leave Wix Data’s permissions checks enabled as much as possible. This way, even backend operations run only for users who have been granted permission for that operation.
Here are 2 examples of using `suppressAuth` to grant specific and temporary access to collection data:
* **Access for site members with specific roles**
Wix allows you to create custom roles for site members. However, you currently can’t set collection permissions by role. To allow only members with a certain role to access a collection, set the collection’s permissions to Admin. Then, implement backend code that suppresses permission checks for site members with the desired role.
Show me how
This code defines two functions:
* **isPermitted()** : Uses the [Wix Members API](https://dev.wix.com/docs/velo/api-reference/wix-members-backend/introduction.md) to check if a site member has a Staff role
* **queryCollection()** : Uses suppressAuth to return a collection's data if isPermitted() returns true.
```javascript
import { Permissions, webMethod } from 'wix-web-module';
import { currentMember } from 'wix-members-backend';
import wixData from 'wix-data';
const isPermitted = webMethod(Permissions.Anyone, async () => {
try {
let hasPermittedRole = currentMember.getRoles()
.then((roles) => {
const staffRoleCheck = roles.filter(obj => obj.title === 'Staff');
if (staffRoleCheck.length > 0) {
return true;
} else {
return false;
}
})
return hasPermittedRole;
} catch (error) {
console.error(error);
return false;
}
});
```
```javascript
export const queryCollection = webMethod(Permissions.Anyone, async () => {
const accessGranted = await isPermitted();
if (accessGranted) {
try {
let collectionData = wixData.query('myCollection')
.find({ suppressAuth: true });
return collectionData;
} catch (error) {
console.log(error);
}
} else {
return "Access denied";
}
});
```
Show me how
This code uses `suppressAuth` to query a collection and returns only the '\_id' and 'comment' fields.
```javascript
import wixData from 'wix-data';
export function getData(){
return wixData.query("myCollection")
.find({"suppressAuth": true})
.then((results) => {
if (results.totalCount > 0) {
const filteredResults = results.items.map( (item) => {
return {"\_id" : item.\_id, "comment" : item.comment}
})
return filteredResults
}
})
.catch((error) => {
console.log("Error:", error.message);
});
}
```