Skip to content

Commit a7b6458

Browse files
committed
docs: update virtual columns and security documentation for backend-only fields
1 parent 07ffa98 commit a7b6458

File tree

3 files changed

+71
-19
lines changed

3 files changed

+71
-19
lines changed

adminforth/documentation/docs/tutorial/03-Customization/03-virtualColumns.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,4 @@ Hook still has access to the virtual field `updates.password`, and we use built-
235235
After hook is executed, `updates.password` will be removed from the record since it is virtual, so password itself will not be saved to the database.
236236
237237
238-
### Backend-only fields
239-
240-
Another important point is that `hashed_password` field should never be passed to frontend due to security reasons.
241-
242-
To do it we have 2 options:
243-
244-
1) Do not list `password_hash` in the `columns` array of the resource. If AdminForth knows nothing about field
245-
it will never pass this field to frontend.
246-
2) Define `password_hash` in columns way but set `backendOnly`. The scond option is more explicit and should be preferrred
247-
248-
```ts
249-
{
250-
name: 'password_hash',
251-
type: AdminForthDataTypes.STRING,
252-
showIn: { all: false },
253-
backendOnly: true, // will never go to frontend
254-
}
255-
```
238+

adminforth/documentation/docs/tutorial/03-Customization/12-security.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,72 @@ server {
140140
proxy_set_header X-Forwarded-For $remote_addr;
141141
}
142142
}
143-
```
143+
```
144+
145+
146+
147+
### Backend-only fields
148+
149+
Some fields should never be accessed on frontend. For example, `hashed_password` field which is always created using CLI initial app, should never be passed to frontend due to security reasons.
150+
If any user of system can read `hashed_password` of another user, it can lead to account compromise.
151+
152+
To eliminate it we have 2 options:
153+
154+
1) Do not list `password_hash` in the `columns` array of the resource. If AdminForth knows nothing about field
155+
it will never pass this field to frontend!
156+
2) Define `password_hash` in columns way but set `backendOnly`.
157+
158+
The second option is more explicit and should be preferred. This option is used by default in CLI-bootstrapped projects:
159+
160+
```ts
161+
{
162+
name: 'password_hash',
163+
type: AdminForthDataTypes.STRING,
164+
showIn: { all: false },
165+
backendOnly: true, // will never go to frontend
166+
}
167+
```
168+
169+
#### Dynamically hide fields depending on user ACL / role
170+
171+
You can use `column.showIn` to show or hide column for user depending on his role.
172+
173+
However even if `showIn` value (or value returned by showIn function) is `false`, record value will still go to frontend and will be
174+
visible in the Network tab, so advanced user can still access field value. We did it in this way to provide AdminForth developers with ability to quickly use any record field in custom components.
175+
176+
However if you need securely hide only certain fields depending on role, you should use `column.backendOnly` and pass function there.
177+
178+
Let's consider example:
179+
180+
```ts
181+
{
182+
name: 'email',
183+
type: AdminForthDataTypes.STRING,
184+
showIn: {
185+
//diff-add
186+
all: false,
187+
//diff-add
188+
list: (user: AdminUser) => user.role === 'superadmin',
189+
},
190+
}
191+
```
192+
193+
So if you will configure the email column in user resource like this, only superadmin will be able to see emails, and only in the list view.
194+
However, the email will still be present in the record and can be accessed by advanced users through the Network tab.
195+
196+
So to completely hide the email field from all users apart superadmins, you should use `column.backendOnly` and pass a function there.
197+
198+
```ts
199+
{
200+
name: 'email',
201+
type: AdminForthDataTypes.STRING,
202+
//diff-add
203+
backendOnly: (user: AdminUser) => user.role !== 'superadmin',
204+
showIn: {
205+
all: false,
206+
list: (user: AdminUser) => user.role === 'superadmin',
207+
},
208+
}
209+
```
210+
211+
So if you will configure the email column in user resource like this, only superadmin will be able to see emails, and only in the list view.

adminforth/modules/restApi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
11051105

11061106
const { record } = body;
11071107

1108+
// todo if showIn.create is function, code below will be buggy (will not detect required fact)
11081109
for (const column of resource.columns) {
11091110
if (
11101111
(column.required as {create?: boolean, edit?: boolean})?.create &&

0 commit comments

Comments
 (0)